function updatePoloniexMarkets() { $poloniex = new poloniex(); $tickers = $poloniex->get_ticker(); if (!$tickers) { return; } foreach ($tickers as $symbol => $ticker) { $a = explode('_', $symbol); if (!isset($a[1])) { continue; } if ($a[0] != 'BTC') { continue; } $symbol = $a[1]; $coin = getdbosql('db_coins', "symbol=:symbol", array(':symbol' => $symbol)); if (!$coin || !$coin->installed) { continue; } $market = getdbosql('db_markets', "coinid={$coin->id} and name='poloniex'"); if (!$market) { $market = new db_markets(); $market->coinid = $coin->id; $market->name = 'poloniex'; } if (empty($market->deposit_address) && $coin->installed) { $poloniex->generate_address($coin->symbol); } $price2 = ($ticker['highestBid'] + $ticker['lowestAsk']) / 2; $market->price2 = AverageIncrement($market->price2, $price2); $market->price = AverageIncrement($market->price, $ticker['highestBid']); $market->save(); } $list = $poloniex->get_deposit_addresses(); foreach ($list as $symbol => $item) { if ($symbol == 'BTC') { continue; } $coin = getdbosql('db_coins', "symbol=:symbol", array(':symbol' => $symbol)); if (!$coin) { continue; } $market = getdbosql('db_markets', "coinid={$coin->id} and name='poloniex'"); if (!$market) { continue; } $market->deposit_address = $item; $market->save(); } }
function doPoloniexTrading() { // debuglog('-------------- doPoloniexTrading()'); $flushall = rand(0, 4) == 0; $poloniex = new poloniex(); $tickers = $poloniex->get_ticker(); if (!$tickers) { return; } // upgrade orders $coins = getdbolist('db_coins', "enable and id in (select distinct coinid from markets where name='poloniex')"); foreach ($coins as $coin) { if ($coin->dontsell) { continue; } $pair = "BTC_{$coin->symbol}"; if (!isset($tickers[$pair])) { continue; } $orders = $poloniex->get_open_orders($pair); if (!$orders || !isset($orders[0])) { dborun("delete from orders where coinid={$coin->id} and market='poloniex'"); continue; } foreach ($orders as $order) { if (!isset($order['orderNumber'])) { debuglog($order); continue; } if ($order['rate'] > $tickers[$pair]['lowestAsk'] + 5.0E-8 || $flushall) { // debuglog("poloniex cancel order for $pair {$order['orderNumber']}"); $poloniex->cancel_order($pair, $order['orderNumber']); $db_order = getdbosql('db_orders', "uuid=:uuid", array(':uuid' => $order['orderNumber'])); if ($db_order) { $db_order->delete(); } sleep(1); } else { $db_order = getdbosql('db_orders', "uuid=:uuid", array(':uuid' => $order['orderNumber'])); if ($db_order) { continue; } debuglog("poloniex adding order {$coin->symbol}"); $db_order = new db_orders(); $db_order->market = 'poloniex'; $db_order->coinid = $coin->id; $db_order->amount = $order['amount']; $db_order->price = $order['rate']; $db_order->ask = $tickers[$pair]['lowestAsk']; $db_order->bid = $tickers[$pair]['highestBid']; $db_order->uuid = $order['orderNumber']; $db_order->created = time(); $db_order->save(); } } $list = getdbolist('db_orders', "coinid={$coin->id} and market='poloniex'"); foreach ($list as $db_order) { $found = false; foreach ($orders as $order) { if (!isset($order['orderNumber'])) { debuglog($order); continue; } if ($order['orderNumber'] == $db_order->uuid) { $found = true; break; } } if (!$found) { debuglog("poloniex deleting order {$coin->name} {$db_order->amount}"); $db_order->delete(); } } } // add orders $savebalance = getdbosql('db_balances', "name='poloniex'"); $balances = $poloniex->get_balances(); foreach ($balances as $symbol => $balance) { if (!$balance) { continue; } if ($symbol == 'BTC') { $savebalance->balance = $balance; $savebalance->save(); continue; } $coin = getdbosql('db_coins', "symbol=:symbol", array(':symbol' => $symbol)); if (!$coin || $coin->dontsell) { continue; } $market = getdbosql('db_markets', "coinid={$coin->id} and name='poloniex'"); if ($market) { $market->lasttraded = time(); $market->save(); } $pair = "BTC_{$symbol}"; if (!isset($tickers[$pair])) { continue; } $sellprice = $tickers[$pair]['highestBid']; if ($balance * $sellprice < 0.0001) { continue; } // debuglog("poloniex selling $pair, $sellprice, $balance"); $res = $poloniex->sell($pair, $sellprice, $balance); if (!isset($res['orderNumber'])) { debuglog($res, 5); continue; } if (!isset($tickers[$pair])) { continue; } $coin = getdbosql('db_coins', "symbol=:symbol", array(':symbol' => $symbol)); if (!$coin) { continue; } $db_order = new db_orders(); $db_order->market = 'poloniex'; $db_order->coinid = $coin->id; $db_order->amount = $balance; $db_order->price = $sellprice; $db_order->ask = $tickers[$pair]['lowestAsk']; $db_order->bid = $tickers[$pair]['highestBid']; $db_order->uuid = $res['orderNumber']; $db_order->created = time(); $db_order->save(); } if ($savebalance->balance >= 0.2) { $btcaddr = YAAMP_BTCADDRESS; //'14LS7Uda6EZGXLtRrFEZ2kWmarrxobkyu9'; $amount = $savebalance->balance; // - 0.0002; debuglog("poloniex withdraw {$amount} to {$btcaddr}"); sleep(1); $res = $poloniex->withdraw('BTC', $amount, $btcaddr); debuglog($res); if ($res && $res->success) { $withdraw = new db_withdraws(); $withdraw->market = 'poloniex'; $withdraw->address = $btcaddr; $withdraw->amount = $amount; $withdraw->time = time(); // $withdraw->uuid = $res->result->uuid; $withdraw->save(); } } // debuglog('-------------- doPoloniexTrading() done'); }
function find_best_prices($pair, $key, $secret) { $object = new poloniex($key, $secret); $orders = $object->get_order_book($pair); //bid is buy order //ask is a sell order $best_bid = 0.0; $best_bid_quantity = 0; $best_ask = 10000000.0; $best_ask_quantity = 0; $check = false; //TODO handle the isFrozen variable to assure that we are not trading with frozen markets foreach ($orders as $one => $two) { //$one denotes asks vs bids foreach ($two as $three => $four) { foreach ($four as $five => $six) { //$five denotes price(0) or quantity(1), $six is value //beware that index 0 does not have anything meaningful if ($one === 'asks') { //if we just found the best price, update the quantity if ($check === true && $five === 1) { $best_ask_quantity = $six; $check = false; } //if this is the best price, update it if ($five === 0 && $six < $best_ask) { $best_ask = $six; $check = true; } } else { if ($one === 'bids') { //if we just found the best price, update the quantity if ($check === true && $five === 1) { $best_bid_quantity = $six; $check = false; } //if this is the best price, update it if ($five === 0 && $six > $best_bid) { $best_bid = $six; $check = true; } } } } } } return array($best_ask, $best_ask_quantity, $best_bid, $best_bid_quantity); }
<?php $polo = new poloniex('UFI8JVZN-G0ST4X5P-QU8UHRZA-3YQEYQ5J', '600b0ccc60ccbbcf0afe8842f74fdcf3b88b846f7fba61be308b35fd6129c981628440c86ca6b764cee287a68e766aedfd61c5d844ff7c9ac8219ef3bf12957d'); $market = 'BTSX'; $depth = 20; $price = $polo->return_latest_average_price($market); //print_r($history); //$chart=$polo->return_chart_data(); //print_r($chart); //$balances=$polo->get_balances(); //print_r($balances); $pair = 'BTC_' . $market; $rate = 0.121212; $amount = 12; $buy = $polo->buy($pair, $price, $amount); //$order_book=$polo->get_trade_history($pair); //print_r($order_book); //$sell_maid=$polo->sell($pair, $rate, $amount); //print_r($sell_maid); // FINAL TESTED CODE - Created by Compcentral // NOTE: currency pairs are reverse of what most exchanges use... // For instance, instead of XPM_BTC, use BTC_XPM class poloniex { protected $api_key; protected $api_secret; protected $trading_url = "https://poloniex.com/tradingApi"; protected $public_url = "https://poloniex.com/public"; public function __construct($api_key, $api_secret) { $this->api_key = $api_key;
echo '<span class="remove-itm"><a href="cart_update.php?removep=' . $cart_itm["code"] . '&return_url=' . $current_url . '">×</a></span>'; echo '<h3>' . $cart_itm["name"] . '</h3>'; echo '<div class="p-code">P code : ' . $cart_itm["code"] . '</div>'; echo '<div class="p-qty">Qty : ' . $cart_itm["qty"] . '</div>'; echo '<div class="p-price">Price :' . $currency . $cart_itm["price"] . '</div>'; echo '</li>'; $subtotal = $cart_itm["price"] * $cart_itm["qty"]; $total = $total + $subtotal; } echo '</ol>'; echo '<span class="check-out-txt"><strong>Total : ' . $currency . $total . '</strong> <a href="view_cart.php">Check-out!</a></span>'; echo '<span class="empty-cart"><a href="cart_update.php?emptycart=1&return_url=' . $current_url . '">Empty Cart</a></span>'; } else { echo 'Your Cart is empty'; } $exchange = new poloniex("apikey", "apisecret"); $ordersLC = $exchange->get_trade_history("BTC_LC"); $ordersLCL = $exchange->get_trade_history("BTC_LCL"); //print_r($orders); //print_r($orders[0]); //print_r($orders[0]["date"]); //print_r($orders[0]["type"]); //print_r($orders[0]["rate"]); //print_r($orders[0]["amount"]); //print_r($orders[0]["total"]); //echo $orders[0]["rate"]; $rateLC = print_r($ordersLC[0]["rate"], true); $rateLCL = print_r($ordersLCL[0]["rate"], true); $url = "https://btc-e.com/api/3/ticker/btc_usd"; $send_curl = curl_init($url); curl_setopt($send_curl, CURLOPT_RETURNTRANSFER, TRUE);
function updateRawcoins() { // debuglog(__FUNCTION__); $list = bittrex_api_query('public/getcurrencies'); if (isset($list->result)) { dborun("update markets set deleted=true where name='bittrex'"); foreach ($list->result as $currency) { updateRawCoin('bittrex', $currency->Currency, $currency->CurrencyLong); } } $list = bleutrade_api_query('public/getcurrencies'); if (isset($list->result)) { dborun("update markets set deleted=true where name='bleutrade'"); foreach ($list->result as $currency) { updateRawCoin('bleutrade', $currency->Currency, $currency->CurrencyLong); } } $list = cryptsy_api_query('getmarkets'); if (isset($list['return'])) { dborun("update markets set deleted=true where name='cryptsy'"); foreach ($list['return'] as $item) { updateRawCoin('cryptsy', $item['primary_currency_code'], $item['primary_currency_name']); } } $ccex = new CcexAPI(); $list = $ccex->getPairs(); if ($list) { dborun("update markets set deleted=true where name='c-cex'"); foreach ($list as $item) { $e = explode('-', $item); $symbol = strtoupper($e[0]); updateRawCoin('c-cex', $symbol); } } $poloniex = new poloniex(); $tickers = $poloniex->get_currencies(); dborun("update markets set deleted=true where name='poloniex'"); foreach ($tickers as $symbol => $ticker) { if ($ticker['disabled']) { continue; } if ($ticker['delisted']) { continue; } updateRawCoin('poloniex', $symbol); } $res = yobit_api_query('info'); if ($res) { dborun("update markets set deleted=true where name='yobit'"); foreach ($res->pairs as $i => $item) { $e = explode('_', $i); $symbol = strtoupper($e[0]); updateRawCoin('yobit', $symbol); } } ////////////////////////////////////////////////////////// dborun("delete from markets where deleted"); $list = getdbolist('db_coins', "not enable and not installed and id not in (select distinct coinid from markets)"); foreach ($list as $coin) { debuglog("{$coin->symbol} is not longer active"); $coin->delete(); } }
<?php include 'include/header.php'; include 'controller/container.php'; $obj = new container(); include 'include/trading-api.php'; $poloniex = new poloniex('AU04SSJF-RGA9O99O-5LN3ZG39-01W6ZZ4Z', '771e542c64fcdd441b10aa9af58f4a8748db848c0d1d254d63e639683fad41b342bbb2bbf7c96042511e2967a3dbddd67a2afd109fb1845fc3c2e1df2d16e2a3'); ?> <script> function calculate() { var sllimit = document.getElementById('sllimit').value; var slamount = document.getElementById('slamount').value; var result = document.getElementById('sltotal'); var myResult = sllimit * slamount; result.value = myResult; } $( document ).ready(function() { $('#buyamount').on('keyup',function(){ var per = $('#buyamount').val() * 0.2/100; $('#buypercent').val(per); var total = $('#buyprice').val() * this.value; $('#buytotal').val(total); }); $('#sellamount').on('keyup',function(){ var total = $('#sellprice').val() * this.value; $('#selltotal').val(total); var sellper = total * 0.2/100; $('#sellpercent').val(sellper); }); });