Example #1
0
 public function NeedSell()
 {
     // Составляем причину покупки
     $reason = array();
     $curtime = $this->curtime;
     //Дата операции
     $dt = date('Y-m-d H:i:s', $curtime);
     //Смотрим, что продать
     //$bought = Buy::model()->findAll(array('condition'=>'sold=0 and order_id=0'));
     $bought = Buy::getNotSold();
     // Если нечего продавать
     if (sizeof($bought) == 0) {
         return false;
     }
     /*
     // Если текущая цена ниже средней не продаем
     $from = date('Y-m-d H:i:s',$this->curtime-60*60*24*7);
     $avg_sell = Exchange::getAvg('sell', $from,  date('Y-m-d H:i:s', $this->curtime));
     if ($avg_sell>$this->current_exchange->sell)
     {
     	Log::notsell('Цена ниже средней за 7 дн. ('.$avg_sell.'>'.$this->current_exchange->buy.'), не продаем.');
     	return false;
     }
     else
     	$reason['avg_price'] = 'Текущая цена выше средней за 7 дней '.('.$this->avg_sell.'>'.$this->current_exchange->buy.'); 
     */
     // Проверяем была ли уже продажа за последнее время, если была и цена была более выгодная чем текущая то не продаем
     $lastSell = Sell::getLast();
     if ($lastSell) {
         $tm = strtotime($lastSell->dtm) + self::min_sell_interval;
         $diff = 1 - $lastSell->price / $this->current_exchange->sell;
         if ($tm > $this->curtime && $diff < $this->sell_imp_dif) {
             Log::notsell('Уже была продажа, ждем до ' . date('Y-m-d H:i:s', $tm) . ' текущая цена ' . $this->current_exchange->sell . ' меньше прошлой ' . $lastSell->price);
             return false;
         } else {
             $reason['avg_price'] = 'Прошлая продажа была ' . ($this->curtime - strtotime($lastSell->dtm)) / 60 . ' мин. назад (допустимы покупки раз в ' . self::min_sell_interval / 60 . ' мин. при отсутствии ощутимого роста цены), цена отличалась от текущей на ' . $diff * 100 . '%, минимальное отличие должно быть ' . $this->sell_imp_dif * 100 . '% ';
         }
     }
     //Перебираем периоды
     $all_tracks = array();
     foreach ($this->sell_periods as $period) {
         $all_tracks[] = $this->getGraphImage($curtime, $period, 'sell', $this->sell_imp_dif);
     }
     //Анализируем треки
     $tracks = $this->getSellTracks($all_tracks);
     if (sizeof($tracks) == 0) {
         Log::notsell('Нет подходящих треков для продажи');
         return false;
     }
     $reason['tracks'] = $tracks;
     $reason['all_tracks'] = $all_tracks;
     // Совершаем вынужденные продажи
     $this->NecesarySell($all_tracks, $bought);
     // Ищем выгодные продажи
     foreach ($bought as $key => $buy) {
         // Цена продажи
         $curcost = $buy->count * $this->current_exchange->sell * (1 - self::fee);
         // Сколько заработаем при продаже (комиссия была уже вычтена в btc при покупке)
         $income = $curcost - $buy->summ;
         // Достаточно ли заработаем
         if ($income / $buy->summ < self::min_income) {
             if ($income > 0) {
                 Log::notsell('Не продали (№' . $buy->id . '), доход слишком мал ' . $income . ' < ' . self::min_income * $curcost . ' купил за ' . $buy->summ . ' можно продать за ' . $curcost . ' sell=' . $this->current_exchange->sell);
             }
             continue;
         }
         // Записываем причину покупки
         $reason['buy'] = 'Найдена подходящая продажа №' . $buy->id . ' с доходом от сделки ' . $income . ' руб., что составляет ' . $income / $buy->summ * 100 . '% от цены покупки';
         Log::Add('Начало продажи №' . $buy->id);
         $this->startSell($buy, $reason);
         //unset($bought[$key]);
         break;
         // не более одной продажи по расчету за раз
     }
 }