Implement more validation and conversion
This commit is contained in:
@@ -1,42 +1,5 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
a = {
|
||||
"positions": [
|
||||
{
|
||||
"instrument": "EUR_USD",
|
||||
"long": {
|
||||
"units": "1",
|
||||
"averagePrice": "0.99361",
|
||||
"pl": "-0.1014",
|
||||
"resettablePL": "-0.1014",
|
||||
"financing": "0.0000",
|
||||
"dividendAdjustment": "0.0000",
|
||||
"guaranteedExecutionFees": "0.0000",
|
||||
"tradeIDs": ["71"],
|
||||
"unrealizedPL": "-0.0002",
|
||||
},
|
||||
"short": {
|
||||
"units": "0",
|
||||
"pl": "0.0932",
|
||||
"resettablePL": "0.0932",
|
||||
"financing": "0.0000",
|
||||
"dividendAdjustment": "0.0000",
|
||||
"guaranteedExecutionFees": "0.0000",
|
||||
"unrealizedPL": "0.0000",
|
||||
},
|
||||
"pl": "-0.0082",
|
||||
"resettablePL": "-0.0082",
|
||||
"financing": "0.0000",
|
||||
"commission": "0.0000",
|
||||
"dividendAdjustment": "0.0000",
|
||||
"guaranteedExecutionFees": "0.0000",
|
||||
"unrealizedPL": "-0.0002",
|
||||
"marginUsed": "0.0286",
|
||||
}
|
||||
],
|
||||
"lastTransactionID": "71",
|
||||
}
|
||||
|
||||
|
||||
class PositionLong(BaseModel):
|
||||
units: str
|
||||
@@ -79,44 +42,6 @@ class OpenPositions(BaseModel):
|
||||
lastTransactionID: str
|
||||
|
||||
|
||||
{
|
||||
"positions": [
|
||||
{
|
||||
"instrument": "EUR_USD",
|
||||
"long": {
|
||||
"units": "1",
|
||||
"averagePrice": "0.99361",
|
||||
"pl": "-0.1014",
|
||||
"resettablePL": "-0.1014",
|
||||
"financing": "-0.0002",
|
||||
"dividendAdjustment": "0.0000",
|
||||
"guaranteedExecutionFees": "0.0000",
|
||||
"tradeIDs": ["71"],
|
||||
"unrealizedPL": "-0.0044",
|
||||
},
|
||||
"short": {
|
||||
"units": "0",
|
||||
"pl": "0.0932",
|
||||
"resettablePL": "0.0932",
|
||||
"financing": "0.0000",
|
||||
"dividendAdjustment": "0.0000",
|
||||
"guaranteedExecutionFees": "0.0000",
|
||||
"unrealizedPL": "0.0000",
|
||||
},
|
||||
"pl": "-0.0082",
|
||||
"resettablePL": "-0.0082",
|
||||
"financing": "-0.0002",
|
||||
"commission": "0.0000",
|
||||
"dividendAdjustment": "0.0000",
|
||||
"guaranteedExecutionFees": "0.0000",
|
||||
"unrealizedPL": "-0.0044",
|
||||
"marginUsed": "0.0287",
|
||||
}
|
||||
],
|
||||
"lastTransactionID": "73",
|
||||
}
|
||||
|
||||
|
||||
def parse_prices(x):
|
||||
if float(x["long"]["units"]) > 0:
|
||||
return x["long"]["averagePrice"]
|
||||
@@ -168,3 +93,196 @@ OpenPositionsSchema = {
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
class AccountDetailsNested(BaseModel):
|
||||
guaranteedStopLossOrderMode: str
|
||||
hedgingEnabled: bool
|
||||
id: str
|
||||
createdTime: str
|
||||
currency: str
|
||||
createdByUserID: int
|
||||
alias: str
|
||||
marginRate: str
|
||||
lastTransactionID: str
|
||||
balance: str
|
||||
openTradeCount: int
|
||||
openPositionCount: int
|
||||
pendingOrderCount: int
|
||||
pl: str
|
||||
resettablePL: str
|
||||
resettablePLTime: str
|
||||
financing: str
|
||||
commission: str
|
||||
dividendAdjustment: str
|
||||
guaranteedExecutionFees: str
|
||||
orders: list # Order
|
||||
positions: list # Position
|
||||
trades: list # Trade
|
||||
unrealizedPL: str
|
||||
NAV: str
|
||||
marginUsed: str
|
||||
marginAvailable: str
|
||||
positionValue: str
|
||||
marginCloseoutUnrealizedPL: str
|
||||
marginCloseoutNAV: str
|
||||
marginCloseoutMarginUsed: str
|
||||
marginCloseoutPositionValue: str
|
||||
marginCloseoutPercent: str
|
||||
withdrawalLimit: str
|
||||
marginCallMarginUsed: str
|
||||
marginCallPercent: str
|
||||
|
||||
|
||||
class AccountDetails(BaseModel):
|
||||
account: AccountDetailsNested
|
||||
lastTransactionID: str
|
||||
|
||||
|
||||
AccountDetailsSchema = {
|
||||
"guaranteedSLOM": "account.guaranteedStopLossOrderMode",
|
||||
"hedgingEnabled": "account.hedgingEnabled",
|
||||
"id": "account.id",
|
||||
"created_at": "account.createdTime",
|
||||
"currency": "account.currency",
|
||||
"createdByUserID": "account.createdByUserID",
|
||||
"alias": "account.alias",
|
||||
"marginRate": "account.marginRate",
|
||||
"lastTransactionID": "account.lastTransactionID",
|
||||
"balance": "account.balance",
|
||||
"openTradeCount": "account.openTradeCount",
|
||||
"openPositionCount": "account.openPositionCount",
|
||||
"pendingOrderCount": "account.pendingOrderCount",
|
||||
"pl": "account.pl",
|
||||
"resettablePL": "account.resettablePL",
|
||||
"resettablePLTime": "account.resettablePLTime",
|
||||
"financing": "account.financing",
|
||||
"commission": "account.commission",
|
||||
"dividendAdjustment": "account.dividendAdjustment",
|
||||
"guaranteedExecutionFees": "account.guaranteedExecutionFees",
|
||||
# "orders": "account.orders",
|
||||
# "positions": "account.positions",
|
||||
# "trades": "account.trades",
|
||||
"unrealizedPL": "account.unrealizedPL",
|
||||
"NAV": "account.NAV",
|
||||
"marginUsed": "account.marginUsed",
|
||||
"marginAvailable": "account.marginAvailable",
|
||||
"positionValue": "account.positionValue",
|
||||
"marginCloseoutUnrealizedPL": "account.marginCloseoutUnrealizedPL",
|
||||
"marginCloseoutNAV": "account.marginCloseoutNAV",
|
||||
"marginCloseoutMarginUsed": "account.marginCloseoutMarginUsed",
|
||||
"marginCloseoutPositionValue": "account.marginCloseoutPositionValue",
|
||||
"marginCloseoutPercent": "account.marginCloseoutPercent",
|
||||
"withdrawalLimit": "account.withdrawalLimit",
|
||||
"marginCallMarginUsed": "account.marginCallMarginUsed",
|
||||
"marginCallPercent": "account.marginCallPercent",
|
||||
}
|
||||
|
||||
|
||||
class PositionDetailsNested(BaseModel):
|
||||
instrument: str
|
||||
long: PositionLong
|
||||
short: PositionShort
|
||||
pl: str
|
||||
resettablePL: str
|
||||
financing: str
|
||||
commission: str
|
||||
dividendAdjustment: str
|
||||
guaranteedExecutionFees: str
|
||||
unrealizedPL: str
|
||||
marginUsed: str
|
||||
|
||||
|
||||
class PositionDetails(BaseModel):
|
||||
position: PositionDetailsNested
|
||||
lastTransactionID: str
|
||||
|
||||
|
||||
PositionDetailsSchema = {
|
||||
"symbol": "position.instrument",
|
||||
"long": "position.long",
|
||||
"short": "position.short",
|
||||
"pl": "position.pl",
|
||||
"resettablePL": "position.resettablePL",
|
||||
"financing": "position.financing",
|
||||
"commission": "position.commission",
|
||||
"dividendAdjustment": "position.dividendAdjustment",
|
||||
"guaranteedExecutionFees": "position.guaranteedExecutionFees",
|
||||
"unrealizedPL": "position.unrealizedPL",
|
||||
"marginUsed": "position.marginUsed",
|
||||
"price": lambda x: parse_prices(x["position"]),
|
||||
"units": lambda x: parse_units(x["position"]),
|
||||
"side": lambda x: parse_side(x["position"]),
|
||||
"value": lambda x: parse_value(x["position"]),
|
||||
}
|
||||
|
||||
|
||||
class InstrumentTag(BaseModel):
|
||||
type: str
|
||||
name: str
|
||||
|
||||
|
||||
class InstrumentFinancingDaysOfWeek(BaseModel):
|
||||
dayOfWeek: str
|
||||
daysCharged: int
|
||||
|
||||
|
||||
class InstrumentFinancing(BaseModel):
|
||||
longRate: str
|
||||
shortRate: str
|
||||
financingDaysOfWeek: list[InstrumentFinancingDaysOfWeek]
|
||||
|
||||
|
||||
class InstrumentGuaranteedRestriction(BaseModel):
|
||||
volume: str
|
||||
priceRange: str
|
||||
|
||||
|
||||
class Instrument(BaseModel):
|
||||
name: str
|
||||
type: str
|
||||
displayName: str
|
||||
pipLocation: int
|
||||
displayPrecision: int
|
||||
tradeUnitsPrecision: int
|
||||
minimumTradeSize: str
|
||||
maximumTrailingStopDistance: str
|
||||
minimumTrailingStopDistance: str
|
||||
maximumPositionSize: str
|
||||
maximumOrderUnits: str
|
||||
marginRate: str
|
||||
guaranteedStopLossOrderMode: str
|
||||
tags: list[InstrumentTag]
|
||||
financing: InstrumentFinancing
|
||||
guaranteedStopLossOrderLevelRestriction: InstrumentGuaranteedRestriction | None
|
||||
|
||||
|
||||
class AccountInstruments(BaseModel):
|
||||
instruments: list[Instrument]
|
||||
|
||||
|
||||
AccountInstrumentsSchema = {
|
||||
"itemlist": (
|
||||
"instruments",
|
||||
[
|
||||
{
|
||||
"name": "name",
|
||||
"type": "type",
|
||||
"displayName": "displayName",
|
||||
"pipLocation": "pipLocation",
|
||||
"displayPrecision": "displayPrecision",
|
||||
"tradeUnitsPrecision": "tradeUnitsPrecision",
|
||||
"minimumTradeSize": "minimumTradeSize",
|
||||
"maximumTrailingStopDistance": "maximumTrailingStopDistance",
|
||||
"minimumTrailingStopDistance": "minimumTrailingStopDistance",
|
||||
"maximumPositionSize": "maximumPositionSize",
|
||||
"maximumOrderUnits": "maximumOrderUnits",
|
||||
"marginRate": "marginRate",
|
||||
"guaranteedSLOM": "guaranteedStopLossOrderMode",
|
||||
"tags": "tags",
|
||||
"financing": "financing",
|
||||
"guaranteedSLOLR": "guaranteedStopLossOrderLevelRestriction",
|
||||
}
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user