from config_bonus_api import session_private from config_bonus_api import limit_order_basis from config_ws_connect import ws_public from func_calculations import get_trade_details import requests # Set leverage def set_leverage(ticker): try: isolation = session_private.cross_isolated_margin_switch( symbol=ticker, is_isolated=True, buy_leverage=1, sell_leverage=1 ) except Exception as e: pass # Place order to long or short or close long or close short def place_order(ticker, price, quantity, direction, stop_loss, trade_type="Open"): # Set variables side = "Buy" reduce_only = False # Structure trade for Opening Long or Short position if trade_type == "Open": stop_loss = round(stop_loss, 3) if direction != "Long": side = "Sell" # Structure trade for Closing Long or Short position if trade_type == "Close": stop_loss = False reduce_only = True if direction == "Long": side = "Sell" exception = True # Place or market order if limit_order_basis: try: order = session_private.place_active_order( symbol=ticker, side=side, order_type="", qty=quantity, price=price, time_in_force="PostOnly", reduce_only=reduce_only, close_on_trigger=False, stop_loss=stop_loss ) exception = False except requests.exceptions.ConnectTimeout: print("Connect timeout") pass except requests.exceptions.ReadTimeout: print("Read timeout") pass else: try: order = session_private.place_active_order( symbol=ticker, side=side, order_type="Market", qty=quantity, time_in_force="GoodTillCancel", reduce_only=reduce_only, close_on_trigger=False, stop_loss=stop_loss ) exception = False except requests.exceptions.ConnectionError as errc: print("Connection error ",errc) pass except requests.exceptions.ReadTimeout as errr: print("Read timeout",errr) pass except requests.exceptions.RequestException as err: print("other errors ",err) pass # Return order return order # Initialise execution def initialise_order_execution(ticker, direction, capital): exception = True try: orderbook = ws_public.fetch(f"orderBookL2_25.{ticker}") mid_price, stop_loss, quantity = get_trade_details(orderbook, direction, capital) order = place_order(ticker, mid_price, quantity, direction, stop_loss) if "result" in order.keys(): if "order_id" in order["result"]: exception = False print(order["result"]["order_id"], order["result"]["created_time"],exception) return (order["result"]["order_id"], order["result"]["created_time"],exception) except requests.exceptions.ConnectionError as errc: print("Connection error ",errc) pass except requests.exceptions.ReadTimeout as errr: print("Read timeout",errr) pass except requests.exceptions.RequestException as err: print("other errors ",err) pass return (0,"", exception)