Testing/test.pl

filter: Statementstmt 75.5% 74 / 98 Branchbran 33.3% 14 / 42 Conditioncond 34.7% 8 / 23 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 57.7% 108 / 187 CC 2 SCAR 1.6
CC
2
Cov
59%
help
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.2
« lib/Finance/IG/REST/Client.pm ↑ Summary Testing/trans_test.pl »
1 #!/usr/bin/env perl
2 #######################################################################################
3 2 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use lib $FindBin::RealBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use lib $FindBin::RealBin."/../lib";
Subroutine BEGIN: called CC=1 SCAR=0.0
6 ##use lib $FindBin::RealBin."/Record";
7 #die "$FindBin::RealBin/../lib";
8 #BEGIN { push(@INC,"$FindBin::Realbin/../lib/Finance/IG") ; }
9 #use lib "$FindBin::RealBin/../lib/Finance/IG";
10  
11 #$"="\n"; die "@INC\n";
12 #use REST::Client;
13 #print "REST::Client from ".$INC{'REST/Client.pm'}."\n";
14 #die REST::Client->whoami();
15 #die;
16 use Package::Alias
17 2 ▶ 'REST::Client'=>'Finance::IG::REST::Client'
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ;
19  
20 2 ▶use Finance::IG;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Getopt::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 2 my $ig;
28 # with Testing::Record::REST;;Client need correct data
29  
30  
31 # die "inc=".$INC{'REST/Client.pm'};
32  
33 2 $ig=Finance::IG->new(
34 username=> "igusername",
35 password=> "ig_correct_password",
36 apikey=> "securitykey",
37 isdemo=>0,
38 );
39  
40 2 my %opt;
41 my $sortlist;
42  
43 2 my $valid=getopts('ho:tf:ng:s:S', \%opt);
44  
45 # t display headers every 10 lines
46 # n no headers
47 # f n : use format number
48 # h help
49 # g grep
50 # S print NO summary line
51 # s sort list
52 # sort list can be
53 # a comma seperated list of fields each with an optional minus sign eg
54 # -s'-profitpc,instrumentName' sorts by profit highest first, and if the same, then orders by name
55 # Here the following can be used as abbreviations
56 # n instrumentName
57 # o open => level
58 # b bid
59 # v value => atRisk
60 # p profitpe
61 # if no commas
62 # or a comma seperated list of elements, each of which may be optionally started with - for reverse sort
63 # the above abbrevs may be used. default is -p,n or -profitpc,instrumentName or -pn
64  
65 2 ▶if (exists $opt{s} and defined $opt{s})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt{"s"} and defined $opt{"s"}) 0 2 2
Condition33%1/3
exists $opt{"s"} and defined $opt{"s"}
!ll&&!rl&&r
200
MC/DC0%0/2
exists $opt{"s"} and defined $opt{"s"}
exists $opt{"s"} defined $opt{"s"}
Truth table33%1/3
A and B
A= exists $opt{"s"}B= defined $opt{"s"}
ABresult
0X0
100
111
66 {
67 0 my @list;
68 0 ▶ if ($opt{s}=~m/^[-nobvp]+$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'s'} =~ /^[-nobvp]+$/) { } 0 0 0
69 {
70 0 @list=split(//,$opt{s});
71 }
72 else
73 {
74 0 @list=split(/,/,$opt{s});
75 0 ▶ @list=map { s/^-//?('-',$_):$_ } @list;
Branch: 1 branch
BranchTrueFalseTotal
s/^-// ? : 0 0 0
76 }
77 0 for (@list)
78 {
79 0 ▶ $_='instrumentName' if ($_ eq 'n');
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "n" 0 0 0
80 0 ▶ $_='level' if ($_ eq 'o');
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "o" 0 0 0
81 0 ▶ $_='bid' if ($_ eq 'b');
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "b" 0 0 0
82 0 ▶ $_='atrisk' if ($_ eq 'v');
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "v" 0 0 0
83 0 ▶ $_='profitpc' if ($_ eq 'p');
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "p" 0 0 0
84 }
85 0 ▶ map { ($list[$_] eq '-') and $list[$_+1]='-'.$list[$_+1]; } (0..$#list);
Condition0%0/2
$list[$_] eq '-' && ($list[$_ + 1] = '-' . $list[$_ + 1])
!ll
00
MC/DC0%0/1
$list[$_] eq '-' && ($list[$_ + 1] = '-' . $list[$_ + 1])
$list[$_] eq '-'
Truth table0%0/2
A
A= $list[$_] eq '-'
Aresult
00
11
86 0 @list=grep { $_ ne '-' } @list;
87 0 $sortlist=\@list;
88 }
89  
90 2 ▶if (exists $opt{g} and !$opt{t})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt{"g"} and not $opt{"t"}) 0 2 2
Condition33%1/3
exists $opt{"g"} and not $opt{"t"}
!ll&&!rl&&r
200
MC/DC0%0/2
exists $opt{"g"} and not $opt{"t"}
exists $opt{"g"} not $opt{"t"}
Truth table33%1/3
A and not(B)
A= exists $opt{"g"}B= not $opt{"t"}
ABresult
0X0
100
111
91 {
92 0 $opt{S}=1;
93 0 $opt{n}=!$opt{n};
94 }
95  
96 2 my $out;
97 2 $out=*STDOUT;
98 2 ▶if ($opt{o})
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"o"}) 0 2 2
99 {
100 # output file supplied
101
102 0 my $d="Testing";
103 0 my $f=$d."/".$opt{o};
104  
105 0 ▶ open($out,">$f") or die "failed to open $f";
Branch: 1 branch
BranchTrueFalseTotal
open $out, ">$f" or die "failed to open $f" 0 0 0
106
107 }
108  
109 2 ▶!$valid and help("Invalid arguments");
Branch: 1 branch
BranchTrueFalseTotal
$valid or help("Invalid arguments") 2 0 2
110 2 ▶$opt{h} and help();
Branch: 1 branch
BranchTrueFalseTotal
$opt{"h"} and help() 1 1 2
111  
112 1 $ig->login();
113  
114 1 my $p=$ig->positions();
115 1 $p=$ig->agg($p,$sortlist);
116  
117 1 ▶printf $out "%d Positions\n",@$p+0 if (!$opt{S});
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"S"} 1 0 1
118  
119 1 my @format=(
120 "%-41sinstrumentName %+6.2fsize %-9.2flevel ".
121 "%-9.2fbid £%-8.2fprofit %5.1fprofitpc%% £%10.2fatrisk\n",
122
123 "%sepic|%sinstrumentName|%0.2fsize|%-0.2flevel|".
124 "%-0.2fbid|£%-0.2fprofit|%0.1fprofitpc%%|£%0.2fatrisk\n",
125
126 "%sepic|%sinstrumentName|%0.2fsize|%-0.2flevel|".
127 "%-0.2fbid|£%-0.2fprofit|%0.1fprofitpc%%|£%0.2fatrisk|%smarketStatus\n",
128 );
129  
130 1 my $format=$format[0];
131 1 ▶if (defined($opt{f}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"f"}) 0 1 1
132 {
133 0 ▶ die "option fn out of range (0..".(@format+0).")" if ($opt{f}<1 || $opt{f}>@format+0);
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"f"} < 1 or $opt{"f"} > @format + 0 0 0 0
Condition0%0/3
$opt{"f"} < 1 or $opt{"f"} > @format + 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{"f"} < 1 or $opt{"f"} > @format + 0
$opt{"f"} < 1 $opt{"f"} > @format + 0
Truth table0%0/3
A or B
A= $opt{"f"} < 1B= $opt{"f"} > @format + 0
ABresult
1X1
011
000
134 0 $format=$format[$opt{f}-1];
135 #if ($opt{f}>1)
136 #{
137 # $ig->uds('');
138 # $demo->uds('');
139 #}
140 }
141  
142 1 my $titles;
143 ## $titles=["Epic", 'Name','Size','Open','Latest','P/L','P/L','Value'];
144 1 $titles=undef;
145  
146 1 my $value=0;
147 1 my $profit=0;
148 1 my $count=0;
149  
150 1 for my $position (@$p)
151 {
152 # $ig-> printpos($out,$position,$format,-0.5,+0.5);
153 $ig->printpos($out , $titles, $format)
154 66 ▶ if (!$opt{n} and ($count==0 or ( $opt{t} and ($count+1)%10==0)));
Branch: 1 branch
BranchTrueFalseTotal
if not $opt{"n"} and $count == 0 || $opt{"t"} && ($count + 1) % 10 == 0 1 65 66
Condition55%5/9
not $opt{"n"} and $count == 0 || $opt{"t"} && ($count + 1) % 10 == 0
!ll&&!rl&&r
0651
$count == 0 or $opt{"t"} and ($count + 1) % 10 == 0
l!l&&r!l&&!r
1065
$opt{"t"} and ($count + 1) % 10 == 0
!ll&&!rl&&r
6500
MC/DC25%1/4
not $opt{"n"} and $count == 0 || $opt{"t"} && ($count + 1) % 10 == 0
not $opt{"n"} $count == 0 $opt{"t"} ($count + 1) % 10 == 0
Truth table40%2/5
not(A) and B or C and D
A= not $opt{"n"}B= $count == 0C= $opt{"t"}D= ($count + 1) % 10 == 0
ABCDresult
0XXX0
100X0
10100
11XX1
10111
155 66 $count++;
156
157 $ig-> printpos($out,$position,$format)
158 66 ▶ if (!$opt{g} or $position->{instrumentName}=~m/$opt{g}/i);
Branch: 1 branch
BranchTrueFalseTotal
if not $opt{"g"} or $position->{"instrumentName"} =~ /$opt{"g"}/i 66 0 66
Condition33%1/3
not $opt{"g"} or $position->{"instrumentName"} =~ /$opt{"g"}/i
l!l&&r!l&&!r
6600
MC/DC0%0/2
not $opt{"g"} or $position->{"instrumentName"} =~ /$opt{"g"}/i
not $opt{"g"} $position->{"instrumentName"} =~ /$opt{"g"}/i
Truth table33%1/3
not(A) or B
A= not $opt{"g"}B= $position->{"instrumentName"} =~ /$opt{"g"}/i
ABresult
1X1
011
000
159  
160 66 $value+=$position->{bid}*$position->{size};
161 66 $profit+=$position->{profit};
162 }
163  
164 1 ▶$ig->printpos($out , $titles, $format) if (!$opt{n});
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"n"} 1 0 1
165  
166 1 my $capital=62000+20000+10000+10000+2000+10000+10000+10000+5000;
167  
168 1 my $accounts=$ig->accounts();
169 # my ($account)=grep { $_->{accountId} eq "..." } @$accounts;
170 1 my ($account)=grep { $_->{accountType} eq "SPREADBET" } @$accounts;
171  
172 1 my $balance=$account->{balance}->{balance};
173 1 my $margin=$account->{balance}->{balance}+$account->{balance}->{profitLoss}-$account->{balance}->{available};
174 1 my $available=$account->{balance}->{available};
175  
176 1 my $pc=int(1000*($profit+$balance-$capital)/$capital)/10;
177 1 my $ppc=int(1000*$profit/$value)/10;
178 1 ▶print "# Total value $value balance=£$balance profit on open trades=£$profit as % of value $ppc% capital=$capital margin/av=£$margin/£$available profit/capital=$pc%\n" if (!$opt{S});
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"S"} 1 0 1
179  
180  
181 sub help
182 1 ▶{ my ($mess)=@_;
Subroutine help: called CC=2 SCAR=7.0
183 # -o auto-generate a time based file and use this to send output to
184 # t display headers every 10 lines
185 # n no headers
186 # f n : use format number
187 # h help
188 # g grep
189 # s print NO summary line
190 1 ▶ print $out "$mess\n" if ($mess);
Branch: 1 branch
BranchTrueFalseTotal
if $mess 0 1 1
191 1 print $out "
192  
193 This is the test program for IG.pm. Its tested via modification of a standard display program
194  
195 -o filename store output in Testing/filenme
196 -f n use one of the inbuilt formats 1 or 2
197 -h this message and exit
198 -n xxx use xxx as a pattern in name to grep for particular positions
199 -S print no summary line.
200 -N toggle printing of headers.
201 -t print title lines every 10 lines.
202 -s x Sort output by x where x is a comma seperated list of fields
203 optionaly preceded by a minus (for reverse sort). Common fields
204 can be abrievated by the following single letter abbreviations,
205 n instrumentName
206 o level (opening price for position)
207 b bid
208 v atRisk (value atRisk in a position)
209 p profitpe (percentage profit. )
210 The default could be written in any of the following ways:
211 -s-profitpc,instrumentName or -s-pn or -s-p,n
212 It sorts by decending order of profit and where this is the
213 same, orders by name.
214 -O [+-=]date[time] Show only positions held Opened later or equal (+) equal to (-)
215 or earlier or equal (-) than the given date or datetime, eg
216 -O 2020/11/05 or -O 2020/11/05T15:00:00
217 Equal to means with ref to the format so that
218 2020/11/05 means the days are equal while 2020/11 means amy time that month
219 ";
220 1 exit(1);
221 }
« lib/Finance/IG/REST/Client.pm ↑ Summary Testing/trans_test.pl »