def reverse_shape_strategy(self, am: ArrayManager, bar: BarData, calc_data, setting={ "atr": 40, "atr_valve": 0.8, "deg1": (40, 20), "deg2": (20, 0), }): deg1 = calc_data["deg40_20"] deg2 = calc_data["deg20_0"] kdj = calc_data["kdj"] atr = self.am.atr(40) if atr < 0.08: return if deg1 > 0 and deg2 > 0 or \ deg1 < 0 and deg2 < 0: return if not (abs(deg1) > 0.15 and abs(deg2) > 0.1 and (abs(deg1) + abs(deg2)) > 0.3): return close = am.close[-40:] min_val = np.min(close) max_val = np.max(close) mid_val = max_val if deg1 > 0 else min_val mid_pos = np.where(close == mid_val)[0][0] if mid_pos < 10 or mid_pos > 30: return start_val = np.min(close[:mid_pos]) if deg1 > 0 else np.max( close[:mid_pos]) start_pos = np.where(close == start_val)[0][0] l = mid_pos - start_pos # pos2 = np.where(close == min_val)[0][0] x_fit = reg_util.regress_y_polynomial(close[:mid_pos], zoom=True) deg1_remake = calc_regress_deg(x_fit[:abs(mid_pos)], False) y_fit = reg_util.regress_y_polynomial(close[mid_pos:], zoom=True) deg2_remake = calc_regress_deg(y_fit[:abs(mid_pos)], False) print(start_pos, mid_pos, deg1, deg2, deg1_remake, deg2_remake, l, start_val, mid_val) if deg2 < 0: if kdj[0] < 20 and kdj[1] < 10 and kdj[2] < 10: # if kdj[2] < 10: return self.short(bar.close_price, 1, type=OrderType.MARKET) else: if kdj[0] > 80 and kdj[1] > 90 and kdj[2] > 90: # if kdj[2] > 90: return self.buy(bar.close_price, 1, type=OrderType.MARKET)
def close2(self, bar: BarData, calc_data): if self.level > 0: self.order_data = np.append(self.order_data, bar.close_price) order_id = None offset = -40 offset_m = int(offset / 2) deg_full = calc_regress_deg(self.strategy.am.close[-10:], False) if len(self.order_data) > abs(offset * 1.5): y_fit = reg_util.regress_y_polynomial(self.order_data, zoom=True) deg_order_short = calc_regress_deg(y_fit[:abs(offset)], False) if self.volumn > 0: if deg_full < -0.01: # if abs(deg_order_short) < abs(deg_full): return self.strategy.sell( bar.close_price, 1, type=OrderType.MARKET, extra={"reason": "平仓:趋势趋弱,deg={}".format(deg_full)}) elif self.volumn < 0: if deg_full > 0.01: # if abs(deg_order_short) < abs(deg_full): return self.strategy.cover( bar.close_price, 1, type=OrderType.MARKET, extra={"reason": "平仓:趋势趋弱,deg={}".format(deg_full)})
def trend_reverse_close(self, am: ArrayManager, bar: BarData, calc_data): if self.trade_mgr.get_status() != PositionStatus.Empty: data = self.ma_info.info[-60:] length = len(data) close = (data[10] + data[20] + data[30]) / 3 if length > 10: y_fit = reg_util.regress_y_polynomial(close[-10:], zoom=True) deg = calc_regress_deg(y_fit[:10], False) else: deg = calc_regress_deg(close[-10:], False) calc_data["trade_close_deg"] = deg if self.trade_mgr.get_status() == PositionStatus.Short: if deg > 0.01: calc_data["trade_close"] = "平仓:趋势反转{}".format(deg) return self.trade_mgr.cover(bar.close_price) elif self.trade_mgr.get_status() == PositionStatus.Long: if deg < -0.01: calc_data["trade_close"] = "平仓:趋势反转{}".format(deg) return self.trade_mgr.sell(bar.close_price)
def ma_trend_strategy(self, am:ArrayManager, bar:BarData, calc_data, setting={"len":40, "atr":40, "atr_valve":0.09, "mid_sign":(10,30)}): # 60个bar后取消 if self.trend_record.std_val is not None: if len(self.trend_record.close) >= 60: self.trend_record.close = [] self.trend_record.std_val = None else: self.trend_record.close.append(bar.close_price) # 如果有新的bar,则覆盖 ma5_std = self.ma_info[-1]["ma5"] if ma5_std <= 0.16: self.trend_record.close = [] self.trend_record.std_val = ma5_std return if self.trend_record.std_val is not None and \ ma5_std > 0.8: y_fit = reg_util.regress_y_polynomial(self.trend_record.close, zoom=True) deg = calc_regress_deg(y_fit[:10], False) if deg < 0: # if k < 20 and d < 10 and j < 10: # if kdj[2] < 10: if self.pos == 0: calc_data["trade_open"] = "开空,deg={}".format(deg) return self.short(bar.close_price, 1, type=OrderType.MARKET) else: # if k > 80 and d > 90 and j > 90: # if kdj[2] > 90: if self.pos == 0: calc_data["trade_open"] = "开多,deg={}".format(deg) return self.buy(bar.close_price, 1, type=OrderType.MARKET)
def reverse2_strategy(self, am:ArrayManager, bar:BarData, calc_data, setting={"len":40, "atr":40, "atr_valve":0.09, "mid_sign":(10,30)}): length = 30 offset1 = -30 offset2 = int(-10) close = am.close deg1 = calc_regress_deg(close[-30:-8], False) deg2 = calc_regress_deg(close[-8:], False) if deg1 > 0 and deg2 > 0 or \ deg1 < 0 and deg2 < 0: return if not (abs(deg1) > 0.15 and abs(deg2) > 0.15 and (abs(deg1) + abs(deg2)) > 0.35) : return close = am.close[-length:] min_val = np.min(close) max_val = np.max(close) mid_val = max_val if deg1 > 0 else min_val mid_pos = np.where(close == mid_val)[0][0] if mid_pos < setting["mid_sign"][0] or mid_pos > setting["mid_sign"][1]: return start_val = np.min(close[:mid_pos]) if deg1 > 0 else np.max(close[:mid_pos]) start_pos = np.where(close == start_val)[0][0] l = mid_pos - start_pos # pos2 = np.where(close == min_val)[0][0] kdj = am.kdj() k = kdj["k"][-1] d = kdj["d"][-1] j = kdj["j"][-1] x_fit = reg_util.regress_y_polynomial(close[:mid_pos], zoom=True) deg1_remake = calc_regress_deg(x_fit[:abs(mid_pos)], False) y_fit = reg_util.regress_y_polynomial(close[mid_pos:], zoom=True) deg2_remake = calc_regress_deg(y_fit[:abs(mid_pos)], False) # print(start_pos, mid_pos, deg1, deg2, deg1_remake, deg2_remake, l, start_val, mid_val) cci = am.cci(20) ma60 = am.sma(60) if deg2 < 0: # if k < 20 and d < 10 and j < 10: # if kdj[2] < 10: if cci < -100 and bar.close_price < ma60: if self.pos == 0: calc_data["trade_open"] = "开空,deg={},cci={}".format(deg2, cci) return self.short(bar.close_price, 1, type=OrderType.MARKET) elif self.pos > 0: calc_data["trade_close"] = "平多后做空仓,deg={},cci={}".format(deg2, cci) order_id_cover = self.sell(bar.close_price, abs(self.volumn), type=OrderType.MARKET) order_id_buy = self.short(bar.close_price, 1, type=OrderType.MARKET) return order_id_cover.extend(order_id_buy) else: # if k > 80 and d > 90 and j > 90: # if kdj[2] > 90: if cci > 100 and bar.close_price > ma60: if self.pos == 0: calc_data["trade_open"] = "开多,deg={},cci={}".format(deg2, cci) return self.buy(bar.close_price, 1, type=OrderType.MARKET) elif self.pos < 0: calc_data["trade_close"] = "平空后多仓,deg={},cci={}".format(deg2, cci) order_id_cover = self.cover(bar.close_price, abs(self.volumn), type=OrderType.MARKET) order_id_buy = self.buy(bar.close_price, 1, type=OrderType.MARKET) return order_id_cover.extend(order_id_buy)