Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alert streamer mapping, small fixes #49

Merged
merged 1 commit into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Tastytrade Python SDK

.. inclusion-marker

A simple, reverse-engineered SDK for Tastytrade built on their (mostly) public API. This will allow you to create trading algorithms for whatever strategies you may have quickly and painlessly in Python.
A simple, reverse-engineered SDK for Tastytrade built on their (now mostly public) API. This will allow you to create trading algorithms for whatever strategies you may have quickly and painlessly in Python.

Installation
------------
Expand Down
2 changes: 1 addition & 1 deletion tastytrade/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

API_URL = 'https://api.tastyworks.com'
CERT_URL = 'https://api.cert.tastyworks.com'
VERSION = '5.2'
VERSION = '5.3'


logger = logging.getLogger(__name__)
Expand Down
6 changes: 5 additions & 1 deletion tastytrade/dxfeed/timeandsale.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
@dataclass
class TimeAndSale(Event):
"""
TimeAndSale event represents a trade or other market event with a price, like market open/close price. TimeAndSale events are intended to provide information about trades in a continuous-time slice (unlike Trade events which are supposed to provide snapshots about the most recent trade). TimeAndSale events have a unique index that can be used for later correction/cancellation processing.
TimeAndSale event represents a trade or other market event with a price, like
market open/close price. TimeAndSale events are intended to provide information
about trades in a continuous-time slice (unlike Trade events which are supposed
to provide snapshots about the most recent trade). TimeAndSale events have a
unique index that can be used for later correction/cancellation processing.
"""
#: symbol of this event
eventSymbol: str
Expand Down
49 changes: 44 additions & 5 deletions tastytrade/streamer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import websockets

from tastytrade import logger
from tastytrade.account import Account
from tastytrade.account import (Account, AccountBalance, CurrentPosition,
TradingStatus)
from tastytrade.dxfeed import Channel
from tastytrade.dxfeed.candle import Candle
from tastytrade.dxfeed.event import Event, EventType
Expand All @@ -20,6 +21,7 @@
from tastytrade.dxfeed.theoprice import TheoPrice
from tastytrade.dxfeed.timeandsale import TimeAndSale
from tastytrade.dxfeed.trade import Trade
from tastytrade.order import PlacedOrder
from tastytrade.session import Session
from tastytrade.utils import TastytradeError, validate_response

Expand Down Expand Up @@ -102,14 +104,51 @@ async def _connect(self) -> None:
logger.debug('raw message: %s', raw_message)
await self._queue.put(json.loads(raw_message))

async def listen(self) -> AsyncIterator[Any]:
async def listen(self) -> AsyncIterator[Union[
AccountBalance,
CurrentPosition,
PlacedOrder,
TradingStatus,
dict # some possible messages are not yet implemented
]]:
"""
Iterate over non-heartbeat messages received from the streamer.
Iterate over non-heartbeat messages received from the streamer,
mapping them to their appropriate data class.
"""
while True:
data = await self._queue.get()
if data.get('action') != SubscriptionType.HEARTBEAT:
yield data
type_str = data.get('type')
if type_str is not None:
yield self._map_message(type_str, data['data'])
elif data.get('action') != 'heartbeat':
logger.debug('subscription message: %s', data)

def _map_message(self, type_str: str, data: dict) -> Union[
AccountBalance,
CurrentPosition,
PlacedOrder,
TradingStatus,
dict # some possible messages are not yet implemented
]:
"""
TODO: implement the following:
- OrderChain
- UnderlyingYearGainSummary
- User status related messages
- Watchlist related messages
- Quote alert messages
- Others?
"""
if type_str == 'AccountBalance':
return AccountBalance(**data)
elif type_str == 'CurrentPosition':
return CurrentPosition(**data)
elif type_str == 'Order':
return PlacedOrder(**data)
elif type_str == 'TradingStatus':
return TradingStatus(**data)
else:
return data

async def account_subscribe(self, accounts: list[Account]) -> None:
"""
Expand Down