Bitcoin Forum
July 04, 2024, 01:58:36 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1] 2 3 4 »  All
  Print  
Author Topic: BTC-E PHP Currency Arbitrage Bot Full Code  (Read 39856 times)
miaviator (OP)
Donator
Hero Member
*
Offline Offline

Activity: 686
Merit: 519


It's for the children!


View Profile WWW
May 05, 2013, 10:53:51 PM
Last edit: August 16, 2014, 02:57:31 AM by miaviator
 #1



EDIT: Rolling Bot code posted: https://bitcointalk.org/index.php?topic=366546.0

This project took a while to write up.

It made several BTC then several USD though it acted way to slow for my needs.  I've changed over to a price rolling bot and no longer need to run this.

This is not a .01B to L to U bot.  You can run from B to B or U to U, it analyses two paths each way (BULB,BLUB) (UBLU,ULBU) and chooses the most profitable path (In B or U) as an amount NOT AS A PERCENTAGE my goal was to the most money not the highest percent and since this is dynamic that feature was required.  You can still set a minimum % profit for each trade and I recommend setting it to at least the exchange fee of .2% as you will frequently get stuck in LTC.  There is a currency flush used on trade leg three that trades out all L for $currency.  This uses a recursive dynamic depth calculation and can perform circular trades of any amount.  If you tell it to trade 1B it will sort the order book and find out what the max B,L,U available is before executing a trade (So if you try with 1B it might trade .01, .2, 1, .98, .023 etc.)  depending on the size of each leg of the order book.

There are a lot of configurable variables.  There is also an unfinished currency change function which would allow you to swap from B to U when the price of B is falling and U to B when it is rising.  This actually works really well, you can take advantage of arbitrage in either currency while holding funds in the most profitable currency.

Output is very minimal because output costs time, the goal was sub 1ms per trade analysis.  When a trade is kicked off there is often several opportunities back to back or a large order book on all legs so the code prints no output after a trade, it simply runs the paths again and keeps trading until no profit opportunity is found.

- = duplicate orderbook
. = trade path analysed
a number = number of trades completed
E# = error getting the order book (The current code was written to run on a round robbin set of servers and will exit after two errors)
o = orders placed
C = orders cancelled

you'll have to enter your key and secret
be careful setting min & max to the same number as this will use a different trading mechanism.

I'm not responsible for any N00B losing money or making money (Though I'll gladly accept donations! 18DBdwc6JkCGrz2svV6mCPeURbZM8La5TN)

Code:
#screen -dmS arbot -h 1024 -L ./roundrobin.sh
<?php
 
//USER CONFIGURABLE
//Print Debug information on trading
$DEBUG NULL;

//Your min and max bid, set to the same for static trades
$minBTC .01;
$maxBTC .025;
$minUSD 2;
$maxUSD 10;
//To use fastTrade set min and max to the same amount.

if ( $minBTC == $maxBTC || $minUSD == $maxUSD){
$fastTrade TRUE;
print "Fast Trade Enabled ->> Remember to Clear currencies";
}
//Minimum Bids the exchange will accept (BTC-E Mins)
$minBidBTC .01;
$minBidLTC .1;
$minBidUSD .01


//Using dynamic depth we can execute the most profitable trade in $currency with a minimum % profit
//set both to 0 to trade everything profitable
$threshold 0// Execute any profitable trade **Trade will not execute unless we beat the P % below
$thresholdP .5//Threshold as a %

//100,001-999,999,999 max wait between non-trade & book duplications reduced 1.2.3.4.
$nanosleep 1//nanoseconds

//The maximum time you want to keep an order in the order book in seconds
//This is only applicable to currency flushing (leg 3) orders.
$maxAge 60//seconds

//Currency To start and End in B || L || U
$currency "U";
$interval 12// your currency change interval, this many ticks will be analyzed to determine changes in currency
$curve 1//the number of opposite ticker pairs to throw away.
/* IE, if you pull $10,11,12,9,13 with an interval 5 and a curve of 1 the price will be assumed to be rising.
 * if your interval is 0 the price will be assumed to be stagnant
 */

//BTC-E Currency pairs & bid ask JSON names
$btcesymbol = array("btc_usd""ltc_btc""ltc_usd");
$btcebidorask = array("bids""asks");
$executeTrade $postTrade FALSE;

//Programs
//A - Currency Arbitrage, Choose UtoU or BtoB arbitrage through L
//B - Portfolio Balancing, Maintain 50% of two currencies at all times
//D - Buy Down, Buy 1/2 spread each .5 drop and sell 1 each $1 gain

//COUNTERS
$count=1//# of trades
$o $i $e 0
$t 0;
$GLOBALS['ticker'] = array();
//Holds order numbers and times

$price $volume = array();
$path $BULB $BLUB $UBLU $ULBU 0;

//get the incriment from our file
$nonce readInc();

//Were starting a fresh run, cancel pending orders.
cancelOrders();

//changeCurrency();
//$currency = changeCurrency();

// repeat forever
while ( 'miaviator' != 'uncool'){
//We did not execute a trade the last loop
if (!$executeTrade){ 
//This is a non-trade loop, write out the incriment to a file.
writeInc($nonce);
//changeCurrency();
//Program Output
print ".";
//we Executed a trade the last run (not this run)
if ($postTrade){
$minUSD $minBTC*$BTCtoUSD['bids'][0][0];
$maxUSD $maxBTC*$BTCtoUSD['asks'][0][0];
//The total trades made this run
//@$total = $total + $count;
print $count;
//Reset the trade counter
$count 0;
//turn off post trade
$postTrade FALSE;
if(@$fastTrade){
cancelOrders();
//$trade = json_decode(btce_query("getInfo"), TRUE);
//DEBUG add currency clearing method.
} else {
while ( $o ){
print "o";
$o--;
}
}
}
}
if ($executeTrade){
//We traded last run
$postTrade TRUE;
//Add a trade to the counter
$count++;
//reset trade BOOL for next run
$executeTrade FALSE
}

//Get order book
$i $e 0;
//this is horrible code \/ 
$BTCtoUSDold = @$BTCtoUSD['bids'][0][0];
$LTCtoBTCold = @$LTCtoBTC['bids'][0][0];
$LTCtoUSDold = @$LTCtoUSD['bids'][0][0];
$BTCtoUSDold1 = @$BTCtoUSD['asks'][0][0];
$LTCtoBTCold1 = @$LTCtoBTC['asks'][0][0];
$LTCtoUSDold1 = @$LTCtoUSD['asks'][0][0];
//blank the orderbooks
$BTCtoUSD $LTCtoBTC $LTCtoUSD = array();
while( empty($BTCtoUSD) || empty($LTCtoBTC) || empty($LTCtoUSD) ){
$BTCtoUSD = @json_decodesend'https://btc-e.com/api/2/btc_usd/depth' ), TRUE );
$LTCtoBTC = @json_decodesend'https://btc-e.com/api/2/ltc_btc/depth' ), TRUE );
$LTCtoUSD = @json_decodesend'https://btc-e.com/api/2/ltc_usd/depth' ), TRUE );
//Verify the order book changed
$old $BTCtoUSDold+$LTCtoBTCold+$LTCtoUSDold+$BTCtoUSDold1+$LTCtoBTCold1+$LTCtoUSDold1;
$new $BTCtoUSD['bids'][0][0]+$LTCtoBTC['bids'][0][0]+$LTCtoUSD['bids'][0][0]+$BTCtoUSD['asks'][0][0]+$LTCtoBTC['asks'][0][0]+$LTCtoUSD['asks'][0][0];
if( !@$BTCtoUSD['bids'] || !@$LTCtoBTC['bids'] || !@$LTCtoUSD['bids']){
$BTCtoUSD $LTCtoBTC $LTCtoUSD = array();
//$sleep=rand(6, 36)*(++$e);
$e++;
print "E" $e;
sleep(5);
if ( $e ) exit;
/*
if ($nanosleep > 100000)
{$nanosleep = $nanosleep+100000;} //up to 9999 runs
else
{$nanosleep = $nanosleep^10;}
*/
}
if ( $old == $new ){
$BTCtoUSD $LTCtoBTC $LTCtoUSD = array();
print "-";
//Dynamic Timer
//$sleep=rand(100000 , $nanosleep);//100000
//time_nanosleep(0, $sleep);
//Static Timer
time_nanosleep(0$nanosleep);
//if (@$postCancel)
//If we have pending orders cancel them (they may not exist)
if (@$times){
//set the current time to compare orders to
$time time();
//find the earliest order in the array (end)
$orderOne end($times);
//If this order is older than maxage start canceling
if ( $orderOne $time-$maxAge){
//echo loop through order times
foreach ($times as $key => $value ){
//if we've hit orders newer than maxage break
if ( $value $time-$maxAge ){
break;
}
else {
$order_id $orders[$key];
//cancel the order by number we don't need to check if it filled or not
$trade json_decode(btce_query("CancelOrder", array("order_id" => $order_id)), TRUE);
//remove this order and time from the array
unset($orders[$key]);
unset($times[$key]);
print "C";
}
}
}

}
}

if ( $currency == "B"){
////////////////////////////////////////////////////////////////////
//BULB Depth Math
////////////////////////////////////////////////////////////////////
//trade 1 math, B to U, B input
$BTC $maxBTC;
$minimumBidBTC $minBTC;
foreach ($BTCtoUSD['bids'] as $value){
$BTCtoUSDbidRate $value[0]; //set Rate
if ($value[1] >= $minimumBidBTC && $value[1] < $BTC ){  
$BTC  $value[1]; //set to book value
break;    
}
if ( $value[1] >= $minimumBidBTC && $value[1] >= $BTC){
break;
}
}
$USD=$BTC*$BTCtoUSDbidRate*.998// set U to move on
//trade 2 math, U to L, L input
foreach ($LTCtoUSD['asks'] as $value){
$minimumBidLTC = ($minimumBidBTC*$BTCtoUSDbidRate/.998)/$value[0];
$LTCtoUSDaskRate $value[0];
if ($value[1] >= $minimumBidLTC/.998 && $value[1]*$value[0] < $USD ) {
$BTC $USD/$BTCtoUSDbidRate/.998// set new B
$USD $BTC*$BTCtoUSDbidRate*.998// set new U to move on
break;
}
if ( $value[1] >= $minimumBidLTC && $value[1]*$value[0] >= $USD ){
break;
  }        
}
$LTC =($USD*.998)/$LTCtoUSDaskRate// set L to move on
//trade 3 math, L to B’, L input
foreach ($LTCtoBTC['bids'] as $value){
$LTCtoBTCbidRate $value[0];
if ( $value[1]/.998 >= $minimumBidLTC && $value[1] < $LTC  ){
$minimumBidBTC = ($minimumBidBTC*$BTCtoUSDbidRate/.998)/$value[0]; //This is in the wrong place and it's not used?
$USDX=($value[1]*$LTCtoUSDaskRate)/.998;
$BTC=($USDX/$BTCtoUSDbidRate)/.998;
$USD = ($BTC*$BTCtoUSDbidRate)*.998;
$LTC = ($USD/$LTCtoUSDaskRate)*.998;
break;
}
if ( $value[1]/.998 >= $minimumBidLTC && $value[1] >= $LTC ){
break;
}
}
$BULBamount = array($BTC,$USD,$LTC);
$BULBrate = array($BTCtoUSDbidRate,$LTCtoUSDaskRate,$LTCtoBTCbidRate);
//$BULB = round((($LTC*$LTCtoBTCbidRate*.998/$BTC)-1)*100,4);
$BULB $LTC*$LTCtoBTCbidRate*.998-$BTC//B Profit
//4/19 testing only trading above .2% commision threshold to save on currency clearing costs.
$BULBpercent = (($LTC*$LTCtoBTCbidRate*.998)/$BTC-1)*100;
if (@$DEBUG) print "\n BULB Amount: {$BULB} BULB Percent: {$BULBpercent}";
if ($BULB $threshold && $BULBpercent $thresholdP ){$executeTrade TRUE;}//Trade if criteria are met.
if ($BULBamount $minBidBTC){exit;}  //ERROR CHECK
////////////////////////////////////////////////////////////////////
//END END END BULB Depth Math
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
//BLUB Depth Math
////////////////////////////////////////////////////////////////////
//trade 1 math, B to L, L input
$BTC $maxBTC;
$minimumBidBTC $minBTC;
foreach ($LTCtoBTC['asks'] as $value){
$minimumBidLTC $minimumBidBTC/$value[0];
$LTCtoBTCaskRate $value[0];
if ($value[1] >= $minimumBidLTC && $value[1]*$value[0] < $BTC && $value[1]*$value[0] >= $minimumBidBTC){
$BTC $value[1]*$value[0]; //set to book value
break;    
}
if ( $value[1] >= $minimumBidLTC && $value[1]*$value[0] >= $BTC){
break;
}
}
$LTC=($BTC/$LTCtoBTCaskRate)*.998// set L to move on
//trade 2 math, L to U, L input
foreach ($LTCtoUSD['bids'] as $value){
$minimumBidLTC $minimumBidLTC/.998;
if ($value[1] >= $minimumBidLTC && $value[1] < $LTC ){
$LTCtoUSDbidRate $value[0];
$BTC $value[1]*$LTCtoBTCaskRate/.998;
$LTC=($BTC/$LTCtoBTCaskRate)*.998// recalc trade 1 with new B
break;
}
if ( $value[1] >= $minimumBidLTC && $value[1] >= $LTC){
$LTCtoUSDbidRate $value[0];
break;

}
$USD = ($LTC*$LTCtoUSDbidRate)*.998// set U to move on
//trade 3 math, U to B’, B input
foreach ($BTCtoUSD['asks'] as $value){
$minimumBidBTC $minimumBidBTC/.998;
if ( $value[1] >= $minimumBidBTC && $value[1]*$value[0] < $USD){
$BTCtoUSDaskRate $value[0];
$LTCX = ($value[1]*$value[0])/( $LTCtoUSDbidRate*.998 ); // set new L from book 
$BTC = ($LTCX*$LTCtoBTCaskRate)/.998// set new B with new L
$LTC = ($BTC/$LTCtoBTCaskRate)*.998// recalc trade 1 with new B
$USD = ($LTC*$LTCtoUSDbidRate)*.998// recalc trade 2 with new U
break;
}
if ( $value[1] >= $minimumBidBTC && $value[1]*$value[0] >= $USD){
$BTCtoUSDaskRate $value[0];
break;
}
}
$BLUBamount = array($BTC,$LTC,$USD);
$BLUBrate = array($LTCtoBTCaskRate,$LTCtoUSDbidRate,$BTCtoUSDaskRate);
//$BLUB = round((((($USD/$BTCtoUSDaskRate)*.998)/$BTC)-1)*100,4);
$BLUB $USD/$BTCtoUSDaskRate*.998-$BTC;
$BLUBpercent = (($USD/$BTCtoUSDaskRate*.998)/$BTC-1)*100;
if ($USD/$BTCtoUSDaskRate*.998-$BTC $threshold && $BLUBpercent $thresholdP )
{$executeTrade TRUE;}
if (@$DEBUG) print "\n BLUB Amount: {$BLUB} BLUB Percent: {$BLUBpercent}";
//ERROR CHECK
if ($BLUBamount $minBidBTC)
{exit;}
////////////////////////////////////////////////////////////////////
//END END END BLUB Depth Math
////////////////////////////////////////////////////////////////////
}
if ( $currency == "U"){
////////////////////////////////////////////////////////////////////
//ULBU Depth Math
////////////////////////////////////////////////////////////////////
//UBLU is ask, ask, bid.
$minimumBidLTC $minBidLTC;
$minimumBidBTC $minBidBTC;
$USD=$maxUSD;
//print "USD: {$USD} \n";
//print "Leg 1 \n";
//trade 1, Buy L with U, L input
foreach ($LTCtoUSD['asks'] as $value){
//print_r($value);
//minbid ltc already set
$LTCtoUSDaskRate=$value[0];               // set rate
//After this transaction and commision you must have at least 1LTC left
//If we buy .1 LTC we can't sell the .0998LTC in LEG 2
if ($value[1] >= $minimumBidLTC/.998 && $value[1]*$value[0] < $USD ){  
$USD=$value[1]*$value[0];                 // set U to book value
//print "USD: {$USD} \n";
break;    
}
if ( $value[1] >= $minimumBidLTC/.998 && $value[1]*$value[0] >= $USD){
//$USD = $USD;                            // use current U
break;
}
}
$LTC =($USD/$LTCtoUSDaskRate)*.998;                   // set L to move on
//print "LTC: {$LTC} \n";
//trade 2, Sell L for B, L input
//print "Leg 2 \n";
//At current rates we don't worry about minimum USD bids
//Value0 is rate(inB) value 1 is amount (inL) 0*1 is total B
foreach ($LTCtoBTC['bids'] as $value){
//print_r($value);
$LTCtoBTCbidRate $value[0];             // set rate
        
if ( $value[1] >= $minimumBidLTC && $value[1] < $LTC && $value[0]*$value[1]*.998 >= $minimumBidBTC ){
            
$USD = ($value[1]*$LTCtoUSDaskRate)/.998;   // set new U up
//print "USD: {$USD} \n";
            
$LTC $USD/$LTCtoUSDaskRate*.998;        // set new L down
//print "LTC: {$LTC} \n";
break;
}
if ($value[1]/.998 >= $minimumBidLTC && $value[1] >= $LTC && $value[0]*$value[1]*.998 >= $minimumBidBTC ){
//$USD = $USD;                            // use current U
//$LTC = $LTC;                            // use current L
            
break;
}       
}
$BTC =($LTC*$LTCtoBTCbidRate)*.998;                   // set B to move on
 
//print "BTC: {$BTC} \n";
//trade 3, Sell B for U’, B input
//print "Leg 3 \n";
foreach ($BTCtoUSD['bids'] as $value){
//print_r($value);
$BTCtoUSDbidRate $value[0];               // set rate 
        
if ( $value[1] >= $minimumBidBTC && $value[1] < $BTC ){
//$LTCX = $value[1]/*Total B*//$LTCtoBTCbidRate/*Div .002 ish for L*//.998/*Add commision*/
//$LTCX=$value[1]/($LTCtoUSDaskRate*.998);    // set new L up
$LTCX $value[1]/$LTCtoBTCbidRate/.998;
//print "LTCX: {$LTCX} \n";
$USD=($LTCX*$LTCtoUSDaskRate)/.998;         // set new U up
//print "USD: {$USD} \n";
$LTC=($USD/$LTCtoUSDaskRate)*.998;          // set new L down
//print "LTC: {$LTC} \n";
$BTC=($LTC*$LTCtoBTCbidRate)*.998;          // set new B down
//print "BTC: {$BTC} \n";
            
break;
        }
        if ( 
$value[1] >= $minimumBidBTC && $value[1] >= $BTC){
//$USD = $USD;                              // use current U
//$LTC = $LTC;                              // use current L
//$BTC = $BTC;                              // use current B
            
break;
        }
}
$USDX =($BTC*$BTCtoUSDbidRate)*.998;                  // final U
//print "USDX: {$USDX} \n";
$ULBUamount = array($USD,$LTC,$BTC);
//print "ULBUamount: {$ULBUamount} \n";
$ULBUrate = array($LTCtoUSDaskRate,$LTCtoBTCbidRate,$BTCtoUSDbidRate);
//print_r($ULBUrate);
$ULBU $USDX-$USD;
//print "ULBU: {$ULBU} \n";
if ( $ULBU $threshold)
{$executeTrade TRUE;}
//ERROR CHECK
if ($ULBUamount $minBidUSD)
{exit;}
////////////////////////////////////////////////////////////////////
//END ULBU Depth Math
////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////
//UBLU Depth Math
////////////////////////////////////////////////////////////////////
/// initial trade amount = $USD
///UBLU///
$minimumBidBTC $minBidBTC;
$USD=$maxUSD;
//print "USD: {$USD} \n";
//(define mins)
//trade 1, Buy B with U, B input 
foreach ($BTCtoUSD['asks'] as $value){
//print_r($value);
        
$BTCtoUSDaskRate=$value[0];               // set rate
        
if ( $value[1] >= $minimumBidBTC && $value[1]*$value[0] < $USD ){  
            
$USD=$value[1]*$value[0];                 // set U to book value
//print "USD: {$USD} \n";
            
break;    
        }
        if ( 
$value[1] >= $minimumBidBTC && $value[1]*$value[0] >= $USD){
            
//$USD = $USD;                            // use current U
            
break;
}
}
$BTC =($USD/$BTCtoUSDaskRate)*.998;                   // set B to move on
//print "BTC: {$BTC} \n";
//trade 2, Buy L with B, L input
foreach ($LTCtoBTC['asks'] as $value){
//print_r($value);
$LTCtoBTCaskRate $value[0];             // set rate
        
if ( $value[1]/.998 >= $minimumBidLTC && $value[1]*$value[0] < $BTC ){
            
$USD=($value[1]*$value[0]*$BTCtoUSDaskRate)/.998;     // set new U up
//print "USD: {$USD} \n";
            
$BTC=($USD/$BTCtoUSDaskRate)*.998;        // set new B down
//print "BTC: {$BTC} \n";
break;
    }
if ($value[1]/.998 >= $minimumBidLTC && $value[1] >= $BTC){
//$USD = $USD;                            // use current U
//$BTC = $BTC;                            // use current B
break;
}       
}
$LTC =($BTC/$LTCtoBTCaskRate)*.998;                   // set L to move on
 
//print "LTC: {$LTC} \n";
//trade 3, Sell L for U’, L input
foreach ($LTCtoUSD['bids'] as $value){
$LTCtoUSDbidRate $value[0];
        if ( 
$value[1] >= $minimumBidLTC && $value[1] < $LTC ){
$BTCX=($value[1]*$LTCtoBTCaskRate)/.998;    // set new B up
//print "BTCX: {$BTCX} \n";
$USD=($BTCX*$BTCtoUSDaskRate)/.998;         // set new U up
//print "USD: {$USD} \n";
$BTC=($USD/$BTCtoUSDaskRate)*.998;          // set new B down
//print "BTC: {$BTC} \n";
$LTC=($BTC/$LTCtoBTCaskRate)*.998;          // set new L down
//print "LTC: {$LTC} \n";
break;
        }
        if ( 
$value[1] >= $minimumBidLTC && $value[1] >= $LTC){
//$USD = $USD;                              // use current U
//$BTC = $BTC;                              // use current B
//$LTC = $LTC;                              // use current L
break;
        }
}
$USDX =($LTC*$LTCtoUSDbidRate)*.998;                  // final U
//print "USDX: {$USDX} \n";
$UBLUamount = array($USD,$BTC,$LTC);
//print "UBLUamount: {$UBLUamount} \n";
$UBLUrate = array($BTCtoUSDaskRate,$LTCtoBTCaskRate,$LTCtoUSDbidRate);
//print_r($UBLUrate);
$UBLU $USDX-$USD;
//print "UBLU: {$UBLU} \n";
if ($UBLU $threshold)
{$executeTrade TRUE;}
//ERROR CHECK
if ($UBLUamount $minBidUSD)
{exit;}
////////////////////////////////////////////////////////////////////
//END UBLU Depth Math
////////////////////////////////////////////////////////////////////
}


////////////////////////////////////////////////////////////////////
//Modular Trade Arrays
////////////////////////////////////////////////////////////////////
if (@$executeTrade){
//FIND THE HIGHEST PROFIT OPPORTUNITY
if ( $currency == "B"){
$path max(@$BULB, @$BLUB);
}

if ( $currency == "U"){
$path max(@$UBLU, @$ULBU);
}
if ( $path == @$BULB )
{
//define bulb trade arrays
$tradeName "BULB";
$type = array("sell","buy","sell");
$symbol = array("btc_usd","ltc_usd","ltc_btc");
$amount $BULBamount;
$rate $BULBrate
$round = array(8,8,8);
$funds = array("btc","usd","ltc");
}
if ( $path == @$BLUB )
{
//define BLUB trade arrays
$tradeName "BLUB";
$type = array("buy","sell","buy");
$symbol = array("ltc_btc","ltc_usd","btc_usd");
$amount $BLUBamount
$rate $BLUBrate
$round = array(8,8,8);
$funds = array("btc","ltc","usd");
}
if ( $path == @$ULBU )
{
$tradeName "ULBU";
$type = array("buy","sell","sell");
$symbol = array("ltc_usd","ltc_btc","btc_usd");
$amount $ULBUamount;
$rate $ULBUrate
$round = array(8,8,8);
$funds = array("usd","ltc","btc");
}
if ( $path == @$UBLU )
{
$tradeName "UBLU";
$type = array("buy","buy","sell");
$symbol = array("btc_usd","ltc_btc","ltc_usd");
$amount $UBLUamount
$rate $UBLUrate
$round = array(8,8,8);
$funds = array("usd","btc","ltc");
}





////////////////////////////////////////////////////////////////////
//Modular Trade Code
////////////////////////////////////////////////////////////////////
//How to speed up trading?
//Use microtrades, no orders, no error testing, nothing.
if (@$fastTrade){
//Execute trade one
$amount[0] = round($amount[0],8);
$trade json_decode(btce_query("Trade", array("pair" => $symbol[0], "type" => $type[0], "amount" => $amount[0], "rate" => $rate[0])), TRUE);
$amount[1] = round($amount[1],8);
//execute trade 2
$trade json_decode(btce_query("Trade", array("pair" => $symbol[1], "type" => $type[1], "amount" => $amount[1], "rate" => $rate[1])), TRUE);
$amount[2] = round($amount[2],8);
if ($amount[2] > 0){
//execute trade 3
$trade json_decode(btce_query("Trade", array("pair" => $symbol[2], "type" => $type[2], "amount" => $amount[2], "rate" => $rate[2])), TRUE);
}
} else {
//Set the trade time for order cancellations
$tradeTime time();
//Start executing trades
//Execute trade one
$amount[0] = round($amount[0],8);
$trade json_decode(btce_query("Trade", array("pair" => $symbol[0], "type" => $type[0], "amount" => $amount[0], "rate" => $rate[0])), TRUE);
if (@$DEBUGprint_r ($trade);
if (@$DEBUG) echo "pair" $symbol[0] . "type" $type[0] . "amount" $amount[0] . "rate" $rate[0] . "\n";
//If we traded, and we recieved an order, log it to Orders
if ( $trade['success'] == && $trade['return']['order_id'] > ){
$cancel json_decode(btce_query("CancelOrder", array("order_id" => $trade['return']['order_id'])), TRUE);
$trade json_decode(btce_query("getInfo"), TRUE);
}
//If the trade failed, get the balance for trade 2
elseif( $trade['success'] !== ){
$trade json_decode(btce_query('getInfo'), TRUE);
}
//Set the available funds for trade 2 (different for buy and sell orders)
if ( $type[1] == "buy" ){
$tmp = (@$trade['return']['funds'][$funds[1]]/$rate[1]);
//print_r($trade);
}
elseif ( $type[1] == "sell" ){
$tmp = (@$trade['return']['funds'][$funds[1]]);
//print_r($trade);
}
//Trade 2 can cause exchange loops if it trades the balance, prevent this
if ( $tmp $amount[1] ){
$amount[1] = $tmp;
}
$amount[1] = round($amount[1],8);
//If we have funds to make trade 2 execute the trade otherwise move to trade 3
//on second thought, just execute it anyways....
//if ($amount[1] > 0){
//execute trade 2
$trade json_decode(btce_query("Trade", array("pair" => $symbol[1], "type" => $type[1], "amount" => $amount[1], "rate" => $rate[1])), TRUE);
if (@$DEBUGprint_r ($trade);
if (@$DEBUG) echo "pair" $symbol[1] . "type" $type[1] . "amount" $amount[1] . "rate" $rate[1] . "\n";
//If we traded, and we recieved an order, log it to Orders
if ( $trade['success'] == && $trade['return']['order_id'] > ){
$cancel json_decode(btce_query("CancelOrder", array("order_id" => $trade['return']['order_id'])), TRUE);
$trade json_decode(btce_query("getInfo"), TRUE);
}
//If the trade failed, get the balance for trade 3 
elseif( $trade['success'] !== ){
$trade json_decode(btce_query("getInfo"), TRUE); 
}
//Set the available funds for trade 3 (different for buy and sell orders)
//Trade 3 clears currency by trading the Balance of this currency out
if ( $type[2] == "buy" ){
$amount[2] = (@$trade['return']['funds'][$funds[2]]/$rate[2]);
//print_r($trade);
}
elseif ( $type[2] == "sell" ){
$amount[2] = (@$trade['return']['funds'][$funds[2]]);
//print_r($trade);
}
//Check if we have funds to trade (or just orders)
$amount[2] = round($amount[2],8);
if ($amount[2] > 0){
//execute trade 3
$trade json_decode(btce_query("Trade", array("pair" => $symbol[2], "type" => $type[2], "amount" => $amount[2], "rate" => $rate[2])), TRUE);
if (@$DEBUGprint_r ($trade);
if (@$DEBUG) echo "pair" $symbol[2] . "type" $type[2] . "amount" $amount[2] . "rate" $rate[2] . "\n";
//If we traded, and we recieved an order, log it to Orders
if ( $trade['success'] == && $trade['return']['order_id'] > ){
$o++;
$orders[$nonce] =  $trade['return']['order_id'];
$times[$nonce] = $tradeTime;
}
}
}
}
}
 
////////////////////////////////////////////////////////////////////
//END Modular Trade Code
////////////////////////////////////////////////////////////////////
 

print "\n\n DANGER miaviator IS NO LONGER COOL? \n\n";
exit;

// FUNCTIONS
    
function btce_query$method NULL$req = array() )
{

//MAIN KEY
$key ''// your API-key
$secret ''// your Secret-key

// API settings
$req['method'] = $method;
$req['nonce'] = $GLOBALS['nonce']++;

// generate the POST data string
$postData http_build_query($req'''&');

// generate the extra headers
$headers = array(
'Sign: '.hash_hmac("sha512"$postData$secret
,"Key: {$key}"
);

$ch curl_init();
curl_setopt($chCURLOPT_HEADERFALSE);
curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
curl_setopt($chCURLOPT_USERAGENT'Mozilla/4.0 (compatible; BTCE PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
curl_setopt($chCURLOPT_URL'https://btc-e.com/tapi');
curl_setopt($chCURLOPT_POSTFIELDS$postData);
curl_setopt($chCURLOPT_HTTPHEADER$headers);
curl_setopt($chCURLOPT_SSL_VERIFYPEERFALSE);

return curl_exec($ch);
}

function writeInc$value NULL )
{
$fp fopen('nonce','w');
fwrite($fp$value);
fclose($fp);

return;
}


function readInc()
{
$fp fopen('nonce','r');
$value = (int)fread($fp8);
fclose($fp);

return $value;
}

function cancelOrders()
{
$return json_decode(btce_query("OrderList"), TRUE);
if($return['success'] > 0){
foreach ($return['return'] as $key => $value){
$order_id $key;
$trade json_decode(btce_query("CancelOrder", array("order_id" => $order_id)), TRUE);
print "C";
}
}
}
function send$url NULL )
{
$ch curl_init();
curl_setopt($chCURLOPT_URL$url);
curl_setopt($chCURLOPT_HEADERFALSE);
curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
$result curl_exec($ch);
curl_close($ch);

return $result;
}
function changeCurrency()
{
//DEBUG if ticker fails to download you could have issues?
//SOLVED if a GAP between buy and sell develops you could change, used buy and sell, now using last
//DEBUG change must be greater than original spread,

//incriment ticker count
$t $GLOBALS["t"];
$t++;
//pull ticker
$GLOBALS['ticker'][$t] = @json_decodesend'https://btc-e.com/api/2/btc_usd/ticker' ), TRUE );
//if we returned invalid data or the price didnt change do nothing
if ( !@$GLOBALS['ticker'][$t] || $GLOBALS['ticker'][$t]['ticker']['last'] < .00000001 || $GLOBALS['ticker'][$t]['ticker']['last'] == $GLOBALS['ticker'][$t-1]['ticker']['last'] ){ 
//unset($GLOBALS['ticker'][$t]); //throw away the data
$t--;//Decriment Ticker 
$GLOBALS["t"] = $t//copy ticker to global
return $GLOBALS['ticker']; //exit the function
}
//if we have enough data to determine direction
if ( $t $GLOBALS['interval']+){
//reset changecurrency
$changeCurrency 0;
if ($GLOBALS['currency'] == "B"){
//reset quote # counter
$q 0;
//loop through last Interval quotes
while ( $q <= $GLOBALS['interval'] ){
//if our first ticker is GT than the tickers after, price is decreasing
if ( $GLOBALS['ticker'][$t-$GLOBALS['interval']]['ticker']['last'] > $GLOBALS['ticker'][$t-$q]['ticker']['last'] ) {$changeCurrency++;}
$q++;
}
if ( $changeCurrency >= $GLOBALS['interval']-$GLOBALS['curve'] ){
//CHANGE CURRENCY B to U SELL B for U
$GLOBALS['sellPrice'] = $GLOBALS['ticker'][$t]['ticker']['sell'];
$GLOBALS['currency'] = "U";

$trade json_decode(btce_query("getInfo"), TRUE);
//Trade out 50% of B for U
$trade json_decode(btce_query("Trade", array("pair" => "btc_usd""type" => "sell""amount" => $trade['return']['funds']['btc'], "rate" => $GLOBALS['ticker'][$t]['ticker']['sell'])), TRUE);
cancelOrders();
print "\n Changed Currecy B to U: {$GLOBALS['sellPrice']} ";
}
}


if ($GLOBALS['currency'] == "U"){
// reset quote # counter
$q 0;
//loop through quotes
while ( $q <= $GLOBALS['interval'] ){
//if first ticker < following tickers price is increasing
if ( $GLOBALS['ticker'][$t-$GLOBALS['interval']]['ticker']['last'] < $GLOBALS['ticker'][$t-$q]['ticker']['last'] ) {$changeCurrency++;}
$q++;
}
if ( $changeCurrency >= $GLOBALS['interval']-$GLOBALS['curve'] ){
$GLOBALS['buyPrice'] = $GLOBALS['ticker'][$t]['ticker']['buy'];
if ( $GLOBALS['sellPrice'] < $GLOBALS['buyPrice'] ){
//You'll loose money if you sell low and buy high.
//You'll also lose money if you sit while currency declines
}
//CHANGE CURRENCY B to U
$GLOBALS['currency'] = "B";

$trade json_decode(btce_query("getInfo"), TRUE);
$trade json_decode(btce_query("Trade", array("pair" => "btc_usd""type" => "buy""amount" => $trade['return']['funds']['usd'] , "rate" => $GLOBALS['ticker'][$t]['ticker']['buy'])), TRUE);
cancelOrders();
print "\n Changed Currecy U to B: {$GLOBALS['buyPrice']}";
}
}
}
$GLOBALS["t"] = $t;
return;
}

/************************************************************************************
 *OLD CHANGE CURRENCY CODE
 * function changeCurrency()
{
//SOLVED the price can't change if the last doesn't change or our calc shouldn't change unless the last changes
//DEBUG if ticker fails to download you could have issues?
//DEBUG if a GAP between buy and sell develops you could change,
//Analyse last buy and sell x 4 not 5 
//
//Execute from non-trading and non-duplicate runs, where arbot prints a .
//incriment ticker count
$t = $GLOBALS["t"];
$t++;

//unset($GLOBALS['ticker'][$t-$GLOBALS['interval']-1]); we can proly leave this.... IDK how large it will get.
//pull ticker
$GLOBALS['ticker'][$t] = @json_decode( send( 'https://btc-e.com/api/2/btc_usd/ticker' ), TRUE );
//if we returned invalid data or the price didnt change

if ( !@$GLOBALS['ticker'][$t] || $GLOBALS['ticker'][$t]['ticker']['last'] < .00000001 || $GLOBALS['ticker'][$t]['ticker']['last'] == $GLOBALS['ticker'][$t-1]['ticker']['last'] ){ 
//unset($GLOBALS['ticker'][$t]); //throw away the data
$t--;//Decriment Ticker 
$GLOBALS["t"] = $t; //copy ticker to global
return $GLOBALS['ticker']; //exit the function
}
if ( $t > $GLOBALS['interval']+1 ){
//print "Last: {$GLOBALS['ticker'][$t]['ticker']['last']}";
//keep last interval price and volumes
//{"ticker":{"high":154.99001,"low":121.12,"avg":138.055005,"vol":4033206.07126,"vol_cur":29514.1577,"last":133,"buy":133.4,"sell":132.2,"server_time":1366830779}}

$changeCurrency = 0;
if ($GLOBALS['currency'] == "B"){
//analyse price trend to sell out of B (sell price)
//first if buyPrice < sellPrice buy at 110 current 102 price up buy price down sell 
//if 4 of 5 are less than first
//we need to analyze 6, 1 = original, 2,3,4,5,6 = 5 comps, 4 of which must be up or down,
$q = 1;
while ( $q <= $GLOBALS['interval']+1 ){
if ( $GLOBALS['ticker'][$t-$GLOBALS['interval']]['ticker']['sell'] > $GLOBALS['ticker'][$t-$q]['ticker']['sell'] ) {$changeCurrency++;}
$q++;
//print "CC: {$changeCurrency} ";
}
if ( $changeCurrency >= $GLOBALS['interval']-$GLOBALS['curve'] ){
//Analyse buy Book
$q = 1;
while ( $q <= $GLOBALS['interval']+1 ){
if ( $GLOBALS['ticker'][$t-$GLOBALS['interval']]['ticker']['buy'] > $GLOBALS['ticker'][$t-$q]['ticker']['buy'] ) {$changeCurrency++;}
$q++;
//print "CC: {$changeCurrency} ";
}

if ( $changeCurrency >= $GLOBALS['interval']-$GLOBALS['curve'] ){
//CHANGE CURRENCY B to U
$GLOBALS['sellPrice'] = $GLOBALS['ticker'][$t]['ticker']['sell'];
$GLOBALS['currency'] = "U";

$trade = json_decode(btce_query("getInfo"), TRUE);
$trade = json_decode(btce_query("Trade", array("pair" => "btc_usd", "type" => "sell", "amount" => $trade['return']['funds']['btc']/2, "rate" => $GLOBALS['ticker'][$t]['ticker']['sell']-.01)), TRUE);
cancelOrders();
print "\n Changed Currecy B to U: {$GLOBALS['sellPrice']} ";
//place order to trade out currency? .1B at a time? how to repeat?
//Trade out 50% of B for U

}
}
}

$changeCurrency = 0;
if ($GLOBALS['currency'] == "U"){
//analyse price trend to sell out of B (sell price)
//first if buyPrice < sellPrice buy at 110 current 102 price up buy price down sell 
//if x-1 of x buys are less than first
$q = 1;
while ( $q <= $GLOBALS['interval']+1 ){
if ( $GLOBALS['ticker'][$t-$GLOBALS['interval']]['ticker']['buy'] < $GLOBALS['ticker'][$t-$q]['ticker']['buy'] ) {$changeCurrency++;}
$q++;
//print "CC: {$changeCurrency} ";
}

//if x-1 of x sells are less than first
//SOLVED make this a nested if,
if ( $changeCurrency >= $GLOBALS['interval']-$GLOBALS['curve'] ){
$q = 1;
while ( $q <= $GLOBALS['interval']+1 ){
if ( $GLOBALS['ticker'][$t-$GLOBALS['interval']]['ticker']['sell'] < $GLOBALS['ticker'][$t-$q]['ticker']['sell'] ) {$changeCurrency++;}
$q++;
//print "CC: {$changeCurrency} ";
}
if ( $changeCurrency >= $GLOBALS['interval']-$GLOBALS['curve'] ){
$GLOBALS['buyPrice'] = $GLOBALS['ticker'][$t]['ticker']['buy'];
if ( $GLOBALS['sellPrice'] < $GLOBALS['buyPrice'] ){
//You'll loose money if you sell low and buy high.
//You'll also lose money if you sit while currency declines
}
//CHANGE CURRENCY B to U
$GLOBALS['currency'] = "B";

$trade = json_decode(btce_query("getInfo"), TRUE);
$trade = json_decode(btce_query("Trade", array("pair" => "btc_usd", "type" => "buy", "amount" => $trade['return']['funds']['usd']/2 , "rate" => $GLOBALS['ticker'][$t]['ticker']['buy']+.01)), TRUE);
cancelOrders();
print "\n Changed Currecy U to B: {$GLOBALS['buyPrice']}";
}
//and if volume is increasing
}
}
//if last is lt low
//if last is gt than high
//if last is lt average
//if last is gt average

//current currency = $GLOBALS['currency']
}
$GLOBALS["t"] = $t;
return;
}
****************************************************************************************************/

?>



miaviator (OP)
Donator
Hero Member
*
Offline Offline

Activity: 686
Merit: 519


It's for the children!


View Profile WWW
May 05, 2013, 10:54:35 PM
 #2

Reserved.

johnblaze
Sr. Member
****
Offline Offline

Activity: 252
Merit: 250


View Profile
May 05, 2013, 10:58:47 PM
 #3

what does the first line do with the shell script call?
miaviator (OP)
Donator
Hero Member
*
Offline Offline

Activity: 686
Merit: 519


It's for the children!


View Profile WWW
May 05, 2013, 11:02:51 PM
 #4

That (is a reminder) to kick off my roundrobbin program inside a screen session -> if Btc-e errors a lot their ddos tends to block IP's so I swap IP's on the second error, here is the roundrobin.sh script.

Code:
#!/bin/bash
NODE1="IP"
NODE2="IP"

#########################
#Arbitrage Bot Round Robin Controller
#must copy nonce
#must update master script
#must swap nodes on second error
#must respect ddos timer
##########################
echo "Round Robbin Started...  Ensure you are in a screen session!"

#node 1
node1(){
    echo "Starting Node 1..."
    ssh -T $NODE1 "cd ~/arbot;php ~/arbot/arbot.php;exit 0;"
    echo "Exited Node 1, Copying Files to Node 2."
    scp $NODE1:/PATH/arbot.php  $NODE2:/PATH/arbot.php
    scp $NODE1:/PATH/nonce $NODE2:/PATH/nonce
}


#node 2
node2(){
    echo "Node 2 Started."
    ssh -T $NODE2 "cd ~/arbot;php ~/arbot/arbot.php;exit 0;"
    echo "Exited Node 2, Copying Files to Node 1."
    scp $NODE2:/PATH/arbot.php  $NODE1:/PATH/arbot.php
    scp $NODE2:/PATH/nonce $NODE1:/PATH/nonce
}

#Main Loop
while [ 1 -eq 1 ];
do
        node1
        sleep 5
        node2
        sleep 5
    done

Jude Austin
Legendary
*
Offline Offline

Activity: 1140
Merit: 1000


The Real Jude Austin


View Profile WWW
May 11, 2013, 12:06:10 PM
 #5

Any recommended settings?

Been messing with it for awhile now.

I decided not to use the roundrobin.sh and I set the error sleep time to 15 seconds and to terminate after 100 errors.

This let's it time out but continue to still run.

Here is some output from it:
Code:
 BULB Amount: -0.026001930645792 BULB Percent: -2.6001930645792
 BLUB Amount: -0.012551582183679 BLUB Percent: -1.2551582183679.-------
 BULB Amount: -0.025993288249436 BULB Percent: -2.5993288249436
 BLUB Amount: -0.012551582183679 BLUB Percent: -1.2551582183679.----------------------------------
 BULB Amount: -0.025993288249436 BULB Percent: -2.5993288249436
 BLUB Amount: -0.014935900543359 BLUB Percent: -1.4935900543359.---------------------
 BULB Amount: -0.031117841275131 BULB Percent: -3.1117841275131
 BLUB Amount: -0.014935900543359 BLUB Percent: -1.4935900543359.-----------
 BULB Amount: -0.031117841275131 BULB Percent: -3.1117841275131
 BLUB Amount: -0.014935900543359 BLUB Percent: -1.4935900543359.------------------------------------------------------------
 BULB Amount: -0.025310014512676 BULB Percent: -2.5310014512676
 BLUB Amount: -0.012210022717122 BLUB Percent: -1.2210022717122.-------------------------------------------------------------------------------------------------------------------------------------------
 BULB Amount: -0.020668865787978 BULB Percent: -2.0668865787978
 BLUB Amount: -0.01340038470649 BLUB Percent: -1.340038470649.-----------------------
 BULB Amount: -0.020668865787978 BULB Percent: -2.0668865787978
 BLUB Amount: -0.01340038470649 BLUB Percent: -1.340038470649.------------
 BULB Amount: -0.020668865787978 BULB Percent: -2.0668865787978
 BLUB Amount: -0.01340038470649 BLUB Percent: -1.340038470649.-----------
 BULB Amount: -0.020668865787978 BULB Percent: -2.0668865787978
 BLUB Amount: -0.01340038470649 BLUB Percent: -1.340038470649.---------------------
 BULB Amount: -0.020668865787978 BULB Percent: -2.0668865787978
 BLUB Amount: -0.01340038470649 BLUB Percent: -1.340038470649.-------------------------
 BULB Amount: -0.020668865787978 BULB Percent: -2.0668865787978
 BLUB Amount: -0.01340038470649 BLUB Percent: -1.340038470649.---------------------
 BULB Amount: -0.020668865787978 BULB Percent: -2.0668865787978
 BLUB Amount: -0.013097281999212 BLUB Percent: -1.3097281999212.-----------
 BULB Amount: -0.020668865787978 BULB Percent: -2.0668865787978
 BLUB Amount: -0.013097281999212 BLUB Percent: -1.3097281999212.----------
 BULB Amount: -0.020668865787978 BULB Percent: -2.0668865787978
 BLUB Amount: -0.013097281999212 BLUB Percent: -1.3097281999212.----------------------
 BULB Amount: -0.019983779300247 BULB Percent: -1.9983779300247
 BLUB Amount: -0.013097281999212 BLUB Percent: -1.3097281999212.-------------------------
 BULB Amount: -0.019983779300247 BULB Percent: -1.9983779300247
 BLUB Amount: -0.013097281999212 BLUB Percent: -1.3097281999212.----------
 BULB Amount: -0.019983779300247 BULB Percent: -1.9983779300247
 BLUB Amount: -0.011582071565528 BLUB Percent: -1.1582071565528.
 BULB Amount: -0.019983779300247 BULB Percent: -1.9983779300247
 BLUB Amount: -0.011240531161784 BLUB Percent: -1.1240531161784.-------------------------------

I assume that when these amounts become positive it will begin to trade them accordingly?

Thanks for your time and script!

Buy or sell $100 of Crypto and get $10!
Stunna
Legendary
*
Offline Offline

Activity: 3192
Merit: 1279


Primedice.com, Stake.com


View Profile
May 11, 2013, 06:59:16 PM
 #6

Is arbitrage still profitable? and how has your experience been so far running this?

Stake.com Fastest growing crypto casino & sportsbook
Primedice.com The original bitcoin instant dice game
grabble
Newbie
*
Offline Offline

Activity: 23
Merit: 0


View Profile
May 12, 2013, 12:31:56 AM
 #7

I've been having a hell of a time getting this thing to work at all. I think my curl is all screwed up. I just keep getting 301 moved permanently ... Anyone else having this issue?
miaviator (OP)
Donator
Hero Member
*
Offline Offline

Activity: 686
Merit: 519


It's for the children!


View Profile WWW
May 12, 2013, 02:28:32 AM
 #8

Any recommended settings?

I assume that when these amounts become positive it will begin to trade them accordingly?

Thanks for your time and script!

You are correct, it should trade as soon as you see a positive result.  Keep in mind it takes a profit amount in B and a profit threshold as a percentage so it has to meet both criteria before trading.

Code:
//Using dynamic depth we can execute the most profitable trade in $currency with a minimum % profit
//set both to 0 to trade everything profitable
$threshold = 0; // Execute any profitable trade **Trade will not execute unless we beat the P % below
$thresholdP = .5; //Threshold as a %

miaviator (OP)
Donator
Hero Member
*
Offline Offline

Activity: 686
Merit: 519


It's for the children!


View Profile WWW
May 12, 2013, 02:32:15 AM
 #9

Is arbitrage still profitable? and how has your experience been so far running this?

Arbitrage is less profitable now that a lot of bots exist.   As I said I have abandoned this for a rolling buy sell bot which is proving more profitable.  I did make several B over the course of a few weeks using this bot.  I would think that other exchanges and auto transfers could be added in order to take advantage of the dozen or so alts out there with different prices and faster transaction times than B.

All of that said.  I'm not one to turn down making even $.01 a day while I sleep, eat and work.  Free money is free money.

miaviator (OP)
Donator
Hero Member
*
Offline Offline

Activity: 686
Merit: 519


It's for the children!


View Profile WWW
May 12, 2013, 02:36:09 AM
 #10

I've been having a hell of a time getting this thing to work at all. I think my curl is all screwed up. I just keep getting 301 moved permanently ... Anyone else having this issue?

Yes.  I have seen this.  99% of the time this means that Cloudflare is either asking you to verify you are human (using browser javascript) or they have banned your IP.

This was a huge Issue for me since I run everything on headless linux server boxes.  I would normally setup a quick socks proxy via ssh and open btc-e via a browser to verify I was human via the javascript and then it would run.  This was one of the reasons I instituted the round Robbin script.  CloudFlare is horrible.  It protects the company from DDOS by preventing valid users access which makes the DDOS successful. 

Let me know if your issue is different and look into it.

grabble
Newbie
*
Offline Offline

Activity: 23
Merit: 0


View Profile
May 12, 2013, 01:59:46 PM
 #11

I think I'm just doing something wrong, I got past the 301 error but now the only output I get is .E1-E2-E3 and then the process terminates. I think I must have configured something wrong with my php or I'm executing it wrong (I'm literally just putting php bot.php). PHP has never been my strong suit haha.
miaviator (OP)
Donator
Hero Member
*
Offline Offline

Activity: 686
Merit: 519


It's for the children!


View Profile WWW
May 12, 2013, 02:46:25 PM
 #12

EDIT: You can also remove the @ symbol before each of those three json_decode lines and it should spit out an error that will help find out what's wrong with the url.

I think I'm just doing something wrong, I got past the 301 error but now the only output I get is .E1-E2-E3 and then the process terminates. I think I must have configured something wrong with my php or I'm executing it wrong (I'm literally just putting php bot.php). PHP has never been my strong suit haha.

Your getting hit by their DDOS protection still.

Those errors show when you can't pull the ticker pages from E:

Code:
		$BTCtoUSD = @json_decode( send( 'https://btc-e.com/api/2/btc_usd/depth' ), TRUE );
$LTCtoBTC = @json_decode( send( 'https://btc-e.com/api/2/ltc_btc/depth' ), TRUE );
$LTCtoUSD = @json_decode( send( 'https://btc-e.com/api/2/ltc_usd/depth' ), TRUE );

check those links from a browser using the same IP as the machine you are running the script from:
https://btc-e.com/api/2/btc_usd/depth
https://btc-e.com/api/2/ltc_btc/depth
https://btc-e.com/api/2/ltc_usd/depth

This is where swapping IP's comes in handy.

You can also stop the program from exiting (so it will keep trying to pull the books until your IP is banned Smiley  by removing this line:

Code:
			
if ( $e > 2 ) exit;

grabble
Newbie
*
Offline Offline

Activity: 23
Merit: 0


View Profile
May 12, 2013, 09:25:21 PM
 #13

I can pull the data just fine with:

Code:

$c = curl_init('https://btc-e.com/api/2/btc_usd/depth');
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);

$html = curl_exec($c);

if (curl_error($c))
    die(curl_error($c));

// Get the status code
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);

curl_close($c);

echo "<pre>".print_r($html, true)."</pre>";


It prints this:

<pre>{"asks":[[110.999,0.482],[111,0.08565512],[111.44,2.42373056],[111.45,0.95797641],[111.46,12.766699],[111.47,43.73969491],[111.499,0.02],[111.5,1.0
4798],[111.59,0.22061],[111.6,1.35216595],[111.608,0.01],[111.64,0.011],[111.65,0.2989164],[111.69,0.08],[111.698,0.010978],[111.7,0.6438],[111.769,0.01],[111
.829,5.282],[111.839,0.1825],[111.84,0.25],[111.93,0.25],[111.949,0.02],[111.95,0.2601796],[111.96,0.01],[111.98,0.14],[111.99,4.55689031],[111.999,0.21026026
],[112,6.56381636],[112.01,0.01],[112.012,0.1117995],[112.06,0.01],[112.07,0.25],[112.1,24.371],[112.14,0.09123795],[112.198,0.010978],[112.2,0.3323],[112.28,
0.25],[112.298,0.01],[112.3,1.925859],[112.312,0.010976],[112.32,0.01],[112.373,0.05],[112.384,0.05],[112.394,0.05],[112.395,0.05],[112.4,3.2528],[112.406,0.1
],[112.41,0.01],[112.417,0.05],[112.418,0.05],[112.428,0.05],[112.43,0.25],[112.44,1.05],[112.45,0.0101796],[112.46,0.01],[112.47,0.11079744],[112.479,0.01],[
112.49,0.01],[112.5,6.79002662],[112.51,0.04998],[112.544,0.1996],[112.55,0.25691786],[112.555,0.01],[112.57,0.25],[112.578,0.05],[112.598,0.0101796],[112.6,0
.07858852],[112.63,0.0113],[112.634,0.01],[112.65,0.2685204],[112.69,10.01300269],[112.698,0.010978],[112.7,0.2994],[112.75,0.04882465],[112.77,0.1127627],[11
2.79,5],[112.8,2.20190831],[112.81,0.02104057],[112.86,0.25],[112.87,0.0109],[112.878,0.019],[112.89,0.30755],[112.9,5.80713602],[112.93,0.474],[112.94,0.2009
6176],[112.95,10.0101796],[112.967,0.01],[112.98,5.89170535],[112.988,0.02],[112.99,6.111],[113,34.99802712],[113.03,0.25],[113.05,0.15],[113.13,10.01996],[11
3.136,0.02062671],[113.16,3.961],[113.17,0.5],[113.171,0.03369487],[113.176,0.04990195],[113.177,0.2],[113.178,2.42799529],[113.179,0.2],[113.187,0.2],[113.18
8,0.2],[113.189,0.2],[113.19,0.6],[113.197,0.2],[113.198,0.010978],[113.199,1],[113.2,1.01400002],[113.22,0.27],[113.227,0.24837326],[113.228,0.05],[113.229,0
.05],[113.23,3.5325],[113.25,0.31757619],[113.3,1.33626884],[113.32,0.25],[113.33,0.2],[113.39,0.84166937],[113.4,10.05373585],[113.412,0.01],[113.421,0.03],[
113.423,0.01],[113.45,0.0101796],[113.47,3.992],[113.471,0.022],[113.49,1.32405],[113.5,232.2819077],[113.58,0.1],[113.59,10],[113.6,1.87955804],[113.606,0.02
],[113.61,0.25],[113.613,0.1996],[113.64,0.05],[113.65,0.05198778],[113.698,0.010978],[113.7,0.69915719],[113.71,0.1],[113.75,0.02],[113.751,0.02],[113.759,0.
0103152],[113.76,0.1],[113.8,1.91389155],[113.82,1],[113.83,0.02],[113.839,0.95492748],[113.84,0.01],[113.865,0.02]],"bids":[[110.9,0.13374988],[110.84,0.15],
[110.83,1.91367751],[110.829,3.02409137],[110.82,0.25],[110.74,0.44115717],[110.71,4.4743],[110.7,8.68410389],[110.699,0.01],[110.69,0.15],[110.506,0.01002],[
110.5,0.0399],[110.401,5.96690474],[110.4,0.743],[110.381,0.01795565],[110.38,0.25],[110.37,0.89],[110.347,0.017],[110.27,0.25],[110.179,0.02004008],[110.01,0
.0102],[110.002,0.011],[110.001,1.803123],[110,11.24350583],[109.981,0.1825],[109.98,0.25],[109.95,0.0102],[109.93,4.90219398],[109.793,0.017],[109.699,0.01],
[109.698,0.03],[109.61,0.12729965],[109.55,0.25],[109.502,0.011],[109.42,0.0101],[109.4,5.17183642],[109.39,0.25],[109.27,0.25],[109.239,0.017],[109.18,0.8502
],[109.176,2.583],[109.061,0.1],[109.02,0.01],[109.01,2.9652],[109.002,0.011],[109,2.48760606],[108.96,0.01],[108.834,0.0122],[108.685,0.017],[108.65,0.261211
22],[108.61,3.7799184],[108.6,22.45158424],[108.59,5.9299154],[108.56,0.25],[108.52,0.02],[108.51,0.7],[108.502,0.011],[108.5,6.47790337],[108.42,1.25680822],
[108.352,0.030764],[108.3,0.55],[108.29,0.25],[108.25,1.05030486],[108.131,0.017],[108.13,1.453],[108.1,4.55772433],[108.085,0.011],[108.084,0.01],[108.07,0.0
5201],[108.06,0.2555],[108.02,9.794],[108.01,0.0202],[108.002,0.011],[108,35.30530543],[107.945,2.2234],[107.847,0.5563],[107.8,0.05],[107.75,0.05],[107.74,0.
25],[107.7,0.1],[107.685,11.972857],[107.66,0.0553],[107.6,0.35039174],[107.577,0.03704008],[107.539,0.2],[107.53,0.03],[107.52,5],[107.51,0.71513592],[107.50
2,0.011],[107.5,8.202],[107.47,0.01],[107.43,0.51262715],[107.4,0.4],[107.348,0.027],[107.337,0.2],[107.33,0.8],[107.32,0.2],[107.3,0.4],[107.28,0.09],[107.21
,0.45],[107.18,0.02],[107.17,0.02],[107.12,1.59895495],[107.111,0.01],[107.1,3.10063865],[107.023,0.017],[107.01,1.16919169],[107.002,0.011],[107,24.92454336]
,[106.93,3],[106.9,1],[106.838,0.32819],[106.76,0.01683],[106.63,1.35803264],[106.614,0.0235],[106.6,0.2056],[106.54,0.04],[106.517,0.01],[106.502,0.011],[106
.5,24.31393613],[106.469,0.017],[106.44,0.01872844],[106.411,0.01],[106.41,0.011],[106.4,1],[106.305,0.01],[106.25,2.91808503],[106.21,10],[106.2,23.19],[106.
12,0.92432135],[106.1,40.05754467],[106.01,10.0617591],[106.002,0.011],[106,18.16052253],[105.99,499.22271165],[105.94,0.7],[105.915,0.017],[105.9,1.2525],[10
5.89,0.664],[105.72,4],[105.711,0.040504],[105.6,3.49097394],[105.56,0.4],[105.55,0.05],[105.502,0.011],[105.5,1.55],[105.45,0.304844],[105.42,1.10233139],[10
5.361,0.017],[105.36,0.011]]}</pre>
C:\Apache24\htdocs>php bot.php
.E1-E2-E3
C:\Apache24\htdocs>

So I don't think I'm running up against the DDOS protection. I've been wracking my brain for 2 days and can't figure it out.

What version of php do you run? Maybe that has something to do with it?
miaviator (OP)
Donator
Hero Member
*
Offline Offline

Activity: 686
Merit: 519


It's for the children!


View Profile WWW
May 12, 2013, 09:38:33 PM
 #14

I'm running PHP 5.3.2-1ubuntu4.19 with Suhosin-Patch (cli)

Try changing this code block out with the changes below and past the output, this should show what's happening.

Code:
	while( empty($BTCtoUSD) || empty($LTCtoBTC) || empty($LTCtoUSD) ){
$BTCtoUSD = @json_decode( send( 'https://btc-e.com/api/2/btc_usd/depth' ), TRUE );
$LTCtoBTC = @json_decode( send( 'https://btc-e.com/api/2/ltc_btc/depth' ), TRUE );
$LTCtoUSD = @json_decode( send( 'https://btc-e.com/api/2/ltc_usd/depth' ), TRUE );
//Verify the order book changed
$old = $BTCtoUSDold+$LTCtoBTCold+$LTCtoUSDold+$BTCtoUSDold1+$LTCtoBTCold1+$LTCtoUSDold1;
$new = $BTCtoUSD['bids'][0][0]+$LTCtoBTC['bids'][0][0]+$LTCtoUSD['bids'][0][0]+$BTCtoUSD['asks'][0][0]+$LTCtoBTC['asks'][0][0]+$LTCtoUSD['asks'][0][0];
if( !@$BTCtoUSD['bids'] || !@$LTCtoBTC['bids'] || !@$LTCtoUSD['bids']){
$BTCtoUSD = $LTCtoBTC = $LTCtoUSD = array();
//$sleep=rand(6, 36)*(++$e);
$e++;
print "E" . $e;
sleep(5);
if ( $e > 2 ) exit;
/*
if ($nanosleep > 100000)
{$nanosleep = $nanosleep+100000;} //up to 9999 runs
else
{$nanosleep = $nanosleep^10;}
*/
}


TO:
Code:
	while( empty($BTCtoUSD) || empty($LTCtoBTC) || empty($LTCtoUSD) ){
$BTCtoUSD = json_decode( send( 'https://btc-e.com/api/2/btc_usd/depth' ), TRUE );
$LTCtoBTC = json_decode( send( 'https://btc-e.com/api/2/ltc_btc/depth' ), TRUE );
$LTCtoUSD = json_decode( send( 'https://btc-e.com/api/2/ltc_usd/depth' ), TRUE );
//Verify the order book changed
$old = $BTCtoUSDold+$LTCtoBTCold+$LTCtoUSDold+$BTCtoUSDold1+$LTCtoBTCold1+$LTCtoUSDold1;
$new = $BTCtoUSD['bids'][0][0]+$LTCtoBTC['bids'][0][0]+$LTCtoUSD['bids'][0][0]+$BTCtoUSD['asks'][0][0]+$LTCtoBTC['asks'][0][0]+$LTCtoUSD['asks'][0][0];
if( !$BTCtoUSD['bids'] || !$LTCtoBTC['bids'] || !$LTCtoUSD['bids']){
$BTCtoUSD = $LTCtoBTC = $LTCtoUSD = array();
$e++;
print "E" . $e;
sleep(5);
if ( $e > 4 ) exit;
                        print_r($BTCtoUSD);
                        print_r($LTCtoBTC);
                        print_r($LTCtoUSD);
}

grabble
Newbie
*
Offline Offline

Activity: 23
Merit: 0


View Profile
May 12, 2013, 09:49:37 PM
 #15

C:\Apache24\htdocs>php bot.php
.E1Array
(
)
Array
(
)
Array
(
)
-E2Array
(
)
Array
(
)
Array
(
)
-E3Array
(
)
Array
(
)
Array
(
)
-E4Array
(
)
Array
(
)
Array
(
)
-E5

I don't even know what to make of it, it's not returning anything it appears. I'm going to try downgrading my PHP as I'm running 5.4.15 maybe this will help? I don't really see why else it does not seem to work.
grabble
Newbie
*
Offline Offline

Activity: 23
Merit: 0


View Profile
May 12, 2013, 10:40:11 PM
 #16

Holy crap. Progress! (I think).

I started to understand php more and got suspicious of the send function.

I modified the function's options to this:

Code:
	

function send( $url = NULL )
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; BTCE PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$result = curl_exec($ch);
curl_close($ch);

return $result;
}


I seem to be getting this:

Code:
C:\Apache2\htdocs>php bot.php
..---..--.-------------------------------------------.--.--------------------.--------.-.-

At least it's not getting errors anymore right? I think it had to do with SSL using HTTPS. It was giving curl an error that wasn't coming over to the php side for me to see but when I started messing with curl I realized it was getting a certificate error ...
miaviator (OP)
Donator
Hero Member
*
Offline Offline

Activity: 686
Merit: 519


It's for the children!


View Profile WWW
May 12, 2013, 11:15:28 PM
 #17

Nice!  I would assume it has something to do with the windows vs linux curl and ssl implementations?

That output shows it's searching for trades.  If you change debug from NULL to TRUE it will spit out some more information.

Keep in mind debug has to be off in order to complete any trades as it takes to much time for php to print messages between trades.

grabble
Newbie
*
Offline Offline

Activity: 23
Merit: 0


View Profile
May 13, 2013, 09:35:26 AM
Last edit: May 13, 2013, 02:00:46 PM by grabble
 #18

It's officially working, I appreciate the help! It totally was the SSL.

My fix should work for anyone using Windows who wants to run the bot.

Thanks again, I'll donate my first week of profit and a little extra next Sunday for taking the time to code this thing and help me out.
Eb0la
Sr. Member
****
Offline Offline

Activity: 350
Merit: 250



View Profile
May 27, 2013, 03:43:52 AM
 #19

It's officially working, I appreciate the help! It totally was the SSL.

My fix should work for anyone using Windows who wants to run the bot.

Thanks again, I'll donate my first week of profit and a little extra next Sunday for taking the time to code this thing and help me out.

Hi!  I am giving this a try to see how it performs.  I tried out grabble's fix on windows here and can confirm it successfully fixed the issue for me as well.  Loaded the account with a couple LTC and smaller amounts of BTC so its not a huge loss if I do lose Cheesy

I will donate some of my profits if I am successful aswell!
billotronic
Legendary
*
Offline Offline

Activity: 1610
Merit: 1000


Crackpot Idealist


View Profile
May 27, 2013, 12:19:48 PM
 #20

Tried running this to see what it would do and it's throwing me these errors:

Code:
gdlinux@gdlinux:~/Desktop$ php phpbot.php
#screen -dmS arbot -h 1024 -L ./roundrobin.sh
PHP Warning:  fopen(nonce): failed to open stream: No such file or directory in /home/gdlinux/Desktop/phpbot.php on line 689
PHP Warning:  fread() expects parameter 1 to be resource, boolean given in /home/gdlinux/Desktop/phpbot.php on line 690
PHP Warning:  fclose() expects parameter 1 to be resource, boolean given in /home/gdlinux/Desktop/phpbot.php on line 691
PHP Fatal error:  Call to undefined function curl_init() in /home/gdlinux/Desktop/phpbot.php on line 665

and this is a little beyond me to figure out. I set debug to TRUE and the output did not give me any more hints. Thoughts?

This post sums up why all this bullshit is a scam
Read It. Hate It. Change the facts that it represents.
https://bitcointalk.org/index.php?topic=1606638.msg16139644#msg16139644
Pages: [1] 2 3 4 »  All
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!