Skip to content

Commit

Permalink
Add place order operations
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoelho93 committed Apr 27, 2024
1 parent 18b9f4e commit 41293ad
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 36 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pie: Pie = trading212.fetch_pie(123)
positions: List[Position] = trading212.fetch_all_open_positions()

trading212.create_pie(
NewPieIn(
Pie(
name='My Pie',
...
)
Expand All @@ -56,10 +56,10 @@ trading212.create_pie(
## Equity Orders

- [x] Fetch all
- [ ] Place Limit order
- [ ] Place Market order
- [ ] Place Stop order
- [ ] Place StopLimit order
- [x] Place Limit order
- [x] Place Market order
- [x] Place Stop order
- [x] Place StopLimit order
- [ ] Cancel by ID
- [ ] Fetch by ID

Expand Down
79 changes: 59 additions & 20 deletions python_trading212/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,23 @@ class Result(BaseModel):


class Pie(BaseModel):
cash: float
dividendDetails: DividendDetails
id: int
progress: float
result: Result
status: str
# Pie Out
cash: Optional[float] = None
dividendDetails: Optional[DividendDetails] = None
id: Optional[int] = None
progress: Optional[float] = None
result: Optional[Result] = None
status: Optional[str] = None
# New Pie Input
name: Optional[str] = None
dividendCashAction: Optional[str] = None
endDate: Optional[datetime] = None
goal: Optional[float] = None
icon: Optional[Icon] = None
instrumentShares: Optional[Dict[str, float]] = None
# New Pie Output
instruments: Optional[List[Instrument]] = None
settings: Optional[Settings] = None


class Order(BaseModel):
Expand Down Expand Up @@ -140,15 +151,6 @@ class AccountMetadata(BaseModel):
id: int


class NewPieIn(BaseModel):
name: str
dividendCashAction: str
endDate: datetime
goal: float
icon: Optional[Icon]
instrumentShares: Dict[str, float]


class Issue(BaseModel):
name: str
severity: str
Expand All @@ -167,11 +169,6 @@ class Settings(BaseModel):
pubicUrl: str


class NewPieOut(BaseModel):
instruments: List[Instrument]
settings: Settings


class Tax(BaseModel):
fillId: str
name: str
Expand Down Expand Up @@ -207,3 +204,45 @@ class HistoryItem(BaseModel):
class HistoricalOrderData(BaseModel):
items: List[HistoryItem]
nextPagePath: str


class LimitOrder(BaseModel):
limitPrice: float
quantity: float
ticker: str
timeValidity: str


class MarketOrder(BaseModel):
quantity: float
ticker: str


class StopOrder(BaseModel):
ticker: str
quantity: float
stopPrice: float
timeValidity: str


class StopLimitOrder(BaseModel):
ticker: str
quantity: float
stopPrice: float
limitPrice: float
timeValidity: str


class FilledOrder(BaseModel):
creationTime: str
filledQuantity: float
filledValue: float
id: int
limitPrice: float
quantity: float
status: str
stopPrice: float
strategy: str
ticker: str
type: str
value: float
87 changes: 76 additions & 11 deletions python_trading212/trading212.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from python_trading212.models import (
Position, Exchange, Instrument,
Pie, Order, AccountCash, AccountMetadata,
NewPieIn, NewPieOut, HistoricalOrderData
HistoricalOrderData, LimitOrder, FilledOrder,
MarketOrder, StopOrder, StopLimitOrder
)


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

def create_pie(self, new_pie: NewPieIn) -> NewPieOut:
def create_pie(self, pie: Pie) -> Pie:
"""Creates a new pie
https://t212public-api-docs.redoc.ly/#operation/create
Args:
new_pie (NewPieIn): the new pie to create
pie (Pie): the new pie to create
Returns:
NewPieOut: the new pie
Pie: the new pie
"""
endpoint = self.url + "equity/pie"
response = self._post(endpoint, new_pie.model_dump())
response = self._post(endpoint, pie.model_dump())

return NewPieOut(**response)
return Pie(**response)

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

return Pie(**response)

def update_pie(self, id: int, new_pie: NewPieIn) -> NewPieOut:
def update_pie(self, id: int, pie: Pie) -> Pie:
"""Updates a pie by ID
https://t212public-api-docs.redoc.ly/#operation/update
Args:
id (int): the ID of the pie
new_pie (NewPieIn): the new pie
pie (Pie): the new pie
Returns:
NewPieOut: the new pie
Pie: the new pie
"""
endpoint = self.url + f"equity/pie/{id}"
response = self._post(endpoint, new_pie.model_dump())
response = self._post(endpoint, pie.model_dump())

return NewPieOut(**response)
return Pie(**response)

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

def place_limit_order(self, limit_order: LimitOrder) -> FilledOrder:
"""Places a limit order
https://t212public-api-docs.redoc.ly/#operation/placeLimitOrder
Args:
limit_order (LimitOrder): the limit order to place
Returns:
FilledOrder: the filled order
"""
endpoint = self.url + "equity/orders/limit"
response = self._post(endpoint, limit_order.model_dump())

return FilledOrder(**response)

def place_market_order(self, market_order: MarketOrder) -> FilledOrder:
"""Places a market order
https://t212public-api-docs.redoc.ly/#operation/placeMarketOrder
Args:
market_order (MarketOrder): the market order to place
Returns:
FilledOrder: the filled order
"""
endpoint = self.url + "equity/orders/market"
response = self._post(endpoint, market_order.model_dump())

return FilledOrder(**response)

def place_stop_order(self, stop_order: StopOrder) -> FilledOrder:
"""Places a stop order
https://t212public-api-docs.redoc.ly/#operation/placeStopOrder
Args:
stop_order (StopOrder): the stop order to place
Returns:
FilledOrder: the filled order
"""
endpoint = self.url + "equity/orders/stop"
response = self._post(endpoint, stop_order.model_dump())

return FilledOrder(**response)

def place_stop_limit_order(self, stop_limit_order: StopLimitOrder) -> FilledOrder:
"""Places a stop limit order
https://t212public-api-docs.redoc.ly/#operation/placeStopLimitOrder
Args:
stop_limit_order (StopLimitOrder): the stop limit order to place
Returns:
FilledOrder: the filled order
"""
endpoint = self.url + "equity/orders/stop_limit"
response = self._post(endpoint, stop_limit_order.model_dump())

return FilledOrder(**response)

def fetch_order(self, id: int) -> Order:
"""Fetches an order by ID
Expand Down

0 comments on commit 41293ad

Please sign in to comment.