def get_last_pm(self, datei): (pnf_joints, pnf_indexes, pnf_pmmask) = self.get_pnf_joints() (indexes, dates, open, high, low, close, volume) = self.data last_idx = -1 for i in range(len(pnf_joints)): if pnf_indexes[i] >= datei: if i > 1: last_idx = i-1 break if last_idx < 0: return (0, last_idx, -1) last_peak = pnf_joints[last_idx] last_pm = pnf_pmmask[last_idx] last_d_idx = pnf_indexes[last_idx] max_p = max(high[last_d_idx:datei+1]) min_p = min(low[last_d_idx:datei+1]) price = close[datei] boxsize = kf.get_boxsize(price) if ((last_peak - price) >= boxsize*3 and last_pm == 1 and max_p <= last_peak) \ or ((price - last_peak) >= boxsize*3 and last_pm == -1 and min_p >= last_peak): return (pnf_pmmask[last_idx], last_idx, pnf_indexes[last_idx]) else: if (last_peak - min_p) >= boxsize*3 and (price - min_p) >= boxsize*3 and last_pm == 1: return (-1, -1, -1) if (max_p - last_peak) >= boxsize*3 and (max_p - price) >= boxsize*3 and last_pm == -1: return (1, -1, -1) return (0, last_idx, -1)
def _get_trend_lines(self, joints, indexes, pmmask): j = 5 up_trend_lines = {} down_trend_lines = {} for i in range(5, len(indexes)): if pmmask[i] != pmmask[i-2] or pmmask[i] != pmmask[i-4]: continue df = indexes[i]-indexes[i-4] if df < 12: continue if df != 0: slope = (joints[i]-joints[i-4])*1.0/df else: slope = 0 const = joints[i-4]-slope*indexes[i-4] midrp = joints[i-2] midlp = self._eq(indexes[i-2], slope, const) #boxsize = kf.get_bollinger_boxsize(midrp) boxsize = kf.get_boxsize(midrp) if midlp - boxsize <= midrp and midlp + boxsize >= midrp: for d in range(indexes[i]+1, indexes[i] + indexes[i]-indexes[i-4]+1): if pmmask[i] == 1: if up_trend_lines.has_key(d) == False: up_trend_lines[d] = [] up_trend_lines[d].append(self._eq(d, slope, const)) if pmmask[i] == -1: if down_trend_lines.has_key(d) == False: down_trend_lines[d] = [] down_trend_lines[d].append(self._eq(d, slope, const)) j += 1 self.trend_lines["up"] = up_trend_lines self.trend_lines["down"] = down_trend_lines
def _judge_enter_proc(self, datei): datei -= 1 (open, high, low, close, volume) = self._get_prices(datei, 1) price = (high + low + close) / 3 msg = "" boxsize = kf.get_boxsize(price) ret = (NO_ENTER, msg) if high - low > boxsize: return ret if len(self.enter_prices) > 0: if price + boxsize >= self.enter_prices[-1] and price - boxsize <= self.enter_prices[-1]: return ret direction = 0 # ichimoku tmp_direction = self._judge_ichimoku_points(datei) if tmp_direction != 0: msg = f.joinstr("Ichimoku", msg) direction += tmp_direction # bollinger tmp_direction = self._judge_bollinger_points(datei) if tmp_direction != 0: msg = f.joinstr("Bollinger", msg) direction += tmp_direction # base lines tmp_direction = self._judge_base_points(datei) if tmp_direction != 0: msg = f.joinstr("Base lines", msg) direction += tmp_direction # dow lines tmp_direction = self._judge_dow_points(datei) if tmp_direction != 0: msg = f.joinstr("Dow", msg) direction += tmp_direction # trend lines tmp_direction = self._judge_trend_points(datei) if tmp_direction != 0: msg = f.joinstr("Trend lines", msg) direction += tmp_direction if abs(direction) > self.min_action_score: if direction > 0: return (YES_ENTER_BUY, msg) elif direction < 0: return (YES_ENTER_SELL, msg) return ret
def _get_last_pm(self, datei): (pnf_joints, pnf_indexes, pnf_pmmask) = self.kl.get_pnf_joints() (indexes, dates, open, high, low, close, volume) = self.data last_idx = -1 for i in range(len(pnf_joints)): if pnf_indexes[i] >= datei: if i > 1: last_idx = i-1 break if last_idx < 0: return (0, last_idx, -1) last_peak = pnf_joints[last_idx] price = close[datei] boxsize = kf.get_boxsize(price) if abs(last_peak - price) >= boxsize*3: return (pnf_pmmask[last_idx], last_idx, pnf_indexes[last_idx]) else: return (0, last_idx, -1)
def old_judge_release(self, datei): (open, high, low, close, volume) = self._get_prices(datei, 1) price = (high + low + close) / 3 ret = (NO_RELEASE, 0, "") if self.curr_trade_mode == MODE_BUY: pm = 1 limit_price = low elif self.curr_trade_mode == MODE_SELL: pm = -1 limit_price = high else: return ret # if self.curr_holding_n <= 0: if len(self.enter_cnts) == 0: return ret start_price = self.enter_prices[0] if len(self.enter_prices) > 2: bottom_price = self.enter_prices[-2] else: bottom_price = start_price if self.bottom_price != bottom_price or self.bottom_price == 0: self.bottom_price = bottom_price self.days_after_bottom_price = 0 else: self.days_after_bottom_price += 1 pos_price = self.enter_prices[-1] # losscut rate if self.losscut_reduce_interval > 0: curr_losscut_rate = self.losscut_rate - (self.days_after_bottom_price / self.losscut_reduce_interval) * 0.01 loss_rate = (self.bottom_price - limit_price) * 1.0 * pm / self.bottom_price border_price = self.bottom_price * (1.0 - pm * self.losscut_rate) if loss_rate > self.losscut_rate: msg = "Loss cut" release_price = close if pm * limit_price <= pm * border_price * (1.0 - pm * 0.01): release_price = border_price * (1.0 - pm * 0.01) else: release_price = limit_price return (MUST_RELEASE, release_price, msg) loss_rate = (pos_price - price) * 1.0 * pm / pos_price if loss_rate > self.losscut_rate: msg = "Line loss cut" return (YES_RELEASE, close, msg) # stop order boxsize = kf.get_boxsize(price) if self.base_price == 0: self.base_price = price if self.base_price * pm < (price - boxsize) * pm: self.base_price = price stop_rate = (self.base_price - price) * 1.0 * pm / self.base_price if stop_rate > self.stoporder_rate: msg = "Stop order" return (YES_RELEASE, close, msg) # enough std if self.spent >= self.std_span: (eopen, ehigh, elow, eclose, evolume) = self._get_prices(datei, self.std_span) cmean = np.mean(eclose) if cmean > 0: std = np.std(close / cmean) if std < self.min_std_rate: msg = "Not enough slope" return (YES_RELEASE, close, msg) return ret