App-financeta

 view release on metacpan or  search on metacpan

lib/App/financeta/gui/tradereport.pm  view on Meta::CPAN

        # origin
        left => 10,
        top => 0,
        visible => 0,
        menuItems => [[
            '~Action' => [
                [
                    'save_report', '~Save', 'Ctrl+S', '^S',
                    sub {
                        my ($win, $item) = @_;
                        my $trw = $win->menu->options($item);
                        $trw->save;
                    },
                    $self,
                ],
                [
                    'close_report', '~Close', 'Ctrl+W', '^W',
                    sub {
                        my ($win, $item) = @_;
                        my $trw = $win->menu->options($item);
                        $trw->close;
                    },
                    $self,
                ],
            ],
        ]],
        onDestroy => sub {
            if ($self->parent and $self->tab_name) {
                $self->parent->close_tradereport($self->tab_name);
            }
        },
    );
    $self->_create_sheet($mw, []);
    $self->_create_label($mw, 0.0);
    return $mw;
}

sub _create_label {
    my ($self, $mw, $grosspnl) = @_;
    my $is_dark_mode = 0;
    if ($mw->backColor ne cl::White) {
        $log->debug("TradeReport window is in dark mode\n");
        $is_dark_mode = 1;
    }
    my $txt = sprintf "Net Income: \$%0.02f", $grosspnl;
    my $color;
    if ($is_dark_mode) {
        $color = ($grosspnl > 0) ? cl::LightGreen : cl::Magenta;
    } else {
        $color = ($grosspnl > 0) ? cl::Blue : cl::Red;
    }
    my @sz = $mw->size;
    return $mw->insert('Label',
        name => 'tradereport_label',
        pack => { fill => 'both' },
        text => $txt,
        origin => [20, 20],
        autoHeight => 1,
        width => $sz[0],
        alignment => ta::Left,
        font => { height => 14, style => fs::Bold },
        color => $color,
        hint => 'This is the sum total P&L',
    );
}

sub _create_sheet {
    my ($self, $mw, $items) = @_;
    my @sz = $mw->size;
    $sz[0] *= 0.80;
    $sz[1] *= 0.80;
    my $headers = ['Date', 'Entry', 'Price($)', 'Quantity', 'Date', 'Exit', 'Price($)',
        'Quantity', 'Net($)'];
    return $mw->insert('DetailedList',
        name => 'tradereport_sheet',
        pack => { expand => 1, fill => 'both' },
        items => $items || [],
        origin => [ 10, 40 ],
        headers => $headers,
        hScroll => 1,
        vScroll => 1,
        growMode => gm::Client | gm::GrowHiX | gm::GrowHiY,
        columns => scalar @$headers,
        size => \@sz,
        visible => 1,
        onSort => sub {
            my ($p, $col, $dir) = @_;
            return if $col != 1; # only sort by date which is the first column
            if ($dir) {
                $p->{items} = [
                    sort {$$a[$col] <=> $$b[$col]}
                    @{$self->{items}}
                ];
            } else {
                $p->{items} = [
                    sort {$$b[$col] <=> $$a[$col]}
                    @{$self->{items}}
                ];
            }
            $p->clear_event;
        },
    );
}

sub update {
    my ($self, $tabname, $buysells) = @_;
    $self->tab_name($tabname) if defined $tabname;
    return unless defined $buysells;
    my $longs = $buysells->{longs};
    my $shorts = $buysells->{shorts};
    my $qty = $buysells->{quantity};
    my @items = ();
    my $tz = $self->parent->timezone;
    my $grosspnl = $buysells->{longs_pnl} + $buysells->{shorts_pnl};
    if (defined $longs and not $longs->isnull and not $longs->isempty) {
        my $longitems = $longs->transpose->unpdl;     
        foreach my $arr (@$longitems) {
            my $dt1 = DateTime->from_epoch(epoch => $arr->[0], time_zone => $tz)->ymd('-');
            my $dt2 = DateTime->from_epoch(epoch => $arr->[2], time_zone => $tz)->ymd('-');
            my $pnl = sprintf "%0.02f", (($arr->[3] - $arr->[1]) * $qty);
            my $row = [ $dt1, 'BUY', $arr->[1], $qty, $dt2, 'SELL', $arr->[3], $qty, $pnl ];



( run in 3.926 seconds using v1.01-cache-2.11-cpan-b16cb0d3907 )