async def fire_new_coro(start: int = Query(..., title="start"), end: int = Query(..., title="end"), step: int = Query(..., title="step")): # <scheduled> is a Queue that we append coros to (on right side) # and pop them from the left when we want to start them from the watcher print("appending coro to deque") settings.SCHEDULED.append((coro_to_fire, locals()))
async def cancel_order( exchange: str, txid: str = Query(..., title="ID of Order to cancel"), retries: int = Query( None, title="Number of times to retry the request if it fails")): api = rest_api_map[exchange]() response = await api.cancel_order(txid=txid, retries=retries) return response
async def fire_new_coro( start: int = Query(..., title="start"), end: int = Query(..., title="end"), step: int = Query(..., title="step"), ): task = BackgroundTask(coro_to_fire, **locals()) msg = {"status": "Background Task started"} return JSONResponse(msg, background=task)
async def get_closed_positions(exchange: str, mode: Literal["by_id", "to_list"] = Query( ..., title="Sorting mode"), symbol: str = Query(..., title="symbol")): api = rest_api_map[exchange]() response = await api.get_closed_positions(symbol=symbol, mode=mode) return response
async def get_single_trade(exchange: str, mode: Literal["by_id", "to_list"] = Query( ..., title="Sorting mode"), trade_id: str = Query(..., title="trdMatchID to query")): api = rest_api_map[exchange]() response = await api.get_user_trade_by_id(mode=mode, trdMatchID=trade_id) return response
async def get_single_order(exchange: str, mode: Literal["by_id", "to_list"] = Query( ..., title="Sorting mode"), order_id: str = Query(..., title="orderID to query")): api = rest_api_map[exchange]() response = await api.get_order(mode=mode, orderID=order_id) return response
async def get_ohlc(exchange: str, symbol: str = Query(..., title="symbol"), timeframe: int = Query(..., title="candle timeframe")): try: api = rest_api_map[exchange]() response = await api.get_ohlc(symbol=symbol, timeframe=int(timeframe)) if response.is_ok: return UJSONResponse(status_code=response.status_code, content={"data": response.value}) else: return UJSONResponse(status_code=response.status_code, content={"error": response.value}) except Exception as e: return UJSONResponse(status_code=status.HTTP_400_BAD_REQUEST, content={"error": repr(e)})
async def cancel_all_orders( exchange: str, retries: int = Query( None, title="Number of times to retry the request if it fails")): api = rest_api_map[exchange]() response = await api.cancel_all_orders(retries=retries) return response
async def get_closed_orders(exchange: str, mode: Literal["by_id", "to_list"] = Query( ..., title="Sorting mode")): api = rest_api_map[exchange]() response = await api.get_closed_orders(mode=mode) return response
async def get_websocket_auth_token( exchange: str, validity: int = Query( None, title="Number of minutes the returned token will be valid")): api = rest_api_map[exchange]() response = await api.get_websocket_auth_token(validity=validity) return response
async def get_open_orders(exchange: str, mode: Literal["by_id", "to_list"] = Query( ..., title="Sorting mode")): #! handle cases where exchange is unknown to return correct error message api = rest_api_map[exchange]() response = await api.get_open_orders(mode=mode) return UJSONResponse(status_code=response.status_code, content=response.value)
async def get_instrument(exchange: str, symbol: str = Query(..., title="symbol")): try: api = rest_api_map[exchange]() response = await api.get_instrument(symbol=symbol) if response.is_ok: return UJSONResponse(status_code=response.status_code, content={**response.value}) else: return UJSONResponse(status_code=response.status_code, content={"error": response.value}) except Exception as e: return UJSONResponse(status_code=status.HTTP_400_BAD_REQUEST, content={"error": repr(e)})
async def get_orderbook( exchange: str, symbol: str = Query(..., title="symbol"), ): try: api = rest_api_map[exchange]() response = await api.get_orderbook(symbol=symbol) if response.is_ok: return UJSONResponse(status_code=response.status_code, content={ "sendingTime": response.value["sendingTime"], "symbol": response.value["symbol"], "asks": response.value["asks"], "bids": response.value["bids"] }) else: return UJSONResponse(status_code=response.status_code, content={"error": response.value}) except Exception as e: return UJSONResponse(status_code=status.HTTP_400_BAD_REQUEST, content={"error": repr(e)})
async def place_order( exchange: str, pair: str = Query(..., title="Dash Separated Pair"), side: str = Query(..., title="Direction of the trade (buy or sell)"), ordertype: str = Query(..., title="Market/Limit/Stop Loss/Take Profit"), price: float = Query(None, title="Price - Leave empty if order type is market"), volume: float = Query(..., title="Volume of Order in lots"), price2: float = Query( None, title= "2nd Price - Depends on ordertype - Usually used for stops/take profits" ), leverage: float = Query(None, title="Leverage Used"), validate: bool = Query( None, title="Only validate Order Input, without actually placing the order"), retries: int = Query( None, title="Number of times to retry the request if it fails"), start_time: int = Query(None, title="Start Time"), expire_time: int = Query(None, title="Expire Time")): api = rest_api_map[exchange]() # no price = market order => we set order price to last close to calculate slippage later if not price: price = await api.get_ticker_as_pandas([pair]) price = float(price[pair.upper(), "close"][0]) response = await api.place_order(pair=[pair], side=side, ordertype=ordertype, price=price, volume=volume, price2=price2, leverage=leverage, validate=validate, start_time=start_time, expire_time=expire_time, retries=retries) return response