Author

Topic: [Code] PHP Arbitrage Calculator for BTC-e (Read 2716 times)

hero member
Activity: 762
Merit: 500
July 21, 2015, 08:28:58 PM
#7
Is there any update?
member
Activity: 70
Merit: 10
I'm getting divide by zero errors...

http://supernova.allalla.com/public_html/index.php

do you have php curl installed on your machine?
full member
Activity: 194
Merit: 100
I'm getting divide by zero errors...

http://supernova.allalla.com/public_html/index.php
hero member
Activity: 616
Merit: 522
There is some kind of type hinting in function arguments declaration since PHP 5.1, so... since 2005?
legendary
Activity: 1862
Merit: 1011
Reverse engineer from time to time
Since when can you specify the array keyword before variable declaration in a function's arguments?
member
Activity: 110
Merit: 10
Thanks for sharing. I always learn something from other people's code.
sr. member
Activity: 350
Merit: 250
This is a project I just recently started. It's written in PHP, upload the script somewhere and run it through your browser.

It shows when there is arbitrage available within the BTC-e exchange itself. It does nothing else yet, I'm still working on getting automatic trades executed. Fees are automatically calculated. If I've missed something here as far as the actual calculations go please let me know. Yes, I'm aware it's only going off last price at the moment, it's going to be able to calculate using the whole order book soon. It's sloppy and quite awful, but I've only just started. I've had good luck with it during the night when prices tend to jump around a bit more, and conversions through RUR/EUR seem to yield the best results.

Code:
function btce_query($method, array $req = array()) {

        static 
$ch null;
        if (
is_null($ch)) {
                
$ch curl_init();
                
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.ru/tapi/');
        
curl_setopt($chCURLOPT_POSTFIELDS$post_data);
        
curl_setopt($chCURLOPT_HTTPHEADER$headers);
        
curl_setopt($chCURLOPT_SSL_VERIFYPEERFALSE);
 
        
// run the query
        
$res curl_exec($ch);
        if (
$res === false) throw new Exception('Could not get reply: '.curl_error($ch));
        
$dec json_decode($restrue);
        if (!
$dec) throw new Exception('Invalid data received, please make sure connection is working and requested API exists');
        return 
$dec;
}


function 
GetJsonFeed($json_url)
{
    
$feed file_get_contents($json_url);
    return 
json_decode($feedtrue);
}
$BTC_RUR GetJsonFeed("http://btc-e.com/api/2/btc_rur/ticker");
$BTC_RUR_SELL $BTC_RUR["ticker"]["sell"];
$BTC_RUR_BUY $BTC_RUR["ticker"]["buy"];
// LTC - RUR
$LTC_RUR GetJsonFeed("http://btc-e.com/api/2/ltc_rur/ticker");
$LTC_RUR_SELL $LTC_RUR["ticker"]["sell"];
$LTC_RUR_BUY $LTC_RUR["ticker"]["buy"];
// USD - RUR
$USD_RUR GetJsonFeed("http://btc-e.com/api/2/usd_rur/ticker");
$USD_RUR_SELL $USD_RUR["ticker"]["sell"];
$USD_RUR_BUY $USD_RUR["ticker"]["buy"];
// BTC -  EUR
$BTC_EUR GetJsonFeed("http://btc-e.com/api/2/btc_eur/ticker");
$BTC_EUR_SELL $BTC_EUR["ticker"]["sell"];
$BTC_EUR_BUY $BTC_EUR["ticker"]["buy"];
// LTC - BTC
$LTC_BTC GetJsonFeed("http://btc-e.com/api/2/ltc_btc/ticker");
$LTC_BTC_SELL $LTC_BTC["ticker"]["sell"];
$LTC_BTC_BUY $LTC_BTC["ticker"]["buy"];
// EUR - USD
$EUR_USD GetJsonFeed("http://btc-e.com/api/2/eur_usd/ticker");
$EUR_USD_SELL $EUR_USD["ticker"]["sell"];
$EUR_USD_BUY $EUR_USD["ticker"]["buy"];
// LTC - USD
$LTC_USD GetJsonFeed("http://btc-e.com/api/2/ltc_usd/ticker");
$LTC_USD_SELL $LTC_USD["ticker"]["sell"];
$LTC_USD_BUY $LTC_USD["ticker"]["buy"];
// BTC - USD
$BTC_USD GetJsonFeed("http://btc-e.com/api/2/btc_usd/ticker");
$BTC_USD_SELL $BTC_USD["ticker"]["sell"];
$BTC_USD_BUY $BTC_USD["ticker"]["buy"];
// Possible Conversions
// BTC -> LTC, USD, EUR, RUR
// LTC -> USD, RUR
// USD -> RUR, EUR

echo('');
echo 
"
";
echo 
"SELL / BUY
"
;
echo 
"BTC -> USD : ".$BTC_USD_SELL." / ".$BTC_USD_BUY."
"
;
echo 
"BTC -> RUR : ".$BTC_RUR_SELL." / ".$BTC_RUR_BUY."
"
;
echo 
"BTC -> EUR : ".$BTC_EUR_SELL." / ".$BTC_EUR_BUY."
"
;
echo 
"LTC -> BTC : ".$LTC_BTC_SELL." / ".$LTC_BTC_BUY."
"
;
echo 
"LTC -> USD : ".$LTC_USD_SELL." / ".$LTC_USD_BUY."
"
;
echo 
"LTC -> RUR : ".$LTC_RUR_SELL." / ".$LTC_RUR_BUY."
"
;
echo 
"USD -> RUR : ".$USD_RUR_SELL." / ".$USD_RUR_BUY."
"
;
echo 
"EUR -> USD : ".$EUR_USD_SELL." / ".$EUR_USD_BUY."
"
;

echo 
"
~ Trade ~"
;
echo 
"
1 BTC -> USD - BTC <- ALL USD"
;
echo 
"
1 BTC -> RUR - BTC <- ALL RUR"
;
echo 
"
1 BTC -> EUR - BTC <- ALL EUR"
;
echo 
"
1 BTC -> LTC - BTC <- ALL LTC"
;
echo 
"
ALL LTC -> USD - LTC <- ALL USD"
;
echo 
"
ALL LTC -> RUR - LTC <- ALL RUR"
;
echo 
"
ALL USD -> RUR - USD <- ALL RUR"
;
echo 
"
ALL EUR -> USD - EUR <- ALL USD"
;

echo 
"

~Arbitrage~
"
;
// BTC -> LTC -> USD -> EUR -> BTC
$BTC_LTC_USD_EUR_BTC $LTC_BTC_BUY 0.998 $LTC_USD_SELL 0.998 $EUR_USD_BUY 0.998 $BTC_EUR_BUY 0.998;
if (
$BTC_LTC_USD_EUR_BTC 1.01) { echo "BTC -> LTC -> USD -> EUR -> BTC - ".$BTC_LTC_USD_EUR_BTC."
"
; echo "Arbitrage Found"; };
// BTC -> LTC -> USD -> RUR -> BTC
$BTC_LTC_USD_RUR_BTC $LTC_BTC_BUY 0.998 $LTC_USD_SELL 0.998 $USD_RUR_SELL 0.995 $BTC_RUR_BUY 0.998;
if (
$BTC_LTC_USD_RUR_BTC 1.01) { echo "BTC -> LTC -> USD -> RUR -> BTC - ".$BTC_LTC_USD_RUR_BTC."
"
; echo "Arbitrage Found"; };
// BTC -> LTC -> RUR -> USD -> BTC
$BTC_LTC_RUR_USD_BTC $LTC_BTC_BUY 0.998 $LTC_RUR_SELL 0.995 $USD_RUR_BUY 0.995 $BTC_USD_BUY 0.998;
if (
$BTC_LTC_RUR_USD_BTC 1.01) { echo "BTC -> LTC -> RUR -> USD -> BTC - ".$BTC_LTC_RUR_USD_BTC."
"
; echo "Arbitrage Found"; };
// BTC -> USD -> RUR -> LTC -> BTC
$BTC_USD_RUR_LTC_BTC $BTC_USD_SELL 0.998 $USD_RUR_SELL 0.995 $LTC_RUR_BUY 0.998 $LTC_BTC_SELL 0.995;
if (
$BTC_USD_RUR_LTC_BTC 1.01) { echo "BTC -> USD -> RUR -> LTC -> BTC - ".$BTC_USD_RUR_LTC_BTC."
"
; echo "Arbitrage Found"; };
// BTC -> USD -> LTC -> RUR -> BTC
$BTC_USD_LTC_RUR_BTC $BTC_USD_SELL 0.998 $LTC_USD_BUY 0.998 $LTC_RUR_SELL 0.998 $BTC_RUR_BUY 0.998;
if (
$BTC_USD_LTC_RUR_BTC 1.01) { echo "BTC -> USD -> LTC -> RUR -> BTC - ".$BTC_USD_LTC_RUR_BTC."
"
; echo "Arbitrage Found"; };
// BTC -> RUR -> USD -> LTC -> BTC
$BTC_RUR_USD_LTC_BTC $BTC_RUR_SELL 0.998 $USD_RUR_BUY 0.995 $LTC_USD_BUY 0.998 $LTC_BTC_SELL 0.998;
if (
$BTC_RUR_USD_LTC_BTC 1.01) { echo "BTC -> RUR -> USD -> LTC -> BTC - ".$BTC_RUR_USD_LTC_BTC."
"
; echo "Arbitrage Found"; };
// BTC -> RUR -> LTC -> USD -> BTC
$BTC_RUR_LTC_USD_BTC $BTC_RUR_SELL 0.998 $LTC_RUR_BUY 0.998 $LTC_USD_BUY 0.998 $BTC_USD_BUY 0.998;
if (
$BTC_RUR_LTC_USD_BTC 1.01) { echo "BTC -> RUR -> LTC -> USD -> BTC - ".$BTC_RUR_LTC_USD_BTC."
"
; echo "Arbitrage Found"; };

// BTC -> EUR -> USD -> BTC 
$BTC_EUR_USD_BTC $BTC_EUR_SELL 0.998 $EUR_USD_SELL 0.998 $BTC_USD_BUY 0.998;
if (
$BTC_EUR_USD_BTC 1.01) { echo "BTC -> EUR -> USD -> BTC - ".$BTC_EUR_USD_BTC."
"
; echo "Arbitrage Found"; };
// BTC -> USD -> EUR -> BTC
$BTC_USD_EUR_BTC $BTC_USD_SELL 0.998 $EUR_USD_BUY 0.998 $BTC_EUR_BUY 0.998;
if (
$BTC_USD_EUR_BTC 1.01) { echo "BTC -> USD -> EUR -> BTC - ".$BTC_USD_EUR_BTC."
"
; echo "Arbitrage Found"; };

// BTC -> RUR -> USD -> BTC
$BTC_RUR_USD_BTC $BTC_RUR_SELL 0.998 $USD_RUR_BUY 0.995 $BTC_USD_BUY 0.998;
if (
$BTC_RUR_USD_BTC 1.01) { echo "BTC -> RUR -> USD -> BTC - ".$BTC_RUR_USD_BTC."
"
; echo "Arbitrage Found"; };
// BTC -> USD -> RUR -> BTC
$BTC_USD_RUR_BTC $BTC_USD_SELL 0.998 $USD_RUR_SELL 0.995 $BTC_RUR_BUY 0.998;
if (
$BTC_USD_RUR_BTC 1.01) { echo "BTC -> USD -> RUR -> BTC - ".$BTC_USD_RUR_BTC."
"
; echo "Arbitrage Found"; };

// BTC -> LTC -> USD -> BTC
$BTC_LTC_USD_BTC $LTC_BTC_BUY 0.998 $LTC_USD_SELL 0.998 $BTC_USD_BUY 0.998;
if (
$BTC_LTC_USD_BTC 1.01) { echo "BTC -> LTC -> USD -> BTC - ".$BTC_LTC_USD_BTC."
"
; echo "Arbitrage Found"; };
// BTC -> USD -> LTC -> BTC
$BTC_USD_LTC_BTC $BTC_USD_SELL 0.998 $LTC_USD_BUY 0.998 $LTC_BTC_SELL 0.998;
if (
$BTC_USD_LTC_BTC 1.01) { echo "BTC -> USD -> LTC -> BTC - ".$BTC_USD_LTC_BTC."
"
; echo "Arbitrage Found"; };

echo 
"
"
;
echo 
"No Arbitrage";
?>
Jump to: