App-indodax

 view release on metacpan or  search on metacpan

lib/App/indodax.pm  view on Meta::CPAN

            if ($rec->{bal_total} > 0) {
                push @idr_markets, $currency;
            }
        } elsif (grep {$currency eq $_} @Btc_Market_Currencies) {
            if ($rec->{bal_total} > 0) {
                push @idr_markets, "btc" unless grep {$_ eq 'btc'} @idr_markets;
                push @btc_markets, $currency;
            }
        } else {
            if ($rec->{bal_total} > 0) {
                log_error "Don't know where to find current price for ".
                    "'$currency' (not found in IDR markets nor BTC markets), skipped";
            }
        }
        push @recs, $rec;
    }

    my $fnum0 = [number => {precision=>0}];
    my $fnum8 = [number => {precision=>8}];

    my %resmeta;
    if ($args{with_idr_estimates}) {
        my %idr_prices;
        for my $currency (@idr_markets) {
            my $res = $indodax->get_ticker(pair => _convert_pair("${currency}_idr"));
            $idr_prices{$currency} = ($res->{ticker}{buy} + $res->{ticker}{sell})/2;
        }
        my %btc_prices;
        for my $currency (@btc_markets) {
            my $res = $indodax->get_ticker(pair => _convert_pair("${currency}_btc"));
            $btc_prices{$currency} = ($res->{ticker}{buy} + $res->{ticker}{sell})/2;
        }

        my $tot_avail_est_idr = 0;
        my $tot_held_est_idr  = 0;
        my $tot_total_est_idr = 0;
        for my $rec (@recs) {
            my $currency = $rec->{currency};
            if ($currency eq 'idr') {
                $rec->{avail_est_idr} = $rec->{bal_avail};
                $rec->{held_est_idr}  = $rec->{bal_held};
                $rec->{total_est_idr} = $rec->{bal_total};
            } elsif (grep {$currency eq $_} @idr_markets) {
                $rec->{price_idr}     = $idr_prices{$currency};
                $rec->{avail_est_idr} = ($idr_prices{$currency} // 0) * $rec->{bal_avail};
                $rec->{held_est_idr}  = ($idr_prices{$currency} // 0) * $rec->{bal_held};
                $rec->{total_est_idr} = ($idr_prices{$currency} // 0) * $rec->{bal_total};
            } else {
                # btc markets
                $rec->{price_btc}     = $btc_prices{$currency};
                $rec->{price_idr}     = ($idr_prices{btc} // 0) * ($btc_prices{$currency} // 0);
                $rec->{avail_est_idr} = ($idr_prices{btc} // 0) * ($btc_prices{$currency} // 0) * $rec->{bal_avail};
                $rec->{held_est_idr}  = ($idr_prices{btc} // 0) * ($btc_prices{$currency} // 0) * $rec->{bal_held};
                $rec->{total_est_idr} = ($idr_prices{btc} // 0) * ($btc_prices{$currency} // 0) * $rec->{bal_total};
            }
            $tot_avail_est_idr += $rec->{avail_est_idr};
            $tot_held_est_idr  += $rec->{held_est_idr};
            $tot_total_est_idr += $rec->{total_est_idr};
        }

        # calculate percentages
        for my $rec (@recs) {
            if ($tot_total_est_idr > 0) {
                $rec->{pct_est_idr} = $rec->{total_est_idr} / $tot_total_est_idr;
            } else {
                $rec->{pct_est_idr} = 0;
            }
        }

        push @recs, {
            currency => 'est_idr',
            avail_est_idr => $tot_avail_est_idr,
            held_est_idr  => $tot_held_est_idr,
            total_est_idr => $tot_total_est_idr,
            pct_est_idr   => 1,
        };
        $resmeta{'table.fields'}        = [qw/currency bal_avail price_btc price_idr avail_est_idr bal_held held_est_idr bal_total total_est_idr pct_est_idr/];
        $resmeta{'table.field_aligns'}  = [qw/left     right     right     right     right         right    right        right     right         right/];
        $resmeta{'table.field_formats'} = [undef,      undef,    $fnum8,   $fnum0,   $fnum0,       $fnum8,  $fnum0,      $fnum8,   $fnum0,       'percent'];
    } else {
        $resmeta{'table.fields'}        = [qw/currency bal_avail bal_held bal_total/];
        $resmeta{'table.field_aligns'}  = [qw/left     right     right    right/];
        $resmeta{'table.field_formats'} = [undef,      $fnum8,   $fnum8,  $fnum8,];
    }

    [200, "OK", \@recs, \%resmeta];
}

$SPEC{hold_details} = {
    v => 1.1,
    summary => 'Show in which open orders your currency is being held',
    args => {
        %args_tapi,
        %arg_0_currency,
    },
};
sub hold_details {
    my %args = @_;
    _init(\%args);

    my $currency = $args{currency};
    my $currency0 = $Rev_Canonical_Currencies{$currency} // $currency;
    my $res = $indodax->get_info;
    my $bal      = $res->{return}{balance}{$currency0}      // 0;
    my $bal_held = $res->{return}{balance_hold}{$currency0} // 0;

    my @rows;
    if ($bal_held > 0) {
        for my $pair (@Market_Pairs) {
            #log_trace "pair=%s", $pair;
            my $buy_with_currency;
            my $currency2;
            if ($pair =~ /\A(\w+)_\Q$currency\E\z/) {
                $buy_with_currency = 1;
                $currency2 = $1;
            } elsif ($pair =~ /\A\Q$currency\E_(\w+)\z/) {
                $buy_with_currency = 0;
                $currency2 = $1;
            } else {
                next;
            }
            my $orders;
            eval {
                $orders = $indodax->get_open_orders(
                    pair => _convert_pair($pair),
                )->{return}{orders};
            };
            if ($@) {
                log_warn "Can't get open orders for pair $pair: $@, skipped";
                next;
            }
            for my $order (@$orders) {
                if ($buy_with_currency) {
                    next unless $order->{type} eq 'buy';
                    push @rows, {
                        order_id          => $order->{order_id},
                        pair              => $pair,
                        submit_time       => $order->{submit_time},
                        type              => $order->{type},



( run in 1.994 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )