Skip to content

Commit

Permalink
Support tasks in stateless runner
Browse files Browse the repository at this point in the history
  • Loading branch information
MDUYN committed Jul 18, 2023
1 parent c06ca68 commit 3fd3bf4
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
59 changes: 59 additions & 0 deletions examples/app_with_tasks_stateless.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from datetime import datetime, timedelta

from investing_algorithm_framework import create_app, PortfolioConfiguration, \
TimeUnit, TradingTimeFrame, TradingDataType, Task, StatelessAction

# No resource directory specified, so an in-memory database will be used
app = create_app(stateless=True)
app.add_portfolio_configuration(
PortfolioConfiguration(
market="<your_market>",
api_key="<your_api_key>",
secret_key="<your_secret_key>",
trading_symbol="<your_trading_symbol>"
)
)


class MyTask(Task):
time_unit = TimeUnit.SECOND
interval = 5

def run(self, algorithm):
print("Hello world from MyTask")


class MyTaskTwo(Task):
time_unit = TimeUnit.SECOND
interval = 5

def run(self, algorithm):
print("Hello world from MyTaskTwo")


app.add_task(MyTask)
app.add_task(MyTaskTwo)


@app.task(time_unit=TimeUnit.SECOND, interval=5)
def say_hello(algorithm):
print("Hello world")


@app.strategy(
time_unit=TimeUnit.SECOND,
interval=5,
market="BINANCE",
symbols=["BTC/EUR"],
trading_data_types=[TradingDataType.OHLCV],
trading_time_frame_start_date=datetime.utcnow() - timedelta(days=1),
trading_time_frame=TradingTimeFrame.ONE_MINUTE
)
def perform_strategy(algorithm, market_data):
print(len(algorithm.get_orders()))


if __name__ == "__main__":
app.run(payload={
"ACTION": StatelessAction.RUN_STRATEGY
})
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ def set_strategy(self, payload):
@staticmethod
def get_action_type(payload):

if payload is None or ("ACTION" not in payload and "action" not in payload):
raise OperationalException("Action type is not defined")

if "action" in payload:
action = payload["action"]
else:
action = payload["ACTION"]

if action is None:
raise OperationalException("Action type not supported")

return action
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class RunStrategyHandler(ActionHandlerStrategy):
def handle_event(self, payload, algorithm):
strategies = algorithm.strategy_orchestrator_service\
.get_strategies(payload.get("strategies", None))
tasks = algorithm.strategy_orchestrator_service.get_tasks()

for strategy in strategies:
algorithm.strategy_orchestrator_service.run_strategy(
Expand All @@ -18,6 +19,13 @@ def handle_event(self, payload, algorithm):
sync=True
)

for task in tasks:
algorithm.strategy_orchestrator_service.run_task(
task=task,
algorithm=algorithm,
sync=True
)

return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ def get_strategies(self, identifiers=None):

return strategies

def get_tasks(self):
return self._tasks

def get_jobs(self):
return schedule.jobs

Expand Down

0 comments on commit 3fd3bf4

Please sign in to comment.