private function calcTrand() { log_msg('calc trand'); $DBconf = $this->db->loadActiveConfiguration(); if (!isset($DBconf['baseCoin'])) { throw new BtceMysqlException('db conf not loaded'); } log_msg(print_r($DBconf, true)); $changePairs = Coin::getPairKeys($DBconf['baseCoin']); log_msg(print_r($changePairs, true)); if (!count($changePairs)) { throw new BtceLogicException('no change pairs for base coin: ' . $DBconf['baseCoin']); } for ($i = 0; $i < count($changePairs); $i++) { $pairCode = $changePairs[$i]; $history = $this->db->loadHistory(array(sprintf("dt >= '%s'", date('Y-m-d H:i:s', strtotime('-10 minutes'))), sprintf("dt <= '%s'", date('Y-m-d H:i:s')), sprintf("pair = '%s'", $pairCode))); if (!count($history)) { continue; } $heights = array(); $weight = 0; for ($j = 0; $j < count($history); $j++) { switch ($history[$j]['vector']) { case 'NOCHANGE': $heights[] = 0; break; case 'UP': $heights[] = 1; break; case 'DOWN': $heights[] = -1; break; } $weight += $history[$j]['weight']; } $weight = (int) round($weight / count($history) * 10); $trend = array_sum($heights); $this->db->setTrend($pairCode, $trend, $weight); echo $pairCode . ' >> trend:' . $trend . ' / weight: ' . $weight . PHP_EOL; } }
private function runWeightStrategy() { $this->refreshFunds(); $baseCoinCode = (string) $this->strategy->baseCoin; // can we make operations? $pairAllowCompare = false; try { $pairAllowCompare = $this->allowCompare(); } catch (BtceLogicException $e) { log_msg('compare blocked: ' . $e->getMessage()); if ($e->getCode() == BtceLogicException::REQUIRE_UPDATE_PRICE) { } elseif ($e->getCode() == BtceLogicException::NO_AVAILABLE_FUNDS) { $this->sleepSec(60 * 15); } } if ($pairAllowCompare) { log_msg("-------------------"); log_msg("Base coin\t" . $this->strategy->baseCoin); log_msg("Operation coin\t" . $this->funds->operationCoin); log_msg(sprintf("Operation amount\t%f", $this->funds->operationCoin->amount)); // for our dance we need only pairs with operation coin type $lookPairs = Coin::getPairKeys((string) $this->funds->operationCoin); if ($lookPairs) { foreach ($lookPairs as $_pair_code) { if (isset($this->pairs->list[$_pair_code]) && $this->pairs->prev[$_pair_code]) { $pair =& $this->pairs->list[$_pair_code]; /** @var Pair $pair */ $pairPrev =& $this->pairs->prev[$_pair_code]; /** @var Pair $pairPrev */ if (!$pair->enabled) { continue; } if (!isset($this->weights[$_pair_code])) { $this->weights[$_pair_code] = array('sell' => 0, 'buy' => 0); } $diff = 0; if ($pair->coin_a->code == $baseCoinCode) { $lookAt = StrategyConf::SELL; // look at sell prices (we need they increases) log_msg("----------- Pair: {$_pair_code} / look at:\t" . $lookAt); $diff = $this->getDiff($pair->sell, $pairPrev->sell); log_msg("Sell was now diff order "); log_msg(" " . str_pad('1 ' . $pairPrev->coin_a->code . ' =', 20, ' ', STR_PAD_RIGHT) . str_pad('1 ' . $pair->coin_a->code . ' =', 20, ' ', STR_PAD_RIGHT) . str_pad('', 14, ' ', STR_PAD_RIGHT) . str_pad($this->funds->operationCoin->amount . ' ' . $pairPrev->coin_a->code . ' =', 14, ' ', STR_PAD_RIGHT)); log_msg(" " . str_pad(sprintf("%f", $pairPrev->sell) . ' ' . $pairPrev->coin_b->code, 20, ' ', STR_PAD_RIGHT) . str_pad(sprintf("%f", $pair->sell) . ' ' . $pair->coin_b->code, 20, ' ', STR_PAD_RIGHT) . str_pad($diff . ' ' . $pair->coin_b->code, 14, ' ', STR_PAD_RIGHT) . str_pad($this->getOrderResult($this->funds->operationCoin->amount, $pair->sell, $pair->fee) . ' ' . $pairPrev->coin_b->code, 14, ' ', STR_PAD_RIGHT)); if (!isset($this->strategy->diff_sell[$_pair_code])) { log_msg('no sell strategy for pair: ' . $_pair_code); $this->pairs->list[$_pair_code]->enabled = false; continue; } log_msg(sprintf("Strategy diff:\t%f / %f", $this->strategy->diff_sell[$_pair_code], $diff)); } else { if ($pair->coin_b->code == $baseCoinCode) { $lookAt = StrategyConf::BUY; // look at buy prices (we need they decreases) log_msg("----------- Pair: {$_pair_code} / look at:\t" . $lookAt); $diff = $this->getDiff($pair->buy, $pairPrev->buy); log_msg("Buy was now diff order "); log_msg(" " . str_pad('1 ' . $pairPrev->coin_a->code . ' =', 20, ' ', STR_PAD_RIGHT) . str_pad('1 ' . $pair->coin_a->code . ' =', 20, ' ', STR_PAD_RIGHT) . str_pad('', 14, ' ', STR_PAD_RIGHT) . str_pad($this->funds->operationCoin->amount . ' ' . $pairPrev->coin_a->code . ' =', 14, ' ', STR_PAD_RIGHT)); log_msg(" " . str_pad(sprintf("%f", $pairPrev->buy) . ' ' . $pairPrev->coin_b->code, 20, ' ', STR_PAD_RIGHT) . str_pad(sprintf("%f", $pair->buy) . ' ' . $pair->coin_b->code, 20, ' ', STR_PAD_RIGHT) . str_pad($diff . ' ' . $pair->coin_b->code, 14, ' ', STR_PAD_RIGHT) . str_pad($this->getOrderResult($this->funds->operationCoin->amount, $pair->buy, $pair->fee) . ' ' . $pairPrev->coin_b->code, 14, ' ', STR_PAD_RIGHT)); if (!isset($this->strategy->diff_buy[$_pair_code])) { log_msg('no buy strategy for pair: ' . $_pair_code); $this->pairs->list[$_pair_code]->enabled = false; continue; } log_msg(sprintf("Strategy diff:\t%f / %f", $this->strategy->diff_buy[$_pair_code], $diff)); } else { $this->pairs->list[$_pair_code]->refreshRequired = false; $this->pairs->list[$_pair_code]->enabled = false; continue; } } $doOrderOperations = false; $this->pairs->list[$_pair_code]->refreshRequired = true; if ($lookAt == StrategyConf::SELL) { if ($this->weights[$_pair_code]['sell'] == $this->strategy->capture_count_sell + 1) { log_msg('Sell weight: MAX'); } else { if ($diff > $this->strategy->diff_sell[$_pair_code]) { log_msg('[CAPTURE]'); log_msg('Sell diff: was [' . $pairPrev->sell . '], now [' . $pair->sell . '], diff = ' . $diff); $this->weights[$_pair_code]['sell']++; } else { if ($diff < 0 && $this->weights[$_pair_code]['sell'] > 0) { $this->weights[$_pair_code]['sell']--; } } log_msg('Sell weight: ' . $this->weights[$_pair_code]['sell']); if ($this->weights[$_pair_code]['sell'] == $this->strategy->capture_count_sell) { // make order operation SELL $orderResult = $this->getOrderResult($this->funds->operationCoin->amount, $pair->sell, $pair->fee); log_msg('[MAKE ORDER] sell:' . $this->funds->operationCoin->amount . ' with price:' . $pair->sell . ', fee:' . $pair->fee . ', result:' . $orderResult); try { $this->orderSell($pair, $this->funds->operationCoin->amount); } catch (BtceLibException $e) { } $doOrderOperations = true; } } } elseif ($lookAt == StrategyConf::BUY) { if ($this->weights[$_pair_code]['buy'] == $this->strategy->capture_count_buy + 1) { log_msg('Buy weight: MAX'); } else { if ($diff * -1 > $this->strategy->diff_buy[$_pair_code]) { log_msg('[CAPTURE]'); log_msg('Buy diff: was [' . $pairPrev->buy . '], now [' . $pair->buy . '], diff = ' . $diff); $this->weights[$_pair_code]['buy']++; } else { if ($diff > 0 && $this->weights[$_pair_code]['buy'] > 0) { $this->weights[$_pair_code]['buy']--; } } log_msg('Buy weight: ' . $this->weights[$_pair_code]['buy']); if ($this->weights[$_pair_code]['buy'] == $this->strategy->capture_count_buy) { // make order operation BUY $this->orderBuy($pair, $this->funds->operationCoin->amount); $doOrderOperations = true; } } } if ($doOrderOperations) { foreach ($this->pairs->list[$pair] as $_pair) { $_pair->refreshRequired = true; } $this->storage->data->pairs = $this->pairs->export(); $this->storage->data->funds = $this->funds->export(); $this->storage->save(); log_msg('operation coin is: ' . $this->funds->operationCoin->infoString()); } } else { log_msg("fail to load pair: " . $_pair_code); } } } } }