Beispiel #1
0
    def find_break(self, klines):
        #===============================================# 
        #边界
        if (self.cur_bar < 20):
            return
        length = 50
        if (self.cur_bar < 50):
            length = self.cur_bar
        bid, self.count_of_less_near = pplib.get_count_of_nearest_less(klines, 5, length)
        bid, self.count_of_greater_near = pplib.get_count_of_nearest_greater(klines, 5, length)

        if (self.count_of_less_near>=50):
            greaters = pplib.get_count_of_greater(klines, length)
            if (greaters > 2):
                self.valley_recorder.add(self.count_of_less_near, self.cur_bar_id, self.cur_bar_time)
            
        if (self.count_of_greater_near >= 50): 
            lesses = pplib.get_count_of_less(klines, length)
            if (lesses > 2):
                self.valley_recorder.add(self.count_of_less_near, self.cur_bar_id, self.cur_bar_time)

        std_count = 120
        if (self.cur_bar < 100):
            std_count = self.cur_bar

        if (self.count_of_less_near > std_count):
            self.last_fit_less_bar = self.get_current_minute_bar()
            self.debug("find break down")
        if (self.count_of_greater_near > std_count):
            self.last_fit_greater_bar = self.get_current_minute_bar()
            self.debug("find break up")
Beispiel #2
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)
Beispiel #3
0
    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)
Beispiel #4
0
    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")
Beispiel #5
0
    def generate_hh_ll(self, klines):
        #开仓
        #排列
        swing_hh = self.hh
        swing_ll = self.ll
        hh_msg = "change swing_hh "
        ll_msg = "change swing_ll"
        if (self.m_size > 0 and self.w_size > 0):
            m_ret = self.check_open_queue("m_opens", self.m_opens)
            w_ret = self.check_open_queue("w_opens", self.w_opens)
            #swing_hh = self.m_opens.loc[0].high + 40
            #swing_ll = self.w_opens.loc[0].low - 40
            if (m_ret == 1 and w_ret == 1):
                #几浪
                if (self.m_size > 3):
                    swing_hh = self.m_opens.loc[0].high + 2
                    swing_ll = swing_hh - 30
                elif (self.m_size > 2):
                    swing_hh = self.m_opens.loc[0].high + 10
                    swing_ll = pplib.get_lest_in_range(klines, 1, 51)
                elif (self.m_size > 1):
                    swing_hh = self.m_opens.loc[0].high + 20
                    swing_ll = pplib.get_lest_in_range(klines, 1, 31)
                else:
                    swing_hh = self.m_opens.loc[0].high + 38
                    swing_ll = pplib.get_lest_in_range(klines, 1, 21)
                hh_msg = "排列多"
                ll_msg = "排列多"
            elif (m_ret == -1 and w_ret == -1):
                #几浪
                if (self.w_size > 3):
                    swing_ll = self.m_opens.loc[0].low - 2
                    swing_hh = swing_ll + 30
                elif (self.w_size > 2):
                    swing_ll = self.m_opens.loc[0].low - 10
                    swing_hh = pplib.get_hest_in_range(klines, 1, 51)
                elif (self.w_size > 1):
                    swing_ll = self.m_opens.loc[0].low - 20
                    swing_hh = pplib.get_hest_in_range(klines, 1, 31)
                else:
                    swing_ll = self.m_opens.loc[0].low - 38
                    swing_hh = pplib.get_hest_in_range(klines, 1, 21)
                hh_msg = "排列空"
                ll_msg = "排列空"
            else:
                #未突破
                hh_msg = "找swing_high"
                ll_msg = "找swing_low"
                range_heigh_8 = pplib.get_height_in_range(klines, 1, 8)
                l_cksum = pplib.get_checksum(klines, 10, 1)
                avg_price = pplib.get_average(klines, 6)
                if (self.w_opens.loc[0].lcount > 60):
                    #突破
                    '''
                    swing_hh = self.w_opens.loc[0].close + 20
                    if(self.w_size > 2):
                        swing_ll = self.w_opens.loc[0].low-15
                    if (self.w_size >= 1):
                        swing_ll = self.w_opens.loc[0].low-25
                    '''
                    if (abs(avg_price - self.w_opens.loc[0].low) < 3
                            and range_heigh_8 < 15 and l_cksum < 0
                            and l_cksum > -15):
                        swing_ll = avg_price
                        ll_msg = "在上一个swing_low附近形成震荡"
                    #突破
                    else:
                        #偏空
                        if (self.w_size > 2):
                            swing_ll = self.w_opens.loc[0].low - 8
                            ll_msg = "w_size>2 and ll=swing_low - 8"
                        if (self.w_size >= 1):
                            swing_ll = self.w_opens.loc[0].low - 15
                            ll_msg = "w_size>2 and ll=swing_low - 15"
                        if (self.get_kpi_daily() > 10):
                            swing_ll += int(self.get_kpi_daily() / 10) + 1
                            ll_msg += "调整%d" % (
                                int(self.get_kpi_daily() / 10) + 1)
                elif (self.w_opens.loc[0].lcount < 30
                      and self.w_opens.loc[0].lcount > 60):
                    if (avg_price < self.w_opens.loc[0].close - 8):
                        swing_hh = self.w_opens.loc[0].close
                        swing_ll = self.w_opens.loc[0].close - 28
                        ll_msg = "swing_low 大小桥 ll下移21"
                        hh_msg = "swing_low 大小桥 low变high 阻变支"
                    else:
                        swing_ll = self.w_opens.loc[0].close
                        ll_msg = "swing_low 大小桥 使用swing_low"
                    #突破跟进

                #突破
            if (self.m_opens.loc[0].lcount > 60):
                #突破
                '''
                    swing_hh = self.w_opens.loc[0].close + 20
                    if(self.w_size > 2):
                        swing_ll = self.w_opens.loc[0].low-15
                    if (self.w_size >= 1):
                        swing_ll = self.w_opens.loc[0].low-25
                    '''
                if (abs(avg_price - self.m_opens.loc[0].high) < 3
                        and range_heigh_8 < 15 and l_cksum > 0
                        and l_cksum < 15):
                    swing_hh = avg_price
                    hh_msg = "swing_high 使用swing_high附件avg_price"
                #突破
                else:
                    #偏空
                    if (self.m_size > 2):
                        swing_hh = self.m_opens.loc[0].high + 8
                        hh_msg = "m_size > 2 swing_high0+8"
                    if (self.m_size >= 1):
                        swing_hh = self.m_opens.loc[0].high + 15
                        hh_msg = "m_size >= 1 swing_high0+15"
                    if (self.get_kpi_daily() < -10):
                        swing_hh += int(self.get_kpi_daily() / 10) + 1
                        hh_msg += "调整%d" % (int(self.get_kpi_daily() / 10) + 1)
            elif (self.m_opens.loc[0].lcount < 30
                  and self.m_opens.loc[0].lcount > 60):
                if (avg_price > self.m_opens.loc[0].close + 5):
                    swing_ll = self.m_opens.loc[0].close - 5
                    swing_hh = self.m_opens.loc[0].close + 25
                    hh_msg = "swing_high 上移21"
                    ll_msg = "swing_high0-5 变ll"
                else:
                    swing_hh = self.m_opens.loc[0].high
                    hh_msg = "大小乔 使用swing_high"
        #
        elif (self.m_size > 0):
            #强升
            m_ret = self.check_open_queue("m_opens", self.m_opens)
            if (m_ret == 1 and self.cur_bar < 110
                    and self.get_kpi_daily() > 30):
                ll_10 = pplib.get_lest_in_range(klines, 1, 11)
                swing_ll = ll_10
                ll_msg = "强升,ll=ll_10"

        elif (self.w_size > 0):
            #强降
            m_ret = self.check_open_queue("w_opens", self.m_opens)
            if (m_ret == -1 and self.cur_bar < 110
                    and self.get_kpi_daily() < -30):
                hh_10 = pplib.get_hest_in_range(klines, 1, 11)
                swing_hh = hh_10
                hh_msg = "强降,hh=hh_10"
        elif (self.cur_bar > 50):
            avg_price = pplib.get_average(klines, 5)
            open_p = self.get_open_price()
            if (avg_price > open_p):
                swing_ll = pplib.get_lest_in_range(klines, 1, 11)
                ll_msg = "流畅上升,ll=ll_10"
            elif (open_p > avg_price):
                swing_hh = pplib.get_hest_in_range(klines, 1, 11)
                hh_msg = "流畅下降,hh=hh_10"
        elif (self.cur_bar < 50):
            ups = pplib.get_count_of_greater(klines, self.cur_bar)
            downs = pplib.get_count_of_less(klines, self.cur_bar)
            if (ups > self.ups_at_open):
                self.ups_at_open = ups
            if (downs > self.downs_at_open):
                self.downs_at_open = downs
            if (self.cur_bar > 25 and self.downs_at_open < 10):
                avg_price = pplib.get_average(klines, 9)
                ll_msg = "未形成swing前强升,ll=lavg9"
                swing_ll = avg_price
            if (self.cur_bar > 25 and self.downs_at_open < 10):
                avg_price = pplib.get_average(klines, 9)
                hh_msg = "未形成swing前强降,hh=avg9"
                swing_hh = avg_price
        if (self.hh == 0):
            swing_hh = self.get_open_price + self.get_atr_daily()
            hh_msg = "hh=0, hh=open+atr"
        elif (self.hh < self.ask_price - 10):
            #突破
            avg_price = pplib.get_average(klines, 15)
            if (avg_price > self.hh + 5):
                swing_ll = self.hh
                swing_hh = self.ask_price + 25
                hh_msg = "逃出区间, hh=cur_price+25"
                ll_msg = "逃出区间, ll = last_hh"

        if (self.ll == 0):
            swing_ll = self.get_open_price - self.get_atr_daily()
            ll_msg = "ll = 0, ll=open-atr"
        elif (self.ll > self.ask_price + 10):
            avg_price = pplib.get_average(klines, 15)
            if (avg_price < self.ll - 5):
                swing_hh = self.ll
                swing_ll = self.ask_price - 23
                hh_msg = "逃出区间, hh=swing+25"
                ll_msg = "逃出区间, ll = cur_price-23"

        if (swing_hh != self.hh):
            self.change_hh(swing_hh, hh_msg)
        if (swing_ll != self.ll):
            self.change_ll(swing_ll, ll_msg)
    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)