def do_initalize(self, line): try: self.client1 = RestClient(ConfigManager.get_config().apiKey1, ConfigManager.get_config().apiSecret1, ConfigManager.get_config().apiUrl) self.client1.account() Util.get_logger().info("Credentials Verified") except: Util.get_logger().info("Api credentials invalid") self.close()
def create_new_buy_order(price, amount): Util.get_logger().info("Placing Buy Order for " + str(amount) + " contracts at " + str(price)) client = RestClient(ConfigManager.get_config().apiKey1, ConfigManager.get_config().apiSecret1, ConfigManager.get_config().apiUrl) order = client.buy(ConfigManager.get_config().tradeInsturment, amount, price, False, "") return order
def create_sl_sell_order(price, amount): Util.get_logger().info("Placing SL Sell Order for " + str(amount) + " contracts at " + str(price)) client = RestClient(ConfigManager.get_config().apiKey1, ConfigManager.get_config().apiSecret1, ConfigManager.get_config().apiUrl) order = client.sell_stop_market_order( ConfigManager.get_config().tradeInsturment, amount, price) return order
def do_set_num_orders(self, line): arguments = Util.safe_str_split_on_space(line) if len(arguments) >= 1: config = ConfigManager.get_config() config.numOfOrders = int(arguments[0]) ConfigManager.update_config(config) Util.get_logger().info("Upddated number of orders to: " + str(config.numOfOrders)) else: Util.get_logger().info("Not enough parameters. Ex. set_num_orders 5")
def get_config(): if os.path.isfile("config.json"): file = open("config.json", "r") return jsonpickle.decode(file.read()) else: Util.get_logger().debug( "No configuration found generating new one") ConfigManager.create_config() file = open("config.json", "r") return jsonpickle.decode(file.read())
def do_set_contract_amount(self, line): arguments = Util.safe_str_split_on_space(line) if len(arguments) >= 1: config = ConfigManager.get_config() config.contractSize = int(arguments[0]) ConfigManager.update_config(config) Util.get_logger().info("Upddated number of contracts to: " + str(config.contractSize)) else: Util.get_logger().info("Not enough parameters. Ex. set_contract_amount 100")
def do_set_base_price(self, line): arguments = Util.safe_str_split_on_space(line) if len(arguments) >= 1: config = ConfigManager.get_config() config.basePrice = float(arguments[0]) ConfigManager.update_config(config) Util.get_logger().info("Upddated base price to: " + str(config.basePrice)) else: Util.get_logger().info("Not enough parameters. Ex. set_base_price 6000")
def do_set_price_distance(self, line): arguments = Util.safe_str_split_on_space(line) if len(arguments) >= 1: config = ConfigManager.get_config() config.priceDistance = float(arguments[0]) ConfigManager.update_config(config) Util.get_logger().info("Upddated price difference to: " + str(config.priceDistance)) else: Util.get_logger().info("Not enough parameters. Ex. set_price_distance 1(in USD)")
def cancel_all_current_orders(): orders = DatabaseManager.get_all_open_orders() client = RestClient(ConfigManager.get_config().apiKey1, ConfigManager.get_config().apiSecret1, ConfigManager.get_config().apiUrl) for order in orders: client.cancel(order.orderId) DatabaseManager.update_order_entry(order.orderId, "cancelled") Util.get_logger().info("Cancelled order: " + str(order.orderId))
def do_reset(self, line): if self.rt: self.rt.stop() TradeManager.cancel_all_current_orders() TradeManager.close_all_positions() TradeManager.stopLossTriggered = False DatabaseManager.delete_all_order_models() Util.get_logger().info("Derishell Reset")
def get_order_by_id(orderid): try: orderModel = OrderModel.get(OrderModel.orderid == orderid) return orderModel except: Util.get_logger().debug("Failed to retrieve order: " + str(orderid)) return None
def update_sell_order_entry(orderid, amount): try: orderModel = OrderModel.get(OrderModel.orderId == orderid) orderModel.amount = amount orderModel.save() return orderModel except: Util.get_logger().debug("Failed to retrieve order: " + str(orderid)) return None
def setup_inital_ladder(): Util.get_logger().info("Setup initial ladder") for x in range(ConfigManager.get_config().numOfOrders): order = TradeManager.create_new_buy_order( (ConfigManager.get_config().basePrice - ConfigManager.get_config().priceDistance) - (ConfigManager.get_config().priceDistance * x), ConfigManager.get_config().contractSize, True) DatabaseManager.create_order_entry(order['order']['orderId'], order['order']['price'], order['order']['amount'], order['order']['direction'])
def update_order_entry(orderid, status): try: orderModel = OrderModel.get(OrderModel.orderId == orderid) orderModel.status = status orderModel.save() return orderModel except: Util.get_logger().debug("Failed to retrieve order: " + str(orderid)) return None
def setup_inital_ladder(): Util.get_logger().info("Setup initial ladder") for x in range(ConfigManager.get_config().numOfOrders): DatabaseManager.create_order_entry( "", (ConfigManager.get_config().basePrice - ConfigManager.get_config().priceDistance) - (ConfigManager.get_config().priceDistance * x), ConfigManager.get_config().contractSize, "buy") TradeManager.update_pending_orders() # Create the stop loss order order = TradeManager.create_sl_sell_order( ConfigManager.get_config().stopLossPrice, ConfigManager.get_config().numOfOrders * ConfigManager.get_config().contractSize) DatabaseManager.create_sl_order_entry( order['order']['orderId'], ConfigManager.get_config().stopLossPrice, ConfigManager.get_config().numOfOrders * ConfigManager.get_config().contractSize)
TradeManager.close_all_positions() def do_cancel_orders(self, line): TradeManager.cancel_all_current_orders() def do_close_positions(self, line): TradeManager.close_all_positions() def do_pause(self, line): self.rt.stop() def do_start_update(self,line): if self.client1 != None: self.rt = RepeatedTimer(10, TradeManager.update_order_status) def do_update_orders(self, line): if self.client1 != None: TradeManager.update_order_status() def do_clear(self, line): Util.clear_screen() def do_quit(self, line): self.close() return True if __name__ == '__main__': Util.clear_screen() print(Util.banner) DatabaseManager.initalize() DeriShell().cmdloop()
def do_clear(self, line): Util.clear_screen()
def update_order_status(): if TradeManager.stopLossTriggered == False: orders = DatabaseManager.get_all_open_orders() client = RestClient(ConfigManager.get_config().apiKey1, ConfigManager.get_config().apiSecret1, ConfigManager.get_config().apiUrl) for order in orders: try: updatedOrder = client.getorderstate(order.orderId) DatabaseManager.update_order_entry(order.orderId, updatedOrder['state']) if updatedOrder['state'] == "filled": if "SLMS" in order.orderId: # Stop loss fired Util.get_logger().info( "STOP LOSS FIRED - CANCELLING ALL ORDERS") TradeManager.cancel_all_current_orders() TradeManager.stopLossTriggered = True else: if updatedOrder['direction'] == "buy": if ConfigManager.get_config().fcbMode: #Create new one DatabaseManager.create_order_entry( "", order.price + ConfigManager.get_config( ).priceDistance, ConfigManager.get_config( ).contractSize, "sell") else: sellPriceOffset = ConfigManager.get_config( ).basePrice - order.price DatabaseManager.create_order_entry( "", ConfigManager.get_config().basePrice + sellPriceOffset, ConfigManager.get_config( ).contractSize, "sell") else: if ConfigManager.get_config().fcbMode: DatabaseManager.create_order_entry( "", order.price - ConfigManager.get_config( ).priceDistance, ConfigManager.get_config( ).contractSize, "buy") else: # put in buy order buyPriceOffset = order.price - ConfigManager.get_config( ).basePrice DatabaseManager.create_order_entry( "", ConfigManager.get_config().basePrice + buyPriceOffset, ConfigManager.get_config( ).contractSize, "buy") except: pass else: #We no longer update our queue until user resets pass
def update_order_status(): orders = DatabaseManager.get_all_open_orders() client = RestClient(ConfigManager.get_config().apiKey1, ConfigManager.get_config().apiSecret1, ConfigManager.get_config().apiUrl) Util.get_logger().info("Updating orders") for order in orders: updatedOrder = client.getorderstate(order.orderId) if updatedOrder['state'] == "filled": if updatedOrder['direction'] == "buy": if ConfigManager.get_config().fcbMode: #Create new one newOrder = TradeManager.create_new_sell_order( updatedOrder["price"] + ConfigManager.get_config().priceDistance, updatedOrder['quantity'], False) DatabaseManager.create_order_entry( newOrder['order']['orderId'], newOrder['order']['price'], newOrder['order']['quantity'], newOrder['order']['direction']) else: sellPriceOffset = ConfigManager.get_config( ).basePrice - updatedOrder["price"] #Create new one newOrder = TradeManager.create_new_sell_order( ConfigManager.get_config().basePrice + sellPriceOffset, updatedOrder['quantity'], False) DatabaseManager.create_order_entry( newOrder['order']['orderId'], newOrder['order']['price'], newOrder['order']['quantity'], newOrder['order']['direction']) else: if ConfigManager.get_config().fcbMode: newOrder = TradeManager.create_new_buy_order( updatedOrder["price"] - ConfigManager.get_config().priceDistance, updatedOrder['quantity'], False) DatabaseManager.create_order_entry( newOrder['order']['orderId'], newOrder['order']['price'], newOrder['order']['quantity'], newOrder['order']['direction']) else: # put in buy order buyPriceOffset = updatedOrder[ "price"] - ConfigManager.get_config().basePrice #Create new one newOrder = TradeManager.create_new_buy_order( ConfigManager.get_config().basePrice + buyPriceOffset, updatedOrder['quantity'], False) DatabaseManager.create_order_entry( newOrder['order']['orderId'], newOrder['order']['price'], newOrder['order']['quantity'], newOrder['order']['direction']) DatabaseManager.update_order_entry(order.orderId, updatedOrder['state'])
def close(self): Util.get_logger().debug("Shutting Down") exit()