Skip to content

Commit 41293ad

Browse files
committed
Add place order operations
1 parent 18b9f4e commit 41293ad

File tree

3 files changed

+140
-36
lines changed

3 files changed

+140
-36
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pie: Pie = trading212.fetch_pie(123)
3030
positions: List[Position] = trading212.fetch_all_open_positions()
3131

3232
trading212.create_pie(
33-
NewPieIn(
33+
Pie(
3434
name='My Pie',
3535
...
3636
)
@@ -56,10 +56,10 @@ trading212.create_pie(
5656
## Equity Orders
5757

5858
- [x] Fetch all
59-
- [ ] Place Limit order
60-
- [ ] Place Market order
61-
- [ ] Place Stop order
62-
- [ ] Place StopLimit order
59+
- [x] Place Limit order
60+
- [x] Place Market order
61+
- [x] Place Stop order
62+
- [x] Place StopLimit order
6363
- [ ] Cancel by ID
6464
- [ ] Fetch by ID
6565

python_trading212/models.py

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,23 @@ class Result(BaseModel):
102102

103103

104104
class Pie(BaseModel):
105-
cash: float
106-
dividendDetails: DividendDetails
107-
id: int
108-
progress: float
109-
result: Result
110-
status: str
105+
# Pie Out
106+
cash: Optional[float] = None
107+
dividendDetails: Optional[DividendDetails] = None
108+
id: Optional[int] = None
109+
progress: Optional[float] = None
110+
result: Optional[Result] = None
111+
status: Optional[str] = None
112+
# New Pie Input
113+
name: Optional[str] = None
114+
dividendCashAction: Optional[str] = None
115+
endDate: Optional[datetime] = None
116+
goal: Optional[float] = None
117+
icon: Optional[Icon] = None
118+
instrumentShares: Optional[Dict[str, float]] = None
119+
# New Pie Output
120+
instruments: Optional[List[Instrument]] = None
121+
settings: Optional[Settings] = None
111122

112123

113124
class Order(BaseModel):
@@ -140,15 +151,6 @@ class AccountMetadata(BaseModel):
140151
id: int
141152

142153

143-
class NewPieIn(BaseModel):
144-
name: str
145-
dividendCashAction: str
146-
endDate: datetime
147-
goal: float
148-
icon: Optional[Icon]
149-
instrumentShares: Dict[str, float]
150-
151-
152154
class Issue(BaseModel):
153155
name: str
154156
severity: str
@@ -167,11 +169,6 @@ class Settings(BaseModel):
167169
pubicUrl: str
168170

169171

170-
class NewPieOut(BaseModel):
171-
instruments: List[Instrument]
172-
settings: Settings
173-
174-
175172
class Tax(BaseModel):
176173
fillId: str
177174
name: str
@@ -207,3 +204,45 @@ class HistoryItem(BaseModel):
207204
class HistoricalOrderData(BaseModel):
208205
items: List[HistoryItem]
209206
nextPagePath: str
207+
208+
209+
class LimitOrder(BaseModel):
210+
limitPrice: float
211+
quantity: float
212+
ticker: str
213+
timeValidity: str
214+
215+
216+
class MarketOrder(BaseModel):
217+
quantity: float
218+
ticker: str
219+
220+
221+
class StopOrder(BaseModel):
222+
ticker: str
223+
quantity: float
224+
stopPrice: float
225+
timeValidity: str
226+
227+
228+
class StopLimitOrder(BaseModel):
229+
ticker: str
230+
quantity: float
231+
stopPrice: float
232+
limitPrice: float
233+
timeValidity: str
234+
235+
236+
class FilledOrder(BaseModel):
237+
creationTime: str
238+
filledQuantity: float
239+
filledValue: float
240+
id: int
241+
limitPrice: float
242+
quantity: float
243+
status: str
244+
stopPrice: float
245+
strategy: str
246+
ticker: str
247+
type: str
248+
value: float

python_trading212/trading212.py

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
from python_trading212.models import (
66
Position, Exchange, Instrument,
77
Pie, Order, AccountCash, AccountMetadata,
8-
NewPieIn, NewPieOut, HistoricalOrderData
8+
HistoricalOrderData, LimitOrder, FilledOrder,
9+
MarketOrder, StopOrder, StopLimitOrder
910
)
1011

1112

@@ -104,21 +105,21 @@ def fetch_all_pies(self) -> List[Pie]:
104105
pies.append(Pie(**pie))
105106
return pies
106107

107-
def create_pie(self, new_pie: NewPieIn) -> NewPieOut:
108+
def create_pie(self, pie: Pie) -> Pie:
108109
"""Creates a new pie
109110
110111
https://t212public-api-docs.redoc.ly/#operation/create
111112
112113
Args:
113-
new_pie (NewPieIn): the new pie to create
114+
pie (Pie): the new pie to create
114115
115116
Returns:
116-
NewPieOut: the new pie
117+
Pie: the new pie
117118
"""
118119
endpoint = self.url + "equity/pie"
119-
response = self._post(endpoint, new_pie.model_dump())
120+
response = self._post(endpoint, pie.model_dump())
120121

121-
return NewPieOut(**response)
122+
return Pie(**response)
122123

123124
def delete_pie(self, id: int) -> None:
124125
"""Deletes a pie by ID
@@ -147,22 +148,22 @@ def fetch_pie(self, id: int) -> Pie:
147148

148149
return Pie(**response)
149150

150-
def update_pie(self, id: int, new_pie: NewPieIn) -> NewPieOut:
151+
def update_pie(self, id: int, pie: Pie) -> Pie:
151152
"""Updates a pie by ID
152153
153154
https://t212public-api-docs.redoc.ly/#operation/update
154155
155156
Args:
156157
id (int): the ID of the pie
157-
new_pie (NewPieIn): the new pie
158+
pie (Pie): the new pie
158159
159160
Returns:
160-
NewPieOut: the new pie
161+
Pie: the new pie
161162
"""
162163
endpoint = self.url + f"equity/pie/{id}"
163-
response = self._post(endpoint, new_pie.model_dump())
164+
response = self._post(endpoint, pie.model_dump())
164165

165-
return NewPieOut(**response)
166+
return Pie(**response)
166167

167168
def fetch_all_orders(self) -> List[Order]:
168169
""" Fetches all orders
@@ -180,6 +181,70 @@ def fetch_all_orders(self) -> List[Order]:
180181
orders.append(Order(**order))
181182
return orders
182183

184+
def place_limit_order(self, limit_order: LimitOrder) -> FilledOrder:
185+
"""Places a limit order
186+
187+
https://t212public-api-docs.redoc.ly/#operation/placeLimitOrder
188+
189+
Args:
190+
limit_order (LimitOrder): the limit order to place
191+
192+
Returns:
193+
FilledOrder: the filled order
194+
"""
195+
endpoint = self.url + "equity/orders/limit"
196+
response = self._post(endpoint, limit_order.model_dump())
197+
198+
return FilledOrder(**response)
199+
200+
def place_market_order(self, market_order: MarketOrder) -> FilledOrder:
201+
"""Places a market order
202+
203+
https://t212public-api-docs.redoc.ly/#operation/placeMarketOrder
204+
205+
Args:
206+
market_order (MarketOrder): the market order to place
207+
208+
Returns:
209+
FilledOrder: the filled order
210+
"""
211+
endpoint = self.url + "equity/orders/market"
212+
response = self._post(endpoint, market_order.model_dump())
213+
214+
return FilledOrder(**response)
215+
216+
def place_stop_order(self, stop_order: StopOrder) -> FilledOrder:
217+
"""Places a stop order
218+
219+
https://t212public-api-docs.redoc.ly/#operation/placeStopOrder
220+
221+
Args:
222+
stop_order (StopOrder): the stop order to place
223+
224+
Returns:
225+
FilledOrder: the filled order
226+
"""
227+
endpoint = self.url + "equity/orders/stop"
228+
response = self._post(endpoint, stop_order.model_dump())
229+
230+
return FilledOrder(**response)
231+
232+
def place_stop_limit_order(self, stop_limit_order: StopLimitOrder) -> FilledOrder:
233+
"""Places a stop limit order
234+
235+
https://t212public-api-docs.redoc.ly/#operation/placeStopLimitOrder
236+
237+
Args:
238+
stop_limit_order (StopLimitOrder): the stop limit order to place
239+
240+
Returns:
241+
FilledOrder: the filled order
242+
"""
243+
endpoint = self.url + "equity/orders/stop_limit"
244+
response = self._post(endpoint, stop_limit_order.model_dump())
245+
246+
return FilledOrder(**response)
247+
183248
def fetch_order(self, id: int) -> Order:
184249
"""Fetches an order by ID
185250

0 commit comments

Comments
 (0)