Wrap API calls in helper and validate response

This commit is contained in:
2022-10-30 19:11:07 +00:00
parent f22fcfdaaa
commit c15ae379f5
15 changed files with 224 additions and 163 deletions

View File

@@ -24,9 +24,9 @@ def get_market_value(account, symbol):
def execute_strategy(callback, strategy):
cash_balance = get_balance(strategy.account)
success, cash_balance = strategy.account.client.get_balance()
log.debug(f"Cash balance: {cash_balance}")
if not cash_balance:
if not success:
return None
user = strategy.user
@@ -41,7 +41,7 @@ def execute_strategy(callback, strategy):
quote = "usd" # TODO: MASSIVE HACK
symbol = f"{base.upper()}/{quote.upper()}"
if symbol not in account.supported_assets:
if symbol not in account.supported_symbols:
log.error(f"Symbol not supported by account: {symbol}")
return False

View File

@@ -0,0 +1,105 @@
from pydantic import BaseModel, Field
class Asset(BaseModel):
id: str
class_: str = Field(..., alias="class")
exchange: str
symbol: str
name: str
status: str
tradable: bool
marginable: bool
maintenance_margin_requirement: int
shortable: bool
easy_to_borrow: bool
fractionable: bool
min_order_size: str
min_trade_increment: str
price_increment: str
# get_all_assets
class GetAllAssets(BaseModel):
itemlist: list[Asset]
# get_open_position
class GetOpenPosition(BaseModel):
asset_id: str
symbol: str
exchange: str
asset_class: str = Field(..., alias="asset_class")
asset_marginable: bool
qty: str
avg_entry_price: str
side: str
market_value: str
cost_basis: str
unrealized_pl: str
unrealized_plpc: str
unrealized_intraday_pl: str
unrealized_intraday_plpc: str
current_price: str
lastday_price: str
change_today: str
qty_available: str
class Position(BaseModel):
asset_id: str
symbol: str
exchange: str
asset_class: str
asset_marginable: bool
qty: str
avg_entry_price: str
side: str
market_value: str
cost_basis: str
unrealized_pl: str
unrealized_plpc: str
unrealized_intraday_pl: str
unrealized_intraday_plpc: str
current_price: str
lastday_price: str
change_today: str
qty_available: str
# get_all_positions
class GetAllPositions(BaseModel):
itemlist: list[Position]
# get_account
class GetAccount(BaseModel):
id: str
account_number: str
status: str
crypto_status: str
currency: str
buying_power: str
regt_buying_power: str
daytrading_buying_power: str
effective_buying_power: str
non_marginable_buying_power: str
bod_dtbp: str
cash: str
accrued_fees: str
pending_transfer_in: str
portfolio_value: str
pattern_day_trader: bool
trading_blocked: bool
transfers_blocked: bool
account_blocked: bool
created_at: str
trade_suspended_by_user: bool
multiplier: str
shorting_enabled: bool
equity: str
last_equity: str
long_market_value: str
short_market_value: str
position_market_value: str
initial_margin: str
maintenance_margin: str
last_maintenance_margin: str
sma: str
daytrade_count: int
balance_asof: str

View File

@@ -0,0 +1,21 @@
from pydantic import BaseModel
class DrakdooMarket(BaseModel):
exchange: str
item: str
currency: str
contract: str
class DrakdooTimestamp(BaseModel):
sent: int
trade: int
class DrakdooCallback(BaseModel):
title: str
message: str
period: str
market: DrakdooMarket
timestamp: DrakdooTimestamp

View File

@@ -0,0 +1 @@
from pydantic import BaseModel

View File

@@ -1,125 +0,0 @@
from serde import Model, fields
# {
# "id": "92f0b26b-4c98-4553-9c74-cdafc7e037db",
# "clientOrderId": "ccxt_26adcbf445674f01af38a66a15e6f5b5",
# "timestamp": 1666096856515,
# "datetime": "2022-10-18T12:40:56.515477181Z",
# "lastTradeTimeStamp": null,
# "status": "open",
# "symbol": "BTC/USD",
# "type": "market",
# "timeInForce": "gtc",
# "postOnly": null,
# "side": "buy",
# "price": null,
# "stopPrice": null,
# "cost": null,
# "average": null,
# "amount": 1.1,
# "filled": 0.0,
# "remaining": 1.1,
# "trades": [],
# "fee": null,
# "info": {
# "id": "92f0b26b-4c98-4553-9c74-cdafc7e037db",
# "client_order_id": "ccxt_26adcbf445674f01af38a66a15e6f5b5",
# "created_at": "2022-10-18T12:40:56.516095561Z",
# "updated_at": "2022-10-18T12:40:56.516173841Z",
# "submitted_at": "2022-10-18T12:40:56.515477181Z",
# "filled_at": null,
# "expired_at": null,
# "canceled_at": null,
# "failed_at": null,
# "replaced_at": null,
# "replaced_by": null,
# "replaces": null,
# "asset_id": "276e2673-764b-4ab6-a611-caf665ca6340",
# "symbol": "BTC/USD",
# "asset_class": "crypto",
# "notional": null,
# "qty": "1.1",
# "filled_qty": "0",
# "filled_avg_price": null,
# "order_class": "",
# "order_type": "market",
# "type": "market",
# "side": "buy",
# "time_in_force": "gtc",
# "limit_price": null,
# "stop_price": null,
# "status": "pending_new",
# "extended_hours": false,
# "legs": null,
# "trail_percent": null,
# "trail_price": null,
# "hwm": null,
# "subtag": null,
# "source": null
# },
# "fees": [],
# "lastTradeTimestamp": null
# }
class CCXTInfo(Model):
id = fields.Uuid()
client_order_id = fields.Str()
created_at = fields.Str()
updated_at = fields.Str()
submitted_at = fields.Str()
filled_at = fields.Optional(fields.Str())
expired_at = fields.Optional(fields.Str())
canceled_at = fields.Optional(fields.Str())
failed_at = fields.Optional(fields.Str())
replaced_at = fields.Optional(fields.Str())
replaced_by = fields.Optional(fields.Str())
replaces = fields.Optional(fields.Str())
asset_id = fields.Uuid()
symbol = fields.Str()
asset_class = fields.Str()
notional = fields.Optional(fields.Str())
qty = fields.Str()
filled_qty = fields.Str()
filled_avg_price = fields.Optional(fields.Str())
order_class = fields.Str()
order_type = fields.Str()
type = fields.Str()
side = fields.Str()
time_in_force = fields.Str()
limit_price = fields.Optional(fields.Str())
stop_price = fields.Optional(fields.Str())
status = fields.Str()
extended_hours = fields.Bool()
legs = fields.Optional(fields.List(fields.Nested("CCXTInfo")))
trail_percent = fields.Optional(fields.Str())
trail_price = fields.Optional(fields.Str())
hwm = fields.Optional(fields.Str())
subtag = fields.Optional(fields.Str())
source = fields.Optional(fields.Str())
class CCXTRoot(Model):
id = fields.Uuid()
clientOrderId = fields.Str()
timestamp = fields.Int()
datetime = fields.Str()
lastTradeTimeStamp = fields.Optional(fields.Str())
status = fields.Str()
symbol = fields.Str()
type = fields.Str()
timeInForce = fields.Str()
postOnly = fields.Optional(fields.Str())
side = fields.Str()
price = fields.Optional(fields.Float())
stopPrice = fields.Optional(fields.Float())
cost = fields.Optional(fields.Float())
average = fields.Optional(fields.Float())
amount = fields.Float()
filled = fields.Float()
remaining = fields.Float()
trades = fields.Optional(fields.List(fields.Dict()))
fee = fields.Optional(fields.Float())
info = fields.Nested(CCXTInfo)
fees = fields.Optional(fields.List(fields.Dict()))
lastTradeTimestamp = fields.Optional(fields.Str())