Placing orders
wetrade makes it easy to place stock trades using StopOrder,
LimitOrder, StopLimitOrder, MarketOrder objects. There is also a
provided ConvertingStopOrder which converts from a stop order to a market
order when out of range or rejected.
See below for examples on placing orders with wetrade:
Example usage
After creating a new Order object, you’ll need to run place_order() to place the order with E-Trade. You can then check your status, cancel or update your order and react to order updates.
import time
from wetrade.api import APIClient
from wetrade.account import Account
from wetrade.order import LimitOrder
def main():
my_api_client = APIClient()
account = Account(client=my_api_client)
# Create then place order
my_order = LimitOrder(
client = my_api_client,
account_key = account.account_key,
symbol = 'NVDA',
action = 'BUY',
quantity = 1,
price = 50.00)
my_order.place_order()
# Update your order's price
my_order.update_price(55.00)
# Check your order status
status = my_order.check_status()
print(f'Order status: {status}')
# Print an update when your order is canceled
my_order.run_when_status(
'CANCELLED',
func = print,
func_args = ['Your order has been canceled'])
# Cancel your order
my_order.cancel_order()
if __name__ == '__main__':
main()
Detailed usage
- class wetrade.order.BaseOrder(client: APIClient, account_key, symbol, action, quantity, price, security_type='EQ')
A base order class containing methods use in other order types
- Parameters:
account_key (str) – your account key
symbol (str) – the symbol of your security
action (str) – The action for your order (BUY, SELL, BUY_TO_COVER, SELL_SHORT, BUY_OPEN, BUY_CLOSE, SELL_OPEN, SELL_CLOSE, EXCHANGE)
quantity (int) – the quantity for your order
price (float) – the price for your order
- cancel_order()
Cancels your active, already-placed order
- check_status()
Checks the status of an already placed order
- log_order_update(update='executed')
- place_order()
Places your order
- run_when_status(status, func, func_args=[], func_kwargs={})
Runs a callback when your order reaches a certain status without waiting
- Parameters:
status (str) – your anticipated status (OPEN, EXECUTED, CANCELLED, INDIVIDUAL_FILLS, CANCEL_REQUESTED, EXPIRED, REJECTED)
then – (optional) a callback function to run after status is met
args (list) – a list of args for your function
kwargs (dict) – a dict containing kwargs for your function
- to_market_order()
Converts an active, already-placed order into a market order which will execute immediately during market hours
- update_price(new_price)
Updates the price of an already placed order
- Parameters:
new_price (float) – the new price for your order
- wait_for_status(status, then=None, args=[], kwargs={})
Waits for your order to reach your specified status then runs an optional callback function
- Parameters:
status (str) – the status to wait for (OPEN, EXECUTED, CANCELLED, INDIVIDUAL_FILLS, CANCEL_REQUESTED, EXPIRED, REJECTED)
then – (optional) a callback function to run after waiting for status
args (list) – a list of args for your function
kwargs (dict) – a dict containing kwargs for your function
- class wetrade.order.StopOrder(client: APIClient, account_key, symbol, action, quantity, price, security_type='EQ')
A stop order
- class wetrade.order.LimitOrder(client: APIClient, account_key, symbol, action, quantity, price, security_type='EQ')
A limit order
- class wetrade.order.StopLimitOrder(client: APIClient, account_key, symbol, action, quantity, price, security_type='EQ')
A stop limit order