def _check_sell_sign(self, dateidx, buyprice): code = self.code price = self.tm.prices[code][dateidx] # Max holding period self.watching_prices.append(price) if len(self.watching_prices) >= self.max_holding_period: return True, "Reached max holding period %d." % self.max_holding_period # Loss cut rate = ((price-buyprice)*1.0/buyprice)*100*(-1)*self.mp if rate > self.loss_cut_rate: return True, "Loss cut. rule:%s, current loss rate:%s" % (str(self.loss_cut_rate), str(rate)) # Stop order if self.mp*price > self.mp*self.base_price: self.base_price = price if self.stop_order_rate > self.stop_order_decrease_rate: self.stop_order_rate -= self.stop_order_decrease_rate stop_order_price = math.floor(self.base_price * (100-self.mp*self.stop_order_rate)*1.0/100) if self.mp*price < self.mp*stop_order_price: return True, "Stop order. rule:%d, stop price:%d, current price:%d" % \ (self.stop_order_rate, stop_order_price, price) # Watch slope if len(self.watching_prices) > self.estimated_period: del self.watching_prices[0] if len(self.watching_prices) > 1: slope = f.get_regression(self.watching_prices)/self.watching_prices[0]*1.0*100 if slope < self.estimated_profit: self.waited_period += 1 else: self.waited_period = 0 if self.waited_period >= self.waiting_period: return True, "Not enough slope %d for %d days. current slope:%s" % \ (self.estimated_profit, self.estimated_period, str(slope)) if self.enough_profit > 0: profit_rate = self.mp*1.0*(price - buyprice)/buyprice*100 if profit_rate >= self.enough_profit: return True, "Gained enough profit %f percent." % (profit_rate) return False, ""
def judge_enter(self, datei): point = 0 (direction, forec_point) = self.check_forecast(datei) (last_pm, idx, pm_idx) = self._get_last_pm(datei) if last_pm == 1 and direction == DIR_UP: return (NO_ENTER, "Last peak and forecast don't match.", point) if last_pm == -1 and direction == DIR_DOWN: return (NO_ENTER, "Last peak and forecast don't match.", point) (enter_level, msg, tmp_direction) = self.check_enter(datei) if enter_level == YES_ENTER_BUY and direction == DIR_DOWN: enter_level = NO_ENTER if enter_level == YES_ENTER_SELL and direction == DIR_UP: enter_level = NO_ENTER (open, high, low, close, volume) = self._get_prices(datei-1, self.validate_period) r = f.get_regression(close, "s") if enter_level == YES_ENTER_BUY: if r > 0: point +=1 if enter_level == YES_ENTER_SELL: if r < 0: point +=1 if enter_level == NO_ENTER: if direction == DIR_UP and last_pm != 0: if self.trade_mode == "BUY" or self.trade_mode == "BOTH": enter_level = YES_ENTER_BUY msg = "Bottom peak. Forecast=Up" point += 1 if direction == DIR_DOWN and last_pm != 0: if self.trade_mode == "SELL" or self.trade_mode == "BOTH": enter_level = YES_ENTER_SELL msg = "Top peak, Forecast=Down" point += 1 if enter_level != NO_ENTER: point += abs(tmp_direction) self.curr_enter_point = point return (enter_level, msg, point)