Example #1
0
    def test_entry_cancel(self):
        bitmex = BitMex()
        bitmex.demo = True

        # 前処理
        bitmex.close_all()

        price = bitmex.get_market_price()

        # 注文、キャンセルの試験
        id = "Long"
        bitmex.entry(id, True, 1, limit=price-1000)
        assert bitmex.get_open_order(id) is not None
        bitmex.cancel(id)
        assert bitmex.get_open_order(id) is None

        # 注文の更新
        id = "Long"
        bitmex.entry(id, True, 1, limit=price-1000)
        order = bitmex.get_open_order(id)
        assert order["orderQty"] == 1
        assert order["price"] == price-1000
        bitmex.entry(id, True, 2, limit=price-900)
        order = bitmex.get_open_order(id)
        assert order["orderQty"] == 2
        assert order["price"] == price-900
        bitmex.cancel(id)
        assert bitmex.get_open_order(id) is None
Example #2
0
    def strategy(self, open, close, high, low, volume):
        self.start += 1

        lot = self.exchange.get_lot()
        # for test lot
        lot = int(round(lot / 10))
        bitmex = BitMex(threading=False)
        price = bitmex.get_market_price()

        sma_base_l = self.input('sma_short_len', int, 200)

        resolution = self.input(
            defval=1, title="resolution", type=int
        )  # defval 변경, 예) 5분 --> 5, 'm' or 1시간  1, 'h', 1Day 1, 'd'
        source = self.exchange.security(str(resolution) +
                                        'h')  # def __init__  비교
        logger.info('source: %s' % source)

        series_high = source['high'].values
        series_low = source['low'].values

        fb100 = last(highest(series_high, 1))  # 1시간 1, 1D의 경우는 resolution도 변경
        fb0 = last(lowest(series_low, 1))

        logger.info('resolution: %s' % resolution)
        logger.info('fb100_resol: %s' % fb100)
        logger.info('fb0_resol: %s' % fb0)

        # for test
        # fb100 = price + 15
        # fb0 = price - 15

        # 최근 1시간을 본봉단위로 획득
        # fibo_l = self.input('length', int, 1440)  # 1Day = 60min * 24hr
        # fibo_l = self.input('length', int, 60)  # 1Day = 60min * 24hr
        # fibo100 = last(highest(high, fibo_l))
        # fibo0 = last(lowest(low, fibo_l))

        fb62 = math.ceil((fb100 - fb0) * 0.618 + fb0)
        fb38 = math.ceil((fb100 - fb0) * 0.382 + fb0)
        fb50 = math.ceil((fb100 - fb0) / 2 + fb0)

        fb200 = math.ceil((fb100 - fb0) * 1.0 + fb100)
        fb162 = math.ceil((fb100 - fb0) * 0.618 + fb100)
        fb138 = math.ceil((fb100 - fb0) * 0.382 + fb100)

        fb038 = math.ceil(fb0 - (fb100 - fb0) * 0.382)
        fb062 = math.ceil(fb0 - (fb100 - fb0) * 0.618)
        fb0100 = math.ceil(fb0 - (fb100 - fb0) * 1.00)

        logger.info('self.start: %s' % self.start)

        if self.start == 1:
            # short position
            self.exchange.order("Short200",
                                False,
                                lot * 3,
                                limit=fb200,
                                post_only=True)
            self.exchange.order("Short162",
                                False,
                                lot * 2,
                                limit=fb162,
                                post_only=True)
            self.exchange.order("Short138",
                                False,
                                lot * 1,
                                limit=fb138,
                                post_only=True)
            self.exchange.order("Short100",
                                False,
                                lot * 1,
                                limit=fb100,
                                post_only=True)

            # long position
            self.exchange.order("Long0",
                                True,
                                lot * 1,
                                limit=fb0,
                                post_only=True)
            self.exchange.order("Long038",
                                True,
                                lot * 1,
                                limit=fb038,
                                post_only=True)
            self.exchange.order("Long062",
                                True,
                                lot * 2,
                                limit=fb062,
                                post_only=True)
            self.exchange.order("Long0100",
                                True,
                                lot * 3,
                                limit=fb0100,
                                post_only=True)

        Long0 = bitmex.get_open_order("Long0")
        Long038 = bitmex.get_open_order("Long038")
        Long062 = bitmex.get_open_order("Long062")
        Long0100 = bitmex.get_open_order("Long0100")
        Short200 = bitmex.get_open_order("Short200")
        Short162 = bitmex.get_open_order("Short162")
        Short138 = bitmex.get_open_order("Short138")
        Short100 = bitmex.get_open_order("Short100")
        #
        #
        # logger.info('Long0: %s' % Long0)
        # logger.info('Long038: %s' % Long038)
        # logger.info('Long062: %s' % Long062)
        # logger.info('Long0100: %s' % Long0100)
        # logger.info('Short200: %s' % Short200)
        # logger.info('Short162: %s' % Short162)
        # logger.info('Short138: %s' % Short138)
        # logger.info('Short100: %s' % Short100)
        #
        # logger.info('(Long0 is None): %s' % (Long0 is None))

        # entry order
        # long position
        self.exchange.order("Long0",
                            True,
                            lot * 1,
                            limit=fb0,
                            when=(Long0 is None),
                            post_only=True)
        self.exchange.order("Long038",
                            True,
                            lot * 1,
                            limit=fb038,
                            when=(Long038 is None),
                            post_only=True)
        self.exchange.order("Long062",
                            True,
                            lot * 2,
                            limit=fb062,
                            when=(Long0100 is None),
                            post_only=True)
        self.exchange.order("Long0100",
                            True,
                            lot * 3,
                            limit=fb0100,
                            when=(Long0100 is None),
                            post_only=True)

        # short position
        self.exchange.order("Short200",
                            False,
                            lot * 3,
                            limit=fb200,
                            when=(Short200 is None),
                            post_only=True)
        self.exchange.order("Short162",
                            False,
                            lot * 2,
                            limit=fb162,
                            when=(Short162 is None),
                            post_only=True)
        self.exchange.order("Short138",
                            False,
                            lot * 1,
                            limit=fb138,
                            when=(Short138 is None),
                            post_only=True)
        self.exchange.order("Short100",
                            False,
                            lot * 1,
                            limit=fb100,
                            when=(Short100 is None),
                            post_only=True)

        # win order
        if price <= fb0:
            self.exchange.order("Long0_w",
                                False,
                                lot * 1,
                                limit=fb38,
                                stop=fb0)
            logger.info('rice <= fb0: %s' % fb0)
        if price <= fb038:
            self.exchange.order("Long038_w",
                                False,
                                lot * 1,
                                limit=fb0,
                                stop=fb038)
        if price <= fb062:
            self.exchange.order("Long062_w",
                                False,
                                lot * 2,
                                limit=fb038,
                                stop=fb062)
        if price <= fb0100:
            self.exchange.order("Long0100_w",
                                False,
                                lot * 3,
                                limit=fb062,
                                stop=fb0100)

        if price >= fb100:
            logger.info('price >= fb100: %s' % fb100)
            self.exchange.order("Short100_w",
                                True,
                                lot * 1,
                                limit=fb62,
                                stop=fb100)
        if price >= fb138:
            self.exchange.order("Short138_w",
                                True,
                                lot * 1,
                                limit=fb100,
                                stop=fb138)
        if price >= fb162:
            self.exchange.order("Short162_w",
                                True,
                                lot * 2,
                                limit=fb138,
                                stop=fb162)
        if price >= fb200:
            self.exchange.order("Short200_w",
                                True,
                                lot * 3,
                                limit=fb162,
                                stop=fb200)

        # logger.info('bitmex.get_margin():%s' % bitmex.get_margin())
        # logger.info('bitmex.get_position():%s' % bitmex.get_position())

        # for debug
        # logger.info('fb200: %s' % fb200)
        # logger.info('fb162: %s' % fb162)
        # logger.info('fb138: %s' % fb138)
        # logger.info('fb100: %s' % fb100)
        # logger.info('fb62: %s' % fb62)
        # logger.info('fb50: %s' % fb50)
        # logger.info('fb38: %s' % fb38)
        # logger.info('fb0: %s' % fb0)
        # logger.info('fb038: %s' % fb038)
        # logger.info('fb062: %s' % fb062)
        # logger.info('fb0100: %s' % fb0100)

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

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

        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('--------------------------------------------------')
Example #3
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('--------------------------------------------------')