A Tradier client for trading stocks and options through Tradier API
Project description
fast_tradier_client in Python
Tradier client in python for trading stocks and options through Tradier API
Dependencies:
- jsonpickle
- httpx
- arrow
- pandas
- python-interface
Examples below:
- Test Account:
from fast_tradier.models.account.Position import Position
from fast_tradier.models.market_data.Quote import Quote
account_position = {
"cost_basis": 207.01,
"date_acquired": "2018-08-08T14:41:11.405Z",
"id": 130089,
"quantity": 1.00000000,
"symbol": "AAPL"
}
position = Position(account_position)
print(position.to_json())
print(position.symbol)
print(position.date_acquired)
print('-------' * 10)
quote1 = {
"symbol": "VXX190517P00016000",
"description": "VXX May 17 2019 $16.00 Put",
"exch": "Z",
"type": "option",
"last": None,
"change": None,
"volume": 0,
"open": None,
"high": None,
"low": None,
"close": None,
"bid": 0.0,
"ask": 0.01,
"underlying": "VXX",
"strike": 16.0,
"change_percentage": None,
"average_volume": 0,
"last_volume": 0,
"trade_date": 0,
"prevclose": None,
"week_52_high": 0.0,
"week_52_low": 0.0,
"bidsize": 0,
"bidexch": "I",
"bid_date": 1557167321000,
"asksize": 618,
"askexch": "Z",
"ask_date": 1557168367000,
"open_interest": 10,
"contract_size": 100,
"expiration_date": "2019-05-17",
"expiration_type": "standard",
"option_type": "put",
"root_symbol": "VXX"
}
my_q = Quote(quote1)
print(my_q.symbol)
print(my_q.ask_date_datetime)
- Test Client:
from fast_tradier.FastTradierAsyncClient import FastTradierAsyncClient
from fast_tradier.FastTradierClient import FastTradierClient
from fast_tradier.models.market_data.Quote import Quote
from fast_tradier.models.trading.OptionOrder import OptionLeg
from fast_tradier.models.trading.OptionOrder import OptionOrder
import asyncio
#TODO: replace the client_id and sandbox access token with yours
sandbox_client_id = 'VA123456789'
sandbox_at = 'abcdefghijklmnopqrstuvwxyzzzz'
def mock_order() -> OptionOrder:
ticker = 'SPX'
order_status = 'pending'
option_symbols = ['SPXW_080823C4510', 'SPXW_080823C4520'] #TODO: replace option symbols
sides = ['sell_to_open', 'buy_to_open']
option_legs = []
for i in range(len(sides)):
opt_symbol = option_symbols[i]
side = sides[i]
option_legs.append(OptionLeg(underlying_symbol=ticker, option_symbol=opt_symbol, side=side, quantity=1))
option_order = OptionOrder(ticker=ticker,
price=1.2,
price_type='credit',
duration='day',
option_legs=option_legs)
return option_order
async def async_test():
tasks = []
count = 4
tradier_client = FastTradierAsyncClient(sandbox_at, sandbox_client_id, is_prod=False)
quote1 = await tradier_client.get_quotes_async(['MSFT'])
print('quote1 last price: ', quote1[0].last)
for i in range(count):
tasks.append(asyncio.ensure_future(tradier_client.place_option_order_async(mock_order())))
order_ids = await asyncio.gather(*tasks)
cancel_tasks = []
for order_id in order_ids:
print('order_id: ', order_id)
cancel_tasks.append(asyncio.ensure_future(tradier_client.cancel_order_async(order_id)))
is_canceled = await asyncio.gather(*cancel_tasks)
for canceled in is_canceled:
print('canceled? ', canceled)
### get option chain for spx
ticker = 'spx'
expiration = '2023-08-31' #TODO: replace the expiration date
opt_chain_result = await tradier_client.get_option_chain_async(symbol=ticker, expiration=expiration)
print('result of option chain: ', opt_chain_result)
positions = await tradier_client.get_positions_async()
print('positions: ', positions)
print('------' * 10)
balances = tradier_client.get_account_balance()
print('balances: ', balances.total_cash)
if hasattr(balances, 'dt') and balances.pdt is not None:
print('balances.pdt.to_json(): ', balances.pdt.to_json())
elif hasattr(balances, 'margin') and balances.margin is not None:
print('balances.margin.to_json: ', balances.margin.to_json())
elif hasattr(balances, 'cash') and balances.cash is not None:
print('balances.cash.to_json: ', balances.cash.to_json())
def sync_test():
count = 4
tradier_client = FastTradierClient(sandbox_at, sandbox_client_id, is_prod=False)
quote1 = tradier_client.get_quotes(['MSFT'])
print('quote1 last price: ', quote1[0].last)
order_id = tradier_client.place_option_order(mock_order())
print('option order id: ', order_id)
canceled_order = tradier_client.cancel_order(order_id)
print('canceled order? ', canceled_order)
### get option chain for spx
ticker = 'spx'
expiration = '2023-08-31' #TODO: replace the expiration date
opt_chain_result = tradier_client.get_option_chain(symbol=ticker, expiration=expiration)
print('result of option chain: ', opt_chain_result)
positions = tradier_client.get_positions()
print('positions: ', positions)
print('------' * 10)
balances = tradier_client.get_account_balance()
print('balances: ', balances.total_cash)
if hasattr(balances, 'dt') and balances.pdt is not None:
print('balances.pdt.to_json(): ', balances.pdt.to_json())
elif hasattr(balances, 'margin') and balances.margin is not None:
print('balances.margin.to_json: ', balances.margin.to_json())
elif hasattr(balances, 'cash') and balances.cash is not None:
print('balances.cash.to_json: ', balances.cash.to_json())
asyncio.run(async_test())
print('-------finished async tests--------')
sync_test()
print('-------finished sync tests-------')
- Test model like
TradierQuote
:
import json
from fast_tradier.models.market_data.TradierQuote import TradierQuote
from fast_tradier.models.trading.OptionOrder import OptionOrder, OptionLeg
quote1 = TradierQuote(symbol='spx', type='stock', open=1000.0, high=2012.1, low=1999.0, close=4910.1, volume=30000, bid=1.2, ask=2.3, last_price=4.3)
json_obj = quote1.serialize()
print('serialize: ', json_obj)
quote2 = TradierQuote.deserialize_from_json(json_obj)
print('quote2.ask: ', quote2.ask)
print('--------' * 10)
ticker = 'SPX'
order_status = 'pending'
option_symbols = ['SPXW_052223C4225', 'SPXW_052223C4235']
sides = ['sell_to_open', 'buy_to_open']
option_legs = []
for i in range(len(sides)):
opt_symbol = option_symbols[i]
side = sides[i]
option_legs.append(OptionLeg(underlying_symbol=ticker, option_symbol=opt_symbol, side=side, quantity=1))
option_order = OptionOrder(ticker=ticker,
price=100.0,
price_type='market',
duration='day',
option_legs=option_legs)
order_json = option_order.to_json()
print('option_order json: ', order_json)
parsable_json = option_order.serialize()
print('parsable order json: ', parsable_json)
option_order2 = OptionOrder.deserialize_from_json(parsable_json)
print('option_order2.price: ', option_order2.price)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
fast_tradier_client-0.0.2.tar.gz
(14.7 kB
view hashes)
Built Distribution
Close
Hashes for fast_tradier_client-0.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7e495b1448dee80f50545584380974fb5a6d1e8126556c60a6c1539bb8dbaf44 |
|
MD5 | 383f3fef0ce1dbc84184ace5b91fb38e |
|
BLAKE2b-256 | 1d8c6054ffd0215f6963348726a20d04ed1da8a348d168043c84c2a6cf6ac066 |
Close
Hashes for fast_tradier_client-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7bd57e3fd047ccff98a39f1f68a7341f84ee1e4cdbbab5239b8050969a33dbc5 |
|
MD5 | 99c41714175c22d9370cea3cc84eac99 |
|
BLAKE2b-256 | 8d7a373f31251926d3b326439d73712f27b1adbad33aeb26b7b2dbc46d868d28 |