def on_bar(self, klines): StrategyBase.on_bar(self, klines) #特殊 #self.monitor_open = False #self.on_run(klines) #会产生order #盈利与损失 #klines转态与account 是否匹配 self.update_data(klines) profit = self.hold_pos pos = self.hold_pos balance = self.manager.get_balance() #account 监控 if (profit < self.calc_max_loss(pos)): self.create_loss_exit_task() elif (profit > self.calc_max_gain(pos)): self.create_profit_exit_task() #监控 数量 if (abs(pos) > abs(self.calc_max_postion(pos))): self.create_monitor_position_task() #监控资金曲线 self.monitor_capital_curve(klines) self.monitor_key_price(klines) self.monitor_key_hour(klines) self.monitor_event(klines) self.monitor_limiter(klines) #实时调整 self.monitor_trade_direction(klines) self.monitor_trade_price(klines) self.run_task(klines)
def on_bar(self, klines): StrategyBase.on_bar(self, klines) if (self.run_flag == False): return lt = self.get_real_time() s_checksum = pplib.get_checksum(klines, 2, 5) cur_bar = self.get_current_minute_bar() self.cur_bar = cur_bar if (lt.tm_min%2==0 and self.cur_bar>2): self.on_bar_2(klines) if (lt.tm_min%5==0 and self.cur_bar>5): self.on_bar_5(klines) # bigbang #break # 窄幅 #===============================================# if (self.market_mode == 0): self.make_trade_daliy(klines) elif (self.market_mode == 1): self.make_break(klines) self.monitor_position(klines)
def on_bar(self, klines): StrategyBase.on_bar(self, klines) if (self.run_flag == False): return #开仓过滤 if (self.wait_open() == True): return #防止跳空 if (self.check_open_condition()): return if (self.boll_top != 0 and self.position >= 0 and self.ask_price >= self.boll_top): if (self.boll_trade_dir == TradeDirection.BUYONLY and self.position > 0): self.set_position(0) self.debug("平多") else: self.set_position(-1) self.debug("开空") elif (self.boll_bottom != 0 and self.position <= 0 and self.ask_price <= self.boll_bottom): if (self.boll_trade_dir == TradeDirection.SELLONLY and self.position > 0): self.set_position(0) self.debug("平空") else: self.set_position(1) self.debug("开多")
def on_bar(self, klines): if (self.run_flag == False): return if (self.init_flag == False): self.mark_start_id = klines.iloc[-1].id self.init_flag = True StrategyBase.on_bar(self, klines) self.cur_bar = self.get_current_minute_bar() if (self.position == 0 and self.done_flag == False): if (self.level == 1): self.level_1(klines) elif (self.level == 2): self.level_2(klines) elif (self.level == 3): self.level_3(klines) ''' lt = time.localtime(time.time()) # 开仓时间 if (lt.tm_hour == 9 and lt.tm_min < 30 or lt.tm_hour == 21 and lt.tm_min < 30): if(self.position == 0): self.open_time(klines, ask_price, bid_price) if (self.position != 0): hest_inday = self.indictor.get_hest_inday() lest_inday = self.indictor.get_lest_inday() open_inday = self.indictor.get_open_inday() if (open_inday-klines.iloc[-1].close - open_inday > 30): self.stop() # ''' if (self.position != 0): self.done_flag = True #出场-时间、空间 #cur_bar = self.get_current_minute_bar() hest_bar = pplib.get_highest_bar_today_fix(klines) #if ((self.cur_bar-self.entry_bar>90) or (self.cur_bar_id-hest_bar>30)): if ((self.cur_bar - self.entry_bar > 90)): l_checksum = pplib.get_checksum(klines, 17, 2) if (abs(l_checksum) < 4): self.debug("持仓时间过长") self.set_position(0) if (self.position != 0): atr_day = self.indictor.get_atr_daily() if (atr_day is None): atr_day = 45 else: atr_day = atr_day - 10 if (self.ask_price - self.entry_price > atr_day): self.set_position(0) elif (self.ask_price - self.entry_price < -10): avg_p = pplib.get_average(klines, 12) if (avg_p - self.entry_price < -20): self.set_position(0)
def on_bar(self, klines): StrategyBase.on_bar(self, klines) if (self.run_flag == False or self.skip_flag == True): return tmp_position = self.position lt = self.get_real_time() self.cur_bar = self.get_current_minute_bar() ma15 = pplib.get_avg_price(klines, 18) if (self.cur_bar < 10): return if (self.low_range_flag == True and ma15 > self.hh_3day and self.position <= 0): tmp_position = 1 elif (self.low_range_flag == True and ma15 < self.ll_3day and self.position >= 0): tmp_position = 0 if (self.continue_flag == False and self.position <= 0): if (self.last_red_or_green == -1): if (self.lastday_close - self.ask_price > 20): tmp_position = 1 elif (self.lastday_close - self.ask_price > 15 and self.cur_bar > 30): tmp_position = 1 elif (self.cur_bar > 60): downs = pplib.get_count_of_less(klines, 60) if (downs > 50): tmp_position = 1 elif (self.continue_flag == True): if (self.lastday_close - self.ask_price > 20): tmp_position = 1 elif (self.lastday_close - self.ask_price > 15 and self.cur_bar > 30): tmp_position = 1 elif (self.cur_bar > 60): downs = pplib.get_count_of_less(klines, 60) if (downs > 30): tmp_position = 1 if (self.position > 0 and self.ask_price >= self.lastday_high + 15): tmp_position = 0 if (self.position > 0 and self.entry_price - self.ask_price > 41): tmp_position = 0 elif (self.position > 0 and self.ask_price - self.entry_price > 25): tmp_position = 0 if (tmp_position != self.position): if (self.check_order(10)): self.set_position(tmp_position)
def on_bar(self, klines): StrategyBase.on_bar(self, klines) if (self.run_flag == False or self.trade_dir == TradeDirection.INVALID): return lt = self.get_real_time() if (lt.tm_hour == 9 and lt.tm_sec <= 20): return cur_bar = self.get_current_minute_bar() if (self.cond_up_count < 15): self.cond_up_count = 15 if (self.cond_down_count < 15): self.cond_down_count = 15 self.on_self_bar(klines, self.cond_up_count, self.cond_down_count, 1, cur_bar)
def on_bar(self, klines): if (self.run_flag == False): return StrategyBase.on_bar(self, klines) if (self.skip_open() == True): return if (self.init_flag == False): self.init_data(klines) lt = self.get_real_time() cur_bar = self.get_current_minute_bar() self.cur_bar = cur_bar open_inday = klines.iloc[-cur_bar].open #print(time_to_str(klines.iloc[-cur_bar].datetime), klines.iloc[-cur_bar].open) #最近两个高点、低点 m_opens = pplib.get_M_opens(klines, 20) w_opens = pplib.get_W_opens(klines, 20) #高低点 差值 m_size = 0 if (m_opens is not None): m_size = len(m_opens) w_size = 0 if (w_opens is not None): w_size = len(w_opens) #bar_id, count_of_nearest_less = pplib.get_count_of_nearest_less(klines, 5, cur_bar) #bar_id, count_of_nearest_greater = pplib.get_count_of_nearest_greater(klines, 5, cur_bar) self.w_size = w_size self.m_size = m_size self.w_opens = w_opens self.m_opens = m_opens if (lt.tm_min % 15 == 0): self.analyze() if (lt.tm_min % 5 == 0): self.on_bar5(klines) self.make_trade(klines) if (self.position != 0): self.monitor_position(klines)
def on_bar(self, klines): if (self.run_flag == False): return if (self.mark_start_bar == 0): self.mark_start_bar = klines.iloc[-1].id StrategyBase.on_bar(self, klines) #self.cur_bar = self.get_current_minute_bar() if (self.position == 0 and self.done_flag == False): if (self.level == 1): self.level_1(klines) elif(self.level == 2): self.level_2(klines) elif(self.level == 3): self.level_3(klines) if (self.position != 0): #出场-时间、空间 self.done_flag = True cur_bar = self.get_current_minute_bar() if ((cur_bar-self.entry_bar>90)):#不用替换_fix l_checksum = pplib.get_checksum(klines, 25, 2) if (abs(l_checksum) < 5): self.set_position(0) if (self.position != 0): atr_day = self.indictor.get_atr_daily() if (atr_day is None): atr_day = 45 else: atr_day = atr_day - 10 if (self.entry_price - self.ask_price > atr_day): self.debug("空止盈") self.set_position(0) elif(self.ask_price-self.entry_price > 10): avg_p = pplib.get_average(klines, 12) if (avg_p-self.entry_price > 20): self.debug("空止损") self.set_position(0)
def on_bar(self, klines): StrategyBase.on_bar(self, klines) cur_bar = self.get_current_minute_bar() s_checksum = pplib.get_checksum(klines, 10, 1) l_checksum = pplib.get_checksum(klines, 50, 0) self.ma5 = pplib.get_avg_price(klines, 6) tmp_position = self.position self.hh_inday, self.hh_inday_bar = pplib.get_highest_bar_today2( klines) #可以不用替换_fix self.ll_inday, self.ll_inday_bar = pplib.get_lowest_bar_today2(klines) # big bang + top、bottom #long top、bottom if (abs(self.hh_inday - self.hh_100) < 20 and self.position >= 0): #if (cur_bar - self.hh_inday_bar > 30): if (self.hh_inday_bar > 30): tmp_position = -1 if (self.hh_inday - self.ma5 > 30): #bigbang tmp_position = -1 if (abs(self.ll_inday - self.ll_100) < 20 and self.position <= 0): #if (cur_bar - self.ll_inday_bar > 30): if (self.ll_inday_bar > 30): tmp_position = 1 if (self.ma5 - self.ll_inday > 30): #bigbang tmp_position = 1 ####################### # 突破 tmp = self.find_breakout(klines) if (tmp != 0): tmp_position = tmp else: tmp = self.find_goback(klines) if (tmp != 0): tmp_position = tmp ''' tmp_cond_flag = self.cond_flag ret = self.find_trend(klines) if (ret == 1): # 趋势 tmp_cond_flag = 1 elif(ret == -1): tmp_cond_flag = -1 else: ret = self.find_breakout(klines) if (ret != 0): #突破 tmp_cond_flag = ret else: ret = self.find_goback(klines) #回归 pass if (self.cond_flag != tmp_cond_flag): self.cond_flag = tmp_cond_flag self.cond_mark_bar = cur_bar if (self.cond_flag != 0 and (cur_bar - self.cond_mark_bar > 3)): if (abs(s_checksum)): if (self.cond_flag > 0 and self.position <= 0): tmp_position = 1 elif (self.cond_flag < 0 and self.position >=0): self.debug("") tmp_position = -1 ''' # ping if (self.position > 0): if (self.entry_price - self.ma5 > abs(self.MAX_LOSS)): self.debug("多止....") tmp_position = 0 elif (cur_bar - self.entry_bar > 120 and abs(s_checksum) < 5): ups = pplib.get_count_of_greater(klines, cur_bar) if (ups > 30): self.debug("时间止") tmp_position = 0 elif (self.ask_price - self.entry_bar > self.MAX_PROFIT and abs(s_checksum) < 5): tmp_position = 0 self.debug("止盈。。。。") elif (self.position < 0): if (self.ma5 - self.entry_price > abs(self.MAX_LOSS)): self.debug("空止....") tmp_position = 0 elif (cur_bar - self.entry_bar > 120 and abs(s_checksum) < 5): downs = pplib.get_count_of_less(klines, cur_bar) if (downs > 30): self.debug("时间止") tmp_position = 0 elif (self.entry_bar - self.ask_price > self.MAX_PROFIT and abs(s_checksum) < 5): tmp_position = 0 self.debug("止盈。。。。") if (tmp_position != self.position): if (tmp_position > 0 and self.check_open_order(1, 20)): self.info5("开仓间隔控制") elif (tmp_position < 0 and self.check_open_order(-1, 20)): self.info5("开仓间隔控制") else: self.set_position(tmp_position)
def on_bar(self, klines): StrategyBase.on_bar(self, klines) #if (self.run_flag == False or self.hh <= 0 or self.ll <= 0): if (self.run_flag == False): return #lt = time.localtime(time.time()) self.cur_bar = self.get_current_minute_bar() lt = self.get_real_time() tmp_position = self.position if (self.inited == False): #init self.init() if (self.inited == False): self.debug("not init") return if (self.trade_dir != TradeDirection.BUYONLY and self.trade_dir != TradeDirection.SELLONLY): return #===============make==================# if (self.position == 0 and self.step == 0): if (self.trade_dir == TradeDirection.BUYONLY): tmp_position = 1 elif (self.trade_dir == TradeDirection.SELLONLY): tmp_position = -1 if (self.position == 0 and self.step == 1): if (self.trade_dir == TradeDirection.BUYONLY): if (self.entry_price - self.ask_price > 10): tmp_position = 1 else: ck_sum10 = pplib.get_checksum(klines, 11, 1) ma3 = pplib.get_average(klines, 4) hh_20 = pplib.get_hest_in_range(klines, 1, 31) if (abs(ck_sum10) < 5 and hh_20 - ma3 > 15): tmp_position = 1 elif (self.trade_dir == TradeDirection.SELLONLY): if (self.entry_price - self.ask_price > 10): tmp_position = -1 else: ck_sum10 = pplib.get_checksum(klines, 11, 1) ma3 = pplib.get_average(klines, 4) ll_20 = pplib.get_lest_in_range(klines, 1, 31) if (abs(ck_sum10) < 5 and ma3 - hh_20 > 15): tmp_position = -1 elif (self.position == 0 and self.step == 2): pass #==================exit===========================# if (self.position == 1): if (self.cur_bar - self.entry_bar > 5 and self.bid_price - self.entry_price >= self.MAX_EARN): tmp_position = 0 ''' my_avg_price = pplib.get_avg_price(klines, 10) if (self.entry_bar > 10 and my_avg_price < self.avg_price): tmp_position = 0 ''' if (self.hh != 0 and self.ask_price >= self.hh): tmp_position = 0 self.hh += 20 if (self.position == -1): if (self.cur_bar - self.entry_bar > 5 and self.entry_price - self.bid_price >= self.MAX_EARN): # 达到最大profit tmp_position = 0 ''' my_avg_price = pplib.get_avg_price(klines, 10) if (self.entry_bar > 10 and my_avg_price > self.avg_price): tmp_position = 0 ''' if (self.ll != 0 and self.ask_price <= self.ll): tmp_position = 0 self.ll -= 20 if (tmp_position != self.position): self.set_position(tmp_position) #===================check==========================# if (self.cur_bar_id - self.start_bar_id > 10): #// 不符合rush条件 my_avg_price = pplib.get_avg_price(klines, 10) if (self.trade_dir == TradeDirection.SELLONLY and my_avg_price > self.mark_price): self.stop() if (self.trade_dir == TradeDirection.BUYONLY and my_avg_price < self.mark_price): self.stop() if (self.position != 0): self.monitor_position(klines)
def on_bar(self, klines): if (self.run_flag == False or self.key_price == 0): return StrategyBase.on_bar(self, klines) if (abs(self.ask_price - self.key_price) > 30 and self.mark_bar == 0): return #lt = self.get_real_time() if (self.mark_bar != 0 and self.init_price_state == 1): #上云盖 cur_bar = self.get_current_minute_bar() hh = pplib.get_hest_in_range(self, 1, 30) #阻击 if (cur_bar - self.mark_bar < 30 and self.position != -1): #c_sum_10 = pplib.get_checksum(klines, 10, 1) #avg_10 = pplib.get_avg_price(klines, 10) if (cur_bar - self.mark_bar < 5 and hh - self.ask_price < 20): avg_6 = pplib.get_avg_price(klines, 6) c_sum_8 = pplib.get_checksum(klines, 8, 1) if (abs(avg_6 - self.key_price) < 10): self.set_position(-1) elif (cur_bar - self.mark_bar > 5 and hh - self.ask_price < 20): avg_6 = pplib.get_avg_price(klines, 6) c_sum_10 = pplib.get_checksum(klines, 10, 1) if (self.key_price - avg_6 > -5 and self.key_price - avg_6 < 14 and abs(c_sum_10) < 4): self.set_position(-1) #顺势 elif (cur_bar - self.mark_bar > 30 and self.position != 1): c_sum_10 = pplib.get_checksum(klines, 10, 1) avg_10 = pplib.get_avg_price(klines, 10) if (avg_10 > self.key_price and abs(c_sum_10) < 4): self.set_position(1) if (self.mark_bar != 0 and self.init_price_state == -1): #上云盖 cur_bar = self.get_current_minute_bar() ll = pplib.get_lest_in_range(self, 1, 30) #阻击 if (cur_bar - self.mark_bar < 30 and self.position != 1): #c_sum_10 = pplib.get_checksum(klines, 10, 1) #avg_10 = pplib.get_avg_price(klines, 10) if (cur_bar - self.mark_bar < 5 and ll - self.ask_price < 20): avg_6 = pplib.get_avg_price(klines, 6) c_sum_8 = pplib.get_checksum(klines, 8, 1) if (abs(avg_6 - self.key_price) < 10 and abs(c_sum_8) < 4): self.set_position(1) elif (cur_bar - self.mark_bar > 5 and ll - self.ask_price < 20): avg_6 = pplib.get_avg_price(klines, 6) c_sum_10 = pplib.get_checksum(klines, 10, 1) if (avg_6 - self.key_price > -5 and avg_6 - self.key_price < 14 and abs(c_sum_10) < 4): self.set_position(1) #顺势 elif (cur_bar - self.mark_bar > 30 and self.position != -1): c_sum_10 = pplib.get_checksum(klines, 10, 1) avg_10 = pplib.get_avg_price(klines, 10) if (avg_10 < self.key_price and abs(c_sum_10) < 4): self.set_position(1) if (self.cur_bar > 10): self.check_key_price(klines)
def on_bar(self, klines): StrategyBase.on_bar(self, klines) lt = self.get_real_time() self.cur_bar_time = klines.iloc[-1].datetime self.cur_bar = self.get_current_minute_bar() #==================================# if (self.init_flag == False): self.init_data(klines) #================test==============# ''' self.debug("cur_bar=%d"%(self.cur_bar)) if (klines.iloc[-1].id - klines.iloc[-33].id > 30): self.debug(">30") else: self.debug("<30") ''' self.avg_overs = self.avg_recorder.bars_of_over(klines) self.avg_unders = self.avg_recorder.bars_of_under(klines) tmp_last_crossdown_bar = self.last_crossdown_bar self.last_crossdown_bar = self.avg_recorder.get_last_cross_under( klines, 2) if (tmp_last_crossdown_bar != self.last_crossdown_bar): kp = KPCrossUnderAvgLine() kp.bar = self.cur_bar kp.score = self.last_crossover_bar tmp_last_crossover_bar = self.last_crossover_bar self.last_crossover_bar = self.avg_recorder.get_last_cross_over( klines, 2) if (tmp_last_crossover_bar != self.last_crossover_bar): kp = KPCrossOverAvgLine() kp.bar = self.cur_bar kp.score = self.last_crossdown_bar self.s_checksum = get_checksum(klines, self.short_term, 1) self.l_checksum = get_checksum(klines, self.long_term, 1) self.hh_long = get_hest_in_range(klines, 0, 60) #长期的高 self.hh_shor = get_hest_in_range(klines, 0, 30) #短期的高 self.ma5 = pplib.get_avg_price(klines, 5) #更新 最高最低 if (klines.iloc[-1].close > self.hest_inday): self.hest_inday = klines.iloc[-1].close self.hest_bar_inday = self.get_current_minute_bar() if (klines.iloc[-1].close < self.lest_inday): self.lest_inday = klines.iloc[-1].close self.lest_bar_inday = self.get_current_minute_bar() if (self.open_price == 0 and self.cur_bar > 1): self.open_price = pplib.get_open_today(klines, 1) if (self.UpdateTradeInterval >= 1 and lt.tm_min % self.UpdateTradeInterval == 0): self.update_trand_indictor() elif (self.UpdateTradeInterval == 0): self.update_trand_indictor() self.count_of_nearest_less = pplib.get_count_of_nearest_less( klines, 5, 50) self.count_of_nearest_greater = pplib.get_count_of_nearest_greater( klines, 5, 50) self.downs_of_this = pplib.get_count_of_less(klines, self.cur_bar) self.overs_of_this = pplib.get_count_of_greater(klines, self.cur_bar) if (self.hest_bar_inday < 60): #近期新高 if (self.Long_HH < self.hest_inday): self.trade_worning = 0 if (lt.tm_min % 4 == 0 and lt.tm_sec == 0): self.my_print("kpi=%d" % (self.score)) #==============================================# self.generate_indicator_event(klines) #==============================================# if (lt.tm_min % 5 == 0 and self.cur_bar > 0): self.on_bar_5(klines) if (self.cur_bar_id % 10 == 0 and self.cur_bar > 0): self.on_bar_10(klines) if (lt.tm_min % 30 == 0 and lt.tm_min == 1 and self.cur_bar > 60): self.on_bar_30(klines)
def on_bar(self, klines): StrategyBase.on_bar(self, klines)
def on_bar(self, klines): StrategyBase.on_bar(self, klines) #if (self.run_flag == False or self.hh <= 0 or self.ll <= 0): self.generate_indicator(klines) if (self.run_flag == False): return if (self.cur_bar < 10): return height8 = pplib.get_height_in_range(klines, 1, 10) cksum = pplib.get_checksum(klines, 10, 1) l_height = pplib.get_height_in_range(klines, 1, 30) l_cksum = pplib.get_checksum(klines, 30, 1) pre_height8 = pplib.get_height_in_range(klines, 4, 4+8) overs = self.get_avg_overs() unders = self.get_avg_unders() greaters = pplib.get_count_of_greater(klines, self.cur_bar) lesses = pplib.get_count_of_less(klines, self.cur_bar) self.greaters = greaters self.lesses = lesses if (height8 < 15 and abs(cksum) < 3): self.debug("height8=%d cksum=%d"%(height8, cksum)) if (l_height< 20 and abs(l_cksum) < 5): self.debug("l_height=%d l_cksum=%d"%(l_height, l_cksum)) ''' if (greaters > 30): self.last_greaters = overs else: if (self.last_greaters > 60): self.last_lesses = lesses if (self.last) self.add_event() if (downs > 30): self.last_downs = downs else: if (self.last_downs>60 and overs > 5): self.last_downs = if (overs > 2 and self.last_downs > 30): pass if (downs > 2 and self.last_overs > 30): pass ''' lt = self.get_real_time() tmp_position = self.position #===============make==================# #一、根据均线上线 if (self.ask_price<self.avg_price and self.position <= 0): kpi_daily = self.get_kpi_daily() cond = False if (unders > 15 and overs/unders > 1.3): cond = True if (kpi_daily > 10): cond = True else: cond = False if (abs(cksum) < 4 and self.avg_price-self.ask_price<20): if (cond == True): self.debug("under avg open......") tmp_position = 1 self.make_type = 1 self.hh = self.ask_price + self.RUSH_PROFIT elif(self.position<0): tmp_position = 0 #有漏洞 elif (self.ask_price > self.avg_price and self.position >=0): kpi_daily = self.get_kpi_daily() cond = False if (unders > 15 and overs/unders > 1.3): cond = True if (kpi_daily < -10): cond = True else: cond = False if (abs(cksum) < 4 and self.ask_price-self.avg_price<20): if (cond == True): self.debug("over avg open......") tmp_position = -1 self.make_type = 1 self.ll = self.ask_price - self.RUSH_PROFIT elif(self.position>0): tmp_position = 0 #有漏洞 #二、根据突破 last_cru_avg = self.get_last_avg_crossdown() last_cro_avg = self.get_last_avg_crossover() kpi_daily = self.get_kpi_daily() if (((last_cro_avg > 38 and last_cro_avg<last_cru_avg) or(kpi_daily > 35 and greaters > 120)) and self.position <=0): if (self.last_greaters > 60 and greaters > 4 and self.cur_bar_id - self.last_greaters_mark_bar < 30): my_kpi = self.get_kpi_myself() dist = self.cur_bar_id - self.last_greaters_mark_bar #短 连续 if (kpi_daily > 35 and greaters>self.last_greaters and dist>3 and dist<10): self.hh = self.ask_price + self.BREAK_PROFIT2 tmp_position = 1 self.make_type = 2 self.debug("连续。。。。") if (tmp_position != 1 and kpi_daily>30 and dist>15): height12 = 100 rang = self.cur_bar_id - self.last_greaters_mark_bar if (rang > 2): height12 = pplib.get_height_in_range(klines, 1, 15) if (height12<14 and abs(cksum)<3): self.hh = self.ask_price + self.BREAK_PROFIT1 tmp_position = 1 self.make_type = 2 self.debug("横盘 等待 break...") if (tmp_position != 1 and self.last_greaters > 240): if (greaters > self.last_greaters and greaters > 5 and greaters < 30): tmp_position = 1 self.make_type = 2 self.debug("长突破。。") if (((last_cro_avg > 38 and last_cro_avg<last_cru_avg) #条件1 or (kpi_daily<-35 and lesses > 120)) and self.position >=0): if (self.last_lesses > 60 and lesses > 4 and self.cur_bar_id - self.last_lesses_mark_bar < 30): my_kpi = self.get_kpi_myself() dist = self.cur_bar_id - self.last_lesses_mark_bar #短 连续 if (kpi_daily < -35 and lesses>self.last_lesses and dist>3 and dist<10): self.ll = self.ask_price - self.BREAK_PROFIT2 tmp_position = -1 self.make_type = 2 self.debug("连续。。。。") if (tmp_position != 1 and kpi_daily>30 and dist>15): height12 = 100 rang = self.cur_bar_id - self.last_greaters_mark_bar if (rang > 2): height12 = pplib.get_height_in_range(klines, 1, 15) if (height12<14 and abs(cksum)<3): self.ll = self.ask_price - self.BREAK_PROFIT1 tmp_position = -1 self.make_type = 2 self.debug("横盘 等待 break...") if (tmp_position != 1 and self.last_lesses > 240): if (lesses > self.last_greaters and greaters > 5 and greaters < 30): tmp_position = -1 self.make_type = 2 self.debug("长突破。。") self.ll = self.ask_price - self.BREAK_PROFIT1 #长 高度小 #三、出现长横盘 #四、开盘来一发 if (self.cur_bar < 30): if (kpi_daily > 35 and self.position <=0): open_p = self.get_open_price() if (open_p-self.ask_price >= 10): tmp_position = 1 self.make_type = 4 self.debug("开盘 enter.....") self.hh = self.ask_price+self.RUSH_PROFIT elif (self.cur_bar>10 and lesses > 9): tmp_position = 1 self.make_type = 4 self.debug("开盘 超时enter.....") self.hh = self.ask_price+self.RUSH_PROFIT if (kpi_daily < -35 and self.position >=0): open_p = self.get_open_price() if (self.ask_price- open_p>= 10): tmp_position = -1 self.make_type = 4 self.ll = self.ask_price - self.RUSH_PROFIT self.debug("开盘 enter.....") elif (self.cur_bar>10 and lesses > 9): tmp_position = -1 self.make_type = 4 self.ll = self.ask_price - self.RUSH_PROFIT self.debug("开盘 超时enter.....") #==================exit===========================# if (self.position > 0 and self.ask_price>=self.hh): tmp_position = 0 self.debug("arrive hh") if (self.position < 0 and self.ask_price<=self.ll): tmp_position = 0 self.debug("arrive ll") if (self.position == 1): if (self.cur_bar - self.entry_bar > 5 and self.bid_price-self.entry_price >= self.MAX_EARN): tmp_position = 0 if (self.position == -1): if (self.cur_bar - self.entry_bar > 5 and self.entry_price-self.bid_price >= self.MAX_EARN):# 达到最大profit tmp_position = 0 if (self.position !=0 and self.make_type == 0): self.monitor_position(klines, 30) elif (self.position !=0 and self.make_type == 1): self.monitor_position(klines, 40) elif (self.position !=0 and self.make_type == 2): self.monitor_position(klines, 50) elif (self.position !=0 and self.make_type == 2): self.monitor_position(klines, 30) #===============================================# #更新放后 if (self.last_greaters>40): if (greaters == 0): self.add_event(RecordEvent.Up, self.last_greaters, self.cur_bar_id, self.cur_bar_time) if (self.cur_bar_id - self.last_greaters_mark_bar < 20 and lesses >5): self.add_event(RecordEvent.DownAfterUp, lesses, self.cur_bar_id, self.cur_bar_time) if (self.last_lesses>40): if (lesses == 0): self.add_event(RecordEvent.Down, self.last_lesses, self.cur_bar_id, self.cur_bar_time) if (self.cur_bar_id - self.last_lesses_mark_bar < 20 and greaters > 5): self.add_event(RecordEvent.UpAfterDown, greaters, self.cur_bar_id, self.cur_bar_time) if (greaters > 30): self.last_greaters = greaters if (greaters > 60): self.last_greaters_mark_bar = self.cur_bar_id if (lesses > 30): self.last_lesses = lesses if (lesses > 60): self.last_lesses_mark_bar = self.cur_bar_id #===================================# if (tmp_position != self.position): if (self.check_order(5)): self.set_position(tmp_position) else: self.debug("间隔检测 false")
def on_bar(self, klines): StrategyBase.on_bar(self, klines) if (self.run_flag == False): return cur_bar = self.get_current_minute_bar() avg_price = get_avg_price(klines, 5) s_checksum = get_checksum(klines, 5, 1) # short l_checksum = get_checksum(klines, 30, 1) # long tmp_position = self.position high = klines.iloc[-1].high low = klines.iloc[-1].low lt = self.get_real_time() #if (lt.tm_min%2==0 and lt.tm_sec==0): # print("[%s] avg_price=%d s_checksum=%d l_checksum=%d "%(self.TAG, avg_price, s_checksum, l_checksum)) #更新数据 if (high >= self.HH2): self.cur_price_flag = 2 self.mark_bar = cur_bar elif (high >= self.HH1): self.cur_price_flag = 1 self.mark_bar = cur_bar elif (low <= self.LL2): self.cur_price_flag = -2 self.mark_bar = cur_bar elif (high <= self.LL1): self.cur_price_flag = -1 self.mark_bar = cur_bar # 到达 if (avg_price > self.HH2): if (self.position < 0 and abs(s_checksum) < self.STD_CHECHSUM): tmp_position = 0 self.debug("[decision] tmp_position=0 (avg_price >self.HH2)") self.trade_dir = TradeDirection.BUYONLY elif (avg_price > self.HH1 and abs(s_checksum) < 5): if (self.position == 0 and abs(s_checksum) < 5): tmp_position = -1 self.debug("[decision] tmp_position=-1 (avg_price > self.HH1)") elif (avg_price < self.LL2): if (self.position == 1 and abs(s_checksum) < 5): tmp_position = 0 self.debug("[decision] tmp_position=0 (avg_price < self.LL2)") self.trade_dir = TradeDirection.SELLONLY elif (avg_price < self.LL1 and abs(s_checksum) < 5): if (self.position <= 0): tmp_position = 1 self.debug("[decision] tmp_position=1 (avg_price < self.LL1)") #出场 #均线到达 if (self.cur_price_flag >= 1 and cur_bar - self.mark_bar > 2): if (self.position > 0 and avg_price > self.HH1 and avg_price < self.HH2): tmp_position = 0 self.debug("[decision] tmp_position=0 (多屏)") elif (self.cur_price_flag <= -1 and cur_bar - self.mark_bar > 2): if (self.position < 0 and avg_price < self.LL1 and avg_price > self.LL2): tmp_position = 0 self.debug("[decision] tmp_position=0 (空瓶)") # #时间过长 if (cur_bar - self.entry_bar >= self.MAX_HOLD and self.position != 0): m_checksum = get_checksum(klines, 10, 1) # short if (self.position > 0 and abs(m_checksum) < 2): tmp_position = 0 self.debug("[decision] tmp_position=0 (多太长)") if (self.position < 0 and abs(m_checksum) < 2): tmp_position = 0 self.debug("[decision] tmp_position=0 (空太长)") # if (tmp_position != self.position): self.set_position(tmp_position)
def on_bar(self, klines): #self.debug("====1111======") #print(self.TAG, self.run_flag) if (self.run_flag == False): return StrategyBase.on_bar(self, klines) lt = self.get_real_time() if (lt.tm_hour == 21 and lt.tm_min < 5): return # update last_crossdown_bar = self.avg_recorder.get_last_cross_under(klines, 2) last_crossover_bar = self.avg_recorder.get_last_cross_over(klines, 2) overs = self.avg_recorder.bars_of_over(klines) downs = self.avg_recorder.bars_of_under(klines) s_checksum = get_checksum(klines, self.short_term, 1) l_checksum = get_checksum(klines, self.long_term, 1) cur_bar = self.get_current_minute_bar() # 弱势 提前 hh_inday, hh_bar_inday = pplib.get_highest_bar_today2( klines) #不用替换_fix ll_inday, ll_bar_inday = pplib.get_lowest_bar_today2(klines) tmp_position = self.position if (last_crossover_bar > 20): self.cross_avg_run_flag = 1 elif (last_crossdown_bar > 20): self.cross_avg_run_flag = -1 else: self.cross_avg_run_flag = 0 if (lt.tm_min % 10 == 0): msg = "[last_crossdown_bar=%d, last_crossover_bar=%d, overs=%d, downs=%d, s_checksum=%d, l_checksum=%d" % ( last_crossdown_bar, last_crossover_bar, overs, downs, s_checksum, l_checksum) self.info(msg) self.debug(msg) #过滤 #中途运行的情况 if (overs + downs < 20): return #开 if (self.position == 0 and lt.tm_hour <= 13): ma10 = pplib.get_avg_price(klines, 10) #用于区间突破 全程在线的一边 if (last_crossover_bar < last_crossdown_bar and last_crossover_bar > 50 and ma10 > self.d_hh_3 and self.position <= 0): self.debug("overs avg and over hh_3") tmp_position = 1 elif (last_crossover_bar > last_crossdown_bar and last_crossdown_bar > 50 and ma10 < self.d_ll_3 and self.position >= 0): self.debug("downs avg and down ll_3") tmp_position = -1 degree = self.manager.get_market_prediction() #degree = #数量 dif = self.avg_price - self.ask_price if (abs(dif) <= 12 and downs / cur_bar < 0.1 and abs(s_checksum) < 5 and degree > 20): if (last_crossover_bar < 15 and degree < 35): tmp_position = 1 print("[%s] 多开 条件1 " % (self.TAG)) if (last_crossdown_bar < 20 and degree > 35): #均线以下 tmp_position = 1 print("[%s] 多开 条件2 " % (self.TAG)) elif (abs(dif) <= 12 and overs / cur_bar < 0.1 and abs(s_checksum) < 5 and degree < -20): if (last_crossdown_bar < 15 and degree > -35): tmp_position = -1 print("[%s] 空开 条件1 " % (self.TAG)) if (last_crossover_bar < 20 and degree < -35): #均线以下 tmp_position = -1 print("[%s] 空开 条件2 " % (self.TAG)) #峰度 if (overs < downs / 5 and degree < -50): if (l_checksum < -20 and last_crossdown_bar < 5): tmp_position = -1 print("[%s] 空开 条件3 " % (self.TAG)) elif (downs < overs / 5 and degree > 35): if (l_checksum > 20 and last_crossover_bar < 5): tmp_position = 1 print("[%s] 多开 条件3 " % (self.TAG)) #趋向 if (overs < downs / 2 and degree < -35 and self.position >= 0): if (self.cross_avg_run_flag == -1 and abs(l_checksum) < 5): #if (self.cross_avg_run_flag == -1)<5): tmp_position = -1 self.debug("趋向 修正") #ups = pplib.get_count_of_greater(klines, 50) #if (ups > 30): elif (downs < overs / 2 and degree > 35 and self.position <= 0): if (self.cross_avg_run_flag == 1 and abs(l_checksum) < 5): tmp_position = 1 self.debug("趋向 修正") #修正 #或者根据degree来修正 #if (cur_bar - hh_bar_inday > 60 and ll_bar_inday < hh_bar_inday and tmp_position >0): if (hh_bar_inday > 60 and tmp_position > 0): tmp_position = 0 print("[%s] degree来修正 " % (self.TAG)) #if (cur_bar - ll_bar_inday > 60 and ll_bar_inday > hh_bar_inday and tmp_position<0): if (ll_bar_inday > 60 and tmp_position < 0): tmp_position = 0 print("[%s] degree来修正 " % (self.TAG)) #===========================================# #*******************************************# # 对应20200601 kpi_daily = self.get_kpi_daily() if (kpi_daily > 35 and self.position <= 0 and overs > downs): if (last_crossdown_bar > 3 and last_crossdown_bar < 30): ups = pplib.get_count_of_greater(klines, 30) if (ups >= 5): tmp_position = 1 self.debug("kpi_daily > 35 and crossdown") if (kpi_daily < -35 and self.position >= 0 and overs < downs): if (last_crossover_bar > 3 and last_crossover_bar < 30): donws = pplib.get_count_of_less(klines, 30) if (downs >= 5): tmp_position = -1 self.debug("kpi_daily < -35 and crossover") #===========================================# #*******************************************# #平 #时间过长 if (self.cur_bar - self.entry_bar >= self.MAX_HOLD and self.position != 0): m_checksum = get_checksum(klines, 10, 1) # short if (self.position > 0 and abs(m_checksum) < 2): ups = pplib.get_count_of_greater(klines, 60) if (ups > 30): tmp_position = 0 print("[%s] 多平 时间过长 " % (self.TAG)) if (self.position < 0 and abs(m_checksum) < 2): downs = pplib.get_count_of_less(klines, 60) if (downs > 30): tmp_position = 0 print("[%s] 空平 时间过长 " % (self.TAG)) if (self.position > 0 and last_crossdown_bar < last_crossover_bar and self.cur_bar - self.entry_bar > 30): ups = pplib.get_count_of_greater(klines, 50) if (ups > 29): tmp_position = 0 # 前高点 if (self.cur_bar - self.entry_bar > 20 and self.position > 0): long_hh = pplib.get_hest_in_range(klines, 0, 60) #长期的高 short_hh = pplib.get_hest_in_range(klines, 0, 30) #短期的高 m_checksum = get_checksum(klines, 20, 1) # short if (long_hh == short_hh and abs(m_checksum) <= 3): tmp_position = 0 print("[%s] 多平 前高点 " % (self.TAG)) if (self.position > 0 and self.ask_price - self.entry_price > 48): tmp_position = 0 self.debug("最大盈...") if (self.position < 0 and last_crossdown_bar > last_crossover_bar and self.cur_bar - self.entry_bar > 30): downs = pplib.get_count_of_less(klines, 50) if (downs > 29): tmp_position = 0 # 前低点 if (self.cur_bar - self.entry_bar > 20 and self.position < 0): long_ll = pplib.get_lest_in_range(klines, 0, 60) #长期的高 short_ll = pplib.get_lest_in_range(klines, 0, 30) #短期的高 m_checksum = get_checksum(klines, 20, 1) # short if (long_ll == short_ll and abs(m_checksum) <= 3): tmp_position = 0 print("[%s] 空平 前低点 " % (self.TAG)) if (self.position < 0 and self.entry_price - self.ask_price > 39): tmp_position = 0 self.debug("最大盈...") if (tmp_position != self.position): if (tmp_position > 0): if (self.check_open_order(1, 10)): self.set_position(tmp_position) elif (tmp_position < 0): if (self.check_open_order(-1, 10)): self.set_position(tmp_position) else: self.set_position(tmp_position)