Finance-CoinbasePro-API-CLI

 view release on metacpan or  search on metacpan

bin/coinbasepro.pl  view on Meta::CPAN

my $order;
my $size = 0;
my $id   = 0;                                   # id to act upon for cancel
my @allowed_actions =
  qw( top buy sell products accounts orders ticker trades fills cancel cancelall );
my $sleep   = 1;
my $top_max = 1;

# Usage() : returns usage information
sub Usage {
    "$prog ("
      . join( "|", @allowed_actions ) . ") \n"
      . "   [--verbose] [--dryrun] [--product=BTC-USD] [--price=N] [--size=N] [--cancel]:\n"
      . "   shows data from GDAX/Coinbase Pro\n"
      . "    for example: $prog ticker --product=BTC-USD  or  $prog products\n";
}

sub ddump {
    my $data = shift;
    dump($data);
}

# call main()
main();

# main()
sub main {
    GetOptions(
        "verbose!"  => \$verbose,
        "dryrun!"   => \$dryrun,
        "product=s" => \$product,
        "order=s"   => \$order,     # order id
        "side=s"    => \$side,      # buy or sell
        "price=f"   => \$price,
        "size=s"    => \$size,
        "id=s"      => \$id,
    ) or die Usage();

    my $action = shift @ARGV;
    if ( !$action ) {
        die Usage()
          . "$prog: allowed actions: ("
          . join( "|", @allowed_actions ) . ")\n"
          . "$prog: first param is action to perform\n";
    }

    if ( $action =~ /^(buy|sell)$/ ) {
        die "$prog: price must be non-zero, not '$price'\n" unless $price;
        die "$prog: size must be non-zero, not '$size'\n"   unless $size;
    }

    my $config_filename = get_config_filename();
    my $config          = $config_filename ? get_config($config_filename) : {};
    my @creds           = (
        key    => $config->{coinbasepro}{api_key}    || $ENV{GDAX_API_KEY} || "",
        secret => $config->{coinbasepro}{api_secret} || $ENV{GDAX_API_SECRET} || "",
        passphrase => $config->{coinbasepro}{api_passphrase} || $ENV{GDAX_API_PASSPHRASE} || "",
        debug => 0
    );
    if ( !$creds[0]  || !$creds[1] || !$creds[2] ) {
        warn "$prog: no credentials in " . join(", ", get_possible_config_filenames()) . " or env vars GDAX_API_(KEY|SECRET|PASSPHRASE)\n";
    }

    $SIG{__DIE__}  = \&Carp::confess;
    $SIG{__WARN__} = \&Carp::confess;

    my @products    = $product ? ($product) : (@all_products);

    ## ACCOUNTS
    if ( $action eq "accounts" ) {
        my $gaccount     = Finance::GDAX::API::Account->new(@creds);
        my $account_list = $gaccount->get_all;

        #print "$prog: $action: " . ddump($account_list) . "\n";
        if ( $gaccount->error ) {
            die "$prog: There was an error " . $gaccount->error;
        }
        my @jaccounts = map { Finance::CoinbasePro::API::CLI::Account->new($_) }
          @$account_list;

        #print "$prog: $action: " . ddump(\@jaccounts) . "\n";
        for my $a (@jaccounts) {
            print "$prog: account: " . $a->to_str() . "\n";
        }
    }

    # PRODUCTS
    elsif ( $action eq "products" ) {
        my $gproduct = Finance::GDAX::API::Product->new;
        my $products = $gproduct->list;

        #print "$prog: $action: " . ddump($products) . "\n";
        my @product_ids = map { $_->{id} } @$products;
        print "$prog: products: @product_ids\n";
    }

# POSITIONS
#elsif ($action eq "positions") {
#    my $position = Finance::GDAX::API::Position->new( @creds );
#    print "$prog: $action: " . ddump($position) . "\n";
#    my $positions = $position->get;
#    print "$prog: $action: " . ddump($positions) . "\n";
#    #print "$prog: $action: " . join(" ", map { $_->{id} } @$positions ) . "\n";
#}
# ORDERS
    elsif ( $action eq "orders" ) {
        my @open_orders = get_open_orders(@creds);
        print "$prog: $action:\n";
        for my $o (@open_orders) {
            printf(
"%s: %s Action: %s %0.3f @ \$%4.2f (value \$%4.2f) [order id: %s]\n",
                $prog, $o->{product_id}, $o->{side}, $o->{size}, $o->{price},
                $o->{size} * $o->{price},
                $o->{id}
            );
        }

    #print "$prog: $action: " . join(" ", map { $_->{id} } @$positions ) . "\n";
    }

    # CANCEL and CANCEL-ALL



( run in 1.729 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )