-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from AsyncAlgoTrading/ibdata
Ibdata
- Loading branch information
Showing
22 changed files
with
456 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .manager import StrategyManager # noqa: F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import sys | ||
import traceback | ||
|
||
from typing import List | ||
|
||
from .order_entry import StrategyManagerOrderEntryMixin | ||
from .risk import StrategyManagerRiskMixin | ||
from .utils import StrategyManagerUtilsMixin | ||
|
||
from aat.core.handler import EventHandler | ||
from aat.exchange import Exchange | ||
|
||
|
||
class StrategyManager(StrategyManagerOrderEntryMixin, StrategyManagerRiskMixin, StrategyManagerUtilsMixin, EventHandler): | ||
def __init__(self, trading_engine, trading_type, exchanges: List[Exchange]): | ||
'''The Manager sits between the strategies and the engine and manages state''' | ||
# store trading engine | ||
self._engine = trading_engine | ||
|
||
# store the exchanges | ||
self._exchanges = exchanges | ||
|
||
# pull from trading engine class | ||
self._risk_mgr = self._engine.risk_manager | ||
self._order_mgr = self._engine.order_manager | ||
|
||
# install self for callbacks | ||
self._risk_mgr._setManager(self) | ||
self._order_mgr._setManager(self) | ||
|
||
# add exchanges for order manager | ||
for exc in exchanges: | ||
self._order_mgr.addExchange(exc) | ||
|
||
# initialize event subscriptions | ||
self._data_subscriptions = {} # type: ignore | ||
|
||
# initialize order and trade tracking | ||
self._strategy_open_orders = {} | ||
self._strategy_past_orders = {} | ||
self._strategy_trades = {} | ||
|
||
# internal use for synchronizing | ||
self._alerted_events = {} | ||
|
||
# internal use for periodics | ||
self._periodics = [] | ||
|
||
# ********************* # | ||
# EventHandler methods * | ||
# ********************** | ||
async def onTrade(self, event): | ||
await self._risk_mgr.onTrade(event) | ||
await self._order_mgr.onTrade(event) | ||
|
||
async def onOpen(self, event): | ||
await self._risk_mgr.onOpen(event) | ||
await self._order_mgr.onOpen(event) | ||
|
||
async def onCancel(self, event): | ||
await self._risk_mgr.onCancel(event) | ||
await self._order_mgr.onCancel(event) | ||
|
||
async def onChange(self, event): | ||
await self._risk_mgr.onChange(event) | ||
await self._order_mgr.onChange(event) | ||
|
||
async def onFill(self, event): | ||
await self._risk_mgr.onFill(event) | ||
await self._order_mgr.onFill(event) | ||
|
||
async def onHalt(self, event): | ||
await self._risk_mgr.onHalt(event) | ||
await self._order_mgr.onHalt(event) | ||
|
||
async def onContinue(self, event): | ||
await self._risk_mgr.onContinue(event) | ||
await self._order_mgr.onContinue(event) | ||
|
||
async def onData(self, event): | ||
# TODO | ||
await self._risk_mgr.onData(event) | ||
await self._order_mgr.onData(event) | ||
|
||
async def onError(self, event): | ||
# TODO | ||
print('\n\nA Fatal Error has occurred:') | ||
traceback.print_exception(type(event.target.exception), event.target.exception, event.target.exception.__traceback__) | ||
sys.exit(1) | ||
|
||
async def onExit(self, event): | ||
# TODO | ||
await self._risk_mgr.onExit(event) | ||
await self._order_mgr.onExit(event) | ||
|
||
async def onStart(self, event): | ||
# TODO | ||
await self._risk_mgr.onStart(event) | ||
await self._order_mgr.onStart(event) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from typing import Callable, Awaitable | ||
from temporalcache.utils import should_expire # type: ignore | ||
|
||
|
||
class Periodic(object): | ||
def __init__(self, loop, last_ts, function, second, minute, hour): | ||
self._loop = loop | ||
self._function: Callable[Awaitable[None]] = function | ||
self._second = second | ||
self._minute = minute | ||
self._hour = hour | ||
|
||
self._last = last_ts | ||
self._continue = True | ||
|
||
def stop(self) -> None: | ||
self._continue = False | ||
|
||
async def execute(self, timestamp): | ||
if should_expire(self._last, timestamp, self._second, self._minute, self._hour): | ||
await self._function() | ||
self._last = timestamp |
Oops, something went wrong.