Package: tdameritrade-ext
InformationThis package extend fromhttps://github.com/timkpaine/tdameritradeI crawled data options from TD Ameritrade API with thousand requests by hour
and my application has generated over 11k tokens in the last few hours when I use tdameritrade package
maybe when I send a get request to TDA then a token generated so TD Ameritrade API team notice me. Then I used redis to keep token and using it in 30minute before re-generated.
I only modified session.py file and keep others.
Bonus, I created 2 methods to get history stock price and options straddle as Yahoo Options.Installationpipinstallpipinstalltdameritrade_ext--upgradeGet OptionsGet optionsfromtdameritrade_ext.clientimportTDClient
importtimec=TDClient()data=c.options('AAPL',fromDate=time.strftime("%Y-%m-%d"))Get Options Chain as Straddle (similar Yahoo Options)defget_option_chain(ticker):df=Nonetry:c=TDClient()df=c.options(ticker,fromDate=time.strftime("%Y-%m-%d"))exceptAssertionErrorase:print("error 1",e)log.exception(e)exceptExceptionase:print("error 2",e)log.exception(e)ifdf==None:returnNoneputExp=df['putExpDateMap']puts={}fordate,putinputExp.items():dates=date.split(":")date_expiration=dates[0]itemputs={}forstrike,itemstrikesinput.items():ifstrike[-2:]=='.0':strike=round(float(strike))itemstrike=itemstrikes[0]itemput={'strike':strike,'put':{'contractSymbol':itemstrike['symbol'],'strike':strike,'lastPrice':itemstrike['last'],'change':itemstrike['netChange'],'percentChange':itemstrike['percentChange'],'volume':itemstrike['totalVolume'],'openInterest':itemstrike['openInterest'],'impliedVolatility':itemstrike['volatility'],'open':itemstrike['openPrice'],'high':itemstrike['highPrice'],'low':itemstrike['lowPrice'],'close':itemstrike['closePrice'],'date':itemstrike['tradeTimeInLong'],'bid':itemstrike['bid'],'ask':itemstrike['ask']}}itemputs[strike]=itemputputs[date_expiration]=itemputscallExp=df['callExpDateMap']calls={}fordate,callincallExp.items():dates=date.split(":")date_expiration=dates[0]put=puts[date_expiration]ifdate_expirationinputselse{}itemcalls=[]forstrike,itemstrikesincall.items():ifstrike[-2:]=='.0':strike=round(float(strike))itemstrike=itemstrikes[0]itemcall={'strike':strike,'call':{'contractSymbol':itemstrike['symbol'],'strike':strike,'lastPrice':itemstrike['last'],'change':itemstrike['netChange'],'percentChange':itemstrike['percentChange'],'volume':itemstrike['totalVolume'],'openInterest':itemstrike['openInterest'],'impliedVolatility':itemstrike['volatility'],'open':itemstrike['openPrice'],'high':itemstrike['highPrice'],'low':itemstrike['lowPrice'],'close':itemstrike['closePrice'],'date':itemstrike['tradeTimeInLong'],'bid':itemstrike['bid'],'ask':itemstrike['ask']},'put':put[strike]['put']ifstrikeinputelse{}}itemcalls.append(itemcall)calls[date_expiration]=itemcallsreturncallsGet history by ticker and interval: 1m, 5m, 10m, 15m, 30m, 1h, 1ddefget_data_ticker(ticker,interval):c=TDClient()period_type='day'period=1frequency_type='minute'frequency=1if(interval=='5m'):frequency=5period=2elif(interval=='10m'):frequency=10period=3elif(interval=='15m'):frequency=15period=5elif(interval=='30m'):frequency=30period=10elif(interval=='1h'):frequency=30period=2elif(interval=='1d'):period_type='month'period=6frequency_type='daily'frequency=1resp=c.history(symbol=ticker,periodType=period_type,period=period,frequencyType=frequency_type,frequency=frequency)if'candles'notinresp:returnNonecandles=resp['candles']if(interval=='1h'):datas=[]foritemincandles:date_time=datetime.fromtimestamp(item['datetime']/1000)m=date_time.minuteifm==0:datas.append(item)else:datas=candlesreturndatas