Ejemplo n.º 1
0
    def test_entry_cancel(self):
        bitmex = BitMex()
        bitmex.demo = False

        # 전처리 모든 포지션 시장가 해제
        # bitmex.close_all()
        price = bitmex.get_market_price()

        id = "Long"
        # bitmex.entry(id, True, 1, limit=price)

        get_position_size = bitmex.get_position_size()
        print('get_position_size: %s' % get_position_size)

        get_position_avg_price = bitmex.get_position_avg_price()
        print('get_position_avg_price: %s' % get_position_avg_price)

        get_position = bitmex.get_position()
        print('get_position: %s' % get_position)

        get_margin = bitmex.get_margin()
        print('get_margin: %s' % get_margin)
Ejemplo n.º 2
0
    def strategy(self, open, close, high, low, volume):
        lot = self.exchange.get_lot()

        # for test lot
        lot = int(round(lot / 50))
        bitmex = BitMex(threading=False)
        price = bitmex.get_market_price()
        position_avg_price = bitmex.get_position_avg_price()

        # variants settings
        rsi2_len = self.input('length', int, 2)
        rsi50_len = self.input('length50', int, 50)
        rsi2 = rsi(close, rsi2_len)
        rsi50 = rsi(close, rsi50_len)

        factor = self.input('factor', int, 3)
        period = self.input('period', int, 7)
        factor2 = self.input('factor2', int, 20)
        period2 = self.input('period2', int, 7)

        resolution = self.input(defval=1, title="resolution",
                                type=int)  # defval 변경, 예) 5분 --> 5
        source = self.exchange.security(str(resolution) +
                                        'm')  # def __init__  비교
        supertrenddf = supertrend(source, factor, period)
        supertrenddf2 = supertrend(source, factor2, period2)

        print('supertrenddf:%s' % supertrenddf)
        print('supertrenddf2:%s' % supertrenddf2)

        fast_len = self.input('fast_len', int, 5)
        half_len = self.input('half_len', int, 50)
        slow_len = self.input('slow_len', int, 200)

        fast_sma = sma(close, fast_len)
        half_sma = sma(close, half_len)
        slow_sma = sma(close, slow_len)

        # conditions
        sma_long = over(fast_sma[-1], slow_sma[-1])
        sma_short = under(fast_sma[-1], slow_sma[-1])

        super_long = over(close[-1], supertrenddf['SuperTrend'][-1])
        super_short = under(close[-1], supertrenddf['SuperTrend'][-1])
        supertrendtrend = supertrenddf['Trend'][-1]

        super2_long = over(close[-1], supertrenddf2['SuperTrend'][-1])
        super2_short = under(close[-1], supertrenddf2['SuperTrend'][-1])
        supertrendtrend2 = supertrenddf2['Trend'][-1]

        super_centerline = (supertrenddf['SuperTrend'][-1] +
                            supertrenddf2['SuperTrend'][-1]) / 2

        rsi2_overbought = over(rsi2[-1], 95)
        rsi2_oversold = under(rsi2[-1], 5)

        rsi50_over = over(rsi50[-1], 50)
        rsi50_under = under(rsi50[-1], 50)

        price_under = under(price, half_sma[-1])
        price_over = over(price, half_sma[-1])

        half_before = over(close[-1], half_sma[-1])
        half_after = under(close[-1], half_sma[-1])

        # show infomations

        logger.info('price: %s' % price)

        logger.info('fast_sma[-1]: %s' % fast_sma[-1])
        logger.info('slow_sma[-1]: %s' % slow_sma[-1])

        logger.info('sma_long: %s' % sma_long)
        logger.info('sma_short: %s' % sma_short)

        logger.info('super_long: %s' % super_long)
        logger.info('super_short: %s' % super_short)
        logger.info('sma_trend: %s\n' % supertrendtrend)

        logger.info('supertrend value:%s' % supertrenddf['SuperTrend'][-1])
        logger.info('supertrend Upper Band:%s' %
                    supertrenddf['Upper Band'][-1])
        logger.info('supertrend Lower Band:%s' %
                    supertrenddf['Lower Band'][-1])
        logger.info('supertrenddf[Trend][-1]:%s' % supertrenddf['Trend'][-1])
        logger.info('supertrenddf[TSL][-1]:%s' % supertrenddf['TSL'][-1])
        logger.info('supertrenddf[ATR][-1]:%s\n' % supertrenddf['ATR'][-1])

        logger.info('supertrend2 value:%s' % supertrenddf2['SuperTrend'][-1])
        logger.info('supertrend2 Upper Band:%s' %
                    supertrenddf2['Upper Band'][-1])
        logger.info('supertrend2 Lower Band:%s' %
                    supertrenddf2['Lower Band'][-1])
        logger.info('supertrenddf2[Trend][-1]:%s' % supertrenddf2['Trend'][-1])
        logger.info('supertrenddf2[TSL][-1]:%s' % supertrenddf2['TSL'][-1])
        logger.info('supertrenddf2[ATR][-1]:%s\n' % supertrenddf2['ATR'][-1])

        logger.info(
            'supertrenddf[SuperTrend][-1]:%s + supertrenddf2[SuperTrend][-1]:%s '
            %
            (supertrenddf['SuperTrend'][-1], supertrenddf2['SuperTrend'][-1]))
        logger.info('super_centerline: %s' % super_centerline)

        logger.info('rsi2[-1 ]%s' % rsi2[-1])
        logger.info('rsi50[-1 ]%s' % rsi50[-1])
        logger.info('rsi2_oversold: %s' % rsi2_oversold)
        logger.info('rsi2_overbought: %s' % rsi2_overbought)

        logger.info('price_under: %s' % price_under)
        logger.info('price_over: %s' % price_over)

        logger.info('half_before: %s' % half_before)
        logger.info('half_after: %s' % half_after)

        logger.info('get_whichpositon(): %s' % bitmex.get_whichpositon())
        logger.info('position_size(): %s' % bitmex.get_position_size())

        # entry
        if super2_long:
            logger.info(
                '+ + + + + LONG + + + + + LONG + + + + + LONG + + + + + ')
            if bitmex.get_whichpositon(
            ) is None:  # and (not supertrendtrend and supertrendtrend2) and rsi2_overbought:
                logger.info('postion condition > None')
                if bitmex.get_open_order('Short'):
                    self.exchange.cancel('Short')
                self.exchange.entry("Long",
                                    True,
                                    lot,
                                    limit=math.ceil(super_centerline),
                                    post_only=True)

            elif bitmex.get_whichpositon() == 'LONG':
                logger.info('postion condition  > LONG')
                if supertrendtrend and supertrendtrend2 and rsi2_oversold:  # closing
                    logger.info('postion condition  > LONG > Closing')
                    self.exchange.order("Long",
                                        False,
                                        abs(bitmex.get_position_size()),
                                        limit=price + 2.5,
                                        post_only=True)
                elif rsi2_overbought:  # add more entry
                    logger.info('postion condition  > LONG > Rsi2 overbout')
                    self.exchange.entry("LongAdd",
                                        True,
                                        lot,
                                        limit=price - 0.5,
                                        post_only=True)
                elif super_short:  # stop loss
                    logger.info(
                        'postion condition  > LONG > super_short(stop loss)')
                    self.exchange.entry("Long", True, lot)
                    self.exchange.entry("LongAdd", True, lot)
                else:
                    logger.info('postion condition  > LONG > else')
                    self.exchange.order("Long",
                                        False,
                                        abs(bitmex.get_position_size()),
                                        limit=price + 10,
                                        post_only=True)

            elif bitmex.get_whichpositon() == 'SHORT':
                logger.info('cancel SHORT on long trend')
                # self.exchange.cancel_all()
                self.exchange.close_all()
                self.exchange.close_all()
            else:

                logger.info('Super Shot --> Else')

        if super2_short:
            logger.info(
                '- - - - - SHORT - - - - - SHORT - - - - - SHORT - - - - - ')
            if bitmex.get_whichpositon(
            ) is None:  #and rsi2_overbought and price_over:
                logger.info('postion condition > None')
                if bitmex.get_open_order('Long'):
                    self.exchange.cancel('Long')
                self.exchange.entry("Short",
                                    False,
                                    lot,
                                    limit=math.floor(super_centerline),
                                    post_only=True)

            elif bitmex.get_whichpositon() == 'SHORT':
                logger.info('postion condition  > SHORT')

                if price_under:  # closing
                    logger.info(
                        'postion condition  > SHORT > price_under(closing)')
                    self.exchange.order("Short",
                                        True,
                                        abs(bitmex.get_position_size()),
                                        limit=price - 2.5,
                                        when=price_under,
                                        post_only=True)
                elif rsi2_oversold:  # add more entry
                    logger.info(
                        'postion condition  > SHORT > rsi2_oversold(add more entry)'
                    )
                    self.exchange.entry("ShortAdd",
                                        False,
                                        lot,
                                        limit=price - 0.5,
                                        post_only=True)
                elif super_long:  # stop loss
                    logger.info(
                        'postion condition  > SHORT > super_short(stop loss)')
                    self.exchange.entry("Short", True, lot)
                    self.exchange.entry("ShortAdd", True, lot)
                else:
                    logger.info('postion condition  > SHORT > else')
                    self.exchange.order("Short",
                                        True,
                                        abs(bitmex.get_position_size()),
                                        limit=price - 10,
                                        post_only=True)

            elif bitmex.get_whichpositon() == 'LONG':
                logger.info('cancel LONG on short trend')
                self.exchange.close_all()
            else:

                logger.info('Super Shot --> Else')

        self.dealcount += 1

        diff = (abs(bitmex.get_balance() - abs(self.prebalance)))

        realised_pnl = bitmex.get_margin()['realisedPnl']

        logger.info('dealcount:%s' % self.dealcount)
        logger.info('prebalance():%s' % self.prebalance)
        logger.info('bitmex.get_balance():%s' % bitmex.get_balance())
        logger.info('diff:%s' % diff)
        logger.info('realised_pnl:%s' % realised_pnl)

        logger.info('--------------------------------------------------')