-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathtest_run_strategy.py
More file actions
133 lines (119 loc) · 4.09 KB
/
Copy pathtest_run_strategy.py
File metadata and controls
133 lines (119 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import os
import shutil
from typing import Dict, Any
from unittest import TestCase
import pandas as pd
from investing_algorithm_framework import create_app, TradingStrategy, \
TimeUnit, PortfolioConfiguration, RESOURCE_DIRECTORY, Algorithm, \
MarketCredential
from investing_algorithm_framework.infrastructure.database import \
teardown_sqlalchemy
from tests.resources import random_string, OrderExecutorTest, \
PortfolioProviderTest
class StrategyOne(TradingStrategy):
id = "strategy_one"
time_unit = TimeUnit.SECOND
interval = 2
def generate_sell_signals(self, data: Dict[str, Any]) -> Dict[
str, pd.Series]:
pass
def generate_buy_signals(self, data: Dict[str, Any]) -> Dict[
str, pd.Series]:
pass
class StrategyTwo(TradingStrategy):
id = "strategy_two"
time_unit = TimeUnit.SECOND
interval = 2
def generate_sell_signals(self, data: Dict[str, Any]) -> Dict[str, pd.Series]:
pass
def generate_buy_signals(self, data: Dict[str, Any]) -> Dict[str, pd.Series]:
pass
class Test(TestCase):
portfolio_configurations = [
PortfolioConfiguration(
market="binance",
trading_symbol="EUR",
initial_balance=1000,
)
]
market_credentials = [
MarketCredential(
market="binance",
api_key="api_key",
secret_key="secret_key",
)
]
external_balances = {"EUR": 1000}
def setUp(self) -> None:
super(Test, self).setUp()
self.resource_dir = os.path.abspath(
os.path.join(
os.path.join(
os.path.join(
os.path.join(
os.path.realpath(__file__),
os.pardir
),
os.pardir
),
os.pardir
),
"resources"
)
)
def tearDown(self) -> None:
super().tearDown()
teardown_sqlalchemy()
database_dir = os.path.join(self.resource_dir, "databases")
if os.path.exists(database_dir):
shutil.rmtree(database_dir, ignore_errors=True)
def test_with_strategy_object(self):
app = create_app(config={RESOURCE_DIRECTORY: self.resource_dir})
app.add_portfolio_provider(PortfolioProviderTest)
app.add_order_executor(OrderExecutorTest)
app.container.portfolio_configuration_service().clear()
app.add_portfolio_configuration(
PortfolioConfiguration(
market="BINANCE",
trading_symbol="EUR",
)
)
app.add_market_credential(
MarketCredential(
market="BINANCE",
api_key=random_string(10),
secret_key=random_string(10)
)
)
algorithm = Algorithm()
algorithm.add_strategy(StrategyOne)
algorithm.add_strategy(StrategyTwo)
app.add_algorithm(algorithm)
app.run(number_of_iterations=1)
self.assertTrue(app.has_run("StrategyOne"))
self.assertTrue(app.has_run("StrategyTwo"))
def test_stateless(self):
app = create_app(config={RESOURCE_DIRECTORY: self.resource_dir})
app.add_portfolio_provider(PortfolioProviderTest)
app.add_order_executor(OrderExecutorTest)
app.container.portfolio_configuration_service().clear()
app.add_portfolio_configuration(
PortfolioConfiguration(
market="BITVAVO",
trading_symbol="EUR"
)
)
app.add_market_credential(
MarketCredential(
market="BITVAVO",
api_key=random_string(10),
secret_key=random_string(10)
)
)
algorithm = Algorithm()
algorithm.add_strategy(StrategyOne)
algorithm.add_strategy(StrategyTwo)
app.add_algorithm(algorithm)
app.run(number_of_iterations=1)
self.assertTrue(app.has_run("StrategyOne"))
self.assertTrue(app.has_run("StrategyTwo"))