Exemple #1
0
def load_bars(days_in_history,
              wanted_tf,
              start_offset_minutes=0,
              exchange='bybit',
              symbol='BTCUSD'):
    #empty symbol is legacy and means btcusd
    end = known_history_files[exchange + "_" + symbol]
    start = max(0, end - int(days_in_history * 1440 / 50000))
    m1_bars_temp = []
    logger.info("loading " + str(end - start + 1) + " history files from " +
                exchange)
    for i in range(start, end + 1):
        with open(history_file_name(i, exchange, symbol)) as f:
            m1_bars_temp += json.load(f)
    logger.info("done loading files, now preparing them")
    start = max(0, len(m1_bars_temp) - (days_in_history * 1440))
    m1_bars = m1_bars_temp[start:]

    subbars: List[Bar] = []
    for b in m1_bars:
        if exchange == 'bybit':
            if b['open'] is None:
                continue
            subbars.append(ByBitInterface.barDictToBar(b))
        elif exchange == 'bitmex':
            if b['open'] is None:
                continue
            subbars.append(BitmexInterface.barDictToBar(b, wanted_tf))
        elif exchange in ['binance_future', 'binanceSpot']:
            subbars.append(BinanceFuturesInterface.barArrayToBar(b))
        elif exchange == 'phemex':
            subbars.append(PhemexInterface.barArrayToBar(b, 10000))
    subbars.reverse()
    return process_low_tf_bars(subbars, wanted_tf, start_offset_minutes)
Exemple #2
0
def load_bars(days_in_history,
              wanted_tf,
              start_offset_minutes=0,
              exchange='bitmex'):
    knownfiles = {"bitmex": 45, "bybit": 14, "binance": 6, "binanceSpot": 28}
    end = knownfiles[exchange]
    start = max(0, end - int(days_in_history * 1440 / 50000))
    m1_bars = []
    logger.info("loading " + str(end - start) + " history files from " +
                exchange)
    for i in range(start, end + 1):
        with open('history/' + exchange + '/M1_' + str(i) + '.json') as f:
            m1_bars += json.load(f)
    logger.info("done loading files, now preparing them")

    subbars: List[Bar] = []
    for b in m1_bars:
        if exchange == 'bybit':
            if b['open'] is None:
                continue
            subbars.append(ByBitInterface.barDictToBar(b))
        elif exchange == 'bitmex':
            if b['open'] is None:
                continue
            subbars.append(BitmexInterface.barDictToBar(b, wanted_tf))
        elif exchange in ['binance', 'binanceSpot']:
            subbars.append(BinanceInterface.barArrayToBar(b))
    subbars.reverse()
    return process_low_tf_bars(subbars, wanted_tf, start_offset_minutes)
Exemple #3
0
    def __init__(self, settings, trading_bot: TradingBot):
        self.settings = settings
        self.id = self.settings.id
        self.last_tick = 0

        self.logger = log.setup_custom_logger(
            name=settings.id,
            log_level=settings.LOG_LEVEL,
            logToConsole=settings.LOG_TO_CONSOLE,
            logToFile=settings.LOG_TO_FILE)
        self.logger.info("#############################")
        self.logger.info("############ Start LiveTrading " + settings.id +
                         " on " + settings.EXCHANGE + " #################")
        self.exchange: ExchangeInterface = None
        if settings.EXCHANGE == 'bitmex':
            self.exchange = BitmexInterface(settings=settings,
                                            logger=self.logger,
                                            on_tick_callback=self.on_tick)
        elif settings.EXCHANGE == 'bybit':
            self.exchange = ByBitInterface(settings=settings,
                                           logger=self.logger,
                                           on_tick_callback=self.on_tick)
        elif settings.EXCHANGE == 'binance':
            self.exchange = BinanceInterface(settings=settings,
                                             logger=self.logger,
                                             on_tick_callback=self.on_tick)
        else:
            self.logger.error("unkown exchange: " + settings.EXCHANGE)
            self.alive = False
            return

        self.alive = True

        if self.exchange.is_open():
            self.logger.info(" Starting Live Trading Engine for %s " %
                             self.exchange.symbol)
            self.symbolInfo: Symbol = self.exchange.get_instrument()
            self.bot: TradingBot = trading_bot
            self.bot.prepare(self.logger, self)
            # init market data dict to be filled later
            self.bars: List[Bar] = []
            self.update_bars()
            self.account: Account = Account()
            self.update_account()
            self.bot.reset()
        else:
            self.alive = False
Exemple #4
0
def load_bars(days_in_history, wanted_tf, start_offset_minutes=0,exchange='bitmex'):
    end = 45 if exchange == 'bitmex' else 14
    start = max(0,end - int(days_in_history * 1440 / 50000))
    m1_bars = []
    logger.info("loading " + str(end - start) + " history files from "+exchange)
    for i in range(start, end + 1):
        with open('history/'+exchange+'/M1_' + str(i) + '.json') as f:
            m1_bars += json.load(f)
    logger.info("done loading files, now preparing them")

    subbars: List[Bar] = []
    if exchange == 'bybit':
        for b in m1_bars:
            if b['open'] is None:
                continue
            subbars.append(ByBitInterface.barDictToBar(b))
    else:
        for b in m1_bars:
            if b['open'] is None:
                continue
            subbars.append(BitmexInterface.barDictToBar(b,wanted_tf))
    subbars.reverse()
    return process_low_tf_bars(subbars, wanted_tf, start_offset_minutes)
Exemple #5
0
    def __init__(self, settings, telegram: TelegramBot,
                 trading_bot: TradingBot):
        self.settings = settings
        self.id = self.settings.id
        self.last_tick = 0

        self.logger = log.setup_custom_logger(
            name=settings.id,
            log_level=settings.LOG_LEVEL,
            logToConsole=settings.LOG_TO_CONSOLE,
            logToFile=settings.LOG_TO_FILE)

        self.telegram_bot = telegram

        self.logger.info("#############################")
        self.logger.info("############ Start LiveTrading " + settings.id +
                         " on " + settings.EXCHANGE + " #################")
        self.exchange: ExchangeInterface = None
        if settings.EXCHANGE == 'bitmex':
            self.exchange = BitmexInterface(settings=settings,
                                            logger=self.logger,
                                            on_tick_callback=self.on_tick)
        elif settings.EXCHANGE == 'bybit':
            self.exchange = ByBitInterface(settings=settings,
                                           logger=self.logger,
                                           on_tick_callback=self.on_tick)
        elif settings.EXCHANGE == 'binance':
            self.exchange = BinanceInterface(settings=settings,
                                             logger=self.logger,
                                             on_tick_callback=self.on_tick)
        elif settings.EXCHANGE == 'phemex':
            self.exchange = PhemexInterface(settings=settings,
                                            logger=self.logger,
                                            on_tick_callback=self.on_tick)
        else:
            self.logger.error("unkown exchange: " + settings.EXCHANGE)
            self.alive = False
            return

        self.alive = True

        if self.exchange.is_open():
            self.logger.info(" Starting Live Trading Engine for %s " %
                             self.exchange.symbol)
            self.symbolInfo: Symbol = self.exchange.get_instrument()
            self.bot: TradingBot = trading_bot
            self.bot.prepare(self.logger, self)
            # init market data dict to be filled later
            self.bars: List[Bar] = []
            self.update_bars()
            self.account: Account = Account()
            self.update_account()
            self.bot.reset()
            if self.telegram_bot is not None:
                pos = "no pos"
                if self.account.open_position is not None and self.account.open_position.avgEntryPrice is not None:
                    pos = "%.2f @ %.2f" % (
                        self.account.open_position.quantity,
                        self.account.open_position.avgEntryPrice)
                self.telegram_bot.send_log(
                    "%s loaded, ready to go with %.2f in wallet and pos %s" %
                    (self.id, self.account.equity, pos))
        else:
            self.alive = False
Exemple #6
0
from kuegi_bot.exchanges.bitmex.bitmex_interface import BitmexInterface
from kuegi_bot.exchanges.bybit.bybit_interface import ByBitInterface
from kuegi_bot.utils import log
from kuegi_bot.utils.helper import load_settings_from_args

settings = load_settings_from_args()

logger = log.setup_custom_logger("cryptobot",
                                 log_level=settings.LOG_LEVEL,
                                 logToConsole=True,
                                 logToFile=False)


def onTick():
    logger.info("got Tick")


if settings.EXCHANGE == 'bybit':
    interface = ByBitInterface(settings=settings,
                               logger=logger,
                               on_tick_callback=onTick)
    b = interface.bybit
    w = interface.ws
else:
    interface = BitmexInterface(settings=settings,
                                logger=logger,
                                on_tick_callback=onTick)

bars = interface.get_bars(240, 0)