2021-09-16 14:27:15 -07:00
|
|
|
import json
|
2021-09-18 10:35:04 -07:00
|
|
|
import os
|
2021-09-24 10:25:57 -07:00
|
|
|
import time
|
2021-09-18 10:35:04 -07:00
|
|
|
from typing import List
|
2021-09-16 14:27:15 -07:00
|
|
|
|
|
|
|
import httpx
|
|
|
|
from pydantic import parse_obj_as
|
|
|
|
|
|
|
|
from mango_service_v3_py.dtos import (
|
|
|
|
Position,
|
|
|
|
Balance,
|
|
|
|
Market,
|
|
|
|
Orderbook,
|
|
|
|
Trade,
|
|
|
|
Candle,
|
|
|
|
Order,
|
|
|
|
PlaceOrder,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# todo add mypy
|
|
|
|
|
|
|
|
|
2021-09-24 10:25:57 -07:00
|
|
|
def delayed(seconds):
|
|
|
|
def decorator(f):
|
|
|
|
def wrapper(*args, **kargs):
|
|
|
|
time.sleep(seconds)
|
|
|
|
return f(*args, **kargs)
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
class MangoServiceV3Client:
|
2021-10-14 00:59:24 -07:00
|
|
|
def __init__(self, base_url=None, timeout=None):
|
2021-09-26 03:44:04 -07:00
|
|
|
self.timeout = timeout if timeout else 10.0
|
2021-09-26 05:32:13 -07:00
|
|
|
if base_url:
|
|
|
|
self.BASE_URL = base_url
|
2021-09-18 10:35:04 -07:00
|
|
|
else:
|
|
|
|
self.BASE_URL = "http://localhost:3000/api"
|
2021-09-16 14:27:15 -07:00
|
|
|
|
|
|
|
def get_open_positions(self) -> List[Position]:
|
2021-09-26 03:44:04 -07:00
|
|
|
response = httpx.get(f"{self.BASE_URL}/positions", timeout=self.timeout)
|
2021-09-16 14:27:15 -07:00
|
|
|
return parse_obj_as(List[Position], json.loads(response.text)["result"])
|
|
|
|
|
|
|
|
def get_balances(self) -> List[Balance]:
|
2021-09-26 03:44:04 -07:00
|
|
|
response = httpx.get(f"{self.BASE_URL}/wallet/balances", timeout=self.timeout)
|
2021-09-16 14:27:15 -07:00
|
|
|
return parse_obj_as(List[Balance], json.loads(response.text)["result"])
|
|
|
|
|
|
|
|
def get_markets(self) -> List[Market]:
|
2021-09-26 03:44:04 -07:00
|
|
|
response = httpx.get(f"{self.BASE_URL}/markets", timeout=self.timeout)
|
2021-09-16 14:27:15 -07:00
|
|
|
return parse_obj_as(List[Market], json.loads(response.text)["result"])
|
|
|
|
|
|
|
|
def get_market_by_market_name(self, market_name: str) -> List[Market]:
|
2021-09-26 03:44:04 -07:00
|
|
|
response = httpx.get(
|
|
|
|
f"{self.BASE_URL}/markets/{market_name}", timeout=self.timeout
|
|
|
|
)
|
2021-09-16 14:27:15 -07:00
|
|
|
return parse_obj_as(List[Market], json.loads(response.text)["result"])
|
|
|
|
|
2021-11-15 10:41:29 -08:00
|
|
|
def get_orderbook(self, market_name: str, depth: int = 30) -> Orderbook:
|
2021-09-16 14:27:15 -07:00
|
|
|
response = httpx.get(
|
|
|
|
f"{self.BASE_URL}/markets/{market_name}/orderbook?depth={depth}"
|
|
|
|
)
|
|
|
|
return parse_obj_as(Orderbook, json.loads(response.text)["result"])
|
|
|
|
|
|
|
|
def get_trades(self, market_name: str) -> List[Trade]:
|
2021-09-24 10:25:57 -07:00
|
|
|
response = httpx.get(
|
2021-09-26 03:44:04 -07:00
|
|
|
f"{self.BASE_URL}/markets/{market_name}/trades", timeout=self.timeout
|
2021-09-24 10:25:57 -07:00
|
|
|
)
|
2021-09-16 14:27:15 -07:00
|
|
|
return parse_obj_as(List[Trade], json.loads(response.text)["result"])
|
|
|
|
|
|
|
|
def get_candles(
|
|
|
|
self, market_name: str, resolution: int, start_time: int, end_time: int
|
|
|
|
) -> List[Candle]:
|
|
|
|
response = httpx.get(
|
|
|
|
f"{self.BASE_URL}/markets/{market_name}/candles?resolution={resolution}&start_time={start_time}&end_time={end_time}"
|
|
|
|
)
|
|
|
|
return parse_obj_as(List[Candle], json.loads(response.text)["result"])
|
|
|
|
|
|
|
|
def get_orders(self,) -> List[Order]:
|
2021-09-26 03:44:04 -07:00
|
|
|
response = httpx.get(f"{self.BASE_URL}/orders", timeout=self.timeout)
|
2021-09-16 14:27:15 -07:00
|
|
|
return parse_obj_as(List[Order], json.loads(response.text)["result"])
|
|
|
|
|
|
|
|
def get_orders_by_market_name(self, market_name: str) -> List[Order]:
|
2021-09-24 10:25:57 -07:00
|
|
|
response = httpx.get(
|
2021-09-26 03:44:04 -07:00
|
|
|
f"{self.BASE_URL}/orders?market={market_name}", timeout=self.timeout
|
2021-09-24 10:25:57 -07:00
|
|
|
)
|
2021-09-16 14:27:15 -07:00
|
|
|
return parse_obj_as(List[Order], json.loads(response.text)["result"])
|
|
|
|
|
|
|
|
def place_order(self, order: PlaceOrder) -> None:
|
2021-09-24 10:25:57 -07:00
|
|
|
response = httpx.post(
|
2021-09-26 03:44:04 -07:00
|
|
|
f"{self.BASE_URL}/orders",
|
|
|
|
json=order.dict(by_alias=True),
|
|
|
|
timeout=self.timeout,
|
2021-09-24 10:25:57 -07:00
|
|
|
)
|
2021-09-16 14:27:15 -07:00
|
|
|
|
2021-09-17 12:52:54 -07:00
|
|
|
def cancel_order_by_client_id(self, client_id):
|
2021-09-24 10:25:57 -07:00
|
|
|
response = httpx.delete(
|
2021-09-26 03:44:04 -07:00
|
|
|
f"{self.BASE_URL}/orders/by_client_id/{client_id}", timeout=self.timeout
|
2021-09-24 10:25:57 -07:00
|
|
|
)
|
2021-09-16 14:27:15 -07:00
|
|
|
|
2021-09-17 12:52:54 -07:00
|
|
|
def cancel_order_by_order_id(self, order_id):
|
2021-09-26 03:44:04 -07:00
|
|
|
response = httpx.delete(
|
|
|
|
f"{self.BASE_URL}/orders/{order_id}", timeout=self.timeout
|
|
|
|
)
|
2021-09-16 14:27:15 -07:00
|
|
|
|
2021-09-17 12:52:54 -07:00
|
|
|
def cancel_all_orders(self):
|
2021-09-26 03:44:04 -07:00
|
|
|
response = httpx.delete(f"{self.BASE_URL}/orders", timeout=self.timeout)
|
2021-09-24 10:25:57 -07:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def to_nearest(num, tickDec):
|
|
|
|
return float(round(num / tickDec, 0)) * tickDec
|