Package: tastyworks-api
Tastyworks API (Unofficial)A simple, async-based, reverse-engineered API for tastyworks. This will allow you to create trading algorithms for whatever strategies you may have.Please note that this is in the very early stages of development so any and all contributions are welcome. Please submit an issue and/or a pull request.This is a fork with modified and added features. You can find the original (unmaintained) GitHub repo at:https://github.com/boyan-soubachov/tastyworks_apiInstallation$ pip install tastyworks-apiUsageHere's a simple example which showcases many different aspects of the API:importasynciofromdatetimeimportdatefromdecimalimportDecimalasDfromtastyworks.modelsimportoption_chain,underlyingfromtastyworks.models.greeksimportGreeksfromtastyworks.models.optionimportOption,OptionTypefromtastyworks.models.orderimport(Order,OrderDetails,OrderPriceEffect,OrderType)fromtastyworks.models.sessionimportTastyAPISessionfromtastyworks.models.trading_accountimportTradingAccountfromtastyworks.models.underlyingimportUnderlyingTypefromtastyworks.streamerimportDataStreamerfromtastyworks.tastyworks_apiimporttasty_sessionfromtastyworks.utilsimportget_third_fridayasyncdefmain_loop(session:TastyAPISession,streamer:DataStreamer):accounts=awaitTradingAccount.get_remote_accounts(session)acct=accounts[0]print(f'Accounts available:{accounts}')orders=awaitOrder.get_remote_orders(session,acct)print(f'Number of active orders:{len(orders)}')# Execute an orderdetails=OrderDetails(type=OrderType.LIMIT,price=D(400),price_effect=OrderPriceEffect.CREDIT)new_order=Order(details)opt=Option(ticker='AKS',quantity=1,expiry=get_third_friday(date.today()),strike=D(3),option_type=OptionType.CALL,underlying_type=UnderlyingType.EQUITY)new_order.add_leg(opt)res=awaitacct.execute_order(new_order,session,dry_run=True)print(f'Order executed successfully:{res}')# Get an options chainundl=underlying.Underlying('AKS')chain=awaitoption_chain.get_option_chain(session,undl)print(f'Chain strikes:{chain.get_all_strikes()}')# Get all expirations for the options for the above equity symbolexp=chain.get_all_expirations()# Choose the next expiration as an example & fetch the entire options chain for that expiration (all strikes)next_exp=exp[0]chain_next_exp=awaitoption_chain.get_option_chain(session,undl,next_exp)options=[]foroptioninchain_next_exp.options:options.append(option)# Get the greeks data for all option symbols via the streamer by subscribingoptions_symbols=[options[x].symbol_dxfforxinrange(len(options))]greeks_data=awaitstreamer.stream('Greeks',options_symbols)fordataingreeks_data:gd=Greeks().from_streamer_dict(data)# gd = Greeks(kwargs=data)idx_match=[options[x].symbol_dxfforxinrange(len(options))].index(gd.symbol)options[idx_match].greeks=gdprint('> Symbol:{}\tPrice:{}\tDelta{}'.format(gd.symbol,gd.price,gd.delta))quote=awaitstreamer.stream('Quote',sub_values)print(f'Received item:{quote}')awaitstreamer.close()if__name__=='__main__':tasty_client=tasty_session.create_new_session('foo','bar')streamer=DataStreamer(tasty_client)loop=asyncio.get_event_loop()try:loop.run_until_complete(main_loop(tasty_client,streamer))exceptException:print('Exception in main loop')finally:# find all futures/tasks still running and wait for them to finishpending_tasks=[taskfortaskinasyncio.Task.all_tasks()ifnottask.done()]loop.run_until_complete(asyncio.gather(*pending_tasks))loop.close()Guidelines and caveatsThere are a few useful things to know which will help you get the most out of this API and use it in the way it was intended.All objects are designed to be independent of each other in theirsteady-state. That is, unless executing an action, all objects are not bound to each other and have no knowledge of each other's awareness.One can have multiple sessions and, due to the inter-object independence, can execute identical actions on identical objects in different sessions.Given the above points, this APIdoes notimplement state management and synchronization (i.e. are my local object representations identical to the remote [Tastyworks] ones?). This is not an indefinitely closed matter and may be re-evaluated if the need arises.DisclaimerThis is an unofficial, reverse-engineered API for Tastyworks. There is no implied warranty for any actions and results which arise from using it.
The only guarantee I make is that you will lose all your money if you use this API.