tastyware / tastytrade

An unofficial, sync/async Python SDK for Tastytrade!
https://tastyworks-api.rtfd.io
MIT License
127 stars 43 forks source link

How create the symbol to get candles of certain periods #62

Closed Quenos closed 1 year ago

Quenos commented 1 year ago

I can't wrap my head around how to get candles for certain periods.

Is it added to the symbol in the symbol list? I've tried:

subs_list = ['BTCUSD&period=10m']
# this function fetches quotes once, then closes the subscription
await streamer.subscribe(EventType.CANDLE, subs_list)

Also had some other permutations without '&' and '=' etc, but just can't get it to work. Also the DXfeed docs don't make me any smarter.

Graeme22 commented 1 year ago

Hey! If you look at the SDK docs (it could probably be a bit clearer, sorry about that...), you actually need to use a separate subscription function for candles:

from tastytrade.instruments import Cryptocurrency
from datetime import datetime, timedelta

btc = Cryptocurrency.get_cryptocurrency(session, "BTC/USD")
await streamer.subscribe_candle(btc.streamer_symbol, start_time=datetime.now() - timedelta(minutes=30), interval='5m')
n = 0
async for data in streamer.listen_candle():
    n += 1
    print(data)
    if n > 5:
        break

I got this output:

Candle(eventSymbol='BTC/USD:CXTALP{=5m}', eventTime=0, eventFlags=4, index=7252381703287603200, time=1688576700000, sequence=0, count=41, open=30470.4291875, high=30481.4343737, low=30464.4982534, close=30481.4343737, volume='NaN', vwap='NaN', bidVolume='NaN', askVolume='NaN', impVolatility='NaN', openInterest=0.0)
Candle(eventSymbol='BTC/USD:CXTALP{=5m}', eventTime=0, eventFlags=0, index=7252380414797414400, time=1688576400000, sequence=0, count=243, open=30463.1579548, high=30479.4223798, low=30448.2561021, close=30470.5827183, volume='NaN', vwap='NaN', bidVolume='NaN', askVolume='NaN', impVolatility='NaN', openInterest=0.0)
Candle(eventSymbol='BTC/USD:CXTALP{=5m}', eventTime=0, eventFlags=0, index=7252379126307225600, time=1688576100000, sequence=0, count=230, open=30450.3426869, high=30491.3908612, low=30450.3426869, close=30463.1566781, volume='NaN', vwap='NaN', bidVolume='NaN', askVolume='NaN', impVolatility='NaN', openInterest=0.0)
Candle(eventSymbol='BTC/USD:CXTALP{=5m}', eventTime=0, eventFlags=0, index=7252377837817036800, time=1688575800000, sequence=0, count=218, open=30436.0089948, high=30450.7425733, low=30425.7046032, close=30449.6946234, volume='NaN', vwap='NaN', bidVolume='NaN', askVolume='NaN', impVolatility='NaN', openInterest=0.0)
Candle(eventSymbol='BTC/USD:CXTALP{=5m}', eventTime=0, eventFlags=0, index=7252376549326848000, time=1688575500000, sequence=0, count=241, open=30445.2262747, high=30446.7292009, low=30426.3221241, close=30436.5038203, volume='NaN', vwap='NaN', bidVolume='NaN', askVolume='NaN', impVolatility='NaN', openInterest=0.0)
Candle(eventSymbol='BTC/USD:CXTALP{=5m}', eventTime=0, eventFlags=0, index=7252375260836659200, time=1688575200000, sequence=0, count=221, open=30448.7940944, high=30448.7940944, low=30432.2586602, close=30445.22495, volume='NaN', vwap='NaN', bidVolume='NaN', askVolume='NaN', impVolatility='NaN', openInterest=0.0)
Quenos commented 1 year ago

Totally overlooked it in the SDK. Probably because I was not looking for a separate method :( But it works. Thanks a bundle.