| 1 | #!/usr/bin/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 | 0 | 'REST::Client'=>'Finance::IG::REST::Client' | |
| 18 | 2 | ▶ | ; |
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
| |||
| 19 | |||
| 20 | use Finance::IG; | ||
| 21 | use Getopt::Std; | ||
| 22 | use Time::Piece; | ||
| 23 | use strict; | ||
| 24 | no strict 'refs'; | ||
| 25 | use warnings; | ||
| 26 | |||
| 27 | my $ig; | ||
| 28 | # with Testing::Record::REST;;Client need correct data | ||
| 29 | |||
| 30 | |||
| 31 | # die "inc=".$INC{'REST/Client.pm'}; | ||
| 32 | |||
| 33 | $ig=Finance::IG->new( | ||
| 34 | username=> "igusername", | ||
| 35 | password=> "ig_correct_password", | ||
| 36 | apikey=> "securitykey", | ||
| 37 | isdemo=>0, | ||
| 38 | ); | ||
| 39 | |||
| 40 | my %opt; | ||
| 41 | my $sortlist; | ||
| 42 | |||
| 43 | 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 | if (exists $opt{s} and defined $opt{s}) | ||
| 66 | { | ||
| 67 | my @list; | ||
| 68 | if ($opt{s}=~m/^[-nobvp]+$/) | ||
| 69 | { | ||
| 70 | @list=split(//,$opt{s}); | ||
| 71 | } | ||
| 72 | else | ||
| 73 | { | ||
| 74 | @list=split(/,/,$opt{s}); | ||
| 75 | @list=map { s/^-//?('-',$_):$_ } @list; | ||
| 76 | } | ||
| 77 | for (@list) | ||
| 78 | { | ||
| 79 | $_='instrumentName' if ($_ eq 'n'); | ||
| 80 | $_='level' if ($_ eq 'o'); | ||
| 81 | $_='bid' if ($_ eq 'b'); | ||
| 82 | $_='atrisk' if ($_ eq 'v'); | ||
| 83 | $_='profitpc' if ($_ eq 'p'); | ||
| 84 | } | ||
| 85 | map { ($list[$_] eq '-') and $list[$_+1]='-'.$list[$_+1]; } (0..$#list); | ||
| 86 | @list=grep { $_ ne '-' } @list; | ||
| 87 | $sortlist=\@list; | ||
| 88 | } | ||
| 89 | |||
| 90 | if (exists $opt{g} and !$opt{t}) | ||
| 91 | { | ||
| 92 | $opt{S}=1; | ||
| 93 | $opt{n}=!$opt{n}; | ||
| 94 | } | ||
| 95 | |||
| 96 | my $out; | ||
| 97 | $out=*STDOUT; | ||
| 98 | if ($opt{o}) | ||
| 99 | { | ||
| 100 | # output file supplied | ||
| 101 | |||
| 102 | my $d="Testing"; | ||
| 103 | my $f=$d."/".$opt{o}; | ||
| 104 | |||
| 105 | open($out,">$f") or die "failed to open $f"; | ||
| 106 | |||
| 107 | } | ||
| 108 | |||
| 109 | !$valid and help("Invalid arguments"); | ||
| 110 | $opt{h} and help(); | ||
| 111 | |||
| 112 | $ig->login(); | ||
| 113 | |||
| 114 | my $p=$ig->positions(); | ||
| 115 | $p=$ig->agg($p,$sortlist); | ||
| 116 | |||
| 117 | printf $out "%d Positions\n",@$p+0 if (!$opt{S}); | ||
| 118 | |||
| 119 | 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 | my $format=$format[0]; | ||
| 131 | if (defined($opt{f})) | ||
| 132 | { | ||
| 133 | die "option fn out of range (0..".(@format+0).")" if ($opt{f}<1 || $opt{f}>@format+0); | ||
| 134 | $format=$format[$opt{f}-1]; | ||
| 135 | #if ($opt{f}>1) | ||
| 136 | #{ | ||
| 137 | # $ig->uds(''); | ||
| 138 | # $demo->uds(''); | ||
| 139 | #} | ||
| 140 | } | ||
| 141 | |||
| 142 | my $titles; | ||
| 143 | ## $titles=["Epic", 'Name','Size','Open','Latest','P/L','P/L','Value']; | ||
| 144 | $titles=undef; | ||
| 145 | |||
| 146 | my $value=0; | ||
| 147 | my $profit=0; | ||
| 148 | my $count=0; | ||
| 149 | |||
| 150 | for my $position (@$p) | ||
| 151 | { | ||
| 152 | # $ig-> printpos($out,$position,$format,-0.5,+0.5); | ||
| 153 | $ig->printpos($out , $titles, $format) | ||
| 154 | if (!$opt{n} and ($count==0 or ( $opt{t} and ($count+1)%10==0))); | ||
| 155 | $count++; | ||
| 156 | |||
| 157 | $ig-> printpos($out,$position,$format) | ||
| 158 | if (!$opt{g} or $position->{instrumentName}=~m/$opt{g}/i); | ||
| 159 | |||
| 160 | $value+=$position->{bid}*$position->{size}; | ||
| 161 | $profit+=$position->{profit}; | ||
| 162 | } | ||
| 163 | |||
| 164 | $ig->printpos($out , $titles, $format) if (!$opt{n}); | ||
| 165 | |||
| 166 | my $capital=62000+20000+10000+10000+2000+10000+10000+10000+5000; | ||
| 167 | |||
| 168 | my $accounts=$ig->accounts(); | ||
| 169 | # my ($account)=grep { $_->{accountId} eq "..." } @$accounts; | ||
| 170 | my ($account)=grep { $_->{accountType} eq "SPREADBET" } @$accounts; | ||
| 171 | |||
| 172 | my $balance=$account->{balance}->{balance}; | ||
| 173 | my $margin=$account->{balance}->{balance}+$account->{balance}->{profitLoss}-$account->{balance}->{available}; | ||
| 174 | my $available=$account->{balance}->{available}; | ||
| 175 | |||
| 176 | my $pc=int(1000*($profit+$balance-$capital)/$capital)/10; | ||
| 177 | my $ppc=int(1000*$profit/$value)/10; | ||
| 178 | 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}); | ||
| 179 | |||
| 180 | |||
| 181 | sub help | ||
| 182 | { my ($mess)=@_; | ||
| 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 | print $out "$mess\n" if ($mess); | ||
| 191 | 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 | exit(1); | ||
| 221 | } | ||