Example #1
0
 def strategy(self, open, close, high, low, volume):
     lot = self.exchange.get_lot()
     fast_len = self.input('fast_len', int, 9)
     slow_len = self.input('slow_len', int, 16)
     fast_sma = sma(close, fast_len)
     slow_sma = sma(close, slow_len)
     golden_cross = crossover(fast_sma, slow_sma)
     dead_cross = crossunder(fast_sma, slow_sma)
     if golden_cross:
         self.exchange.entry("Long", True, lot)
     if dead_cross:
         self.exchange.entry("Short", False, lot)
Example #2
0
    def strategy(self, open, close, high, low, volume):
        lot = self.exchange.get_lot()
        fast_len = self.input('fast_len', int, 9)
        slow_len = self.input('slow_len', int, 16)
        fast_sma = sma(close, fast_len)
        slow_sma = sma(close, slow_len)
        golden_cross = crossover(fast_sma, slow_sma)
        dead_cross = crossunder(fast_sma, slow_sma)

        def entry_callback(avg_price=close[-1]):
            long = True if self.exchange.get_position_size() > 0 else False
            logger.info(
                f"{'Long' if long else 'Short'} Entry Order Successful")

        if golden_cross:
            self.exchange.entry("Long", True, lot, \
                round_decimals=3, callback=entry_callback)
        if dead_cross:
            self.exchange.entry("Short", False, lot, \
                round_decimals=3, callback=entry_callback)
Example #3
0
    def strategy(self, open, close, high, low, volume):
        lot = self.exchange.get_lot()
        # for test
        # lot = int(round(lot / 100))

        bitmex = BitMex(threading=False)

        price = bitmex.get_market_price()

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

        atrvar = atr(high, low, close, period=period)
        # up = (high + low) / 2 - (factor * atr(high, low, close, period=period))
        # logger.info('up:%s\n' % up)
        # dn = (high + low) / 2 + (factor * atr(high, low, close, period=period))
        # logger.info('atrvar: %s' % atrvar[-1])

        resolution = self.input(defval=15, title="resolution",
                                type=int)  # defval 변경, 예) 5분 --> 5
        source = self.exchange.security(str(resolution) + 'm')  # init  참고
        supertrenddf = supertrend(source, factor, period)

        # logger.info('supertrend:%s' % supertrenddf.describe())
        # logger.info('supertrend:%s' % supertrenddf.columns)

        logger.info('price:%s\n' % price)
        # logger.info('source:%s\n' % source[-1])
        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' % supertrenddf['ATR'][-1])

        longCondition_supertrend = crossover(
            close, supertrenddf['SuperTrend']
        ) and close[-1] > supertrenddf['SuperTrend'][-1]
        shortCondition_supertrend = crossunder(
            close, supertrenddf['SuperTrend']
        ) and close[-1] < supertrenddf['SuperTrend'][-1]

        if longCondition_supertrend:
            self.exchange.entry("Long", True, lot)
            logger.info('longCondition_supertrend:%s\n' %
                        longCondition_supertrend)

        elif shortCondition_supertrend:
            self.exchange.entry("Short", False, lot)
            logger.info('shortCondition_supertrend:%s\n' %
                        shortCondition_supertrend)
        else:
            # self.exchange.close_all()
            logger.info('Condition_supertrend:%s\n' % '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('bitmex.get_margin():%s' % bitmex.get_margin())
        # logger.info('bitmex.get_position():%s' % bitmex.get_position())

        # logger.info('bitmex.get_balance():%s' % bitmex.get_balance())
        # logger.info('get_pre_prebalance:%s' % get_pre_prebalance(self.prebalance, bitmex.get_balance()))
        #     # self.exchange.close_all()
        #     # self.exchange.cancel_all()

        logger.info('--------------------------------------------------')
Example #4
0
    def strategy(self, open, close, high, low, volume):

        # get lot or set your own value which will be used to size orders
        # careful default lot is about 20x your account size !!!
        lot = self.exchange.get_lot()

        # indicator lengths
        fast_len = self.input('fast_len', int, 6)
        slow_len = self.input('slow_len', int, 18)

        # setting indicators, they usually take source and length as arguments
        sma1 = sma(close, fast_len)
        sma2 = sma(close, slow_len)

        # entry conditions
        long_entry_condition = crossover(sma1, sma2)
        short_entry_condition = crossunder(sma1, sma2)

        # setting a simple stop loss and profit target in % using built-in simple profit take and stop loss implementation
        # which is placing the sl and tp automatically after entering a position
        self.exchange.sltp(profit_long=1.25,
                           profit_short=1.25,
                           stop_long=1,
                           stop_short=1.1,
                           round_decimals=0)

        # example of calculation of stop loss price 0.8% round on 2 decimals hardcoded inside this class
        # sl_long = round(close[-1] - close[-1]*0.8/100, 2)
        # sl_short = round(close[-1] - close[-1]*0.8/100, 2)

        # order execution logic
        if long_entry_condition:
            # entry - True means long for every other order other than entry use self.exchange.order() function
            self.exchange.entry("Long", True, lot / 20)
            # stop loss hardcoded inside this class
            #self.exchange.order("SLLong", False, lot/20, stop=sl_long, reduce_only=True, when=False)

        if short_entry_condition:
            # entry - False means short for every other order other than entry use self.exchange.order() function
            self.exchange.entry("Short", False, lot / 20)
            # stop loss hardcoded inside this class
            # self.exchange.order("SLShort", True, lot/20, stop=sl_short, reduce_only=True, when=False)

        # storing history for entry signals, you can store any variable this way to keep historical values
        self.long_entry_signal_history.append(long_entry_condition)
        self.short_entry_signal_history.append(short_entry_condition)

        # OHLCV and indicator data, you can access history using list index
        # log indicator values
        logger.info(f"sma1: {sma1[-1]}")
        logger.info(f"second last sma2: {sma2[-2]}")
        # log last candle OHLCV values
        logger.info(f"open: {open[-1]}")
        logger.info(f"high: {high[-1]}")
        logger.info(f"low: {low[-1]}")
        logger.info(f"close: {close[-1]}")
        logger.info(f"volume: {volume[-1]}")
        #second last candle OHLCV values
        logger.info(f"second last open: {open[-2]}")
        logger.info(f"second last high: {high[-2]}")
        logger.info(f"second last low: {low[-2]}")
        logger.info(f"second last close: {close[-2]}")
        logger.info(f"second last volume: {volume[-2]}")
        # log history entry signals
        logger.info(f"long_entry_hist: {self.long_entry_signal_history}")
        logger.info(f"short_entry_hist: {self.short_entry_signal_history}")
Example #5
0
    def strategy(self, open, close, high, low, volume):

        lot = self.exchange.get_lot()
        lot = int(round(lot / 6, self.decimal_num))

        resolution = self.input(defval=1, title="resolution", type=int)
        variant_type = self.input(defval=5, title="variant_type", type=int)
        basis_len = self.input(defval=19, title="basis_len", type=int)

        fast_len = self.input("fast_len", int, 1)
        slow_len = self.input("slow_len", int, 30)
        trend_len = self.input("slow_len", int, 60)
        longtrend_len = self.input("slow_len", int, 120)

        source = self.exchange.security(str(resolution) + "m")

        hadf = heikinashi(source)
        hadf_fast = heikinashi(hadf)

        ha_open_values = hadf_fast["HA_open"].values
        ha_close_values = hadf_fast["HA_close"].values
        variant = self.variants[variant_type]

        ha_open_fast = variant(ha_open_values, fast_len)
        ha_close_fast = variant(ha_close_values, fast_len)
        haopen_fast = ha_open_fast[-1]
        haclose_fast = ha_close_fast[-1]
        haup_fast = haclose_fast > haopen_fast
        hadown_fast = haclose_fast <= haopen_fast
        # logger.info('haup_fast:%s\n' % haup_fast)

        ha_open_slow = variant(ha_open_values, slow_len)
        ha_close_slow = variant(ha_close_values, slow_len)
        haopen_slow = ha_open_slow[-1]
        haclose_slow = ha_close_slow[-1]
        haup_slow = haclose_slow > haopen_slow
        hadown_slow = haclose_slow <= haopen_slow
        # logger.info('haup_slow:%s\n' % haup_slow)

        ha_open_trend = variant(ha_open_values, trend_len)
        ha_close_trend = variant(ha_close_values, trend_len)
        haopen_trend = ha_open_trend[-1]
        haclose_trend = ha_close_trend[-1]
        haup_trend = haclose_trend > haopen_trend
        hadown_trend = haclose_trend <= haopen_trend
        # logger.info('haup_trend:%s\n' % haup_trend)

        ha_open_longtrend = variant(ha_open_values, longtrend_len)
        ha_close_longtrend = variant(ha_close_values, longtrend_len)
        haopen_longtrend = ha_open_longtrend[-1]
        haclose_longtrend = ha_close_longtrend[-1]
        haup_longtrend = haclose_longtrend > haopen_longtrend
        hadown_longtrend = haclose_longtrend <= haopen_longtrend
        logger.info("ha_close_longtrend:%s\n" % ha_close_longtrend)
        logger.info("ha_open_longtrend:%s\n" % ha_open_longtrend)

        if not eval(os.environ.get("BOT_TEST", "False")):
            "long"
            self.exchange.entry("Long",
                                True,
                                lot,
                                when=crossover(ha_close_longtrend,
                                               ha_open_longtrend))
            " short "
            self.exchange.entry(
                "Short",
                False,
                lot,
                when=crossunder(ha_close_longtrend, ha_open_longtrend),
            )
Example #6
0
    def strategy(self, open, close, high, low, volume):
        lot = self.exchange.get_lot()
        lot = int(round(lot / 6, self.decimal_num))

        price = self.exchange.get_market_price()
        pos_size = self.exchange.get_position_size()

        fast_len = self.input("fast_len", int,
                              int(os.environ.get("BOT_FAST_LEN", 5)))
        slow_len = self.input("slow_len", int,
                              int(os.environ.get("BOT_SLOW_LEN", 18)))
        trend_len = self.input("trend_len", int,
                               int(os.environ.get("BOT_TREND_LEN", 1200)))

        fast_sma = sma(close, fast_len)
        slow_sma = sma(close, slow_len)
        trend_sma = sma(close, trend_len)

        uptrend = True if trend_sma[-1] > trend_sma[-3] or trend_sma[
            -1] > trend_sma[-10] else False
        downtrend = True if trend_sma[-1] < trend_sma[-3] or trend_sma[
            -1] < trend_sma[-10] else False

        golden_cross = crossover(fast_sma, slow_sma)
        dead_cross = crossunder(fast_sma, slow_sma)

        nc = "golden" if round(fast_sma[-1] - slow_sma[-1],
                               self.price_decimal_num) < 0 else "dead"
        ct = "sideways" if downtrend and uptrend else (
            "down" if downtrend else "up")

        np = "short" if nc == "golden" and (
            pos_size > 0 or (pos_size >= 0 and downtrend)) else "long"
        nt = "golden" if (nc == "golden" and np == "short") else (
            "dead" if nc == "dead" and np == "long" else not nc)

        logger.info(f"--------------------------------------")
        logger.info(f"trend: {ct}")
        logger.info(
            f'next trade @ {nt} cross > {np} {lot} @ {calc_entry_price(price, False, self.price_decimal_num) if np == "short" else calc_entry_price(price, True, self.price_decimal_num)}'
        )
        if trend_sma[-1] != trend_sma[-1] or trend_sma[-3] != trend_sma[
                -3] or trend_sma[-10] != trend_sma[-10]:
            logger.info(f"--------------------------------------")
            logger.info(f"Bot status: NEEDS RESTART")
        # logger.info(f'--------------------------------------')
        # logger.info(f'{abs(pos_size)}')

        if not eval(os.environ.get("BOT_TEST", "False")):
            if dead_cross and uptrend:
                # self.exchange.cancel_orders_by_side('BUY')
                self.exchange.order(
                    "Long",
                    True,
                    lot,
                    limit=calc_entry_price(price, True,
                                           self.price_decimal_num),
                    when=True,
                    post_only=True,
                )
                logger.info("in dead_cross and uptrend for long")

            if float(self.exchange.get_position()["notional"]) > 0.0:
                self.exchange.order(
                    "Long",
                    False,
                    lot,
                    limit=calc_entry_price(price, False,
                                           self.price_decimal_num),
                    when=golden_cross,
                    post_only=True,
                )

            if golden_cross and downtrend:
                # self.exchange.cancel_orders_by_side('SELL')
                self.exchange.entry(
                    "Short",
                    False,
                    lot,
                    limit=calc_entry_price(price, False,
                                           self.price_decimal_num),
                    when=True,
                    post_only=True,
                )
                logger.info("in golden_cross and downtrend for short")

            if float(self.exchange.get_position()["notional"]) < 0.0:
                self.exchange.order(
                    "Short",
                    True,
                    lot,
                    limit=calc_entry_price(price, True,
                                           self.price_decimal_num),
                    stop=(calc_entry_price(price, True,
                                           self.price_decimal_num)),
                    when=dead_cross,
                    post_only=True,
                )
Example #7
0
    def strategy(self, open, close, high, low, volume):

        lot = self.exchange.get_lot()
        lot = get_calc_lot(lot=lot,
                           decimal_num=self.decimal_num,
                           leverage=20.0,
                           actual_leverage=3.0)

        fast_len = self.input("fast_len", int,
                              int(os.environ.get("BOT_FAST_LEN", 5)))
        slow_len = self.input("slow_len", int,
                              int(os.environ.get("BOT_SLOW_LEN", 18)))
        trend_len = self.input("trend_len", int, 90)

        logger.info(f"fast_len: {fast_len}")
        logger.info(f"slow_len: {slow_len}")
        logger.info(f"trend_len: {trend_len}")

        fast_sma = sma(close, fast_len)
        slow_sma = sma(close, slow_len)
        trend_sma = sma(close, trend_len)

        uptrend = True if trend_sma[-1] > trend_sma[-3] or trend_sma[
            -1] > trend_sma[-10] else False
        downtrend = True if trend_sma[-1] < trend_sma[-3] or trend_sma[
            -1] < trend_sma[-10] else False

        golden_cross = crossover(fast_sma, slow_sma)
        dead_cross = crossunder(fast_sma, slow_sma)
        # inc_trend = fast_sma[-1] > slow_sma[-1]
        # dec_trend = fast_sma[-1] < slow_sma[-1]

        reward = self.risk * self.rr_ratio
        self.exchange.sltp(
            profit_long=reward,
            profit_short=reward,
            stop_long=self.risk,
            stop_short=self.risk,
            round_decimals=self.price_decimal_num,
        )

        # if float(self.exchange.get_position()['notional']) == 0.0:
        if self.exchange.get_position_size() == 0.0:

            self.exchange.cancel_all()

            if golden_cross:
                print("inc_trend detected")
                while True:
                    # check if in long position
                    if float(self.exchange.get_position()
                             ["notional"]) > 0.0 or downtrend:
                        print("long position opened")
                        break
                    print("trying to open long position...")
                    self.exchange.entry("Long", True, lot)

            if dead_cross:
                print("dec_trend detected")
                while True:
                    # check if in short position
                    if float(self.exchange.get_position()
                             ["notional"]) < 0.0 or uptrend:
                        print("short position opened")
                        break
                    print("trying to open short position...")
                    self.exchange.entry("Short", False, lot)

        # OHLCV and indicator data, you can access history using list index
        # log indicator values
        print()
        logger.info(f"fast_sma: {fast_sma[-1]}")
        logger.info(f"slow_sma: {slow_sma[-1]}")
        logger.info(f"trend_sma: {trend_sma[-1]}")
        logger.info(f"uptrend: {uptrend}")
        logger.info(f"downtrend: {downtrend}")
        logger.info(f"golden_cross: {golden_cross}")
        logger.info(f"dead_cross: {dead_cross}")
        # log last candle OHLCV values
        logger.info(f"open: {open[-1]}")
        logger.info(f"high: {high[-1]}")
        logger.info(f"low: {low[-1]}")
        logger.info(f"close: {close[-1]}")
        logger.info(f"volume: {volume[-1]}")