public static function make($order) { // Пробуем присоединить покупку к уже существующей $buy = Buy::model()->findByAttributes(array('price' => $order->price, 'sold' => 0)); if ($buy) { $buy->count += $order->count - $order->fee; $buy->summ += $order->summ; $buy->fee += $order->fee; } else { $buy = new Buy(); $buy->dtm = $order->close_dtm; $buy->count = $order->count - $order->fee; $buy->price = $order->price; $buy->summ = $order->summ; $buy->fee = $order->fee; } $buy->save(); return $buy; }
private function AnalizeBuy($exdata) { $exlen = sizeof($exdata); $prev_stok_direction = 0; // Предыдущее направление $stok_direction = 0; // Текущее направление $lastbuy = Buy::getLast(); // Получаем дату последней продажи $canbuy = false; for ($i = 0; $i < $exlen; $i++) { $exitem = $exdata[$i]; // Если есть что анализировать if ($i <= $this->buystep_n + 1) { continue; } //Определяем направление $exstep = array_slice($exdata, $i - $this->buystep_n, $this->buystep_n + 1); $stok_direction = $this->getDirection($exstep, 'buy'); Log::Add($exitem['dt'], 'Направление курса: ' . $stok_direction); // Изменение курса if ($prev_stok_direction != $stok_direction) { $canbuy = true; } // Если анализ до последней покупки - ничего не покупаем if ($exitem['dt'] <= $lastbuy->dtm) { $canbuy = false; } if ($this->virtual == 1) { // Пока не дошли до последнего (актуального) элемента не покупаем if ($i < $exlen - 1) { $canbuy = false; } } // Для логов if ($this->virtual == 1 && $i == $exlen - 1 && !$canbuy) { Log::Add($exitem['dt'], 'Не купил, курс не меняется: ' . $prev_stok_direction . ' => ' . $stok_direction, 1); } // Проверяем покупку if ($stok_direction == 1 && $canbuy) { // Если сумма покупки больше баланса то уменьшить до баланса /*if ($this->buy_sum>$this->balance) $this->buy_sum=$this->balance; $buy_value = floor(($this->buy_sum / $exitem['buy']*(1+$this->fee))*10000)/10000; // Если 0 то поищем подешевле if ($buy_value == 0) continue; */ $price = $exitem['buy'] * $this->buy_value * (1 + $this->fee); // Првоеряем остаток денег на балансе, если кончились - выходим if ($this->balance - $price < 0) { break; } // Покупаем $btc = new Buy(); $btc->dtm = $exitem['dt']; $btc->count = $this->buy_value; $btc->price = $exitem['buy']; $btc->summ = $price; if ($btc->save()) { $this->bought[] = $btc; $this->total_buy += $price; // Всего куплено $this->balance -= $price; // Актуализируем баланс RUB $this->balance_btc += $this->buy_value; // Актуализируем баланс BTC $this->order_cnt++; // Увеличиваем число сделок $canbuy = false; // Блокируем покупки до конца роста Log::Add($exitem['dt'], '<b>Купил (№' . $btc->id . ') ' . $this->buy_value . ' ед. за ' . $exitem['buy'] . ' (' . $exitem['buy'] * $this->fee . ' комиссия) на сумму ' . $price . ' руб.</b>', 1); } } $prev_stok_direction = $stok_direction; } }