Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2014
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 17.12 KB | None | 0 0
  1. get '/' => sub {
  2.   $log->info("looking for " . $basecurr . " / " . $altcurr . " OPPORTUNITIES, trade minimum: $trade_min, exchanges: @exs");
  3.     # $log->info("trading: " . $basecurr . " / " . $altcurr . ", supported exchanges: @exs");
  4.   $log->info("buy exchanges: @buy_exs, sell exchanges: @sell_exs");
  5.   my $self = shift;
  6.  
  7.   $best{sell}{price} = -1;
  8.   $best{buy}{price} = -1;
  9.  
  10.   $self->render_later;
  11.   $show_opportunities = 0;
  12.   my $delay_openorders = Mojo::IOLoop->delay(sub{
  13.     my ($del, @args) = @_;
  14.     my $delay = shift;
  15.     # $log->info("\nargs: " . Dumper(@args) . "/args\n");
  16.    
  17.     foreach my $index (0..$#_) {
  18.       my $host = $args[$index]->req->url->host;
  19.       my @h = split('\.', $host);
  20.       my $ex = $h[$#h-1];
  21.       my $success = 0;
  22.       my $error_msg;
  23.       my $crit = '- CRITICAL';
  24.       my @orders;
  25.       my $orders_ref;
  26.       my %orders;
  27.      
  28.       my @body = map { $_->res->body } $_[$index];
  29.       my $body = decode_json(@body);
  30.      
  31.       # $log->info(Dumper($body));
  32.      
  33.       if ($ex eq "cryptsy") { $success = $body->{success}; $error_msg = $success?'':$body->{error} }
  34.       if ($ex eq "kraken") { $success = ! defined($body->{error}[0])?1:0; $error_msg = $success?'':$body->{error} }
  35.       if ($ex eq "hitbtc") { $success = ! defined($body->{code})?1:0; $error_msg = $success?'':$body->{error} }
  36.       if ($ex eq "vircurex") { $success = ( (defined($body->{status})) && ($body->{status} == 0) )?1:0; }
  37.      
  38.       if ( $success ) {
  39.         $crit = '';
  40.         if ($ex eq "cryptsy") {
  41.           # $orders_ref = defined($body->{return}[0])?$body->{return}:'-1';
  42.           my $c = 0;
  43.           for my $k (@{$body->{return}}) {
  44.             # print Dumper($k);
  45.             $orders{$c}{symbol} = $basecurr . $altcurr;
  46.             $orders{$c}{price} = ${$k}{price};
  47.             $orders{$c}{size} = ${$k}{quantity};
  48.             $orders{$c}{side} = lc ${$k}{ordertype};
  49.             $c++;
  50.           }
  51.         }
  52.         if ($ex eq "kraken") {
  53.           my $c = 0;
  54.           # my @k = map { $_ } keys %{$body->{result}->{open}};
  55.           my %open = %{$body->{result}->{open}};
  56.           for my $k (keys %open) {
  57.             # print Dumper($open{$k});
  58.             $orders{$c}{symbol} = $open{$k}{descr}{pair};
  59.             $orders{$c}{price} = $open{$k}{descr}{price};
  60.             $orders{$c}{size} = $open{$k}{vol};
  61.             $orders{$c}{side} = lc $open{$k}{descr}{type};
  62.             $c++;
  63.           }
  64.           # %orders = defined($k[0])?%{$body->{result}->{open}}:('-1','1');
  65.         }
  66.         if ($ex eq "hitbtc") {
  67.           # $orders_ref = defined($body->{orders}[0])?$body->{orders}:'-1';
  68.           my $c = 0;
  69.           for my $k (@{$body->{orders}}) {
  70.             # print Dumper($k);
  71.             $orders{$c}{symbol} = ${$k}{symbol};
  72.             $orders{$c}{price} = ${$k}{orderPrice};
  73.             $orders{$c}{size} = ${$k}{orderQuantity} * ${$exchanges}{$ex}{lotsize}{$basecurr . $altcurr};
  74.             $orders{$c}{side} = lc ${$k}{side};
  75.             $c++;
  76.           }
  77.         }
  78.         if ($ex eq "vircurex") {
  79.           my $no_orders = $body->{numberorders};
  80.          
  81.           for (my $no = 1; $no <= $no_orders; $no++) {
  82.             my $orderno = 'order-' . $no;
  83.             # print "orderno: $orderno\n";
  84.             # print Dumper($body->{$orderno});
  85.             $orders{$no}{symbol} = $body->{$orderno}->{currency1} . $body->{$orderno}->{currency2};
  86.             # print "unitprice ";
  87.             # print Dumper($body->{$orderno}->{unitprice});
  88.             # print "\n/unitprice\n";
  89.             $orders{$no}{price} = $body->{$orderno}->{unitprice};
  90.             # print "/orderno: $orderno\n";
  91.             $orders{$no}{size} = $body->{$orderno}->{quantity};
  92.             $orders{$no}{side} = lc $body->{$orderno}->{ordertype};
  93.           }
  94.         }
  95.       } else {
  96.         $crit .= ", error: $error_msg";
  97.       }
  98.      
  99.       %{$openorders{$ex}} = %orders; # map { $_ => $orders{$_} } keys %orders;
  100.       $log->info("----------------------------------------- OPEN ORDERS - $ex $crit - -----------------------------------------");
  101.      
  102.       for my $k (keys %{$openorders{$ex}} ) {
  103.         $log->info($openorders{$ex}{$k}{side} . "|" . $openorders{$ex}{$k}{size} . " " . $openorders{$ex}{$k}{symbol} . " for " . $openorders{$ex}{$k}{price});
  104.       }
  105.     }
  106.     $openorders_read = 1;
  107.   });
  108.  
  109.   my @openorders_exs = ( "cryptsy", "hitbtc", "vircurex") ;
  110.   # my @openorders_exs = ( "vircurex" ) ;
  111.   foreach my $ex (@openorders_exs) {
  112.     my $request_data = $self->get_openorders($ex, $basecurr, $altcurr, $log);
  113.     my $url = "$request_data->{url}";
  114.     my $type = lc $request_data->{type};
  115.     my $header = $request_data->{header};
  116.     my $content = $request_data->{content};
  117.     $log->debug("url, type, content: $url, $type, $content");
  118.     $log->debug("header " . Dumper($header) . "/header");
  119.    
  120.     $self->ua->$type($url => $header => $content => $delay_openorders->begin);
  121.   }
  122.  
  123.   my $delay_getbalances = Mojo::IOLoop->delay(sub{
  124.     my ($del, @args) = @_;
  125.     my $delay = shift;
  126.     # $log->info("\nargs: " . Dumper(@args) . "/args\n");
  127.    
  128.     foreach my $index (0..$#_) {
  129.       my $host = $args[$index]->req->url->host;
  130.       my @h = split('\.', $host);
  131.       my $ex = $h[$#h-1];
  132.       my $success = 0;
  133.       my $error_msg;
  134.       my $crit = '- CRITICAL';
  135.       my @orders;
  136.       my $orders_ref;
  137.       my %orders;
  138.      
  139.       my @body = map { $_->res->body } $_[$index];
  140.       my $body = decode_json(@body);
  141.      
  142.       # $log->info(Dumper($body));
  143.      
  144.       if ($ex eq "cryptsy") { $success = $body->{success}; $error_msg = $success?'':$body->{error} }
  145.       if ($ex eq "kraken") { $success = ! defined($body->{error}[0])?1:0; $error_msg = $success?'':$body->{error} }
  146.       if ($ex eq "hitbtc") { $success = ! defined($body->{code})?1:0; $error_msg = $success?'':$body->{error} }
  147.       if ($ex eq "vircurex") { $success = ( (defined($body->{status})) && ($body->{status} == 0) )?1:0; }
  148.      
  149.       if ( $success ) {
  150.         $crit = '';
  151.         if ($ex eq "cryptsy") {
  152.           for my $cur (keys %{$body->{return}->{balances_available}}) {
  153.             $balances{$ex}{$cur} = $body->{return}{balances_available}{$cur};
  154.           }
  155.         }
  156.         # if ($ex eq "kraken") {
  157.           # my $c = 0;
  158.           # # my @k = map { $_ } keys %{$body->{result}->{open}};
  159.           # my %open = %{$body->{result}->{open}};
  160.           # for my $k (keys %open) {
  161.             # # print Dumper($open{$k});
  162.             # $orders{$c}{symbol} = $open{$k}{descr}{pair};
  163.             # $orders{$c}{price} = $open{$k}{descr}{price};
  164.             # $orders{$c}{size} = $open{$k}{vol};
  165.             # $orders{$c}{side} = lc $open{$k}{descr}{type};
  166.             # $c++;
  167.           # }
  168.           # # %orders = defined($k[0])?%{$body->{result}->{open}}:('-1','1');
  169.         # }
  170.         # if ($ex eq "hitbtc") {
  171.           # # $orders_ref = defined($body->{orders}[0])?$body->{orders}:'-1';
  172.           # my $c = 0;
  173.           # for my $k (@{$body->{orders}}) {
  174.             # # print Dumper($k);
  175.             # $orders{$c}{symbol} = ${$k}{symbol};
  176.             # $orders{$c}{price} = ${$k}{orderPrice};
  177.             # $orders{$c}{size} = ${$k}{orderQuantity} * ${$exchanges}{$ex}{lotsize}{$basecurr . $altcurr};
  178.             # $orders{$c}{side} = lc ${$k}{side};
  179.             # $c++;
  180.           # }
  181.         # }
  182.         if ($ex eq "vircurex") {
  183.           for my $cur (keys %{$body->{balances}}) {
  184.             $balances{$ex}{$cur} = $body->{balances}{$cur}{availablebalance};
  185.           }
  186.         }
  187.       } else {
  188.         $crit .= ", error: $error_msg";
  189.       }
  190.      
  191.       $log->info("----------------------------------------- BALANCES - $ex $crit -----------------------------------------");
  192.      
  193.       my $logline;
  194.       for my $cur (keys %{$balances{$ex}} ) {
  195.         if ( ($cur eq "$basecurr") || ($cur eq "$altcurr") ) {
  196.           $logline .= "$cur $balances{$ex}{$cur}|";
  197.         }
  198.       }
  199.       $log->info($logline);
  200.       # print "finishing get_balances\n";
  201.       # $e = $e->emit('get_balances');
  202.       $balances_read = 1;
  203.     }
  204.   });
  205.  
  206.   # $delay_getbalances->on(finish => sub {
  207.    
  208.   # });
  209.  
  210.   # my @getbalances_exs = @trade_exs;
  211.   my @getbalances_exs = ( "cryptsy", "vircurex" ) ;
  212.   foreach my $ex (@getbalances_exs) {
  213.     my $request_data = $self->get_balances($ex, $log);
  214.     my $url = "$request_data->{url}";
  215.     my $type = lc $request_data->{type};
  216.     my $header = $request_data->{header};
  217.     my $content = $request_data->{content};
  218.     $log->debug("url, type, content: $url, $type, $content");
  219.     $log->debug("header " . Dumper($header) . "/header");
  220.    
  221.     $self->ua->$type($url => $header => $content => $delay_getbalances->begin);
  222.   }
  223.  
  224.   my $delay_orderbooks = Mojo::IOLoop->delay(sub{
  225.     my ($del, @args) = @_;
  226.     my $delay = shift;
  227.     foreach my $index (0..$#_) {
  228.       @asks = ();
  229.       @bids = ();
  230.       my $host = $args[$index]->req->url->host;
  231.       my @h = split('\.', $host);
  232.       # $log->debug("$index: $host");
  233.  
  234.       my $remote_path = $args[$index]->req->url->path;
  235.       my @map = map { $_->res->body } $_[$index];
  236.       my $ex = $h[$#h-1];
  237.       $log->debug("---------------------------- $ex -----------------------------");
  238.       # $log->debug(Dumper($_[$index]));
  239.       if ($map[0] ne "") {
  240.         my $map_plain = decode_json(@map);
  241.  
  242.         if ($ex eq "hitbtc") { @asks = $map_plain->{asks}; @bids = $map_plain->{bids}; }
  243.         if ($ex eq "btc-e") { @asks = $map_plain->{asks}; @bids = $map_plain->{bids}; }
  244.         if ($ex eq "crypto-trade") { @asks = $map_plain->{asks}; @bids = $map_plain->{bids};  #print Dumper($map_plain);
  245.         }
  246.         if ($ex eq "bter") {
  247.           @asks = $map_plain->{asks}; @bids = $map_plain->{bids};
  248.         }
  249.         if ($ex eq "kraken") {
  250.           my $CURR;
  251.           my $invert = ${$exchanges}{$ex}{inverted}{$basecurr . $altcurr};
  252.           if ( defined($invert) ) {
  253.             $CURR = $invert;
  254.             @asks = $map_plain->{result}->{$CURR}->{bids};
  255.             foreach my $i (keys $asks[0]) { $asks[0][$i][0] = sprintf("%0.8f", 1 / $asks[0][$i][0]); }
  256.             @bids = $map_plain->{result}->{$CURR}->{asks};
  257.             foreach my $i (keys $bids[0]) { $bids[0][$i][0] = sprintf("%0.8f", 1 / $bids[0][$i][0]); }
  258.           } else  {
  259.             $CURR = "X" . $basecurr . "Z" . $altcurr;
  260.             @asks = $map_plain->{result}->{$CURR}->{asks};
  261.             @bids = $map_plain->{result}->{$CURR}->{bids};
  262.           }
  263.         }
  264.         if ($ex eq "vircurex") { @asks = $map_plain->{"asks"}; @bids = $map_plain->{"bids"}; }
  265.         if ($ex eq "cryptsy") {
  266.           my @asks_ = $map_plain->{return}->{$basecurr}->{sellorders};
  267.           my @bids_ = $map_plain->{return}->{$basecurr}->{buyorders};
  268.          
  269.           if (defined($asks_[0])) {
  270.             foreach my $k (keys $asks_[0]) {
  271.               $asks[0][$k][0] = $asks_[0][$k]{price};
  272.               $asks[0][$k][1] = $asks_[0][$k]{quantity};
  273.             }
  274.           }
  275.           if (defined($bids_[0]) ) {
  276.             foreach my $k (keys $bids_[0]) {
  277.               $bids[0][$k][0] = $bids_[0][$k]{price};
  278.               $bids[0][$k][1] = $bids_[0][$k]{quantity};
  279.             }
  280.           }
  281.         }
  282.  
  283.         if ( ! defined($bids[0][0][0]) ) { $bids[0][0][0] = 0; $bids[0][1][0] = 0; $bids[0][2][0] = 0; $bids[0][3][0] = 0; $bids[0][4][0] = 0; $bids[0][0][1] = 0; $bids[0][1][1] = 0; $bids[0][2][1] = 0; $bids[0][3][1] = 0; $bids[0][4][1] = 0; }
  284.         if ( ! defined($asks[0][0][0]) ) { $asks[0][0][0] = 99999999; $asks[0][1][0] = 99999999; $asks[0][2][0] = 99999999; $asks[0][3][0] = 99999999; $asks[0][4][0] = 99999999; $asks[0][0][0]; $asks[0][0][1] = 99999999; $asks[0][1][1] = 99999999; $asks[0][2][1] = 99999999; $asks[0][3][1] = 99999999; $asks[0][4][1] = 99999999;}
  285.  
  286.         $orderbooks{$ex}{'bids'} = $bids[0];
  287.         $orderbooks{$ex}{'asks'} = $asks[0];
  288.  
  289.         my $logline;
  290.         $logline = "we BUY|";
  291.         my $asks_no;
  292.         if ( @{$asks[0]} > 2 ) { $asks_no = 2 } else { $asks_no = @{$asks[0]} - 1 }
  293.         foreach my $index (0..$asks_no) {
  294.           $logline .= "$asks[0][$index][0] ($asks[0][$index][1])|";
  295.         }
  296.         $log->debug($logline);
  297.        
  298.         $logline = "we SELL|";
  299.         my $bids_no;
  300.         if ( @{$bids[0]} > 2 ) { $bids_no = 2 } else { $bids_no = @{$bids[0]} - 1 }
  301.         foreach my $index (0..$bids_no) {
  302.           $logline .= "$bids[0][$index][0] ($bids[0][$index][1])|";
  303.         }
  304.         $log->debug($logline);
  305.       }
  306.     }
  307.     check_bids_asks;
  308.     $orderbooks_read = 1;
  309.   });
  310.  
  311.   # $e->on(orderbooks_done => sub {
  312.    
  313.   # });
  314.  
  315.   my $delay_createorder = Mojo::IOLoop->delay(sub{
  316.     my ($del, @args) = @_;
  317.     my $delay = shift;
  318.     # $log->info("\nargs: " . Dumper(@args) . "/args\n");
  319.    
  320.       print "\nIN CREATEORDER DELAY\n";
  321.     foreach my $index (0..$#_) {
  322.       my $host = $args[$index]->req->url->host;
  323.       my @h = split('\.', $host);
  324.       my $ex = $h[$#h-1];
  325.       my $success = 0;
  326.       my $error_msg;
  327.       my $crit = '- CRITICAL';
  328.       my @orders;
  329.       my $orders_ref;
  330.  
  331.       calc_prices;
  332.       display_trade_info;
  333.       run_pretrade_checks;
  334.      
  335.       if ( ($pretrade_checks{size} eq "GO") && ($pretrade_checks{profit} eq "GO") ) {
  336.         if ( ($pretrade_checks{buy_exchange} eq "GO") && ($pretrade_checks{sell_exchange} eq "GO") ) {
  337.           my $result = $self->create_order($best{buy}{exchange}, "Buy", $trade{size}, $basecurr, $altcurr, $best{buy}{price}, $log);
  338.           $log->debug(Dumper($result));
  339.           if ( (defined($result->{code})) &&  ($result->{code} == 0) ) {
  340.             $log->info("CREATE ORDER - SUCCESS (" . $result->{traded_size} . " traded)");
  341.             $log->info("CREATE ORDER - trading contra...");
  342.             my $result = $self->create_order($best{sell}{exchange}, "sell", $trade{size}, $basecurr, $altcurr, $best{sell}{price});
  343.             $log->debug(Dumper($result));
  344.             if ($result->{code} == 0) {
  345.               $log->info("CREATE ORDER - SUCCESS (" . $result->{traded_size} . " traded)");
  346.             } else {
  347.               $log->info("CREATE ORDER - CRITICAL: " . $result->{text});
  348.             }
  349.           } else {
  350.             $log->info("CREATE ORDER - CRITICAL: " . $result->{text});
  351.           }
  352.         }
  353.       }
  354.     }
  355.     # $log->info("TESTING");
  356.     # # my @test_ex = ( "cryptsy", "vircurex", "hitbtc", "kraken" );
  357.     # my @test_ex = ( "kraken" );
  358.     # for my $ex (@test_ex) {
  359.         # my $result = $self->create_order($ex, "sell", "1", $basecurr, $altcurr, 0.025, $log);
  360.         # # print Dumper($result);
  361.         # # my $result = $self->create_order("cryptsy", "buy", "0.01", $basecurr, $altcurr, "0.2222");
  362.         # if ( (defined($result->{code})) &&  ($result->{code} == 0) ) {
  363.           # $log->info(" - SUCCESS (" . $result->{traded_size} . " traded");
  364.           # # my $result = $self->create_order("cryptsy", "sell", "0.01", $basecurr, $altcurr, "0.0220");
  365.           # # if ($result->{code} == 0) {
  366.             # # print " - SUCCESS (" . $result->{traded_size} . " traded)\n";
  367.           # # } else {
  368.             # # print " - CRITICAL: " . $result->{text} . "\n";
  369.           # # }
  370.         # } else {
  371.           # $log->info(" - CRITICAL: " . $result->{text});
  372.         # }
  373.         # # $self->get_openorders("hitbtc");
  374.     # }
  375.     # $log->info("/TESTING");
  376.   });
  377.   # print "\ndelay_getbalances: " . Dumper($delay_getbalances) . "/delay_getbalances\n";
  378.   $delay_createorder->wait;
  379.   # $delay_createorder->steps(sub { say 'BOOM!' }) if $balances_read == 1;
  380.  
  381.   foreach my $ex (@exs) {
  382.     my $url;
  383.     my $marketid;
  384.     my ($basec, $altc);
  385.     $basec = $basecurr;
  386.     $altc = $altcurr;
  387.     undef $inverted;
  388.     $inverted = ${$exchanges}{$ex}{inverted}{$basec . $altc};
  389.     if ( defined($inverted) ) {
  390.       $basec = $altcurr;
  391.       $altc = $basecurr;
  392.     }
  393.    
  394.     my $method1 = ${$exchanges}{$ex}{orderbook}{method};
  395.     $method1 =~ s/\{BASECURR\}/$basec/g;
  396.     $method1 =~ s/\{ALTCURR\}/$altc/g;
  397.    
  398.     my $method2 = ${$exchanges}{$ex}{orderbook}{method2};
  399.     $method2 =~ s/\{BASECURR\}/$basec/g;
  400.     $method2 =~ s/\{ALTCURR\}/$altc/g;
  401.  
  402.     my $params = ${$exchanges}{$ex}{orderbook}{params};
  403.     $params =~ s/\{BASECURR\}/$basec/g;
  404.     $params =~ s/\{ALTCURR\}/$altc/g;
  405.    
  406.     if ( $ex eq "cryptsy" ) {
  407.       $marketid = ${$exchanges}{$ex}{markets}{$basec . $altc};
  408.       $method1 =~ s/\{MARKETID\}/$marketid/g;
  409.       $method2 =~ s/\{MARKETID\}/$marketid/g;
  410.     }
  411.    
  412.     # print "method1/method2: $method1 / $method2 \n";
  413.    
  414.     $url = ${$exchanges}{$ex}{http} . "://" . ${$exchanges}{$ex}{host} . "/" . ${$exchanges}{$ex}{uripath} . $method1 . $method2 . $params;
  415.    
  416.     if ( ( $ex eq "btc-e" ) || ( $ex eq "crypto-trade") || ( $ex eq "bter") ) {$url = lc $url}
  417.  
  418.     $log->debug("url: $url");
  419.     $self->ua->get( $url => $delay_orderbooks->begin);
  420.  
  421.   }
  422.   # $delay_createorder->wait unless $delay_getbalances->ioloop->is_running;
  423.   # $delay_createorder->begin;
  424.   #( ($openorders_done == 0) && ($balances_done == 0) );  
  425.   # sleep 5;
  426.   # $delay->on(finish => sub {
  427.     # foreach my $ex (@exs) {
  428.       # find_best_buy_sell($ex);
  429.     # }
  430.   # });
  431. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement