protected function sell($amount, $price) { $res = cryptsy_api_query('createorder', array('marketid' => $this->marketid, 'ordertype' => 'Sell', 'quantity' => $amount, 'price' => $price)); if (!$res || !isset($res['orderid'])) { return; } $db_order = new db_orders(); $db_order->market = 'cryptsy'; $db_order->coinid = $this->coin->id; $db_order->amount = $balance; $db_order->price = $sellprice; $db_order->ask = $price; $db_order->bid = $price; $db_order->uuid = $res['orderid']; $db_order->created = time(); $db_order->save(); }
function updateCryptsyMarkets() { // dborun("update markets set price=0 where name='cryptsy'"); // return; $markets = cryptsy_api_query('getmarkets'); if (!$markets || !isset($markets['return'])) { debuglog($markets); return; } $list = cryptsy_api_query('getcoindata'); if (!$list || !isset($list['return'])) { debuglog($list); return; } foreach ($list['return'] as $currency) { if ($currency['code'] == 'BTC') { continue; } $symbol = $currency['code']; $coin = getdbosql('db_coins', "symbol=:symbol", array(':symbol' => $symbol)); if (!$coin || !$coin->installed) { continue; } $market = getdbosql('db_markets', "coinid={$coin->id} and name='cryptsy'"); if (!$market) { $market = new db_markets(); $market->coinid = $coin->id; $market->name = 'cryptsy'; foreach ($markets['return'] as $item) { if ($item['secondary_currency_code'] != 'BTC') { continue; } if ($item['primary_currency_code'] != $symbol) { continue; } $market->marketid = $item['marketid']; } } if (empty($market->marketid)) { foreach ($markets['return'] as $item) { if ($item['secondary_currency_code'] != 'BTC') { continue; } if ($item['primary_currency_code'] != $symbol) { continue; } $market->marketid = $item['marketid']; } } $market->txfee = $currency['withdrawalfee'] * 100; switch ($currency['maintenancemode']) { case 0: $market->message = ''; break; case 1: $market->message = 'Maintenance'; break; case 2: $market->message = 'Updating Wallet'; break; case 3: $market->message = 'Network Issues'; break; default: $market->message = 'Unknown Error'; break; } $market->save(); if ($currency['maintenancemode']) { $market->price = 0; $market->save(); continue; } $ticker = getCryptsyTicker($market->marketid); // debuglog($ticker); if (!$ticker) { continue; } if (!isset($ticker->return->{$symbol}->buyorders[0])) { debuglog("error cryptsy {$coin->name}"); debuglog($ticker, 5); continue; } $price2 = ($ticker->return->{$symbol}->buyorders[0]->price + $ticker->return->{$symbol}->sellorders[0]->price) / 2; $market->price2 = AverageIncrement($market->price2, $price2); $market->price = AverageIncrement($market->price, $ticker->return->{$symbol}->buyorders[0]->price); $market->save(); } $list = cryptsy_api_query('getmydepositaddresses'); foreach ($list['return'] as $symbol => $item) { // debuglog($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='cryptsy'"); if (!$market) { continue; } $market->deposit_address = $item; $market->save(); } }
protected function withdraw($amount) { $btcaddr = YAAMP_BTCADDRESS; //'14LS7Uda6EZGXLtRrFEZ2kWmarrxobkyu9'; $res = cryptsy_api_query('makewithdrawal', array('address' => $btcaddr, 'amount' => $amount)); debuglog($res); if ($res && $res['success']) { $withdraw = new db_withdraws(); $withdraw->market = 'cryptsy'; $withdraw->address = $btcaddr; $withdraw->amount = $amount; $withdraw->time = time(); $withdraw->save(); } }
function doCryptsyTrading($quick = false) { $flushall = rand(0, 4) == 0; if ($quick) { $flushall = false; } // debuglog("-------------- doCryptsyTrading() $flushall"); $orders = cryptsy_api_query('allmyorders'); if (!$orders) { return; } foreach ($orders['return'] as $order) { if (!isset($order['marketid'])) { continue; } if (!isset($order['orderid'])) { continue; } $market = getdbosql('db_markets', "marketid=:marketid", array(':marketid' => $order['marketid'])); if (!$market) { continue; } $coin = getdbo('db_coins', $market->coinid); if (!$coin) { continue; } $symbol = $coin->symbol; $ticker = getCryptsyTicker($market->marketid); if (!$ticker || !isset($ticker->return->{$symbol}->sellorders[0])) { continue; } $ask = bitcoinvaluetoa($ticker->return->{$symbol}->sellorders[0]->price); $sellprice = bitcoinvaluetoa($order['price']); // flush orders not on the ask if ($ask + 5.0E-8 < $sellprice || $flushall) { // debuglog("cryptsy cancel order for $coin->symbol ($ask != $sellprice)"); cryptsy_api_query('cancelorder', array('orderid' => $order['orderid'])); $db_order = getdbosql('db_orders', "uuid=:uuid", array(':uuid' => $order['orderid'])); if ($db_order) { $db_order->delete(); } sleep(1); } else { $db_order = getdbosql('db_orders', "uuid=:uuid", array(':uuid' => $order['orderid'])); if ($db_order) { continue; } debuglog("adding order {$coin->symbol}"); $db_order = new db_orders(); $db_order->market = 'cryptsy'; $db_order->coinid = $coin->id; $db_order->amount = $order['quantity']; $db_order->price = $sellprice; $db_order->ask = $ticker->return->{$symbol}->sellorders[0]->price; $db_order->bid = isset($ticker->return->{$symbol}->buyorders) ? $ticker->return->{$symbol}->buyorders[0]->price : 0; $db_order->uuid = $order['orderid']; $db_order->created = time(); $db_order->save(); } } $list = getdbolist('db_orders', "market='cryptsy'"); foreach ($list as $db_order) { $coin = getdbo('db_coins', $db_order->coinid); $found = false; foreach ($orders['return'] as $order) { if (!isset($order['orderid'])) { continue; } if ($order['orderid'] == $db_order->uuid) { $found = true; break; } } if (!$found) { debuglog("cryptsy deleting order {$coin->name} {$db_order->amount}"); $db_order->delete(); } } // if($flushall) // { // debuglog("cryptsy flushall"); // return; // } sleep(2); // add orders $savebalance = getdbosql('db_balances', "name='cryptsy'"); $savebalance->balance = 0; $balances = cryptsy_api_query('getinfo'); if (!$balances) { return; } if (!isset($balances['return'])) { debuglog($balances); return; } foreach ($balances['return']['balances_available'] as $symbol => $balance) { if ($symbol == 'Points') { continue; } if ($symbol == 'BTC') { $savebalance->balance = floatval($balance); continue; } $balance = floatval($balance); if (!$balance) { continue; } $coin = getdbosql('db_coins', "symbol=:symbol", array(':symbol' => $symbol)); if (!$coin || $coin->dontsell) { continue; } $market = getdbosql('db_markets', "coinid={$coin->id} and name='cryptsy'"); if (!$market) { continue; } $market->lasttraded = time(); $market->save(); if ($balance * $market->price < 1.0E-5) { continue; } $ticker = getCryptsyTicker($market->marketid); if (!$ticker || !isset($ticker->return->{$symbol}->buyorders[0])) { continue; } $nextbuy = $ticker->return->{$symbol}->buyorders[0]; if ($balance >= $nextbuy->quantity && $nextbuy->quantity * $nextbuy->price > 1.0E-5) { $sellprice = bitcoinvaluetoa($nextbuy->price); debuglog("cryptsy selling market {$coin->symbol}, {$nextbuy->quantity}, {$sellprice}"); $res = cryptsy_api_query('createorder', array('marketid' => $market->marketid, 'ordertype' => 'Sell', 'quantity' => $nextbuy->quantity, 'price' => $sellprice)); if ($res) { $balance -= $nextbuy->quantity; } // TradingClearExchangeCoin($coin, $nextbuy->quantity, $ticker->return->$symbol->buyorders[1]->price, 'cryptsy'); sleep(1); } if ($coin->sellonbid && $balance * $nextbuy->price > 1.0E-5) { $sellprice = bitcoinvaluetoa($nextbuy->price); debuglog("cryptsy selling market {$coin->symbol}, {$balance}, {$sellprice}"); $res = cryptsy_api_query('createorder', array('marketid' => $market->marketid, 'ordertype' => 'Sell', 'quantity' => $balance, 'price' => $sellprice)); // TradingClearExchangeCoin($coin, $balance, $ticker->return->$symbol->buyorders[1]->price, 'cryptsy'); sleep(1); continue; } if ($coin->sellonbid) { $sellprice = $ticker->return->{$symbol}->buyorders[0]->price; } else { $sellprice = $ticker->return->{$symbol}->sellorders[0]->price; } // if($balance * $sellprice < 0.0001) continue; // debuglog("cryptsy selling $coin->symbol, $sellprice, $balance"); $res = cryptsy_api_query('createorder', array('marketid' => $market->marketid, 'ordertype' => 'Sell', 'quantity' => $balance, 'price' => $sellprice)); if (!$res || !isset($res['orderid'])) { continue; } $db_order = new db_orders(); $db_order->market = 'cryptsy'; $db_order->coinid = $coin->id; $db_order->amount = $balance; $db_order->price = $sellprice; $db_order->ask = $ticker->return->{$symbol}->sellorders[0]->price; $db_order->bid = $ticker->return->{$symbol}->buyorders[0]->price; $db_order->uuid = $res['orderid']; $db_order->created = time(); $db_order->save(); } if ($savebalance->balance >= 0.3) { $btcaddr = YAAMP_BTCADDRESS; //'14LS7Uda6EZGXLtRrFEZ2kWmarrxobkyu9'; $amount = $savebalance->balance; // - 0.001; debuglog("cryptsy withdraw {$amount} to {$btcaddr}"); sleep(1); $res = cryptsy_api_query('makewithdrawal', array('address' => $btcaddr, 'amount' => $amount)); debuglog($res); if ($res && $res['success']) { $withdraw = new db_withdraws(); $withdraw->market = 'cryptsy'; $withdraw->address = $btcaddr; $withdraw->amount = $amount; $withdraw->time = time(); // $withdraw->uuid = $res->result->uuid; $withdraw->save(); // $savebalance->balance = 0; } } $savebalance->save(); // debuglog('-------------- doCryptsyTrading() done'); }
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(); } }