App/financeta/gui/tradereport.pm

filter: Statementstmt 92.3% 24 / 26 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 94.2% 33 / 35 CC 1 SCAR 0.3
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« App/financeta/gui/editor.pm ↑ Summary App/financeta/gui.pm »
1 package App::financeta::gui::tradereport;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 our $VERSION = '0.16';
7 $VERSION = eval $VERSION;
8 1 ▶use App::financeta::mo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use App::financeta::utils qw(dumper log_filter);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Log::Any '$log', filter => \&App::financeta::utils::log_filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 if ($^O !~ /win32/i) {
14 eval {
15 require POE;
16 require POE::Kernel;
17 POE::Kernel->import({loop => 'Prima'});
18 require POE::Session;
19 } or die "Unable to load POE::Loop::Prima";
20 }
21 #use Prima qw(Application DetailedList ScrollWidget MsgBox StdDlg);
22 0 ▶use Prima qw(
Subroutine BEGIN: called CC=1 SCAR=2.6
23 Application Buttons MsgBox Calendar ComboBox Notebooks
24 Widget::ScrollWidget DetailedList Dialog::ColorDialog
25 Dialog::FileDialog Dialog::FindDialog ScrollBar
26 Dialog::PrintDialog Dialog::ImageDialog Dialog::FontDialog
27 sys::GUIException
28 0 );
29 use PDL::Lite;
30  
31 $| = 1;
32 has debug => 0;
33 has parent => undef;
34 has main => (builder => '_build_main');
35 has brand => __PACKAGE__;
36 has tab_name => undef;
37  
38 sub _build_main {
39 my $self = shift;
40 my $mw = new Prima::Window(
41 name => 'tradereport',
42 text => $self->brand,
43 size => [640, 480],
44 owner => $self->parent->main,
45 centered => 1,
46 # force border styles for consistency
47 borderIcons => bi::All,
48 borderStyle => bs::Sizeable,
49 windowState => ws::Normal,
50 # origin
51 left => 10,
52 top => 0,
53 visible => 0,
54 menuItems => [[
55 '~Action' => [
56 [
57 'save_report', '~Save', 'Ctrl+S', '^S',
58 sub {
59 my ($win, $item) = @_;
60 my $trw = $win->menu->options($item);
61 $trw->save;
62 },
63 $self,
64 ],
65 [
66 'close_report', '~Close', 'Ctrl+W', '^W',
67 sub {
68 my ($win, $item) = @_;
69 my $trw = $win->menu->options($item);
70 $trw->close;
71 },
72 $self,
73 ],
74 ],
75 ]],
76 onDestroy => sub {
77 if ($self->parent and $self->tab_name) {
78 $self->parent->close_tradereport($self->tab_name);
79 }
80 },
81 );
82 $self->_create_sheet($mw, []);
83 $self->_create_label($mw, 0.0);
84 return $mw;
85 }
86  
87 sub _create_label {
88 my ($self, $mw, $grosspnl) = @_;
89 my $is_dark_mode = 0;
90 if ($mw->backColor ne cl::White) {
91 $log->debug("TradeReport window is in dark mode\n");
92 $is_dark_mode = 1;
93 }
94 my $txt = sprintf "Net Income: \$%0.02f", $grosspnl;
95 my $color;
96 if ($is_dark_mode) {
97 $color = ($grosspnl > 0) ? cl::LightGreen : cl::Magenta;
98 } else {
99 $color = ($grosspnl > 0) ? cl::Blue : cl::Red;
100 }
101 my @sz = $mw->size;
102 return $mw->insert('Label',
103 name => 'tradereport_label',
104 pack => { fill => 'both' },
105 text => $txt,
106 origin => [20, 20],
107 autoHeight => 1,
108 width => $sz[0],
109 alignment => ta::Left,
110 font => { height => 14, style => fs::Bold },
111 color => $color,
112 hint => 'This is the sum total P&L',
113 );
114 }
115  
116 sub _create_sheet {
117 my ($self, $mw, $items) = @_;
118 my @sz = $mw->size;
119 $sz[0] *= 0.80;
120 $sz[1] *= 0.80;
121 my $headers = ['Date', 'Entry', 'Price($)', 'Quantity', 'Date', 'Exit', 'Price($)',
122 'Quantity', 'Net($)'];
123 return $mw->insert('DetailedList',
124 name => 'tradereport_sheet',
125 pack => { expand => 1, fill => 'both' },
126 items => $items || [],
127 origin => [ 10, 40 ],
128 headers => $headers,
129 hScroll => 1,
130 vScroll => 1,
131 growMode => gm::Client | gm::GrowHiX | gm::GrowHiY,
132 columns => scalar @$headers,
133 size => \@sz,
134 visible => 1,
135 onSort => sub {
136 my ($p, $col, $dir) = @_;
137 return if $col != 1; # only sort by date which is the first column
138 if ($dir) {
139 $p->{items} = [
140 sort {$$a[$col] <=> $$b[$col]}
141 @{$self->{items}}
142 ];
143 } else {
144 $p->{items} = [
145 sort {$$b[$col] <=> $$a[$col]}
146 @{$self->{items}}
147 ];
148 }
149 $p->clear_event;
150 },
151 );
152 }
153  
154 sub update {
155 my ($self, $tabname, $buysells) = @_;
156 $self->tab_name($tabname) if defined $tabname;
157 return unless defined $buysells;
158 my $longs = $buysells->{longs};
159 my $shorts = $buysells->{shorts};
160 my $qty = $buysells->{quantity};
161 my @items = ();
162 my $tz = $self->parent->timezone;
163 my $grosspnl = $buysells->{longs_pnl} + $buysells->{shorts_pnl};
164 if (defined $longs and not $longs->isnull and not $longs->isempty) {
165 my $longitems = $longs->transpose->unpdl;
166 foreach my $arr (@$longitems) {
167 my $dt1 = DateTime->from_epoch(epoch => $arr->[0], time_zone => $tz)->ymd('-');
168 my $dt2 = DateTime->from_epoch(epoch => $arr->[2], time_zone => $tz)->ymd('-');
169 my $pnl = sprintf "%0.02f", (($arr->[3] - $arr->[1]) * $qty);
170 my $row = [ $dt1, 'BUY', $arr->[1], $qty, $dt2, 'SELL', $arr->[3], $qty, $pnl ];
171 push @items, $row;
172 }
173 }
174 if (defined $shorts and not $shorts->isnull and not $shorts->isempty) {
175 my $shortitems = $shorts->transpose->unpdl;
176 foreach my $arr (@$shortitems) {
177 my $dt1 = DateTime->from_epoch(epoch => $arr->[0], time_zone => $tz)->ymd('-');
178 my $dt2 = DateTime->from_epoch(epoch => $arr->[2], time_zone => $tz)->ymd('-');
179 my $pnl = sprintf "%0.02f", (($arr->[1] - $arr->[3]) * $qty);
180 my $row = [ $dt1, 'SELL', $arr->[1], $qty, $dt2, 'BUY', $arr->[3], $qty, $pnl];
181 push @items, $row;
182 }
183 }
184 $self->main->tradereport_sheet->close;
185 $self->main->tradereport_label->close;
186 $self->_create_sheet($self->main, \@items);
187 $self->_create_label($self->main, $grosspnl);
188 $self->main->show;
189 $self->main->bring_to_front;
190 1;
191 }
192  
193 sub close {
194 my $self = shift;
195 if ($self->parent) {
196 $self->parent->close_tradereport($self->tab_name);
197 }
198 $self->main->close;
199 }
200  
201 sub save {
202 my $self = shift;
203 my @headers = $self->main->tradereport_sheet->headers;
204 my $items = $self->main->tradereport_sheet->items;
205 unless (scalar @$items) {
206 message("Nothing to save in the report");
207 return;
208 }
209 my $symbol = $self->tab_name;
210 $symbol =~ s/tab_//g;
211 my $docdir = File::HomeDir->my_documents || File::HomeDir->my_home;
212 my $ext = 'csv';
213 my $dlg = Prima::Dialog::SaveDialog->new(
214 fileName => "tradereport_$symbol\.$ext",
215 filter => [
216 ['CSV files' => "*.$ext"],
217 ['All files' => '*'],
218 ],
219 filterIndex => 0,
220 multiSelect => 0,
221 overwritePrompt => 1,
222 pathMustExist => 1,
223 directory => $docdir,
224 );
225 my $filename = $dlg->fileName if $dlg->execute;
226 if ($filename) {
227 if ($^O !~ /Win32/) {
228 $filename = File::Spec->catfile($docdir, $filename) unless ($filename =~ /^\//);
229 } else {
230 $filename .= ".$ext" unless $filename =~ /\.$ext$/; #windows is weird
231 }
232 } else {
233 $log->warn("Saving the report was canceled.");
234 return;
235 }
236 CORE::open (my $fh, '>', $filename) or message("Unable to open $filename to write the report");
237 if ($fh) {
238 print $fh join(',', @headers), "\n";
239 foreach my $arr (@$items) {
240 print $fh join(',', @$arr), "\n";
241 }
242 CORE::close $fh;
243 $log->debug("Done writing $filename");
244 }
245 }
246  
247 1;
248 __END__
« App/financeta/gui/editor.pm ↑ Summary App/financeta/gui.pm »