Bitcoin Forum
May 08, 2024, 07:47:00 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: [Code] PHP Arbitrage Calculator for BTC-e  (Read 2687 times)
n4ru (OP)
Sr. Member
****
Offline Offline

Activity: 350
Merit: 250



View Profile
April 24, 2013, 10:54:02 PM
 #1

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:
<?php
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('<meta http-equiv="refresh" content="5">');
echo 
"<center><pre>";
echo 
"SELL / BUY</br>";
echo 
"BTC -> USD : ".$BTC_USD_SELL." / ".$BTC_USD_BUY."</br>";
echo 
"BTC -> RUR : ".$BTC_RUR_SELL." / ".$BTC_RUR_BUY."</br>";
echo 
"BTC -> EUR : ".$BTC_EUR_SELL." / ".$BTC_EUR_BUY."</br>";
echo 
"LTC -> BTC : ".$LTC_BTC_SELL." / ".$LTC_BTC_BUY."</br>";
echo 
"LTC -> USD : ".$LTC_USD_SELL." / ".$LTC_USD_BUY."</br>";
echo 
"LTC -> RUR : ".$LTC_RUR_SELL." / ".$LTC_RUR_BUY."</br>";
echo 
"USD -> RUR : ".$USD_RUR_SELL." / ".$USD_RUR_BUY."</br>";
echo 
"EUR -> USD : ".$EUR_USD_SELL." / ".$EUR_USD_BUY."</br>";

echo 
"</br>~ Trade ~";
echo 
"</br><a href='#'>1 BTC -> USD</a> - <a href='#'>BTC <- ALL USD</a>";
echo 
"</br><a href='#'>1 BTC -> RUR</a> - <a href='#'>BTC <- ALL RUR</a>";
echo 
"</br><a href='#'>1 BTC -> EUR</a> - <a href='#'>BTC <- ALL EUR</a>";
echo 
"</br><a href='#'>1 BTC -> LTC</a> - <a href='#'>BTC <- ALL LTC</a>";
echo 
"</br><a href='#'>ALL LTC -> USD</a> - <a href='#'>LTC <- ALL USD</a>";
echo 
"</br><a href='#'>ALL LTC -> RUR</a> - <a href='#'>LTC <- ALL RUR</a>";
echo 
"</br><a href='#'>ALL USD -> RUR</a> - <a href='#'>USD <- ALL RUR</a>";
echo 
"</br><a href='#'>ALL EUR -> USD</a> - <a href='#'>EUR <- ALL USD</a>";

echo 
"</br></br>~Arbitrage~</br>";
// 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 "<script>alert('Arbitrage Found!')</script>BTC -> LTC -> USD -> EUR -> BTC - ".$BTC_LTC_USD_EUR_BTC."</br>"; echo "<title>Arbitrage Found</title>"; };
// 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 "<script>alert('Arbitrage Found!')</script>BTC -> LTC -> USD -> RUR -> BTC - ".$BTC_LTC_USD_RUR_BTC."</br>"; echo "<title>Arbitrage Found</title>"; };
// 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 "<script>alert('Arbitrage Found!')</script>BTC -> LTC -> RUR -> USD -> BTC - ".$BTC_LTC_RUR_USD_BTC."</br>"; echo "<title>Arbitrage Found</title>"; };
// 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 "<script>alert('Arbitrage Found!')</script>BTC -> USD -> RUR -> LTC -> BTC - ".$BTC_USD_RUR_LTC_BTC."</br>"; echo "<title>Arbitrage Found</title>"; };
// 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 "<script>alert('Arbitrage Found!')</script>BTC -> USD -> LTC -> RUR -> BTC - ".$BTC_USD_LTC_RUR_BTC."</br>"; echo "<title>Arbitrage Found</title>"; };
// 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 "<script>alert('Arbitrage Found!')</script>BTC -> RUR -> USD -> LTC -> BTC - ".$BTC_RUR_USD_LTC_BTC."</br>"; echo "<title>Arbitrage Found</title>"; };
// 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 "<script>alert('Arbitrage Found!')</script>BTC -> RUR -> LTC -> USD -> BTC - ".$BTC_RUR_LTC_USD_BTC."</br>"; echo "<title>Arbitrage Found</title>"; };

// 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 "<script>alert('Arbitrage Found!')</script>BTC -> EUR -> USD -> BTC - ".$BTC_EUR_USD_BTC."</br>"; echo "<title>Arbitrage Found</title>"; };
// 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 "<script>alert('Arbitrage Found!')</script>BTC -> USD -> EUR -> BTC - ".$BTC_USD_EUR_BTC."</br>"; echo "<title>Arbitrage Found</title>"; };

// 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 "<script>alert('Arbitrage Found!')</script>BTC -> RUR -> USD -> BTC - ".$BTC_RUR_USD_BTC."</br>"; echo "<title>Arbitrage Found</title>"; };
// 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 "<script>alert('Arbitrage Found!')</script>BTC -> USD -> RUR -> BTC - ".$BTC_USD_RUR_BTC."</br>"; echo "<title>Arbitrage Found</title>"; };

// 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 "<script>alert('Arbitrage Found!')</script>BTC -> LTC -> USD -> BTC - ".$BTC_LTC_USD_BTC."</br>"; echo "<title>Arbitrage Found</title>"; };
// 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 "<script>alert('Arbitrage Found!')</script>BTC -> USD -> LTC -> BTC - ".$BTC_USD_LTC_BTC."</br>"; echo "<title>Arbitrage Found</title>"; };

echo 
"</pre></center>";
echo 
"<title>No Arbitrage</title>";
?>
1715197620
Hero Member
*
Offline Offline

Posts: 1715197620

View Profile Personal Message (Offline)

Ignore
1715197620
Reply with quote  #2

1715197620
Report to moderator
1715197620
Hero Member
*
Offline Offline

Posts: 1715197620

View Profile Personal Message (Offline)

Ignore
1715197620
Reply with quote  #2

1715197620
Report to moderator
There are several different types of Bitcoin clients. The most secure are full nodes like Bitcoin Core, which will follow the rules of the network no matter what miners do. Even if every miner decided to create 1000 bitcoins per block, full nodes would stick to the rules and reject those blocks.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715197620
Hero Member
*
Offline Offline

Posts: 1715197620

View Profile Personal Message (Offline)

Ignore
1715197620
Reply with quote  #2

1715197620
Report to moderator
1715197620
Hero Member
*
Offline Offline

Posts: 1715197620

View Profile Personal Message (Offline)

Ignore
1715197620
Reply with quote  #2

1715197620
Report to moderator
minzie
Member
**
Offline Offline

Activity: 110
Merit: 10



View Profile WWW
April 24, 2013, 11:45:10 PM
 #2

Thanks for sharing. I always learn something from other people's code.

Professional Freelance Web Apps Engineer ° Portfolio °  ModoBot Automated Trading Platform ° BitThingy ° My Github  ° Skype: cclites_1
Remember remember the 5th of November
Legendary
*
Offline Offline

Activity: 1862
Merit: 1011

Reverse engineer from time to time


View Profile
April 25, 2013, 05:57:57 AM
 #3

Since when can you specify the array keyword before variable declaration in a function's arguments?

BTC:1AiCRMxgf1ptVQwx6hDuKMu4f7F27QmJC2
Terk
Hero Member
*****
Offline Offline

Activity: 616
Merit: 522



View Profile
April 25, 2013, 07:35:11 AM
 #4

There is some kind of type hinting in function arguments declaration since PHP 5.1, so... since 2005?

theblazehen
Full Member
***
Offline Offline

Activity: 194
Merit: 100



View Profile
May 03, 2013, 07:12:35 AM
 #5

I'm getting divide by zero errors...

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

BURST: BURST-ZRT2-GB5S-A6CS-HBVAE
OdinHephaestus
Member
**
Offline Offline

Activity: 70
Merit: 10



View Profile
May 03, 2013, 07:07:52 PM
 #6

I'm getting divide by zero errors...

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

do you have php curl installed on your machine?

Bitcoin: 1odinQ5YUdbfuopNF2D1FZRN8qcrC4y1L
Litecoin: LeodinXzG7CgHqPDZxaUgsDKUKBUu46qa5
wdnj
Hero Member
*****
Offline Offline

Activity: 762
Merit: 500



View Profile
July 22, 2015, 01:28:58 AM
 #7

Is there any update?
Pages: [1]
  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!