Nadex.pm

filter: Statementstmt 88.3% 341 / 386 Branchbran 60.9% 161 / 264 Conditioncond 46.0% 29 / 63 MC/DCmcdc 11.1% 4 / 36 Subroutinesub 90.4% 38 / 42 Podpod 100% 17 / 17 totaltotal 73.0% 590 / 808 CC 169 SCAR 31.8
CC
169
Cov
74%
get_time_series
74.2
get_epic
45.1
cancel_order
30.0
CRAP 24.1
« Nadex/Order.pm ↑ Summary Nadex/Contract.pm »
1 package Finance::Nadex;
2  
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 our $VERSION = '0.12';
6  
7 14 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use Finance::Nadex::Order;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use Finance::Nadex::Position;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use Finance::Nadex::Contract;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 14 ▶use constant LOGIN_URL => '/iDeal/v2/security/authenticate';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 14 ▶use constant SEND_ORDER_URL => '/iDeal/dma/workingorders';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 14 ▶use constant RETRIEVE_ORDERS_URL => '/iDeal/orders/workingorders';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 14 ▶use constant RETRIEVE_ORDER_URL => '/iDeal/markets/details/workingorders';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 14 ▶use constant CANCEL_ORDER_URL => '/iDeal/orders/workingorders/dma';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 14 ▶use constant RETRIEVE_POSITIONS_URL => '/iDeal/orders/positions';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 14 ▶use constant RETRIEVE_POSITION_URL => '/iDeal/markets/details/position';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 14 ▶use constant EPIC_URL => '/iDeal/markets/details';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 14 ▶use constant MARKET_LIST_URL => '/iDeal/markets/navigation';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 14 my $session_id;
25  
26 # allows caller to use aliases for the market names in addition to those used by the exchange
27 14 my %index_name = (
28 'FTSE100', 'FTSE 100', 'G30', 'Germany 30',
29 'J225', 'Japan 225', 'TECH100', 'US Tech 100',
30 'US500', 'US 500', 'WALL30', 'Wall St 30',
31 'SC2000', 'US SmallCap 2000'
32 );
33  
34 0 ▶sub balance {
Subroutine balance: not called CC=3 SCAR=24.8
Pod documented
35  
36 0 my $self = shift;
37  
38 0 ▶ croak "ERROR: Finance::Nadex::balance(): must be logged in\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->logged_in 0 0 0
39 unless $self->logged_in;
40  
41 # it appears the only way to force a
42 # balance refresh is to login again
43 0 $self->login();
44  
45 0 ▶ return $self->{'balance'} || undef;
Condition0%0/2
$self->{'balance'} || undef
l!l
00
MC/DC0%0/1
$self->{'balance'} || undef
$self->{'balance'}
Truth table0%0/2
A
A= $self->{'balance'}
Aresult
11
00
46  
47 }
48  
49 0 ▶sub cancel_all_orders {
Subroutine cancel_all_orders: not called CC=4 SCAR=30.0
Pod documented
50  
51 0 my $self = shift;
52  
53 0 ▶ croak "ERROR: Finance::Nadex::cancel_all_orders(): must be logged in\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->logged_in 0 0 0
54 unless $self->logged_in;
55 0 my @orders = $self->retrieve_orders();
56  
57 0 ▶ return if !scalar @orders;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @orders 0 0 0
58  
59 0 foreach my $order_entry (@orders) {
60 0 $self->cancel_order( id => $order_entry->id );
61 }
62  
63 }
64  
65 0 ▶sub cancel_order {
Subroutine cancel_order: not called CC=5 SCAR=34.0
Pod documented
66  
67 0 my $self = shift;
68 0 my %args = @_;
69  
70 0 ▶ croak "ERROR: Finance::Nadex::cancel_order(): must be logged in\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->logged_in 0 0 0
71 unless $self->logged_in;
72 croak
73 "ERROR: Finance::Nadex::cancel_order(): must specify a named argument 'id'\n"
74 0 ▶ unless exists $args{id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"id"} 0 0 0
75 croak "ERROR: Finance::Nadex::cancel_order(): invalid id\n"
76 0 ▶ unless $args{id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"id"} 0 0 0
77  
78 0 my $deal_id = $args{id};
79  
80 0 my $cancel_order_url = $self->{base_url}.CANCEL_ORDER_URL . '/' . $deal_id;
81  
82 0 my $cancel_time = time;
83 0 my $cancel_content = qq~
84 {
85 "lsServerName": "https://mdp.nadex.com:443",
86 "timeStamp": "$cancel_time"
87 }~;
88  
89 0 my $cancelled_response =
90 $self->_delete( $cancel_order_url, $cancel_content );
91  
92 0 ▶ return undef unless $cancelled_response;
Branch: 1 branch
BranchTrueFalseTotal
unless $cancelled_response 0 0 0
93  
94 0 return $cancelled_response->{'dealReference'};
95 }
96  
97 18 ▶sub create_order {
Subroutine create_order: called CC=15 SCAR=27.1
Pod documented
98  
99 18 my $self = shift;
100 18 my %args = @_;
101  
102 18 my $order_time = time;
103  
104 18 ▶ croak "ERROR: Finance::Nadex::create_order(): must be logged in\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->logged_in 6 12 18
105 unless $self->logged_in;
106  
107 croak
108 "ERROR: Finance::Nadex::create_order(): must specify a named argument 'price'\n"
109 12 ▶ unless exists $args{price};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"price"} 2 10 12
110 croak
111 "ERROR: Finance::Nadex::create_order(): must specify a named argument 'direction'\n"
112 10 ▶ unless exists $args{direction};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"direction"} 1 9 10
113 croak
114 "ERROR: Finance::Nadex::create_order(): must specify a named argument 'epic'\n"
115 9 ▶ unless exists $args{epic};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"epic"} 1 8 9
116 croak
117 "ERROR: Finance::Nadex::create_order(): must specify a named argument 'size'\n"
118 8 ▶ unless exists $args{size};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"size"} 1 7 8
119  
120 7 $args{direction} = lc( $args{direction} );
121  
122 croak "ERROR: Finance::Nadex::create_order(): invalid epic\n"
123 7 ▶ unless $args{epic};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"epic"} 0 7 7
124  
125 7 my $contract = $self->get_contract( epic => $args{epic} );
126  
127 croak
128 "ERROR: Finance::Nadex::create_order(): named argument 'price' (->$args{price}<-) is not valid\n"
129 7 ▶ unless $self->_is_valid_price( $args{price}, $contract->type );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_valid_price($args{"price"}, $contract->type) 1 6 7
130 croak
131 "ERROR: Finance::Nadex::create_order(): named argument 'direction' (->$args{direction}<-) is not valid\n"
132 6 ▶ unless $self->_is_valid_direction( $args{direction} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_valid_direction($args{"direction"}) 1 5 6
133 5 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
unless $contract 0 5 5
134 "ERROR: Finance::Nadex::create_order(): named argument 'epic' (->$args{epic}<-) is not valid\n"
135 unless $contract;
136 croak
137 "ERROR: Finance::Nadex::create_order(): named argument 'size' (->$args{size}<-) is not valid\n"
138 5 ▶ unless $self->_is_valid_size( $args{size} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_valid_size($args{"size"}) 1 4 5
139  
140 # the market accepts only + or - for the direction; this enables the aliases 'buy' and 'sell'
141 4 ▶ $args{direction} = '+' if $args{direction} eq 'buy';
Branch: 1 branch
BranchTrueFalseTotal
if $args{"direction"} eq "buy" 2 2 4
142 4 ▶ $args{direction} = '-' if $args{direction} eq 'sell';
Branch: 1 branch
BranchTrueFalseTotal
if $args{"direction"} eq "sell" 1 3 4
143  
144 # the price is dollars and cents for binaries and some instrument level
145 # for spreads; only format the price to a currency if the order type is 'binary'
146 $args{price} = sprintf( "%.2f", $args{price} )
147 4 ▶ if $contract->type eq 'binary';
Branch: 1 branch
BranchTrueFalseTotal
if $contract->type eq "binary" 3 1 4
148  
149 4 my $order_content = qq~
150 {
151 "direction": "$args{direction}",
152 "epic": "$args{epic}",
153 "limitLevel": null,
154 "lsServerName": "https://mdp.nadex.com:443",
155 "orderLevel": "$args{price}",
156 "orderSize": "$args{size}",
157 "orderType": "Limit",
158 "sizeForPandLCalculation": $args{size},
159 "stopLevel": null,
160 "timeInForce": "GoodTillCancelled",
161 "timeStamp": "$order_time"
162  
163 }~;
164  
165 4 my $order = $self->_post( $self->{base_url}.SEND_ORDER_URL, $order_content );
166  
167 4 ▶ my $deal_reference_id = $order->{dealReference} if defined $order;
Branch: 1 branch
BranchTrueFalseTotal
if defined $order 4 0 4
168  
169 4 return $deal_reference_id;
170 }
171  
172 7 ▶sub get_contract {
Subroutine get_contract: called CC=10 SCAR=27.3
Pod documented
173  
174 7 my $self = shift;
175 7 my %args = @_;
176  
177 7 ▶ croak "ERROR: Finance::Nadex::get_contracts(): must be logged in\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->logged_in 0 7 7
178 unless $self->logged_in;
179  
180 croak
181 "ERROR: Finance::Nadex::get_contract(): must specify a named argument 'epic'\n"
182 7 ▶ unless exists $args{epic};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"epic"} 0 7 7
183 croak
184 "ERROR: Finance::Nadex::get_contract(): specified 'epic' (->$args{epic}<-) is not valid\n"
185 7 ▶ unless $args{epic};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"epic"} 0 7 7
186  
187 7 my $epic_url = $self->{base_url}.EPIC_URL . '/' . $args{epic};
188 7 my $epic_ref = $self->_get($epic_url);
189  
190 7 ▶ return unless $epic_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $epic_ref 0 7 7
191  
192 return
193 unless exists $epic_ref->{instrument}
194 7 ▶ && exists $epic_ref->{marketSnapshot};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $epic_ref->{"instrument"} and exists $epic_ref->{"marketSnapshot"} 0 7 7
Condition33%1/3
exists $epic_ref->{"instrument"} and exists $epic_ref->{"marketSnapshot"}
!ll&&!rl&&r
007
MC/DC0%0/2
exists $epic_ref->{"instrument"} and exists $epic_ref->{"marketSnapshot"}
exists $epic_ref->{"instrument"} exists $epic_ref->{"marketSnapshot"}
Truth table33%1/3
A and B
A= exists $epic_ref->{"instrument"}B= exists $epic_ref->{"marketSnapshot"}
ABresult
0X0
100
111
195 return
196 unless $epic_ref->{instrument}{instrumentType}
197 && $epic_ref->{instrument}{marketName}
198 7 ▶ && $epic_ref->{instrument}{displayPrompt};
Branch: 1 branch
BranchTrueFalseTotal
unless $epic_ref->{"instrument"}{"instrumentType"} and $epic_ref->{"instrument"}{"marketName"} and $epic_ref->{"instrument"}{"displayPrompt"} 0 7 7
Condition33%2/6
$epic_ref->{"instrument"}{"instrumentType"} and $epic_ref->{"instrument"}{"marketName"} and $epic_ref->{"instrument"}{"displayPrompt"}
!ll&&!rl&&r
007
$epic_ref->{"instrument"}{"instrumentType"} and $epic_ref->{"instrument"}{"marketName"}
!ll&&!rl&&r
007
MC/DC0%0/3
$epic_ref->{"instrument"}{"instrumentType"} and $epic_ref->{"instrument"}{"marketName"} and $epic_ref->{"instrument"}{"displayPrompt"}
$epic_ref->{"instrument"}{"instrumentType"} $epic_ref->{"instrument"}{"marketName"} $epic_ref->{"instrument"}{"displayPrompt"}
Truth table25%1/4
A and B and C
A= $epic_ref->{"instrument"}{"instrumentType"}B= $epic_ref->{"instrument"}{"marketName"}C= $epic_ref->{"instrument"}{"displayPrompt"}
ABCresult
0XX0
10X0
1100
1111
199  
200 return Finance::Nadex::Contract::_new(
201 {
202 instrumentType => $epic_ref->{instrument}{instrumentType},
203 epic => $args{epic},
204 displayOffer => $epic_ref->{marketSnapshot}{displayOffer},
205 displayBid => $epic_ref->{marketSnapshot}{displayBid},
206 displayBidSize => $epic_ref->{marketSnapshot}{displayBidSize},
207 displayOfferSize => $epic_ref->{marketSnapshot}{displayOfferSize},
208 instrumentName => $epic_ref->{instrument}{marketName},
209 displayPeriod => $epic_ref->{instrument}{displayPrompt}
210 }
211 7 );
212  
213 }
214  
215 5 ▶sub get_contracts {
Subroutine get_contracts: called CC=21 SCAR=32.0
Pod documented
216  
217 5 my $self = shift;
218 5 my %args = @_;
219 5 my $found;
220  
221 5 ▶ croak "ERROR: Finance::Nadex::get_contracts(): must be logged in\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->logged_in 0 5 5
222 unless $self->logged_in;
223  
224 croak
225 "ERROR: Finance::Nadex::get_contracts(): must specify a named argument 'market'\n"
226 5 ▶ unless exists $args{market};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"market"} 2 3 5
227 croak
228 "ERROR: Finance::Nadex::get_contracts(): must specify a named argument 'instrument'\n"
229 3 ▶ unless exists $args{instrument};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"instrument"} 1 2 3
230 croak
231 "ERROR: Finance::Nadex::get_conttracts(): must specify a named argument 'series'\n"
232 2 ▶ unless exists $args{series};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"series"} 1 1 2
233  
234 croak "ERROR: Finance::Nadex::get_contracts(): invalid market\n"
235 1 ▶ unless $args{market};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"market"} 0 1 1
236 croak "ERROR: Finance::Nadex::get_contracts(): invalid instrument\n"
237 1 ▶ unless $args{instrument};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"instrument"} 0 1 1
238 croak "ERROR: Finance::Nadex::get_conttracts(): invalid series\n"
239 1 ▶ unless $args{series};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"series"} 0 1 1
240  
241 $args{instrument} = $index_name{ $args{instrument} }
242 1 ▶ if exists $index_name{ $args{instrument} };
Branch: 1 branch
BranchTrueFalseTotal
if exists $index_name{$args{"instrument"}} 0 1 1
243  
244 1 my $market_list_ref = $self->_get($self->{base_url}.MARKET_LIST_URL);
245  
246 1 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless $market_list_ref 0 1 1
247 "ERROR: Finance::Nadex::get_contracts(): failed to retrieve the market list from the exchange\n"
248 if !$market_list_ref;
249  
250 my $market_id = $self->_get_market_id(
251 name => $args{market},
252 1 market_list_ref => $market_list_ref
253 );
254  
255 1 ▶ return unless $market_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $market_id 0 1 1
256  
257 my $instruments_list_ref =
258 1 $self->_get( $self->{base_url}.MARKET_LIST_URL . '/' . $market_id );
259  
260 1 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless $instruments_list_ref 0 1 1
261 "ERROR: Finance::Nadex::get_contracts(): failed to retrieve the market list from the exchange for market $market_id\n"
262 if !$instruments_list_ref;
263  
264 1 my $instrument_id;
265 1 foreach my $instrument ( @{ $instruments_list_ref->{'hierarchy'} } ) {
266 $instrument_id = $instrument->{id}
267 1 ▶ if $instrument->{name} eq $args{instrument};
Branch: 1 branch
BranchTrueFalseTotal
if $instrument->{"name"} eq $args{"instrument"} 1 0 1
268 }
269  
270 1 ▶ return unless $instrument_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $instrument_id 0 1 1
271  
272 my $instrument_list_ref =
273 1 $self->_get( $self->{base_url}.MARKET_LIST_URL . '/' . $instrument_id );
274  
275 1 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless $instrument_list_ref 0 1 1
276 "ERROR: Finance::Nadex::get_contracts(): failed to retrieve the market list from the exchange for market $market_id\n"
277 if !$instrument_list_ref;
278  
279 1 my $time_series_id;
280 1 foreach my $series ( @{ $instrument_list_ref->{'hierarchy'} } ) {
281 1 ▶ $time_series_id = $series->{id} if $series->{name} eq $args{series};
Branch: 1 branch
BranchTrueFalseTotal
if $series->{"name"} eq $args{"series"} 1 0 1
282 }
283  
284 1 ▶ return unless $time_series_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $time_series_id 0 1 1
285  
286 1 my @contracts;
287 my $series_list_ref =
288 1 $self->_get( $self->{base_url}.MARKET_LIST_URL . '/' . $time_series_id );
289  
290 1 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless $series_list_ref 0 1 1
291 "ERROR: Finance::Nadex::get_contracts(): failed to retrieve the market list from the exchange for market $market_id\n"
292 if !$series_list_ref;
293  
294 1 foreach my $contract ( @{ $series_list_ref->{'markets'} } ) {
295 1 push( @contracts, Finance::Nadex::Contract::_new($contract) );
296 }
297  
298 1 return @contracts;
299 }
300  
301 6 ▶sub get_epic {
Subroutine get_epic: called CC=34 SCAR=38.1
Pod documented
302  
303 6 my $self = shift;
304 6 my %args = @_;
305  
306 6 my $market_id;
307 my $instrument;
308  
309 6 ▶ croak "ERROR: Finance::Nadex::get_epic(): must be logged in\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->logged_in 0 6 6
310 unless $self->logged_in;
311  
312 croak
313 "ERROR: Finance::Nadex::get_epic(): must specify a named argument 'period'\n"
314 6 ▶ unless exists $args{period};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"period"} 2 4 6
315 croak
316 "ERROR: Finance::Nadex::get_epic(): must specify a named argument 'market'\n"
317 4 ▶ unless exists $args{market};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"market"} 0 4 4
318 croak
319 "ERROR: Finance::Nadex::get_epic(): must specify a named argument 'time'\n"
320 unless exists $args{time}
321 || ( exists $args{period}
322 4 ▶ && $args{period} =~ /^event$/i );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"time"} or exists $args{"period"} and $args{"period"} =~ /^event$/i 1 3 4
Condition83%5/6
exists $args{"time"} or exists $args{"period"} and $args{"period"} =~ /^event$/i
l!l&&r!l&&!r
211
exists $args{"period"} and $args{"period"} =~ /^event$/i
!ll&&!rl&&r
011
MC/DC66%2/3
exists $args{"time"} or exists $args{"period"} and $args{"period"} =~ /^event$/i
exists $args{"time"} exists $args{"period"} $args{"period"} =~ /^event$/i
Truth table75%3/4
A or B and C
A= exists $args{"time"}B= exists $args{"period"}C= $args{"period"} =~ /^event$/i
ABCresult
1XX1
0111
00X0
0100
323 croak
324 "ERROR: Finance::Nadex::get_epic(): must specify a named argument 'instrument'\n"
325 3 ▶ unless exists $args{instrument};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"instrument"} 1 2 3
326 croak
327 "ERROR: Finance::Nadex::get_epic(): must specify a named argument 'strike'\n"
328 2 ▶ unless exists $args{strike};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"strike"} 0 2 2
329  
330 croak "ERROR: Finance::Nadex::get_epic(): invalid period\n"
331 2 ▶ unless $args{period};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"period"} 0 2 2
332 croak "ERROR: Finance::Nadex::get_epic(): invalid market\n"
333 2 ▶ unless $args{market};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"market"} 0 2 2
334 croak "ERROR: Finance::Nadex::get_epic(): invalid time\n"
335 unless $args{time}
336 || ( exists $args{period}
337 2 ▶ && $args{period} =~ /^event$/i );
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"time"} or exists $args{"period"} and $args{"period"} =~ /^event$/i 0 2 2
Condition50%3/6
$args{"time"} or exists $args{"period"} and $args{"period"} =~ /^event$/i
l!l&&r!l&&!r
110
exists $args{"period"} and $args{"period"} =~ /^event$/i
!ll&&!rl&&r
001
MC/DC0%0/3
$args{"time"} or exists $args{"period"} and $args{"period"} =~ /^event$/i
$args{"time"} exists $args{"period"} $args{"period"} =~ /^event$/i
Truth table50%2/4
A or B and C
A= $args{"time"}B= exists $args{"period"}C= $args{"period"} =~ /^event$/i
ABCresult
1XX1
0111
00X0
0100
338 croak "ERROR: Finance::Nadex::get_epic(): invalid instrument\n"
339 2 ▶ unless $args{instrument};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"instrument"} 0 2 2
340 croak "ERROR: Finance::Nadex::get_epic(): invalid strike\n"
341 2 ▶ unless $args{strike};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"strike"} 0 2 2
342  
343 2 ▶ $args{period} = ucfirst( lc( $args{period} ) ) if exists $args{period};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"period"} 2 0 2
344  
345 2 ▶ $args{time} = lc( $args{time} ) if exists $args{time};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"time"} 1 1 2
346  
347 2 my $market_list_ref = $self->_get($self->{base_url}.MARKET_LIST_URL);
348  
349 2 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless $market_list_ref 0 2 2
350 "ERROR: Finance::Nadex::get_epic(): failed to retrieve the market list from the exchange\n"
351 if !$market_list_ref;
352  
353 $market_id = $self->_get_market_id(
354 name => $args{market},
355 2 market_list_ref => $market_list_ref
356 );
357  
358 2 ▶ return undef unless $market_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $market_id 0 2 2
359  
360 2 $market_list_ref = $self->_get( $self->{base_url}.MARKET_LIST_URL . "/$market_id" );
361  
362 2 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless $market_list_ref 0 2 2
363 "ERROR: Finance::Nadex::get_epic(): failed to retrieve the market list from the exchange for market $market_id\n"
364 if !$market_list_ref;
365  
366 $market_id = $self->_get_market_id(
367 name => $args{instrument},
368 2 market_list_ref => $market_list_ref
369 );
370  
371 2 ▶ return undef unless $market_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $market_id 0 2 2
372  
373 2 $market_list_ref = $self->_get( $self->{base_url}.MARKET_LIST_URL . "/$market_id" );
374  
375 2 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless $market_list_ref 0 2 2
376 "ERROR: Finance::Nadex::get_epic(): failed to retrieve the market list from the exchange for market $market_id\n"
377 if !$market_list_ref;
378  
379 2 my $target_period_time;
380  
381 $target_period_time = "$args{period} ($args{time})"
382 2 ▶ if $args{period} eq 'Daily';
Branch: 1 branch
BranchTrueFalseTotal
if $args{"period"} eq "Daily" 1 1 2
383 2 ▶ $target_period_time = "-$args{time}" if $args{period} eq 'Intraday';
Branch: 1 branch
BranchTrueFalseTotal
if $args{"period"} eq "Intraday" 0 2 2
384 2 ▶ $target_period_time = "$args{period}" if $args{period} eq 'Weekly';
Branch: 1 branch
BranchTrueFalseTotal
if $args{"period"} eq "Weekly" 0 2 2
385 2 ▶ $target_period_time = "Open" if $args{period} eq 'Event';
Branch: 1 branch
BranchTrueFalseTotal
if $args{"period"} eq "Event" 1 1 2
386  
387 2 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
unless $target_period_time 0 2 2
388 "ERROR: Finance::Nadex::get_epic(): invalid period; must be one of: daily, weekly, intraday, event\n"
389 if !$target_period_time;
390  
391 2 $market_id = $self->_get_market_id(
392 name => $target_period_time,
393 market_list_ref => $market_list_ref,
394 accept_match => 1
395 );
396  
397 2 ▶ return undef unless $market_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $market_id 0 2 2
398  
399 2 $market_list_ref = $self->_get( $self->{base_url}.MARKET_LIST_URL . "/$market_id" );
400  
401 2 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless $market_list_ref 0 2 2
402 "ERROR: Finance::Nadex::get_epic(): failed to retrieve the market list from the exchange for market $market_id\n"
403 if !$market_list_ref;
404  
405 2 my $epic;
406 2 foreach my $market ( @{ $market_list_ref->{'markets'} } ) {
407 2 ▶ $args{time} = uc( $args{time} ) if exists $args{time};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"time"} 1 1 2
408 2 ▶ $args{time} = "" if !exists $args{time};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"time"} 1 1 2
409 2 ▶ if ( $market->{instrumentName} =~ /$args{strike}( \($args{time}\))?$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($market->{"instrumentName"} =~ /$args{"strike"}( \($args{"time"}\))?$/) 2 0 2
410 {
411 2 $epic = $market->{epic};
412 2 last;
413 }
414 }
415  
416 2 return $epic;
417  
418 }
419  
420 2 ▶sub get_market_instruments {
Subroutine get_market_instruments: called CC=8 SCAR=21.2
Pod documented
421  
422 2 my $self = shift;
423 2 my %args = @_;
424  
425 2 ▶ croak "ERROR: Finance::Nadex::get_market_instruments(): must be logged in\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->logged_in 0 2 2
426 unless $self->logged_in;
427  
428 croak
429 "ERROR: Finance::Nadex::get_market_instruments(): must provide market as named argument 'name'\n"
430 2 ▶ unless exists $args{name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"name"} 1 1 2
431 croak
432 "ERROR: Finance::Nadex::get_market_instruments(): invalid market name\n"
433 1 ▶ unless $args{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"name"} 0 1 1
434  
435 1 my $market_list_ref = $self->_get($self->{base_url}.MARKET_LIST_URL);
436  
437 1 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless $market_list_ref 0 1 1
438 "ERROR: Finance::Nadex::get_market_instruments(): failed to retrieve the market list from the exchange\n"
439 if !$market_list_ref;
440  
441 my $market_id = $self->_get_market_id(
442 name => $args{name},
443 1 market_list_ref => $market_list_ref
444 );
445  
446 1 ▶ return unless $market_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $market_id 0 1 1
447  
448 1 $market_list_ref = $self->_get( $self->{base_url}.MARKET_LIST_URL . '/' . $market_id );
449  
450 1 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless $market_list_ref 0 1 1
451 "ERROR: Finance::Nadex::get_market_instruments(): failed to retrieve the market list from the exchange for market $market_id\n"
452 if !$market_list_ref;
453  
454 1 my @instruments;
455 1 foreach my $market ( @{ $market_list_ref->{'hierarchy'} } ) {
456 1 push( @instruments, $market->{name} );
457 }
458 1 return @instruments;
459 }
460  
461 1 ▶sub get_markets {
Subroutine get_markets: called CC=4 SCAR=14.0
Pod documented
462  
463 1 my $self = shift;
464  
465 1 ▶ croak "ERROR: Finance::Nadex::get_markets(): must be logged in\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->logged_in 0 1 1
466 unless $self->logged_in;
467  
468 1 my $market_list_ref = $self->_get($self->{base_url}.MARKET_LIST_URL);
469  
470 1 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless $market_list_ref 0 1 1
471 "ERROR: Finance::Nadex::get_markets(): failed to retrieve the market list from the exchange\n"
472 if !$market_list_ref;
473  
474 1 my @markets;
475 1 foreach my $market ( @{ $market_list_ref->{'hierarchy'} } ) {
476 1 push( @markets, $market->{name} );
477 }
478  
479 1 return @markets;
480 }
481  
482 4 ▶sub get_time_series {
Subroutine get_time_series: called CC=25 SCAR=43.1
Pod documented
483  
484 4 my $self = shift;
485 4 my %args = @_;
486 4 my $found;
487  
488 4 ▶ croak "ERROR: Finance::Nadex::get_time_series(): must be logged in\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->logged_in 0 4 4
489 unless $self->logged_in;
490  
491 $args{instrument} = $index_name{ $args{instrument} }
492 4 ▶ if exists $args{instrument} && $index_name{ $args{instrument} };
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"instrument"} and $index_name{$args{"instrument"}} 0 4 4
Condition66%2/3
exists $args{"instrument"} and $index_name{$args{"instrument"}}
!ll&&!rl&&r
220
MC/DC0%0/2
exists $args{"instrument"} and $index_name{$args{"instrument"}}
exists $args{"instrument"} $index_name{$args{"instrument"}}
Truth table66%2/3
A and B
A= exists $args{"instrument"}B= $index_name{$args{"instrument"}}
ABresult
0X0
100
111
493  
494 croak
495 "ERROR: Finance::Nadex::get_time_series(): must specify a named argument 'market'\n"
496 4 ▶ unless exists $args{market};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"market"} 2 2 4
497 croak
498 "ERROR: Finance::Nadex::get_time_series(): must specify a named argument 'instrument'\n"
499 2 ▶ unless exists $args{instrument};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"instrument"} 1 1 2
500  
501 1 my $market_list_ref = $self->_get($self->{base_url}.MARKET_LIST_URL);
502  
503 1 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless $market_list_ref 0 1 1
504 "ERROR: Finance::Nadex::get_time_series(): failed to retrieve the market list from the exchange\n"
505 if !$market_list_ref;
506  
507 my $market_id = $self->_get_market_id(
508 name => $args{market},
509 1 market_list_ref => $market_list_ref
510 );
511  
512 1 ▶ return unless $market_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $market_id 0 1 1
513  
514 my $instruments_list_ref =
515 1 $self->_get( $self->{base_url}.MARKET_LIST_URL . '/' . $market_id );
516  
517 1 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless $market_list_ref 0 1 1
518 "ERROR: Finance::Nadex::get_time_series(): failed to retrieve the market list from the exchange for market $market_id\n"
519 if !$market_list_ref;
520  
521 1 my $instrument_id;
522 1 foreach my $instrument ( @{ $instruments_list_ref->{'hierarchy'} } ) {
523 $instrument_id = $instrument->{id}
524 1 ▶ if $instrument->{name} eq $args{instrument};
Branch: 1 branch
BranchTrueFalseTotal
if $instrument->{"name"} eq $args{"instrument"} 1 0 1
525 $instrument_id = $instrument->{id}
526 if $instrument->{name} eq 'Forex'
527 1 ▶ && $args{market} eq '5 Minute Binaries';
Branch: 1 branch
BranchTrueFalseTotal
if $instrument->{"name"} eq "Forex" and $args{"market"} eq "5 Minute Binaries" 0 1 1
Condition33%1/3
$instrument->{"name"} eq "Forex" and $args{"market"} eq "5 Minute Binaries"
!ll&&!rl&&r
100
MC/DC0%0/2
$instrument->{"name"} eq "Forex" and $args{"market"} eq "5 Minute Binaries"
$instrument->{"name"} eq "Forex" $args{"market"} eq "5 Minute Binaries"
Truth table33%1/3
A and B
A= $instrument->{"name"} eq "Forex"B= $args{"market"} eq "5 Minute Binaries"
ABresult
0X0
100
111
528 $instrument_id = $instrument->{id}
529 if $instrument->{name} eq 'Indices'
530 1 ▶ && $args{market} eq '20 Minute Binaries';
Branch: 1 branch
BranchTrueFalseTotal
if $instrument->{"name"} eq "Indices" and $args{"market"} eq "20 Minute Binaries" 0 1 1
Condition33%1/3
$instrument->{"name"} eq "Indices" and $args{"market"} eq "20 Minute Binaries"
!ll&&!rl&&r
100
MC/DC0%0/2
$instrument->{"name"} eq "Indices" and $args{"market"} eq "20 Minute Binaries"
$instrument->{"name"} eq "Indices" $args{"market"} eq "20 Minute Binaries"
Truth table33%1/3
A and B
A= $instrument->{"name"} eq "Indices"B= $args{"market"} eq "20 Minute Binaries"
ABresult
0X0
100
111
531 }
532  
533 1 ▶ return unless $instrument_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $instrument_id 0 1 1
534  
535 my $instrument_list_ref =
536 1 $self->_get( $self->{base_url}.MARKET_LIST_URL . '/' . $instrument_id );
537  
538 1 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless $market_list_ref 0 1 1
539 "ERROR: Finance::Nadex::get_time_series(): failed to retrieve the market list from the exchange for market $instrument_id\n"
540 if !$market_list_ref;
541  
542 1 ▶ if ( $args{market} eq '5 Minute Binaries'
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"market"} eq "5 Minute Binaries" or $args{"market"} eq "20 Minute Binaries") 0 1 1
Condition33%1/3
$args{"market"} eq "5 Minute Binaries" or $args{"market"} eq "20 Minute Binaries"
l!l&&r!l&&!r
001
MC/DC0%0/2
$args{"market"} eq "5 Minute Binaries" or $args{"market"} eq "20 Minute Binaries"
$args{"market"} eq "5 Minute Binaries" $args{"market"} eq "20 Minute Binaries"
Truth table33%1/3
A or B
A= $args{"market"} eq "5 Minute Binaries"B= $args{"market"} eq "20 Minute Binaries"
ABresult
1X1
011
000
543 || $args{market} eq '20 Minute Binaries' )
544 {
545 0 my @instruments;
546 0 foreach my $instrument ( @{ $instrument_list_ref->{'hierarchy'} } ) {
547 $instrument_id = $instrument->{id}
548 0 ▶ if $instrument->{name} eq $args{instrument};
Branch: 1 branch
BranchTrueFalseTotal
if $instrument->{"name"} eq $args{"instrument"} 0 0 0
549 }
550  
551 0 ▶ return unless $instrument_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $instrument_id 0 0 0
552  
553 $instrument_list_ref =
554 0 $self->_get( $self->{base_url}.MARKET_LIST_URL . '/' . $instrument_id );
555  
556 0 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless $market_list_ref 0 0 0
557 "ERROR: Finance::Nadex::get_time_series(): failed to retrieve the market list from the exchange for market $instrument_id\n"
558 if !$market_list_ref;
559  
560 0 my @contracts;
561 0 foreach my $contract ( @{ $instrument_list_ref->{'markets'} } ) {
562 0 push( @contracts, Finance::Nadex::Contract::_new($contract) );
563 }
564 0 return @contracts;
565  
566 }
567  
568 1 my @time_series;
569 1 foreach my $series ( @{ $instrument_list_ref->{'hierarchy'} } ) {
570 1 push( @time_series, $series->{name} );
571 }
572  
573 1 return @time_series;
574  
575 }
576  
577 65 ▶sub logged_in {
Subroutine logged_in: called CC=3 SCAR=11.0
Pod documented
578  
579 65 my $self = shift;
580  
581 65 ▶ return 0 unless $self->{security_token};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"security_token"} 11 54 65
582  
583 54 ▶ return 0 unless $self->{session_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"session_id"} 0 54 54
584  
585 54 return 1;
586  
587 }
588  
589 12 ▶sub login {
Subroutine login: called CC=7 SCAR=21.1
Pod documented
590  
591 12 my $self = shift;
592 12 my %args = @_;
593  
594 croak
595 "ERROR: Finance::Nadex::login(): must specify a named argument 'username'\n"
596 12 ▶ unless exists $args{username} || exists $self->{username};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"username"} or exists $self->{"username"} 0 12 12
Condition33%1/3
exists $args{"username"} or exists $self->{"username"}
l!l&&r!l&&!r
1200
MC/DC0%0/2
exists $args{"username"} or exists $self->{"username"}
exists $args{"username"} exists $self->{"username"}
Truth table33%1/3
A or B
A= exists $args{"username"}B= exists $self->{"username"}
ABresult
1X1
011
000
597 croak
598 "ERROR: Finance::Nadex::login(): must specify a named argument 'password'\n"
599 12 ▶ unless exists $args{password} || exists $self->{password};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"password"} or exists $self->{"password"} 0 12 12
Condition33%1/3
exists $args{"password"} or exists $self->{"password"}
l!l&&r!l&&!r
1200
MC/DC0%0/2
exists $args{"password"} or exists $self->{"password"}
exists $args{"password"} exists $self->{"password"}
Truth table33%1/3
A or B
A= exists $args{"password"}B= exists $self->{"password"}
ABresult
1X1
011
000
600  
601 12 ▶ $self->{username} = $args{username} if exists $args{username};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"username"} 12 0 12
602 12 ▶ $self->{password} = $args{password} if exists $args{password};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"password"} 12 0 12
603  
604 12 my $login_url = $self->{base_url}.LOGIN_URL;
605  
606 12 my $login_content = qq~
607 {
608 "advertisingId" : "",
609 "password": "$self->{password}",
610 "username": "$self->{username}"
611 }~;
612  
613 12 my $json_obj = $self->_post( $login_url, $login_content );
614  
615 12 $self->{user_agent}->cookie_jar->scan( \&_get_session_id );
616  
617 12 $self->{session_id} = $session_id;
618  
619 12 $self->{balance} = $json_obj->{accountInfo}->{available};
620  
621 12 return $self->logged_in();
622  
623 }
624  
625 23 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod documented
626  
627 23 my $class = shift;
628 23 my %args = @_;
629  
630 23 ▶ if (exists $args{platform} && $args{platform} eq 'demo') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'platform'} and $args{'platform'} eq 'demo') { } 0 23 23
Condition33%1/3
exists $args{"platform"} and $args{"platform"} eq "demo"
!ll&&!rl&&r
2300
MC/DC0%0/2
exists $args{"platform"} and $args{"platform"} eq "demo"
exists $args{"platform"} $args{"platform"} eq "demo"
Truth table33%1/3
A and B
A= exists $args{"platform"}B= $args{"platform"} eq "demo"
ABresult
0X0
100
111
631 0 $args{base_url} = 'https://demo-trade.nadex.com';
632 } else {
633 23 $args{base_url} = 'https://trade.nadex.com';
634 }
635  
636 23 my $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0 } );
637  
638 23 push @{ $ua->requests_redirectable }, 'POST', 'DELETE';
639 23 $ua->agent(
640 "vendor=IG Group | applicationType=dxd | platform=Android | deviceType=generic | version=1.13.2"
641 );
642 23 $ua->cookie_jar( { autosave => 1, ignore_discard => 1 } );
643  
644 23 $args{user_agent} = $ua;
645  
646 23 bless \%args, __PACKAGE__;
647  
648 }
649  
650 3 ▶sub retrieve_order {
Subroutine retrieve_order: called CC=5 SCAR=16.2
Pod documented
651  
652 3 my $self = shift;
653 3 my %args = @_;
654  
655 3 ▶ croak "ERROR: Finance::Nadex::retrieve_order(): must be logged in\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->logged_in 1 2 3
656 unless $self->logged_in;
657  
658 croak "ERROR: retrieve_order(): must specify a named argument 'id'\n"
659 2 ▶ unless exists $args{id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"id"} 1 1 2
660 1 ▶ croak "ERROR: retrieve_order(): invalid id\n" unless $args{id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"id"} 0 1 1
661  
662 1 my $order_id = $args{id};
663  
664 1 my $retrieve_order_url = $self->{base_url}.RETRIEVE_ORDER_URL . '/' . $order_id;
665  
666 1 my $order_ref = $self->_get($retrieve_order_url);
667  
668 1 ▶ return undef unless $order_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $order_ref 0 1 1
669  
670 1 return Finance::Nadex::Order::_new($order_ref);
671  
672 }
673  
674 2 ▶sub retrieve_orders {
Subroutine retrieve_orders: called CC=4 SCAR=13.9
Pod documented
675  
676 2 my $self = shift;
677  
678 2 ▶ croak "ERROR: Finance::Nadex::retrieve_orders(): must be logged in\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->logged_in 1 1 2
679 unless $self->logged_in();
680  
681 1 my $retrieve_orders_url = $self->{base_url}.RETRIEVE_ORDERS_URL;
682  
683 1 my $order_list_ref = $self->_get($retrieve_orders_url);
684  
685 1 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless $order_list_ref 0 1 1
686 "ERROR: Finance::Nadex::retrieve_orders(): failed to retrieve the order list from the exchange \n"
687 if !$order_list_ref;
688  
689 1 my $order_obj_list_ref = [];
690 1 foreach my $order (@$order_list_ref) {
691 1 push( @$order_obj_list_ref, Finance::Nadex::Order::_new($order) );
692 }
693  
694 1 return @$order_obj_list_ref;
695 }
696  
697 3 ▶sub retrieve_position {
Subroutine retrieve_position: called CC=5 SCAR=16.2
Pod documented
698  
699 3 my $self = shift;
700 3 my %args = @_;
701  
702 3 ▶ croak "ERROR: Finance::Nadex::retrieve_position(): must be logged in\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->logged_in 1 2 3
703 unless $self->logged_in;
704  
705 croak
706 "ERROR: Finance::Nadex::retrieve_position(): must specify a named argument 'id'\n"
707 2 ▶ unless exists $args{id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"id"} 1 1 2
708 croak "ERROR: Finance::Nadex::retrieve_position(): invalid id\n"
709 1 ▶ unless $args{id};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"id"} 0 1 1
710  
711 1 my $position_id = $args{id};
712  
713 1 my $retrieve_positions_url = $self->{base_url}.RETRIEVE_POSITION_URL . '/' . $position_id;
714  
715 1 my $position_ref = $self->_get($retrieve_positions_url);
716  
717 1 ▶ return undef unless $position_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $position_ref 0 1 1
718  
719 1 return Finance::Nadex::Position::_new($position_ref);
720  
721 }
722  
723 2 ▶sub retrieve_positions {
Subroutine retrieve_positions: called CC=4 SCAR=13.9
Pod documented
724  
725 2 my $self = shift;
726  
727 2 ▶ croak "ERROR: Finance::Nadex::retrieve_positions(): must be logged in\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->logged_in 1 1 2
728 unless $self->logged_in;
729  
730 1 my $retrieve_positions_url = $self->{base_url}.RETRIEVE_POSITIONS_URL;
731  
732 1 my $position_list_ref = $self->_get($retrieve_positions_url);
733  
734 1 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless $position_list_ref 0 1 1
735 "ERROR: Finance::Nadex::retrieve_positions(): failed to retrieve the position list from the exchange\n"
736 if !$position_list_ref;
737  
738 1 my $position_obj_list_ref = [];
739 1 foreach my $position (@$position_list_ref) {
740 1 push( @$position_obj_list_ref,
741 Finance::Nadex::Position::_new($position) );
742 }
743  
744 1 return @$position_obj_list_ref;
745  
746 }
747  
748 0 ▶sub _delete {
Subroutine _delete: not called CC=2 SCAR=17.9
749  
750 0 my $self = shift;
751 0 my $url = shift;
752 0 my $delete_content = shift;
753  
754 my $response = $self->{user_agent}->delete(
755 $url,
756 'Accept' => 'application/json; charset=UTF-8',
757 'Content-Type' => 'application/json; charset=UTF-8',
758 'Accept-Encoding' => 'text/html',
759 'X-SECURITY-TOKEN' => $self->{security_token},
760 0 'X-DEVICE-USER-AGENT' => 'vendor=IG | applicationType=Nadex | platform=web | deviceType=phone | version=0.707.0+8fa77b16',
761 'Content' => $delete_content
762 );
763  
764 0 ▶ $self->{security_token} = $response->header('X-SECURITY-TOKEN') if $response->header('X-SECURITY-TOKEN');
Branch: 1 branch
BranchTrueFalseTotal
if $response->header("X-SECURITY-TOKEN") 0 0 0
765 0 $self->{code} = $response->code;
766 0 $self->{content} = $response->content;
767  
768 my $delete_response =
769 0 eval { JSON->new->utf8->decode( $response->content ); };
770  
771 0 return $delete_response;
772 }
773  
774 29 ▶sub _get {
Subroutine _get: called CC=2 SCAR=6.9
775  
776 29 my $self = shift;
777 29 my $url = shift;
778  
779 my $response = $self->{user_agent}->get(
780 $url,
781 'Accept' => 'application/json; charset=UTF-8',
782 'Content-Type' => 'application/json; charset=UTF-8',
783 'Accept-Encoding' => 'text/html',
784 'X-SECURITY-TOKEN' => $self->{security_token},
785 29 'X-DEVICE-USER-AGENT' => 'vendor=IG | applicationType=Nadex | platform=web | deviceType=phone | version=0.707.0+8fa77b16',
786 Content => ""
787 );
788  
789 29 ▶ $self->{security_token} = $response->header('X-SECURITY-TOKEN') if $response->header('X-SECURITY-TOKEN');
Branch: 1 branch
BranchTrueFalseTotal
if $response->header("X-SECURITY-TOKEN") 29 0 29
790 29 $self->{code} = $response->code;
791 29 $self->{content} = $response->content;
792  
793 29 my $json = eval { JSON->new->utf8->decode( $response->content ); };
794  
795 29 return $json;
796 }
797  
798 16 ▶sub _post {
Subroutine _post: called CC=3 SCAR=11.0
799  
800 16 my $self = shift;
801 16 my $url = shift;
802 16 my $post_content = shift;
803  
804 my $response = $self->{user_agent}->post(
805 16 $url,
806 'Accept' => 'application/json; charset=UTF-8',
807 'Content-Type' => 'application/json; charset=UTF-8',
808 'Accept-Encoding' => 'text/html',
809 'Accept-Language' => 'en-US,en;q=0.9',
810 'X-DEVICE-USER-AGENT' => 'vendor=IG | applicationType=Nadex | platform=web | deviceType=phone | version=0.707.0+8fa77b16',
811 Content => $post_content
812 );
813  
814 16 ▶ $self->{security_token} = $response->header('X-SECURITY-TOKEN') if $response->header('X-SECURITY-TOKEN');
Branch: 1 branch
BranchTrueFalseTotal
if $response->header("X-SECURITY-TOKEN") 15 1 16
815 16 $self->{code} = $response->code;
816 16 ▶ $self->{content} = $response->content || undef;
Condition100%2/2
$response->content || undef
l!l
412
MC/DC100%1/1
$response->content || undef
$response->content
Truth table100%2/2
A
A= $response->content
Aresult
11
00
817  
818 16 my $json_obj = eval { JSON->new->utf8->decode( $response->content ); };
819  
820 16 return $json_obj;
821  
822 }
823  
824 9 ▶sub _get_market_id {
Subroutine _get_market_id: called CC=8 SCAR=21.1
825  
826 9 my $self = shift;
827 9 my %args = @_;
828 9 my $market_id;
829  
830 9 foreach my $market ( @{ $args{market_list_ref}->{'hierarchy'} } ) {
831 11 ▶ if ( !exists $args{accept_match} || $args{accept_match} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $args{'accept_match'} or $args{'accept_match'} == 0) { } 9 2 11
Condition66%2/3
not exists $args{"accept_match"} or $args{"accept_match"} == 0
l!l&&r!l&&!r
902
MC/DC50%1/2
not exists $args{"accept_match"} or $args{"accept_match"} == 0
not exists $args{"accept_match"} $args{"accept_match"} == 0
Truth table66%2/3
not(A) or B
A= not exists $args{"accept_match"}B= $args{"accept_match"} == 0
ABresult
1X1
011
000
832 9 ▶ if ( $market->{name} eq $args{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($market->{"name"} eq $args{"name"}) 7 2 9
833 7 $market_id = $market->{id};
834 }
835 }
836 else {
837 2 ▶ if ( $market->{name} =~ /$args{name}/
Branch: 1 branch
BranchTrueFalseTotal
if ($market->{"name"} =~ /$args{"name"}/ or $market->{"name"} eq $args{"name"}) 2 0 2
Condition66%2/3
$market->{"name"} =~ /$args{"name"}/ or $market->{"name"} eq $args{"name"}
l!l&&r!l&&!r
110
MC/DC0%0/2
$market->{"name"} =~ /$args{"name"}/ or $market->{"name"} eq $args{"name"}
$market->{"name"} =~ /$args{"name"}/ $market->{"name"} eq $args{"name"}
Truth table66%2/3
A or B
A= $market->{"name"} =~ /$args{"name"}/B= $market->{"name"} eq $args{"name"}
ABresult
1X1
011
000
838 || $market->{name} eq $args{name} )
839 {
840 2 $market_id = $market->{id};
841 }
842 }
843 }
844  
845 9 ▶ return $market_id || undef;
Condition50%1/2
$market_id || undef
l!l
90
MC/DC0%0/1
$market_id || undef
$market_id
Truth table50%1/2
A
A= $market_id
Aresult
11
00
846 }
847  
848 11 ▶sub _get_session_id {
Subroutine _get_session_id: called CC=2 SCAR=7.1
849  
850 11 my $key = $_[1];
851 11 my $val = $_[2];
852  
853 11 ▶ $session_id = $val if $key =~ /JSESSIONID/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /JSESSIONID/ 11 0 11
854  
855 }
856  
857 7 ▶sub _is_valid_price {
Subroutine _is_valid_price: called CC=11 SCAR=26.4
858  
859 7 my $self = shift;
860 7 my $price = shift;
861 7 my $type = shift;
862  
863 7 ▶ return 0 if $price =~ /-|\+/;
Branch: 1 branch
BranchTrueFalseTotal
if $price =~ /-|\+/ 1 6 7
864  
865 6 ▶ if ( $type eq 'binary' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "binary") 5 1 6
866 5 ▶ return 0 if $price !~ /^(\d+\.\d{1,2}|\.\d{1,2}|\d+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $price =~ /^(\d+\.\d{1,2}|\.\d{1,2}|\d+)$/ 0 5 5
867  
868 5 ▶ if ( $price =~ /\.(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($price =~ /\.(\d+)/) 5 0 5
869 5 ▶ return 0 if $1 != 0 && $1 != 25 && $1 != 50 && $1 != 75;
Branch: 1 branch
BranchTrueFalseTotal
if $1 != 0 and $1 != 25 and $1 != 50 and $1 != 75 0 5 5
Condition33%3/9
$1 != 0 and $1 != 25 and $1 != 50 and $1 != 75
!ll&&!rl&&r
500
$1 != 0 and $1 != 25 and $1 != 50
!ll&&!rl&&r
500
$1 != 0 and $1 != 25
!ll&&!rl&&r
500
MC/DC0%0/4
$1 != 0 and $1 != 25 and $1 != 50 and $1 != 75
$1 != 0 $1 != 25 $1 != 50 $1 != 75
Truth table20%1/5
A and B and C and D
A= $1 != 0B= $1 != 25C= $1 != 50D= $1 != 75
ABCDresult
0XXX0
10XX0
110X0
11100
11111
870 }
871 }
872  
873 6 ▶ if ( $type eq 'spread' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "spread") 1 5 6
874 1 ▶ return 0 if $price !~ /^(\d+|\d+\.\d{1,4})$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $price =~ /^(\d+|\d+\.\d{1,4})$/ 0 1 1
875 }
876  
877 6 return 1;
878 }
879  
880 6 ▶sub _is_valid_direction {
Subroutine _is_valid_direction: called CC=2 SCAR=6.9
881  
882 6 my $self = shift;
883 6 my $direction = shift;
884  
885 6 my %valid = qw( - 1 + 1 buy 1 sell 1);
886  
887 6 ▶ return exists $valid{$direction} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $valid{$direction} ? : 5 1 6
888 }
889  
890 5 ▶sub _is_valid_size {
Subroutine _is_valid_size: called CC=3 SCAR=11.0
891  
892 5 my $self = shift;
893 5 my $size = shift;
894  
895 5 ▶ return 0 if $size !~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $size =~ /^\d+$/ 1 4 5
896  
897 4 ▶ return 0 if $size == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $size == 0 0 4 4
898  
899 4 return 1;
900  
901 }
902  
903 =head1 NAME
904  
905 Finance::Nadex - Interact with the North American Derivatives Exchange
906  
907 =head1 VERSION
908  
909 Version 0.12
910  
911 =head1 SYNOPSIS
912  
913 Easily create orders, cancel orders, retrieve orders and retrieve positions on the North American Derivatives Exchange
914  
915  
916 use Finance::Nadex;
917  
918 # connects to the live platform when called as Finance::Nadex->new(); as an alternative
919 # it is possible to connect to the demo platform with Finance::Nadex->new(platform => 'demo')
920 my $client = Finance::Nadex->new();
921  
922 # must login to perform any actions, including simply querying market info
923 $client->login(username => 'yourusername', password => 'yourpassword');
924
925 # get the available balance in the account
926 my $balance = $client->balance();
927
928 # retrieve the epic (Nadex-assigned contract unique identifier) for the
929 # Daily, 3pm, GBP/USD > 1.5120 contract
930 my $epic = $client->get_epic( period => 'Daily', market => 'Forex (Binaries)', time => '3pm', instrument => 'GBP/USD', strike => '1.5120');
931
932 # suppose $epic now contains 'NB.D.GBP-USD.OPT-23-17-23Jan15.IP';
933 # create an order to buy 3 of those contracts for $34.50 each
934 my $order_id = $client->create_order( price => '34.50', direction => 'buy', epic => $epic, size => 3 );
935
936 # check the status of the order using the order id returned by the exchange;
937 # this call will return undef if the order doesn't exist; the order may not exist
938 # because the order was rejected by the exchange or because it was filled immediately
939 # and therefore is no longer a working order
940 my $order = $client->retrieve_order( id => $order_id );
941
942 # let's assume the order was created and is still a working (or pending) order;
943 # get the details of the order using the accessor methods provided by Finance::Nadex::Order
944 print join(" ", $order->direction, $order->size, $order->contract, $order->price), "\n";
945  
946 # suppose the order has now been filled; this means we have one open position;
947 # get the open positions
948 my @positions = $client->retrieve_positions();
949
950 # @positions now has 1 element which is a Finance::Nadex::Position; get its details
951 print join(" ", $positions[0]->id, $positions[0]->direction(), $positions[0]->size(), $positions[0]->contract(), $positions[0]->price), "\n";
952
953 # get the current best bid (the price at which we could sell the contract back immediately)
954 my $bid = $positions[0]->bid();
955
956 # suppose $bid is now $64.50 (we bought at $34.50, so we have a profit of $30);
957 # sell to close the position at $64.50
958 my $sell_to_close_order_id = $client->create_order( price => $bid, direction => 'sell', epic => $positions[0]->epic, size => $positions[0]->size() );
959  
960 # get all the time series (trading periods for contracts) currently
961 # available for GBP/USD binaries; the list of currently available markets
962 # can be obtained via a call to get_markets()
963 my @series = $client->get_time_series( market => 'Forex (Binaries)', instrument => 'GBP/USD' );
964
965 # elements of @series are simply strings, such as '2pm-4pm' or 'Daily (3pm)';
966 # suppose one of the elements of series is '8pm-10pm'; get a list of
967 # contracts available for trading within that market
968 my @contracts = $client->get_contracts( market => 'Forex (Binaries)', instrument => 'GBP/USD', series => 'Daily (3pm)' );
969
970 # @contracts now has a list in which each element is a Finance::Nadex::Contract; get
971 # the details of the available contracts using the accessors of Finance::Nadex::Contract
972 # including the current best bid and offer available on the exchange
973 foreach my $contract (@contracts) {
974 print join(" ", $contract->epic(), $contract->contract(), $contract->expirydate(), $contract->bid(), $contract->offer());
975 }
976
977  
978 # cancel any remaining open orders
979 $client->cancel_all_orders();
980  
981
982 =head1 SUBROUTINES/METHODS
983  
984 =head2 balance
985  
986 Retrieves the available account balance
987  
988 balance()
989  
990 Returns a number representing the available account balance
991  
992 =head2 cancel_all_orders
993  
994 Cancels all pending orders
995  
996 cancel_all_orders()
997  
998 Returns nothing
999  
1000 =head2 cancel_order
1001  
1002 Cancels the order with the specified order id
1003  
1004 cancel_order( id => 'NZ1234FGQ4AFFOPA12Z' )
1005  
1006 Returns the reference id created by the exchange for the cancelled order
1007  
1008 =head2 create_order
1009  
1010 Creates an order on the exchange with the specified parameters
1011  
1012 create_order( price => '34.50', direction => 'buy', epic => 'NB.D.GBP-USD.OPT-23-17-23Jan15.IP', size => 2 )
1013  
1014 price : the amount at which to buy or sell; the decimal portion of the number, if provided, must be .50 or .00 for binaries
1015  
1016 direction : one of 'buy', 'sell', '+', '-'
1017
1018 size : the number of contracts to buy or sell
1019  
1020 epic : the unique identifier for the contract to be bought or sold
1021  
1022 Returns: the order id created by the exchange to identify the order
1023  
1024 Note: '+' is an alias for 'buy'; '-' is an alias for 'sell'; the get_epic() method can be used to get the identifier for the contract of interest
1025  
1026 =head2 get_contract
1027  
1028 Retrieves the contract specified by an epic
1029  
1030 get_contract( epic => 'NB.D.GBP-USD.OPT-23-17-23Jan15.IP' )
1031  
1032 epic : the unique identifier created by the exchange for the contract
1033  
1034 Returns a L<Finance::Nadex::Contract> instance for the specified epic
1035  
1036 =head2 get_contracts
1037  
1038 Retrieves all the contracts available for trading within the given time series for the specified market and instrument
1039  
1040 get_contracts( market => 'Forex (Binaries)', instrument => 'GBP/USD', series => 'Daily (3pm)' )
1041  
1042 market : the name of the market for which the contracts are to be retrieved
1043
1044 instrument : the name of the instrument within the market for which contracts are to be retrieved; the
1045 instrument specified must be one of the instruments currently available for trading on the exchange
1046 for the provided market; the list of valid instruments can be obtained via get_market_instruments()
1047
1048 Returns a list in which each element is a L<Finance::Nadex::Contract> instance for each contract in the specified
1049 time series
1050  
1051 =head2 get_epic
1052  
1053 Retrieves the epic(unique identifier) for a contract with the specified parameters
1054
1055 get_epic(period => 'daily', strike => '1.5080', time => '3pm', instrument => 'GBP/USD', market => 'Forex (Binaries)')
1056  
1057 retrieves the epic with the specified parameters
1058
1059 period : specifies the frequency or period of the contract being searched for; one of 'daily', 'intraday', 'weekly', or 'event'
1060
1061 time : specifies the time at which the contract being searched for expires (not required when retrieving an event epic)
1062
1063 instrument : the asset type from which the contract being searched for derives value; an index, currency, or commodity
1064
1065 market : the market in which the specified contract exists (e.g. 'Forex (Binaries)', 'Indices (Binaries)'); must be one of markets returned by get_markets()
1066
1067 strike : the level of the underlying asset for the desired contract (e.g. 1.5010)
1068
1069 Returns the unique identifier of the contract
1070  
1071 =head2 get_market_instruments
1072  
1073 Retrieves the list of instruments associated with the specified market
1074  
1075 get_market_instruments( name => 'Forex (Binaries)' )
1076  
1077 name : the name of the market for which instruments are to be retrieved; this must match one of the names returned by get_markets()
1078
1079 Returns a list in which each element is a string containing the name of an instrument available for trading in the markets
1080  
1081  
1082 =head2 get_markets
1083  
1084 Retrieves the list of available markets on the exchange; the list of market names returned can be used in a call to get_market_instruments() to get further
1085 information about the market
1086
1087 get_markets()
1088  
1089 Returns a list in which each element is a string containing the name of a market in which instruments are traded
1090  
1091 =head2 get_time_series
1092  
1093 Retrieves the contract periods available for trading in the specified market for the given instrument
1094  
1095 get_time_series( market => 'Forex (Binaries)', instrument => 'AUD/USD' )
1096  
1097 market : the name of the market for which a time series is to be retrieved
1098
1099 instrument : the name of the instrument within the market for which a time series is to be retrieved; the
1100 instrument specified must be one of the instruments currently available for trading on the exchange
1101 for the provided market; the list of valid instruments can be obtained via get_market_instruments()
1102  
1103 In the case of the '5 Minute Binaries' and '20 Minute Binaries' markets, returns a list in which each element is
1104 a L<Finance::Nadex::Contract> representing each contract available in the series; for all other markets, returns
1105 a string containing the name of a time series for the given market and instrument;
1106 a time series designates the period during which the contract is available for trading including the expiration time
1107  
1108 =head2 login
1109  
1110 Submits the specified username and password to the exchange for authorization
1111  
1112 login( username => 'someusername', password => 'somepassword' );
1113  
1114 username : the username of the account
1115
1116 password : the password of the account
1117  
1118 Returns a true value on successful login; otherwise returns a false value
1119  
1120 =head2 logged_in
1121  
1122 Reports whether login was previously attempted and succeeded
1123  
1124 logged_in()
1125  
1126 Returns true if login was previously attempted and succeded; otherwise returns false
1127  
1128 =head2 new
1129  
1130 Creates an instance of a Finance::Nadex object
1131  
1132 new()
1133
1134 Returns a reference to a Finance::Nadex instance
1135  
1136 =head2 retrieve_order
1137  
1138 Gets the details of the pending order with the specified order id
1139  
1140 retrieve_order( id => 'NZ1234FGQ4AFFOPA12Z' )
1141  
1142 Returns an instance of L<Finance::Nadex::Order>
1143  
1144 =head2 retrieve_orders
1145  
1146 Gets the details of all pending orders
1147  
1148 retrieve_orders()
1149  
1150 Returns a list in which each element is a L<Finance::Nadex::Order>
1151  
1152 =head2 retrieve_position
1153  
1154 Retrieves the details of an individual open position
1155  
1156 retrieve_position ( id => 'NA12DZ45BNVA12A9BQZ' )
1157  
1158 Returns a L<Finance::Nadex::Position> object corresponding to the specified position id
1159  
1160 =head2 retrieve_positions
1161  
1162 Retrieves the details of all the open positions
1163  
1164 retrieve_positions()
1165  
1166 Returns a list in which each element is a L<Finance::Nadex::Position>
1167  
1168 =head1 AUTHOR
1169  
1170 mhandisi, C<< <mhandisi at cpan.org> >> AKA Charles Larry
1171  
1172 =head1 BUGS
1173  
1174 Please report any bugs or feature requests to C<bug-nadex-api at rt.cpan.org>, or through
1175 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Nadex-API>. I will be notified, and then you will
1176 automatically be notified of progress on your bug as I make changes.
1177  
1178 =head1 TODO
1179  
1180 Add support for watchlists.
1181  
1182 =head1 SUPPORT
1183  
1184 You can find documentation for this module with the perldoc command.
1185  
1186 perldoc Finance::Nadex
1187  
1188  
1189 You can also look for information at:
1190  
1191 =over 4
1192  
1193 =item * RT: CPAN's request tracker (report bugs here)
1194  
1195 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Nadex-API>
1196  
1197 =item * AnnoCPAN: Annotated CPAN documentation
1198  
1199 L<http://annocpan.org/dist/Nadex-API>
1200  
1201 =item * CPAN Ratings
1202  
1203 L<http://cpanratings.perl.org/d/Nadex-API>
1204  
1205 =item * Search CPAN
1206  
1207 L<http://search.cpan.org/dist/Nadex-API/>
1208  
1209 =back
1210  
1211  
1212 =head1 ACKNOWLEDGEMENTS
1213  
1214  
1215 =head1 LICENSE AND COPYRIGHT
1216  
1217 Copyright 2015 mhandisi.
1218  
1219 This program is free software; you can redistribute it and/or modify it
1220 under the terms of the the Artistic License (2.0). You may obtain a
1221 copy of the full license at:
1222  
1223 L<http://www.perlfoundation.org/artistic_license_2_0>
1224  
1225 Any use, modification, and distribution of the Standard or Modified
1226 Versions is governed by this Artistic License. By using, modifying or
1227 distributing the Package, you accept this license. Do not use, modify,
1228 or distribute the Package, if you do not accept this license.
1229  
1230 If your Modified Version has been derived from a Modified Version made
1231 by someone other than you, you are nevertheless required to ensure that
1232 your Modified Version complies with the requirements of this license.
1233  
1234 This license does not grant you the right to use any trademark, service
1235 mark, tradename, or logo of the Copyright Holder.
1236  
1237 This license includes the non-exclusive, worldwide, free-of-charge
1238 patent license to make, have made, use, offer to sell, sell, import and
1239 otherwise transfer the Package with respect to any patent claims
1240 licensable by the Copyright Holder that are necessarily infringed by the
1241 Package. If you institute patent litigation (including a cross-claim or
1242 counterclaim) against any party alleging that the Package constitutes
1243 direct or contributory patent infringement, then this Artistic License
1244 to you shall terminate on the date that such litigation is filed.
1245  
1246 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
1247 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
1248 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
1249 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
1250 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
1251 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
1252 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
1253 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1254  
1255  
1256 =cut
1257  
1258 14 42; # End of Finance::Nadex
« Nadex/Order.pm ↑ Summary Nadex/Contract.pm »