예제 #1
0
    def on_bar(self, klines):
        self.cur_bar_time = klines.iloc[-1].datetime
        self.cur_bar_id = klines.iloc[-1].id
        if (self.open_bar_id == 0):
            self.open_bar_id = self.get_open_bar(klines)
        #print(self.TAG, self.cur_bar_time)
        if (self.bid_price == 0):
            self.bid_price = klines.iloc[-1].close
        if (self.ask_price == 0):
            self.ask_price = klines.iloc[-1].close
        if (self.avg_price == 0):
            self.avg_price = klines.iloc[-1].close

        self.cur_bar = self.get_current_minute_bar()

        self.update_kclock()

        if (self.cur_bar_id % 6 == 6 and self.cur_bar > 0):
            self.on_bar6(klines)

        if (self.cur_bar_id % 10 == 6 and self.cur_bar > 0):
            self.on_bar10(klines)

        if (self.cur_bar_id % 30 == 6 and self.cur_bar > 0):
            self.on_bar30(klines)

        if (self.backtest_flag == True):
            cur_price = klines.iloc[-1].close
            if (abs(self.ask_price - cur_price) > 2):
                self.ask_price = cur_price
                leng = len(klines)
                if (leng > 30):
                    self.avg_price = pplib.get_avg_price(klines, 30)
            if (abs(self.bid_price - cur_price) > 2):
                self.bid_price = cur_price
예제 #2
0
 def on_bar_10(self, klines):
     self.range_height_30 = pplib.get_height_in_range(klines, 1, 31)
     if (self.range_height_30 < 20):
         hh = pplib.get_hest_in_range(klines, 1, 30)
         ll = pplib.get_lest_in_range(klines, 1, 30)
         bars = pplib.get_continus_bars_in_height(klines, 1, self.cur_bar,
                                                  hh, ll)  #在区间范围内的bar数
         range_inday = self.hest_inday - self.lest_inday
         if (range_inday > 30):
             # todo: 排除个别
             param = ParamUnion()
             param.put_param("bars", bars)
             param.put_param("height", self.range_height_30)
             avg_10 = pplib.get_avg_price(klines, 11)
             if (self.hest_inday - avg_10 < (avg_10 - self.lest_inday)):
                 self.manager.drive_event(self.TAG, StgEvent.LowRangeAtTop,
                                          range_inday)
             else:
                 self.manager.drive_event(self.TAG,
                                          StgEvent.LowRangeAtBottom,
                                          range_inday)
         else:
             self.manager.drive_event(self.TAG, StgEvent.LowRange,
                                      range_inday)
     self.generate_price_action()
예제 #3
0
 def find_break_day(self, klines):
     if (self.cur_bar < 30):
         return
     avg_p = pplib.get_avg_price(klines, 30)
     if (abs(self.get_kpi_daily()) > 35):
         avg_p = pplib.get_avg_price(klines, 10)
     if (avg_p > self.hh_3day):
         self.change_trade_mode(0, 1, 1)
         '''
         self.trade_hh += 31
         
         if (self.position <= 0):
             self.debug("hh breakout.....")
             
             self.set_position(1)
             self.market_mode = 1
             self.trade_hh += 30
         '''
     elif (avg_p < self.ll_3day):
         self.change_trade_mode(0, 1, -1)
         '''
예제 #4
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)
예제 #5
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)
예제 #6
0
    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)
예제 #7
0
    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)
예제 #8
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)
예제 #9
0
    def make_long(self, klines):
        lt = time.localtime(time.time())
        cur_bar = self.get_current_minute_bar()
        if (self.preset_HH == 0 and lt.tm_hour == 9 and lt.tm_min < 30):
            return
        if (self.preset_HH == 0):
            self.preset_HH = pplib.get_lest_in_range(klines, 1, cur_bar)

        end_bar = cur_bar
        if (end_bar > 120):
            end_bar = 120
        hh, hh_bar = pplib.get_hest_in_range_fix(klines, 1, end_bar)
        ll, ll_bar = pplib.get_lest_in_range_fix(klines, 1, end_bar)
        ma5 = pplib.get_avg_price(self, 6)

        tmp_position = self.position

        #零段
        if (cur_bar < 60):
            open_inday = pplib.get_open_today(klines, 0)
            if (hh - open_inday > open_inday - ll):
                self.step = 1
        #一段
        if (abs(ma5 - self.preset_HH) < 15 and self.cur_bar_id - hh_bar < 20):
            lcsum = pplib.get_checksum(klines, 10, 1)
            if (abs(lcsum) <= 5):
                tmp_position = -1
                self.setup = 1

        degree = 0
        indictor = self.manager.get_indicotor()
        if (indictor is not None):
            degree = indictor.get_degree()

        #二段
        if (cur_bar > 60 and self.step == 1 and lt.tm_hour < 14):
            '''
            degree = 0
            indictor = self.manager.get_indicotor()
            if (indictor is not None):
                degree = indictor.get_degree()
            '''
            swinghighs = pplib.SwingHighPriceSeries(klines, 20, 1)
            swinglows = pplib.SwingHighPriceSeries(klines, 30, 1)

            swing_h = 0
            swing_h_len = len(swinghighs)
            if (swing_h_len > 0):
                swing_h = swinghighs[swing_h_len - 1]["price"]
            swing_l = 0
            swing_l_len = len(swinglows)
            if (swing_l_len > 0):
                swing_l = swinglows[swing_l_len - 1]["price"]
            else:
                swing_l = ll - 10
            #底
            if (hh - swing_l < 20):
                swing_l = (swing_l + open_inday) / 2

            if (self.position < 0 and cur_bar - self.entry_bar > 20
                    and self.cur_price > swing_h):
                tmp_position = 0

            #时间
            scsum = pplib.get_checksum(klines, 10, 1)
            if (self.position < 0 and self.cur_price <= swing_l):
                tmp_position = 1
                self.setup = 2
            elif (self.position < 0 and cur_bar - self.entry_bar > 25
                  and degree > 50 and abs(self.cur_price - swing_l) < 10
                  and scsum < 3):
                tmp_position = 0
            elif (self.position < 0 and cur_bar - self.entry_bar > 40):
                if (abs(scsum) < 5):
                    tmp_position = 1
                    self.setup = 2
            if (degree > 10 and self.position == 0
                    and abs(self.cur_price - swing_l) < 10 and scsum < 3):
                tmp_position = 1
                self.setup = 2

            if (self.position > 0 and cur_bar - self.entry_bar > 20
                    and self.cur_price > swing_h):
                tmp_position = 0

        #三段
        if (lt.tm_hour >= 13 or self.step == 2 and degree > 10):
            lcsum = pplib.get_checksum(klines, 10, 1)
            lesses = pplib.get_count_of_nearest_less(klines, 10, 100)
            if (abs(lcsum) < 5 and lesses > 30):
                tmp_position = 1

        if (self.position > 0
                and self.entry_price - self.cur_price > self.MAX_LOSS):
            tmp_position = 0
        if (self.position < 0
                and self.cur_price - self.entry_price > self.MAX_LOSS):
            tmp_position = 0

        #ping
        if (tmp_position != self.position):
            self.set_position(tmp_position)
예제 #10
0
    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)
예제 #11
0
    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)