def __init__(self, account, pair, threading=True): """ constructor :account: :pair: :param threading: """ self.pair = pair BinanceFutures.__init__(self, account, pair, threading=threading)
def on_update(self, bin_size, strategy): """ Register function of strategy. :param strategy: """ def __override_strategy(open, close, high, low, volume): new_open_orders = [] if self.get_position_size() > 0 and low[-1] > self.get_trail_price( ): self.set_trail_price(low[-1]) if self.get_position_size( ) < 0 and high[-1] < self.get_trail_price(): self.set_trail_price(high[-1]) for _, order in enumerate(self.open_orders): id = order["id"] long = order["long"] qty = order["qty"] limit = order["limit"] stop = order["stop"] post_only = order["post_only"] if limit > 0 and stop > 0: if (long and high[-1] > stop and close[-1] < limit) or ( not long and low[-1] < stop and close[-1] > limit): self.commit(id, long, qty, limit, False) continue elif (long and high[-1] > stop) or (not long and low[-1] < stop): new_open_orders.append({ "id": id, "long": long, "qty": qty, "limit": limit, "stop": 0 }) continue elif limit > 0: if (long and low[-1] < limit) or (not long and high[-1] > limit): self.commit(id, long, qty, limit, False) continue elif stop > 0: if (long and high[-1] > stop) or (not long and low[-1] < stop): self.commit(id, long, qty, stop, False) continue new_open_orders.append(order) self.open_orders = new_open_orders strategy(open, close, high, low, volume) self.eval_exit() BinanceFutures.on_update(self, bin_size, __override_strategy)
def run(self): """ ˜ Function to run the bot """ if self.hyperopt: logger.info(f"Bot Mode : Hyperopt") self.params_search() return elif self.stub_test: logger.info(f"Bot Mode : Stub") if self.exchange_arg == "binance": self.exchange = BinanceFuturesStub(account=self.account, pair=self.pair) elif self.exchange_arg == "bitmex": self.exchange = BitMexStub(account=self.account, pair=self.pair) else: logger.info(f"--exchange argument missing or invalid") return elif self.back_test: logger.info(f"Bot Mode : Back test") if self.exchange_arg == "binance": self.exchange = BinanceFuturesBackTest(account=self.account, pair=self.pair) elif self.exchange_arg == "bitmex": self.exchange = BitMexBackTest(account=self.account, pair=self.pair) else: logger.info(f"--exchange argument missing or invalid") return else: logger.info(f"Bot Mode : Trade") if self.exchange_arg == "binance": self.exchange = BinanceFutures(account=self.account, pair=self.pair, demo=self.test_net) elif self.exchange_arg == "bitmex": self.exchange = BitMex(account=self.account, pair=self.pair, demo=self.test_net) else: logger.info(f"--exchange argument missing or invalid") return self.exchange.ohlcv_len = self.ohlcv_len() self.exchange.on_update(self.bin_size, self.strategy) logger.info(f"Starting Bot") logger.info(f"Strategy : {type(self).__name__}") logger.info(f"Balance : {self.exchange.get_balance()}") notify(f"Starting Bot\n" f"Strategy : {type(self).__name__}\n" f"Balance : {self.exchange.get_balance()}") self.exchange.show_result()