|
| 1 | +import os |
| 2 | +from unittest import TestCase |
| 3 | + |
| 4 | +from investing_algorithm_framework import create_app, TradingStrategy, \ |
| 5 | + TimeUnit, PortfolioConfiguration, RESOURCE_DIRECTORY, \ |
| 6 | + Algorithm, MarketCredential |
| 7 | +from tests.resources import random_string, MarketServiceStub, \ |
| 8 | + OrderExecutorTest, PortfolioProviderTest |
| 9 | + |
| 10 | + |
| 11 | +class StrategyOne(TradingStrategy): |
| 12 | + time_unit = TimeUnit.SECOND |
| 13 | + interval = 2 |
| 14 | + |
| 15 | + def apply_strategy(self, context, market_data): |
| 16 | + pass |
| 17 | + |
| 18 | + |
| 19 | +class StrategyTwo(TradingStrategy): |
| 20 | + time_unit = TimeUnit.SECOND |
| 21 | + interval = 2 |
| 22 | + |
| 23 | + def apply_strategy(self, context, market_data): |
| 24 | + pass |
| 25 | + |
| 26 | + |
| 27 | +class Test(TestCase): |
| 28 | + portfolio_configurations = [ |
| 29 | + PortfolioConfiguration( |
| 30 | + market="binance", |
| 31 | + trading_symbol="EUR", |
| 32 | + initial_balance=1000, |
| 33 | + ) |
| 34 | + ] |
| 35 | + market_credentials = [ |
| 36 | + MarketCredential( |
| 37 | + market="binance", |
| 38 | + api_key="api_key", |
| 39 | + secret_key="secret_key", |
| 40 | + ) |
| 41 | + ] |
| 42 | + external_balances = {"EUR": 1000} |
| 43 | + |
| 44 | + def setUp(self) -> None: |
| 45 | + super(Test, self).setUp() |
| 46 | + self.resource_dir = os.path.abspath( |
| 47 | + os.path.join( |
| 48 | + os.path.join( |
| 49 | + os.path.join( |
| 50 | + os.path.join( |
| 51 | + os.path.realpath(__file__), |
| 52 | + os.pardir |
| 53 | + ), |
| 54 | + os.pardir |
| 55 | + ), |
| 56 | + os.pardir |
| 57 | + ), |
| 58 | + "resources" |
| 59 | + ) |
| 60 | + ) |
| 61 | + |
| 62 | + def tearDown(self) -> None: |
| 63 | + super().tearDown() |
| 64 | + # Delete the resources database directory |
| 65 | + |
| 66 | + database_dir = os.path.join(self.resource_dir, "databases") |
| 67 | + |
| 68 | + if os.path.exists(database_dir): |
| 69 | + for root, dirs, files in os.walk(database_dir, topdown=False): |
| 70 | + for name in files: |
| 71 | + os.remove(os.path.join(root, name)) |
| 72 | + for name in dirs: |
| 73 | + os.rmdir(os.path.join(root, name)) |
| 74 | + |
| 75 | + def test_with_number_of_iterations(self): |
| 76 | + app = create_app(config={RESOURCE_DIRECTORY: self.resource_dir}) |
| 77 | + app.add_portfolio_provider(PortfolioProviderTest) |
| 78 | + app.add_order_executor(OrderExecutorTest) |
| 79 | + app.container.market_service.override(MarketServiceStub(None)) |
| 80 | + app.container.portfolio_configuration_service().clear() |
| 81 | + app.add_portfolio_configuration( |
| 82 | + PortfolioConfiguration( |
| 83 | + market="BINANCE", |
| 84 | + trading_symbol="EUR", |
| 85 | + ) |
| 86 | + ) |
| 87 | + app.add_market_credential( |
| 88 | + MarketCredential( |
| 89 | + market="BINANCE", |
| 90 | + api_key=random_string(10), |
| 91 | + secret_key=random_string(10) |
| 92 | + ) |
| 93 | + ) |
| 94 | + algorithm = Algorithm() |
| 95 | + algorithm.add_strategy(StrategyOne) |
| 96 | + algorithm.add_strategy(StrategyTwo) |
| 97 | + app.add_algorithm(algorithm) |
| 98 | + app.run(number_of_iterations=1) |
| 99 | + self.assertTrue(app.has_run("StrategyOne")) |
| 100 | + self.assertTrue(app.has_run("StrategyTwo")) |
0 commit comments