def on_bar(self, data): """ Callback of new bar data update. """ best_bardata = {} for bar in data.values(): flag = get_symbol_flag(bar.symbol) if flag not in Future_Params: continue if bar.symbol not in self.cache_bar: self.cache_bar[bar.symbol] = [] self.cache_bar[bar.symbol].append(bar) if not self.is_expire(bar): # 剩余天数 小于等于7天 排除 best_bar = best_bardata.get(flag, None) if not best_bar or bar.volume > best_bar.volume: best_bardata[flag] = bar self.day += 1 if not self.inited: return if len(self.active_symbol) <= 0: # 选取对冲合约 self.select_symbol(best_bardata) return if self.day >= self.Z: # 持续持有Z(参数)日后全部平仓 第二天重新开始选取对冲合约 self.clear_all_symbol() return
def on_start(self): """ Callback when strategy is started. """ self.write_log("策略启动") self.put_event() for data in list(self.cache_bar.values()): bar = data[-1] flag = get_symbol_flag(bar.symbol) self.change_flag.add(flag) for flag in self.change_flag: self.change_active_symbol(flag) self.change_flag.clear()
def loss_plan_2(self): # 若整体亏损超过P1(2%),则平仓所有品种 loss_price = 0 max_price = 0 for symbol, temp in self.active_symbol.items(): pos = temp['pos'] price = temp['price'] bar = self.cache_bar[symbol][-1] flag = get_symbol_flag(bar.symbol) param = Future_Params[flag] if pos > 0: loss_price += (bar.close_price - price) * abs(pos) * param['size'] elif pos < 0 and bar.close_price > price: loss_price += (price - bar.close_price) * abs(pos) * param['size'] max_price += price * abs(pos) * param['size'] if loss_price / max_price * 100 <= -self.P1: self.clear_all_symbol()
def change_active_symbol(self): self.change_flag = False best_bar = None for data in list(self.cache_bar.values()): bar = data[-1] if self.symbol_expire[bar.symbol] - bar.datetime <= timedelta( days=15): self.cache_bar.pop(bar.symbol) continue if not best_bar: best_bar = bar elif bar.volume > best_bar.volume: best_bar = bar elif bar.volume == best_bar.volume and self.symbol_expire[ bar.symbol] < self.symbol_expire[best_bar.symbol]: best_bar = bar if not best_bar: print('not active symbol', self.active_symbol) self.active_symbol = None return self.active_symbol = best_bar.symbol flag = get_symbol_flag(best_bar.symbol) param = Future_Params[flag] param['pos'] = int(150000 / (best_bar.close_price * param['size'] * 0.3)) print('active symbol', best_bar.symbol) data = self.cache_bar[self.active_symbol] self.am = ArrayManager() for bar in data: am = self.am am.update_bar(bar) am.update_extra( (bar.low_price + bar.high_price + bar.close_price * 2) / 4) for symbol in list(self.cache_bar.keys()): if self.is_expire(symbol): self.cache_bar.pop(symbol)
def change_active_symbol(self, flag): best_bar = None for data in list(self.cache_bar.values()): bar = data[-1] if get_symbol_flag(bar.symbol) != flag: continue if self.symbol_expire[bar.symbol] - bar.datetime <= timedelta( days=15): self.cache_bar.pop(bar.symbol) continue if not best_bar: best_bar = bar elif bar.volume > best_bar.volume: best_bar = bar elif bar.volume == best_bar.volume and self.symbol_expire[ bar.symbol] < self.symbol_expire[best_bar.symbol]: best_bar = bar if not best_bar: self.active_symbol[flag] = None print('change_active_symbol not best symbol', flag) return self.active_symbol[flag] = best_bar.symbol param = Future_Params[flag] param['pos'] = int( 150000 / (best_bar.close_price * self.cta_engine.get_size() * 0.3)) print('active symbol', best_bar.symbol) data = self.cache_bar[best_bar.symbol] self.am[flag] = ArrayManager() am = self.am[flag] for bar in data: am.update_bar(bar) am.update_extra( (bar.low_price + bar.high_price + bar.close_price * 2) / 4) for symbol in list(self.cache_bar.keys()): if self.is_expire(symbol): self.cache_bar.pop(symbol)
def reset_symbol_pos(self, bar): flag = get_symbol_flag(bar.symbol) param = Future_Params[flag] pos = int(150000 / (bar.close_price * param['size'] * 0.3)) return pos
def on_bar(self, data): """ Callback of new bar data update. """ if self.change_flag and self.pos == 0: self.change_active_symbol() best_bar = None for bar in data.values(): if self.is_expire(bar.symbol): continue if bar.symbol not in self.cache_bar: self.cache_bar[bar.symbol] = [] self.cache_bar[bar.symbol].append(bar) if not best_bar or bar.volume > best_bar.volume: best_bar = bar if not self.active_symbol: return # 交易合约是否切换 expire = self.symbol_expire[self.active_symbol] bar = self.cache_bar[self.active_symbol][-1] am = self.am am.update_bar(bar) am.update_extra( (bar.low_price + bar.high_price + bar.close_price * 2) / 4) self.cta_engine.set_order_bar(bar) change = False flag = get_symbol_flag(self.active_symbol) one_pos = Future_Params[flag]['pos'] if expire - bar.datetime <= timedelta(days=7): # 小于7天平仓做主力合约 change = True elif expire - bar.datetime <= timedelta(days=15): # 小于15天未开仓做主力合约 if self.pos == 0: change = True elif best_bar.volume > 0: if not self.change_flag and bar.volume / best_bar.volume < 0.01: # 小于主力合约35% 下一次平仓做主力合约 if self.pos == 0: change = True else: self.change_flag = True elif bar.volume / best_bar.volume < 0.001: # 小于主力合约10%平仓做主力合约 change = True if change: if self.pos > 0: self.sell(bar.close_price, one_pos) elif self.pos < 0: self.cover(bar.close_price, one_pos) self.change_flag = True # self.change_active_symbol() elif len(self.cache_bar[self.active_symbol]) >= self.test_day: mac = talib.MA(am.extra, self.mac_day)[-1] qg = max(am.open[-2], am.close[-2]) qd = min(am.open[-2], am.close[-2]) #这个地方我改成了min macd, signal, hist = am.macd(self.macd_param1, self.macd_param2, self.macd_param3, True) if self.pos > 0: if bar.close_price < mac and bar.close_price < qd: self.sell(bar.close_price, one_pos) if self.pos < 0: if bar.close_price > mac and bar.close_price > qg: self.cover(bar.close_price, one_pos) if self.pos == 0: if bar.close_price > mac and bar.close_price > qg and macd[ -1] > signal[-1] and bar.close_price > bar.open_price: self.buy(bar.close_price, one_pos) if bar.close_price < mac and bar.close_price < qd and macd[ -1] < signal[-1] and bar.close_price < bar.open_price: self.short(bar.close_price, one_pos) self.put_event()
def get_active_symbol(self, symbol): flag = get_symbol_flag(symbol) return self.active_symbol.get(flag, None)
def add_symbol_pos(self, symbol, pos_change): flag = get_symbol_flag(symbol) old = self.symbol_pos.get(flag, 0) self.symbol_pos[flag] = old + pos_change
def on_bar(self, data): """ Callback of new bar data update. """ for flag in list(self.change_flag): if self.get_symbol_pos(flag) == 0: self.change_active_symbol(flag) self.change_flag.remove(flag) best_bardata = {} for bar in data.values(): flag = get_symbol_flag(bar.symbol) if flag not in Future_Params: continue if self.is_expire(bar.symbol): continue if bar.symbol not in self.cache_bar: self.cache_bar[bar.symbol] = [] self.cache_bar[bar.symbol].append(bar) best_bar = best_bardata.get(flag, None) if not best_bar or bar.volume > best_bar.volume: best_bardata[flag] = bar if not self.inited: return for flag, best_bar in best_bardata.items(): # 交易合约是否切换 active_symbol = self.active_symbol.get(flag, None) if not active_symbol: if len(self.cache_bar[best_bar.symbol]) >= self.test_day: self.change_active_symbol(flag) active_symbol = self.active_symbol.get(flag, None) if not active_symbol: continue expire = self.symbol_expire[active_symbol] bar = self.cache_bar[active_symbol][-1] am = self.am[flag] am.update_bar(bar) am.update_extra( (bar.low_price + bar.high_price + bar.close_price * 2) / 4) self.cta_engine.set_order_bar(bar) change = False pos = self.get_symbol_pos(flag) one_pos = Future_Params[flag]['pos'] if bar.datetime + timedelta(days=7) >= expire: # 小于7天平仓做主力合约 print(bar.symbol, expire - bar.datetime) change = True elif bar.datetime + timedelta(days=15) >= expire: # 小于15天未开仓做主力合约 if pos == 0: change = True print(bar.symbol, expire - bar.datetime) elif best_bar.volume > 0: if flag not in self.change_flag and bar.open_interest / best_bar.open_interest < 0.1: # 小于主力合约35% 下一次平仓做主力合约 if pos == 0: change = True else: self.change_flag.add(flag) elif bar.open_interest / best_bar.open_interest < 0.05: # 小于主力合约10%平仓做主力合约 change = True if change: if pos > 0: self.sell(bar.close_price, one_pos) elif pos < 0: self.cover(bar.close_price, one_pos) self.change_flag.add(flag) elif len(self.cache_bar[active_symbol]) >= self.test_day: mac = talib.MA(am.extra, self.mac_day)[-1] qg = max(am.open[-2], am.close[-2]) qd = min(am.open[-2], am.close[-2]) #这个地方我改成了min macd, signal, hist = am.macd(self.macd_param1, self.macd_param2, self.macd_param3, True) if pos > 0: if bar.close_price < mac and bar.close_price < qd: self.sell(bar.close_price, one_pos) if pos < 0: if bar.close_price > mac and bar.close_price > qg: self.cover(bar.close_price, one_pos) if pos == 0: if bar.close_price > mac and bar.close_price > qg and macd[ -1] > signal[ -1] and bar.close_price > bar.open_price: self.buy(bar.close_price, one_pos) if bar.close_price < mac and bar.close_price < qd and macd[ -1] < signal[ -1] and bar.close_price < bar.open_price: self.short(bar.close_price, one_pos) self.put_event()