Design A Trader
~4 mins read
Trade financial instruments like stocks, bonds, and currencies on one or more exchanges, in real-time
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
BasePoint = Decimal(1/10000)
UID = str
class UID:
...
class UIDGenerator:
def generate()->UID:
...
class Asset:
symbol: str
name: str
class Currency(Asset):
...
class Crypto(Currency):
...
class CurrencyPair:
base: Currency
quote : Currency
@property
def symbol(self):
return self.base.symbol + self.quote.symbol
def __str__(self):
return f"{self.base}_{self.quote}"
class Stock(Asset):
...
class Bond(Asset):
...
class ETF(Asset):
...
class AssetBalance:
asset: Asset
free: Decimal
locked: Decimal
class OrderBook:
ask: Decimal
mid: Decimal
bid: Decimal
spread: Decimal
class OrderSide(Enum):
LONG = 1
SHORT = 2
class OrderType(Enum):
MARKET = 1
LIMIT = 2
class Order:
side: OrderSide
typ: OrderType
class DataSource:
def stream(asset):
class ExchangeAPI:
name: str
def auth():
...
def get_balance():
...
def submit_order(order:Order):
...
def cancel_order(uid):
...
def cancel_open_orders():
...
def close_all_positions():
...
def deposit(asset:Asset, amount:Decimal)->AssetBalance:
...
def withdraw(asset:Asset, amount:Decimal)->AssetBalance:
...
class DecisionInput:
...
class Decision:
inputs: list[DecisionInput]
output: Optional[Order]
class Strategy:
uid: UID
name: str
def decide(list[DecisionInput])->Decision:
...
class Trader:
strategy: Strategy
exchange: ExchangeAPI