Skip to content

Instantly share code, notes, and snippets.

@fwolf
Last active January 2, 2016 09:29
Show Gist options
  • Save fwolf/8283026 to your computer and use it in GitHub Desktop.
Save fwolf/8283026 to your computer and use it in GitHub Desktop.
Compare price on btc38 and okcoin
#! /usr/bin/php
<?php
/**
* btc38-okcoin-price-compare.php
*
* Copyright 2014 Fwolf <[email protected]>
* All rights reserved.
*
* Distributed under the MIT License.
* http://opensource.org/licenses/mit-license
*
* Compare price on btc38 and okcoin, use CNY currency.
*
* @copyright Copyright 2014 Fwolf
* @author Fwolf <[email protected]>
* @license http://opensource.org/licenses/mit-license MIT
* @since 2014-01-06
* @version 1.0
*/
use Fwlib\Net\Curl;
use Fwlib\Util\UtilContainer;
// Include
require 'fwlib/autoload.php';
// Check parameter amount, at least 2 param needed
if (3 > $argc) {
printUsage();
exit(-1);
}
// Main body
$coinName = $argv[1];
$percentThreshold = $argv[2] / 100;
$curl = new Curl;
$utilContainer = UtilContainer::getInstance();
$env = $utilContainer->get('Env');
// Start a endless loop with pause
while (true) {
try {
// Query Okcoin first
$url = 'http://www.okcoin.com/api/depth.do?symbol=' .
strtolower($coinName) . '_cny';
$result = json_decode($curl->get($url), true);
$ar = array_pop($result['asks']);
$lowestSellOnOkcoin = array(
'price' => $ar[0],
'amount' => $ar[1],
);
$ar = array_shift($result['bids']);
$highestBuyOnOkcoin = array(
'price' => $ar[0],
'amount' => $ar[1],
);
// Then query btc38
$url = 'http://www.btc38.com/trade/getTradeList.php?coinname=' .
strtolower($coinName);
$result = $curl->get($url);
if ('{' != $result{0}) {
// Remove BOM
$result = substr($result, 3);
}
$result = json_decode($result, true);
$lowestSellOnBtc38 = array_shift($result['sellOrder']);
$highestBuyOnBtc38 = array_shift($result['buyOrder']);
// Compare
$btc38ToOkcoin = round(
$highestBuyOnOkcoin['price'] - $lowestSellOnBtc38['price'],
2
);
$btc38ToOkcoinPercent = round(
$btc38ToOkcoin / $lowestSellOnBtc38['price'] * 100,
2
);
$env->ecl(
"btc38 {$lowestSellOnBtc38['price']} --> " .
"okcoin {$highestBuyOnOkcoin['price']} : " .
"$btc38ToOkcoin / $btc38ToOkcoinPercent% * " .
min($lowestSellOnBtc38['amount'], $highestBuyOnOkcoin['amount'])
);
$okcoinToBtc38 = round(
$highestBuyOnBtc38['price'] - $lowestSellOnOkcoin['price'],
2
);
$okcoinToBtc38Percent = round(
$okcoinToBtc38 / $lowestSellOnOkcoin['price'] * 100,
2
);
$env->ecl(
"okcoin {$lowestSellOnOkcoin['price']} --> " .
"btc38 {$highestBuyOnBtc38['price']} : " .
"$okcoinToBtc38 / $okcoinToBtc38Percent% * " .
min($lowestSellOnOkcoin['amount'], $highestBuyOnBtc38['amount'])
);
$env->ecl();
} catch (\Exception $e) {
$env->ecl($e->getMessage());
}
sleep(12);
}
// Functions define
/**
* Print usage message
*/
function printUsage()
{
$s = basename(__FILE__);
echo <<<EOF
Usage: $s CoinName Percent
Parameters:
CoinName BTC or LTC
Percent Only print price spread large than Percent%
EOF;
}
/**
* ChangeLog
*
* v1.0
* - Basic feature: claw, compare and print
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment