Skip to content

Implement sell buy (Market & Limit) operations #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/wealthsimple/requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def post(self, URL, params=None):
except Exception as err:
print(err)


def get(self, URL, params=None):
"""Make a GET request to a given API endpoint

Expand Down
170 changes: 165 additions & 5 deletions src/wealthsimple/wealthsimple.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ def __init__(self, email: str, password: str, two_factor_callback: callable = No
self.login(email, password, two_factor_callback=two_factor_callback)

def login(
self,
email: str = None,
password: str = None,
two_factor_callback: callable = None,
self,
email: str = None,
password: str = None,
two_factor_callback: callable = None,
) -> None:
"""Login to Wealthsimple Trade account

Expand Down Expand Up @@ -247,7 +247,7 @@ def get_security(self, id: str) -> dict:
return response

def get_securities_from_ticker(self, symbol: str) -> list:
"""Get information about a securitys with matching ticker symbols
"""Get information about a securities with matching ticker symbols

Parameters
----------
Expand Down Expand Up @@ -338,3 +338,163 @@ def get_forex(self) -> dict:
"""
response = self.TradeAPI.makeRequest("GET", "forex")
return response.json()

def get_security_id_from_stock_symbol(self, stock_symbol: str) -> str:
"""Get Wealthsimple Security ID of a specific stock symbol (ticker)

Parameters
----------
stock_symbol : str
Security symbol to search for

Returns
-------
str
A str containing Wealthsimple Security ID for the stock
"""
response = self.TradeAPI.makeRequest("GET", f"securities?query={stock_symbol}")
response = response.json()
if response['total_count'] == 1:
# This check ensure we have an exact match for our search
return response["results"][0]['id']
else:
return ''

def place_order(self, order: dict) -> dict:

"""Posts an order (market) to Wealthsimple API

Parameters
----------
:param order:
The order dict containing the data to be submitted

Returns
-------
dict
A dict representing the submitted order
"""

response = self.TradeAPI.makeRequest("POST", "orders", order)
response = response.json()
return response["results"]

def market_buy(self, account_id, security_id, quantity):

"""Places a market buy order for to the Wealthsimple API under the specified account id
Parameters
----------
:param quantity: int
The The number of securities to Buy
:param security_id:
The Wealthsimple Security ID
:param account_id : str
The Wealthsimple Account id

Returns
-------
dict
A dict representing the returned order

"""

quote = self.get_security(security_id)['quote']['amount']
if quote:
order = {
"account_id": account_id,
"security_id": security_id,
"limit_price": str(quote),
"quantity": quantity,
"order_type": "buy_quantity",
"order_sub_type": "market",
"time_in_force": "day",
}
return self.place_order(order)
raise RuntimeError('Failed to get quote amount when submitting market buy order')

def market_sell(self, account_id, security_id, quantity):

"""Places a market sell order for to the Wealthsimple API under the specified account id
Parameters
----------
:param quantity: int
The The number of securities to Buy
:param security_id:
The Wealthsimple Security ID
:param account_id : str
The Wealthsimple Account id

Returns
-------
dict
A dict representing the returned order

"""
quote = self.get_security(security_id)['quote']['amount']
if quote:
order = {
"account_id": account_id,
"security_id": security_id,
"quantity": quantity,
"limit_price": quote,
"order_type": "sell_quantity",
"order_sub_type": "market",
"time_in_force": "day",
}
return self.place_order(order)
raise RuntimeError('Failed to get quote amount when submitting market sell order')

def limit_buy(self, account_id, security_id, quantity, limit_price) -> dict:

""" Places a limit buy order for the Wealthsimple API with the specified account_id and security_id
Parameters
----------
:param account_id:
:param security_id:
:param quantity:
:param limit_price:

Returns
----------
dict
A dict representing the order returned from the API
"""

order = {
"account_id": account_id,
"security_id": security_id,
"quantity": quantity,
"limit_price": limit_price,
"order_type": "buy_quantity",
"order_sub_type": "limit",
"time_in_force": "day",
}

return self.place_order(order)

def limit_sell(self, account_id, security_id, quantity, limit_price) -> dict:

""" Places a limit sell order for the Wealthsimple API with the specified parameters
Parameters
----------
:param account_id:
:param security_id:
:param quantity:
:param limit_price:

Returns
----------
dict
A dict representing the order returned from the API
"""

order = {
"account_id": account_id,
"security_id": security_id,
"quantity": quantity,
"limit_price": limit_price,
"order_type": "sell_quantity",
"order_sub_type": "limit",
"time_in_force": "day",
}
return self.place_order(order)