investing-algorithm-framework 6.9.1__py3-none-any.whl → 7.19.15__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of investing-algorithm-framework might be problematic. Click here for more details.
- investing_algorithm_framework/__init__.py +147 -44
- investing_algorithm_framework/app/__init__.py +23 -6
- investing_algorithm_framework/app/algorithm/algorithm.py +5 -41
- investing_algorithm_framework/app/algorithm/algorithm_factory.py +17 -10
- investing_algorithm_framework/app/analysis/__init__.py +15 -0
- investing_algorithm_framework/app/analysis/backtest_data_ranges.py +121 -0
- investing_algorithm_framework/app/analysis/backtest_utils.py +107 -0
- investing_algorithm_framework/app/analysis/permutation.py +116 -0
- investing_algorithm_framework/app/analysis/ranking.py +297 -0
- investing_algorithm_framework/app/app.py +1322 -707
- investing_algorithm_framework/app/context.py +196 -88
- investing_algorithm_framework/app/eventloop.py +590 -0
- investing_algorithm_framework/app/reporting/__init__.py +16 -5
- investing_algorithm_framework/app/reporting/ascii.py +57 -202
- investing_algorithm_framework/app/reporting/backtest_report.py +284 -170
- investing_algorithm_framework/app/reporting/charts/__init__.py +10 -2
- investing_algorithm_framework/app/reporting/charts/entry_exist_signals.py +66 -0
- investing_algorithm_framework/app/reporting/charts/equity_curve.py +37 -0
- investing_algorithm_framework/app/reporting/charts/equity_curve_drawdown.py +11 -26
- investing_algorithm_framework/app/reporting/charts/line_chart.py +11 -0
- investing_algorithm_framework/app/reporting/charts/ohlcv_data_completeness.py +51 -0
- investing_algorithm_framework/app/reporting/charts/rolling_sharp_ratio.py +1 -1
- investing_algorithm_framework/app/reporting/generate.py +100 -114
- investing_algorithm_framework/app/reporting/tables/key_metrics_table.py +40 -32
- investing_algorithm_framework/app/reporting/tables/time_metrics_table.py +34 -27
- investing_algorithm_framework/app/reporting/tables/trade_metrics_table.py +23 -19
- investing_algorithm_framework/app/reporting/tables/trades_table.py +1 -1
- investing_algorithm_framework/app/reporting/tables/utils.py +1 -0
- investing_algorithm_framework/app/reporting/templates/report_template.html.j2 +10 -16
- investing_algorithm_framework/app/strategy.py +315 -175
- investing_algorithm_framework/app/task.py +5 -3
- investing_algorithm_framework/cli/cli.py +30 -12
- investing_algorithm_framework/cli/deploy_to_aws_lambda.py +131 -34
- investing_algorithm_framework/cli/initialize_app.py +20 -1
- investing_algorithm_framework/cli/templates/app_aws_lambda_function.py.template +18 -6
- investing_algorithm_framework/cli/templates/aws_lambda_dockerfile.template +22 -0
- investing_algorithm_framework/cli/templates/aws_lambda_dockerignore.template +92 -0
- investing_algorithm_framework/cli/templates/aws_lambda_requirements.txt.template +2 -2
- investing_algorithm_framework/cli/templates/azure_function_requirements.txt.template +1 -1
- investing_algorithm_framework/create_app.py +3 -5
- investing_algorithm_framework/dependency_container.py +25 -39
- investing_algorithm_framework/domain/__init__.py +45 -38
- investing_algorithm_framework/domain/backtesting/__init__.py +21 -0
- investing_algorithm_framework/domain/backtesting/backtest.py +503 -0
- investing_algorithm_framework/domain/backtesting/backtest_date_range.py +96 -0
- investing_algorithm_framework/domain/backtesting/backtest_evaluation_focuss.py +242 -0
- investing_algorithm_framework/domain/backtesting/backtest_metrics.py +459 -0
- investing_algorithm_framework/domain/backtesting/backtest_permutation_test.py +275 -0
- investing_algorithm_framework/domain/backtesting/backtest_run.py +605 -0
- investing_algorithm_framework/domain/backtesting/backtest_summary_metrics.py +162 -0
- investing_algorithm_framework/domain/backtesting/combine_backtests.py +280 -0
- investing_algorithm_framework/domain/config.py +27 -0
- investing_algorithm_framework/domain/constants.py +6 -34
- investing_algorithm_framework/domain/data_provider.py +200 -56
- investing_algorithm_framework/domain/exceptions.py +34 -1
- investing_algorithm_framework/domain/models/__init__.py +10 -19
- investing_algorithm_framework/domain/models/base_model.py +0 -6
- investing_algorithm_framework/domain/models/data/__init__.py +7 -0
- investing_algorithm_framework/domain/models/data/data_source.py +214 -0
- investing_algorithm_framework/domain/models/{market_data_type.py → data/data_type.py} +7 -7
- investing_algorithm_framework/domain/models/market/market_credential.py +6 -0
- investing_algorithm_framework/domain/models/order/order.py +34 -13
- investing_algorithm_framework/domain/models/order/order_status.py +1 -1
- investing_algorithm_framework/domain/models/order/order_type.py +1 -1
- investing_algorithm_framework/domain/models/portfolio/portfolio.py +14 -1
- investing_algorithm_framework/domain/models/portfolio/portfolio_configuration.py +5 -1
- investing_algorithm_framework/domain/models/portfolio/portfolio_snapshot.py +51 -11
- investing_algorithm_framework/domain/models/position/__init__.py +2 -1
- investing_algorithm_framework/domain/models/position/position.py +9 -0
- investing_algorithm_framework/domain/models/position/position_size.py +41 -0
- investing_algorithm_framework/domain/models/risk_rules/__init__.py +7 -0
- investing_algorithm_framework/domain/models/risk_rules/stop_loss_rule.py +51 -0
- investing_algorithm_framework/domain/models/risk_rules/take_profit_rule.py +55 -0
- investing_algorithm_framework/domain/models/snapshot_interval.py +0 -1
- investing_algorithm_framework/domain/models/strategy_profile.py +19 -151
- investing_algorithm_framework/domain/models/time_frame.py +7 -0
- investing_algorithm_framework/domain/models/time_interval.py +33 -0
- investing_algorithm_framework/domain/models/time_unit.py +63 -1
- investing_algorithm_framework/domain/models/trade/__init__.py +0 -2
- investing_algorithm_framework/domain/models/trade/trade.py +56 -32
- investing_algorithm_framework/domain/models/trade/trade_status.py +8 -2
- investing_algorithm_framework/domain/models/trade/trade_stop_loss.py +106 -41
- investing_algorithm_framework/domain/models/trade/trade_take_profit.py +161 -99
- investing_algorithm_framework/domain/order_executor.py +19 -0
- investing_algorithm_framework/domain/portfolio_provider.py +20 -1
- investing_algorithm_framework/domain/services/__init__.py +0 -13
- investing_algorithm_framework/domain/strategy.py +1 -29
- investing_algorithm_framework/domain/utils/__init__.py +5 -1
- investing_algorithm_framework/domain/utils/custom_tqdm.py +22 -0
- investing_algorithm_framework/domain/utils/jupyter_notebook_detection.py +19 -0
- investing_algorithm_framework/domain/utils/polars.py +17 -14
- investing_algorithm_framework/download_data.py +40 -10
- investing_algorithm_framework/infrastructure/__init__.py +13 -25
- investing_algorithm_framework/infrastructure/data_providers/__init__.py +7 -4
- investing_algorithm_framework/infrastructure/data_providers/ccxt.py +811 -546
- investing_algorithm_framework/infrastructure/data_providers/csv.py +433 -122
- investing_algorithm_framework/infrastructure/data_providers/pandas.py +599 -0
- investing_algorithm_framework/infrastructure/database/__init__.py +6 -2
- investing_algorithm_framework/infrastructure/database/sql_alchemy.py +81 -0
- investing_algorithm_framework/infrastructure/models/__init__.py +0 -13
- investing_algorithm_framework/infrastructure/models/order/order.py +9 -3
- investing_algorithm_framework/infrastructure/models/trades/trade_stop_loss.py +27 -8
- investing_algorithm_framework/infrastructure/models/trades/trade_take_profit.py +21 -7
- investing_algorithm_framework/infrastructure/order_executors/__init__.py +2 -0
- investing_algorithm_framework/infrastructure/order_executors/backtest_oder_executor.py +28 -0
- investing_algorithm_framework/infrastructure/repositories/repository.py +16 -2
- investing_algorithm_framework/infrastructure/repositories/trade_repository.py +2 -2
- investing_algorithm_framework/infrastructure/repositories/trade_stop_loss_repository.py +6 -0
- investing_algorithm_framework/infrastructure/repositories/trade_take_profit_repository.py +6 -0
- investing_algorithm_framework/infrastructure/services/__init__.py +0 -4
- investing_algorithm_framework/services/__init__.py +105 -8
- investing_algorithm_framework/services/backtesting/backtest_service.py +536 -476
- investing_algorithm_framework/services/configuration_service.py +14 -4
- investing_algorithm_framework/services/data_providers/__init__.py +5 -0
- investing_algorithm_framework/services/data_providers/data_provider_service.py +850 -0
- investing_algorithm_framework/{app/reporting → services}/metrics/__init__.py +48 -17
- investing_algorithm_framework/{app/reporting → services}/metrics/drawdown.py +10 -10
- investing_algorithm_framework/{app/reporting → services}/metrics/equity_curve.py +2 -2
- investing_algorithm_framework/{app/reporting → services}/metrics/exposure.py +60 -2
- investing_algorithm_framework/services/metrics/generate.py +358 -0
- investing_algorithm_framework/{app/reporting → services}/metrics/profit_factor.py +36 -0
- investing_algorithm_framework/{app/reporting → services}/metrics/recovery.py +2 -2
- investing_algorithm_framework/{app/reporting → services}/metrics/returns.py +146 -147
- investing_algorithm_framework/services/metrics/risk_free_rate.py +28 -0
- investing_algorithm_framework/{app/reporting/metrics/sharp_ratio.py → services/metrics/sharpe_ratio.py} +6 -10
- investing_algorithm_framework/{app/reporting → services}/metrics/sortino_ratio.py +3 -7
- investing_algorithm_framework/services/metrics/trades.py +500 -0
- investing_algorithm_framework/services/metrics/volatility.py +97 -0
- investing_algorithm_framework/{app/reporting → services}/metrics/win_rate.py +70 -3
- investing_algorithm_framework/services/order_service/order_backtest_service.py +21 -31
- investing_algorithm_framework/services/order_service/order_service.py +9 -71
- investing_algorithm_framework/services/portfolios/portfolio_provider_lookup.py +0 -2
- investing_algorithm_framework/services/portfolios/portfolio_service.py +3 -13
- investing_algorithm_framework/services/portfolios/portfolio_snapshot_service.py +62 -96
- investing_algorithm_framework/services/portfolios/portfolio_sync_service.py +0 -3
- investing_algorithm_framework/services/repository_service.py +5 -2
- investing_algorithm_framework/services/trade_order_evaluator/__init__.py +9 -0
- investing_algorithm_framework/services/trade_order_evaluator/backtest_trade_oder_evaluator.py +113 -0
- investing_algorithm_framework/services/trade_order_evaluator/default_trade_order_evaluator.py +51 -0
- investing_algorithm_framework/services/trade_order_evaluator/trade_order_evaluator.py +80 -0
- investing_algorithm_framework/services/trade_service/__init__.py +7 -1
- investing_algorithm_framework/services/trade_service/trade_service.py +51 -29
- investing_algorithm_framework/services/trade_service/trade_stop_loss_service.py +39 -0
- investing_algorithm_framework/services/trade_service/trade_take_profit_service.py +41 -0
- investing_algorithm_framework-7.19.15.dist-info/METADATA +537 -0
- {investing_algorithm_framework-6.9.1.dist-info → investing_algorithm_framework-7.19.15.dist-info}/RECORD +159 -148
- investing_algorithm_framework/app/reporting/evaluation.py +0 -243
- investing_algorithm_framework/app/reporting/metrics/risk_free_rate.py +0 -8
- investing_algorithm_framework/app/reporting/metrics/volatility.py +0 -69
- investing_algorithm_framework/cli/templates/requirements_azure_function.txt.template +0 -3
- investing_algorithm_framework/domain/models/backtesting/__init__.py +0 -9
- investing_algorithm_framework/domain/models/backtesting/backtest_date_range.py +0 -47
- investing_algorithm_framework/domain/models/backtesting/backtest_position.py +0 -120
- investing_algorithm_framework/domain/models/backtesting/backtest_reports_evaluation.py +0 -0
- investing_algorithm_framework/domain/models/backtesting/backtest_results.py +0 -440
- investing_algorithm_framework/domain/models/data_source.py +0 -21
- investing_algorithm_framework/domain/models/date_range.py +0 -64
- investing_algorithm_framework/domain/models/trade/trade_risk_type.py +0 -34
- investing_algorithm_framework/domain/models/trading_data_types.py +0 -48
- investing_algorithm_framework/domain/models/trading_time_frame.py +0 -223
- investing_algorithm_framework/domain/services/market_data_sources.py +0 -543
- investing_algorithm_framework/domain/services/market_service.py +0 -153
- investing_algorithm_framework/domain/services/observable.py +0 -51
- investing_algorithm_framework/domain/services/observer.py +0 -19
- investing_algorithm_framework/infrastructure/models/market_data_sources/__init__.py +0 -16
- investing_algorithm_framework/infrastructure/models/market_data_sources/ccxt.py +0 -746
- investing_algorithm_framework/infrastructure/models/market_data_sources/csv.py +0 -270
- investing_algorithm_framework/infrastructure/models/market_data_sources/pandas.py +0 -312
- investing_algorithm_framework/infrastructure/services/market_service/__init__.py +0 -5
- investing_algorithm_framework/infrastructure/services/market_service/ccxt_market_service.py +0 -471
- investing_algorithm_framework/infrastructure/services/performance_service/__init__.py +0 -7
- investing_algorithm_framework/infrastructure/services/performance_service/backtest_performance_service.py +0 -2
- investing_algorithm_framework/infrastructure/services/performance_service/performance_service.py +0 -322
- investing_algorithm_framework/services/market_data_source_service/__init__.py +0 -10
- investing_algorithm_framework/services/market_data_source_service/backtest_market_data_source_service.py +0 -269
- investing_algorithm_framework/services/market_data_source_service/data_provider_service.py +0 -350
- investing_algorithm_framework/services/market_data_source_service/market_data_source_service.py +0 -377
- investing_algorithm_framework/services/strategy_orchestrator_service.py +0 -296
- investing_algorithm_framework-6.9.1.dist-info/METADATA +0 -440
- /investing_algorithm_framework/{app/reporting → services}/metrics/alpha.py +0 -0
- /investing_algorithm_framework/{app/reporting → services}/metrics/beta.py +0 -0
- /investing_algorithm_framework/{app/reporting → services}/metrics/cagr.py +0 -0
- /investing_algorithm_framework/{app/reporting → services}/metrics/calmar_ratio.py +0 -0
- /investing_algorithm_framework/{app/reporting → services}/metrics/mean_daily_return.py +0 -0
- /investing_algorithm_framework/{app/reporting → services}/metrics/price_efficiency.py +0 -0
- /investing_algorithm_framework/{app/reporting → services}/metrics/standard_deviation.py +0 -0
- /investing_algorithm_framework/{app/reporting → services}/metrics/treynor_ratio.py +0 -0
- /investing_algorithm_framework/{app/reporting → services}/metrics/ulcer.py +0 -0
- /investing_algorithm_framework/{app/reporting → services}/metrics/value_at_risk.py +0 -0
- {investing_algorithm_framework-6.9.1.dist-info → investing_algorithm_framework-7.19.15.dist-info}/LICENSE +0 -0
- {investing_algorithm_framework-6.9.1.dist-info → investing_algorithm_framework-7.19.15.dist-info}/WHEEL +0 -0
- {investing_algorithm_framework-6.9.1.dist-info → investing_algorithm_framework-7.19.15.dist-info}/entry_points.txt +0 -0
|
@@ -1,350 +0,0 @@
|
|
|
1
|
-
import logging
|
|
2
|
-
from collections import defaultdict
|
|
3
|
-
from datetime import datetime
|
|
4
|
-
from typing import List, Optional
|
|
5
|
-
|
|
6
|
-
from investing_algorithm_framework.domain import DataProvider, \
|
|
7
|
-
OperationalException, ImproperlyConfigured, TradingDataType
|
|
8
|
-
|
|
9
|
-
logger = logging.getLogger("investing_algorithm_framework")
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class DataProviderIndex:
|
|
13
|
-
"""
|
|
14
|
-
Class to index data providers. Given a list of data providers, each data
|
|
15
|
-
provider will be indexed given that it has support for the given query
|
|
16
|
-
params
|
|
17
|
-
"""
|
|
18
|
-
|
|
19
|
-
def __init__(self, data_providers: List[DataProvider] = []):
|
|
20
|
-
self.data_providers: List[DataProvider] = data_providers
|
|
21
|
-
|
|
22
|
-
def register_data_provider(self, data_provider: DataProvider) -> None:
|
|
23
|
-
"""
|
|
24
|
-
Register a new data provider.
|
|
25
|
-
|
|
26
|
-
Args:
|
|
27
|
-
data_provider (DataProvider): The data provider to register.
|
|
28
|
-
"""
|
|
29
|
-
self.data_providers.append(data_provider)
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
class OHLCVDataProviderIndex:
|
|
33
|
-
"""
|
|
34
|
-
Efficient lookup for ohlcv data providers in O(1) time.
|
|
35
|
-
|
|
36
|
-
Attributes:
|
|
37
|
-
data_providers (List[DataProvider]): List of data providers
|
|
38
|
-
order_executor_lookup (dict): Dictionary to store the lookup
|
|
39
|
-
for order executors based on market.
|
|
40
|
-
"""
|
|
41
|
-
def __init__(self, data_providers=[]):
|
|
42
|
-
self.data_providers = data_providers
|
|
43
|
-
self.data_providers_lookup = defaultdict()
|
|
44
|
-
|
|
45
|
-
def add(self, data_provider: DataProvider):
|
|
46
|
-
"""
|
|
47
|
-
Add a data provider to the lookup.
|
|
48
|
-
|
|
49
|
-
Args:
|
|
50
|
-
data_provider (DataProvider): The data provider to be added.
|
|
51
|
-
|
|
52
|
-
Returns:
|
|
53
|
-
None
|
|
54
|
-
"""
|
|
55
|
-
self.data_providers.append(data_provider)
|
|
56
|
-
|
|
57
|
-
def register(self, symbol, market) -> DataProvider:
|
|
58
|
-
"""
|
|
59
|
-
Register an ohlcv data provider for a given market and symbol.
|
|
60
|
-
|
|
61
|
-
This method will also check if the data provider supports
|
|
62
|
-
the market. If no data provider is found for the market and symbol,
|
|
63
|
-
it will raise an ImproperlyConfigured exception.
|
|
64
|
-
|
|
65
|
-
If multiple data providers are found for the market and symbol,
|
|
66
|
-
it will sort them by priority and pick the best one.
|
|
67
|
-
|
|
68
|
-
Args:
|
|
69
|
-
market (str): The market to register the data provider for.
|
|
70
|
-
symbol (str): The symbol to register the data provider for.
|
|
71
|
-
|
|
72
|
-
Returns:
|
|
73
|
-
None
|
|
74
|
-
"""
|
|
75
|
-
matches = []
|
|
76
|
-
|
|
77
|
-
for data_provider in self.data_providers:
|
|
78
|
-
|
|
79
|
-
if data_provider.supports(market, symbol):
|
|
80
|
-
matches.append(data_provider)
|
|
81
|
-
|
|
82
|
-
if len(matches) == 0:
|
|
83
|
-
raise ImproperlyConfigured(
|
|
84
|
-
f"No data provider found for market "
|
|
85
|
-
f"{market} and symbol {symbol}."
|
|
86
|
-
f" Please make sure that you have registered a data provider "
|
|
87
|
-
f"provider for the market and symbol you are trying to use"
|
|
88
|
-
)
|
|
89
|
-
|
|
90
|
-
# Sort by priority and pick the best one
|
|
91
|
-
best_provider = sorted(matches, key=lambda x: x.priority)[0]
|
|
92
|
-
self.data_providers_lookup[(market, symbol)] = best_provider
|
|
93
|
-
return best_provider
|
|
94
|
-
|
|
95
|
-
def get(self, symbol, market: str):
|
|
96
|
-
"""
|
|
97
|
-
Get the ohlcv data provider for a given market and symbol.
|
|
98
|
-
If no data provider is found for the market and symbol,
|
|
99
|
-
it will return None.
|
|
100
|
-
|
|
101
|
-
Args:
|
|
102
|
-
market (str): The market to get the order executor for.
|
|
103
|
-
symbol (str): The symbol to get the order executor for.
|
|
104
|
-
|
|
105
|
-
Returns:
|
|
106
|
-
DataProvider: The data provider for the market and symbol,
|
|
107
|
-
"""
|
|
108
|
-
return self.data_providers_lookup.get((market, symbol), None)
|
|
109
|
-
|
|
110
|
-
def get_all(self) -> List[DataProvider]:
|
|
111
|
-
"""
|
|
112
|
-
Get all order executors.
|
|
113
|
-
This method will return all order executors that are currently
|
|
114
|
-
registered in the order_executors list.
|
|
115
|
-
|
|
116
|
-
Returns:
|
|
117
|
-
List[OrderExecutor]: A list of all order executors.
|
|
118
|
-
"""
|
|
119
|
-
return self.data_providers
|
|
120
|
-
|
|
121
|
-
def reset(self):
|
|
122
|
-
"""
|
|
123
|
-
Function to reset the order executor lookup table
|
|
124
|
-
|
|
125
|
-
Returns:
|
|
126
|
-
None
|
|
127
|
-
"""
|
|
128
|
-
self.data_providers_lookup = defaultdict()
|
|
129
|
-
self.data_providers = []
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
class DataProviderService:
|
|
133
|
-
data_providers: List[DataProvider] = []
|
|
134
|
-
default_data_providers: List[DataProvider] = [
|
|
135
|
-
# Add default data providers here
|
|
136
|
-
]
|
|
137
|
-
data_provider_index: DataProviderIndex = None
|
|
138
|
-
ohlcv_data_provider_index: OHLCVDataProviderIndex = None
|
|
139
|
-
|
|
140
|
-
def __init__(
|
|
141
|
-
self,
|
|
142
|
-
configuration_service,
|
|
143
|
-
market_credentials_service,
|
|
144
|
-
default_data_providers: List[DataProvider] = [],
|
|
145
|
-
default_ohlcv_data_providers: List[DataProvider] = []
|
|
146
|
-
):
|
|
147
|
-
"""
|
|
148
|
-
Initialize the DataProviderService with a list of data providers.
|
|
149
|
-
|
|
150
|
-
Args:
|
|
151
|
-
default_data_providers (List[DataProvider]): A list of default
|
|
152
|
-
data providers to use.
|
|
153
|
-
"""
|
|
154
|
-
self.default_data_providers = default_data_providers
|
|
155
|
-
self.data_provider_index = DataProviderIndex(default_data_providers)
|
|
156
|
-
self.configuration_service = configuration_service
|
|
157
|
-
self.market_credentials_service = market_credentials_service
|
|
158
|
-
self.ohlcv_data_provider_index = OHLCVDataProviderIndex(
|
|
159
|
-
data_providers=default_ohlcv_data_providers
|
|
160
|
-
)
|
|
161
|
-
|
|
162
|
-
def get_data(
|
|
163
|
-
self,
|
|
164
|
-
symbol: str,
|
|
165
|
-
data_type=None,
|
|
166
|
-
date: datetime = None,
|
|
167
|
-
market: str = None,
|
|
168
|
-
time_frame: str = None,
|
|
169
|
-
start_date: datetime = None,
|
|
170
|
-
end_date: datetime = None,
|
|
171
|
-
storage_path=None,
|
|
172
|
-
window_size=None,
|
|
173
|
-
pandas=False,
|
|
174
|
-
save: bool = False,
|
|
175
|
-
):
|
|
176
|
-
"""
|
|
177
|
-
Function to get data from the data provider.
|
|
178
|
-
|
|
179
|
-
Args:
|
|
180
|
-
data_type (str): The type of data to get (e.g., "ohlcv", "ticker").
|
|
181
|
-
date (datetime): The date to get data for.
|
|
182
|
-
symbol (str): The symbol to get data for.
|
|
183
|
-
market (str): The market to get data from.
|
|
184
|
-
time_frame (str): The time frame to get data for.
|
|
185
|
-
start_date (datetime): The start date for the data.
|
|
186
|
-
end_date (datetime): The end date for the data.
|
|
187
|
-
storage_path (str): The path to store the data.
|
|
188
|
-
window_size (int): The size of the data window.
|
|
189
|
-
pandas (bool): Whether to return the data as a pandas DataFrame.
|
|
190
|
-
|
|
191
|
-
Returns:
|
|
192
|
-
DataFrame: The data for the given symbol and market.
|
|
193
|
-
"""
|
|
194
|
-
|
|
195
|
-
data_provider = self.find_data_provider(
|
|
196
|
-
symbol=symbol, market=market, data_type=data_type
|
|
197
|
-
)
|
|
198
|
-
|
|
199
|
-
if data_provider is None:
|
|
200
|
-
self._throw_no_data_provider_exception(
|
|
201
|
-
{
|
|
202
|
-
"symbol": symbol,
|
|
203
|
-
"market": market,
|
|
204
|
-
"data_type": data_type,
|
|
205
|
-
"time_frame": time_frame,
|
|
206
|
-
}
|
|
207
|
-
)
|
|
208
|
-
|
|
209
|
-
if self.configuration_service is not None:
|
|
210
|
-
data_provider.config = self.configuration_service.get_config()
|
|
211
|
-
|
|
212
|
-
return data_provider.get_data(
|
|
213
|
-
data_type=data_type,
|
|
214
|
-
date=date,
|
|
215
|
-
symbol=symbol,
|
|
216
|
-
market=market,
|
|
217
|
-
time_frame=time_frame,
|
|
218
|
-
start_date=start_date,
|
|
219
|
-
end_date=end_date,
|
|
220
|
-
storage_path=storage_path,
|
|
221
|
-
window_size=window_size,
|
|
222
|
-
pandas=pandas,
|
|
223
|
-
save=save
|
|
224
|
-
)
|
|
225
|
-
|
|
226
|
-
def get_backtest_data(
|
|
227
|
-
self,
|
|
228
|
-
symbol: str,
|
|
229
|
-
data_type: str,
|
|
230
|
-
market: str = None,
|
|
231
|
-
backtest_index_date: datetime = None,
|
|
232
|
-
time_frame: str = None,
|
|
233
|
-
start_date: datetime = None,
|
|
234
|
-
end_date: datetime = None,
|
|
235
|
-
storage_path=None,
|
|
236
|
-
window_size=None,
|
|
237
|
-
pandas=False,
|
|
238
|
-
save: bool = False,
|
|
239
|
-
):
|
|
240
|
-
|
|
241
|
-
"""
|
|
242
|
-
Function to get backtest data from the data provider.
|
|
243
|
-
|
|
244
|
-
Args:
|
|
245
|
-
symbol (str): The symbol to get data for.
|
|
246
|
-
market (str): The market to get data from.
|
|
247
|
-
time_frame (str): The time frame to get data for.
|
|
248
|
-
start_date (datetime): The start date for the data.
|
|
249
|
-
end_date (datetime): The end date for the data.
|
|
250
|
-
storage_path (str): The path to store the data.
|
|
251
|
-
window_size (int): The size of the data window.
|
|
252
|
-
pandas (bool): Whether to return the data as a pandas DataFrame.
|
|
253
|
-
|
|
254
|
-
Returns:
|
|
255
|
-
DataFrame: The backtest data for the given symbol and market.
|
|
256
|
-
"""
|
|
257
|
-
data_provider = self.data_provider_index.find_data_provider(
|
|
258
|
-
symbol=symbol,
|
|
259
|
-
market=market,
|
|
260
|
-
time_frame=time_frame,
|
|
261
|
-
)
|
|
262
|
-
|
|
263
|
-
if data_provider is None:
|
|
264
|
-
self._throw_no_data_provider_exception(
|
|
265
|
-
{
|
|
266
|
-
"symbol": symbol,
|
|
267
|
-
"market": market,
|
|
268
|
-
"data_type": data_type,
|
|
269
|
-
"time_frame": time_frame,
|
|
270
|
-
}
|
|
271
|
-
)
|
|
272
|
-
|
|
273
|
-
return data_provider.get_backtest_data(
|
|
274
|
-
symbol=symbol,
|
|
275
|
-
market=market,
|
|
276
|
-
time_frame=time_frame,
|
|
277
|
-
backtest_index_date=backtest_index_date,
|
|
278
|
-
start_date=start_date,
|
|
279
|
-
end_date=end_date,
|
|
280
|
-
storage_path=storage_path,
|
|
281
|
-
window_size=window_size,
|
|
282
|
-
pandas=pandas,
|
|
283
|
-
)
|
|
284
|
-
|
|
285
|
-
def _throw_no_data_provider_exception(self, params):
|
|
286
|
-
"""
|
|
287
|
-
Raise an exception if no data provider is found for the given params
|
|
288
|
-
"""
|
|
289
|
-
non_null_params = {k: v for k, v in params.items() if v is not None}
|
|
290
|
-
if len(non_null_params) == 0:
|
|
291
|
-
raise OperationalException(
|
|
292
|
-
"No data provider found for the given parameters"
|
|
293
|
-
)
|
|
294
|
-
|
|
295
|
-
params = ", ".join(
|
|
296
|
-
[f"{k}: {v}" for k, v in non_null_params.items()]
|
|
297
|
-
)
|
|
298
|
-
|
|
299
|
-
raise OperationalException(
|
|
300
|
-
f"No data provider found for the given parameters: {params}"
|
|
301
|
-
)
|
|
302
|
-
|
|
303
|
-
def add_data_provider(
|
|
304
|
-
self, data_provider: DataProvider, priority: int = 0
|
|
305
|
-
):
|
|
306
|
-
"""
|
|
307
|
-
Add a data provider to the service.
|
|
308
|
-
|
|
309
|
-
Args:
|
|
310
|
-
data_provider (DataProvider): The data provider to add.
|
|
311
|
-
priority (int): The priority of the data provider.
|
|
312
|
-
"""
|
|
313
|
-
self.data_providers.append(data_provider)
|
|
314
|
-
self.data_providers.sort(key=lambda x: x.priority, reverse=True)
|
|
315
|
-
|
|
316
|
-
def find_data_provider(
|
|
317
|
-
self,
|
|
318
|
-
symbol: Optional[str] = None,
|
|
319
|
-
market: Optional[str] = None,
|
|
320
|
-
data_type: Optional[str] = None,
|
|
321
|
-
time_frame: Optional[str] = None,
|
|
322
|
-
):
|
|
323
|
-
"""
|
|
324
|
-
Function to find a data provider based on the given parameters.
|
|
325
|
-
|
|
326
|
-
Args:
|
|
327
|
-
symbol (str): The symbol to find the data provider for.
|
|
328
|
-
market (str): The market to find the data provider for.
|
|
329
|
-
data_type (str): The type of data to find the data provider for.
|
|
330
|
-
time_frame (str): The time frame to find the data provider for.
|
|
331
|
-
|
|
332
|
-
Returns:
|
|
333
|
-
DataProvider: The data provider that matches the given parameters.
|
|
334
|
-
"""
|
|
335
|
-
data_provider = None
|
|
336
|
-
|
|
337
|
-
if TradingDataType.OHLCV.equals(data_type):
|
|
338
|
-
# Check if there is already a registered data provider
|
|
339
|
-
data_provider = self.ohlcv_data_provider_index.get(
|
|
340
|
-
symbol=symbol, market=market
|
|
341
|
-
)
|
|
342
|
-
|
|
343
|
-
if data_provider is None:
|
|
344
|
-
# Try to register a new data provider if it is not
|
|
345
|
-
# already registered
|
|
346
|
-
data_provider = self.ohlcv_data_provider_index.register(
|
|
347
|
-
symbol=symbol, market=market
|
|
348
|
-
)
|
|
349
|
-
|
|
350
|
-
return data_provider
|