investing-algorithm-framework 1.3.1__py3-none-any.whl → 7.25.6__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.
- investing_algorithm_framework/__init__.py +195 -16
- investing_algorithm_framework/analysis/__init__.py +16 -0
- investing_algorithm_framework/analysis/backtest_data_ranges.py +202 -0
- investing_algorithm_framework/analysis/data.py +170 -0
- investing_algorithm_framework/analysis/markdown.py +91 -0
- investing_algorithm_framework/analysis/ranking.py +298 -0
- investing_algorithm_framework/app/__init__.py +31 -4
- investing_algorithm_framework/app/algorithm/__init__.py +7 -0
- investing_algorithm_framework/app/algorithm/algorithm.py +193 -0
- investing_algorithm_framework/app/algorithm/algorithm_factory.py +118 -0
- investing_algorithm_framework/app/app.py +2233 -264
- investing_algorithm_framework/app/app_hook.py +28 -0
- investing_algorithm_framework/app/context.py +1724 -0
- investing_algorithm_framework/app/eventloop.py +620 -0
- investing_algorithm_framework/app/reporting/__init__.py +27 -0
- investing_algorithm_framework/app/reporting/ascii.py +921 -0
- investing_algorithm_framework/app/reporting/backtest_report.py +349 -0
- investing_algorithm_framework/app/reporting/charts/__init__.py +19 -0
- 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 +74 -0
- investing_algorithm_framework/app/reporting/charts/line_chart.py +11 -0
- investing_algorithm_framework/app/reporting/charts/monthly_returns_heatmap.py +70 -0
- investing_algorithm_framework/app/reporting/charts/ohlcv_data_completeness.py +51 -0
- investing_algorithm_framework/app/reporting/charts/rolling_sharp_ratio.py +79 -0
- investing_algorithm_framework/app/reporting/charts/yearly_returns_barchart.py +55 -0
- investing_algorithm_framework/app/reporting/generate.py +185 -0
- investing_algorithm_framework/app/reporting/tables/__init__.py +11 -0
- investing_algorithm_framework/app/reporting/tables/key_metrics_table.py +217 -0
- investing_algorithm_framework/app/reporting/tables/stop_loss_table.py +0 -0
- investing_algorithm_framework/app/reporting/tables/time_metrics_table.py +80 -0
- investing_algorithm_framework/app/reporting/tables/trade_metrics_table.py +147 -0
- investing_algorithm_framework/app/reporting/tables/trades_table.py +75 -0
- investing_algorithm_framework/app/reporting/tables/utils.py +29 -0
- investing_algorithm_framework/app/reporting/templates/report_template.html.j2 +154 -0
- investing_algorithm_framework/app/stateless/action_handlers/__init__.py +6 -3
- investing_algorithm_framework/app/stateless/action_handlers/action_handler_strategy.py +1 -1
- investing_algorithm_framework/app/stateless/action_handlers/check_online_handler.py +2 -1
- investing_algorithm_framework/app/stateless/action_handlers/run_strategy_handler.py +14 -7
- investing_algorithm_framework/app/stateless/exception_handler.py +1 -1
- investing_algorithm_framework/app/strategy.py +873 -52
- investing_algorithm_framework/app/task.py +5 -3
- investing_algorithm_framework/app/web/__init__.py +2 -1
- investing_algorithm_framework/app/web/controllers/__init__.py +2 -2
- investing_algorithm_framework/app/web/controllers/orders.py +4 -3
- investing_algorithm_framework/app/web/controllers/portfolio.py +1 -1
- investing_algorithm_framework/app/web/controllers/positions.py +3 -3
- investing_algorithm_framework/app/web/create_app.py +4 -2
- investing_algorithm_framework/app/web/error_handler.py +1 -1
- investing_algorithm_framework/app/web/schemas/order.py +2 -2
- investing_algorithm_framework/app/web/schemas/position.py +1 -0
- investing_algorithm_framework/cli/__init__.py +0 -0
- investing_algorithm_framework/cli/cli.py +231 -0
- investing_algorithm_framework/cli/deploy_to_aws_lambda.py +501 -0
- investing_algorithm_framework/cli/deploy_to_azure_function.py +718 -0
- investing_algorithm_framework/cli/initialize_app.py +603 -0
- investing_algorithm_framework/cli/templates/.gitignore.template +178 -0
- investing_algorithm_framework/cli/templates/app.py.template +18 -0
- investing_algorithm_framework/cli/templates/app_aws_lambda_function.py.template +48 -0
- investing_algorithm_framework/cli/templates/app_azure_function.py.template +14 -0
- investing_algorithm_framework/cli/templates/app_web.py.template +18 -0
- 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_readme.md.template +110 -0
- investing_algorithm_framework/cli/templates/aws_lambda_requirements.txt.template +2 -0
- investing_algorithm_framework/cli/templates/azure_function_function_app.py.template +65 -0
- investing_algorithm_framework/cli/templates/azure_function_host.json.template +15 -0
- investing_algorithm_framework/cli/templates/azure_function_local.settings.json.template +8 -0
- investing_algorithm_framework/cli/templates/azure_function_requirements.txt.template +3 -0
- investing_algorithm_framework/cli/templates/data_providers.py.template +17 -0
- investing_algorithm_framework/cli/templates/env.example.template +2 -0
- investing_algorithm_framework/cli/templates/env_azure_function.example.template +4 -0
- investing_algorithm_framework/cli/templates/market_data_providers.py.template +9 -0
- investing_algorithm_framework/cli/templates/readme.md.template +135 -0
- investing_algorithm_framework/cli/templates/requirements.txt.template +2 -0
- investing_algorithm_framework/cli/templates/run_backtest.py.template +20 -0
- investing_algorithm_framework/cli/templates/strategy.py.template +124 -0
- investing_algorithm_framework/cli/validate_backtest_checkpoints.py +197 -0
- investing_algorithm_framework/create_app.py +43 -9
- investing_algorithm_framework/dependency_container.py +121 -33
- investing_algorithm_framework/domain/__init__.py +109 -22
- investing_algorithm_framework/domain/algorithm_id.py +69 -0
- investing_algorithm_framework/domain/backtesting/__init__.py +25 -0
- investing_algorithm_framework/domain/backtesting/backtest.py +548 -0
- investing_algorithm_framework/domain/backtesting/backtest_date_range.py +113 -0
- investing_algorithm_framework/domain/backtesting/backtest_evaluation_focuss.py +241 -0
- investing_algorithm_framework/domain/backtesting/backtest_metrics.py +470 -0
- investing_algorithm_framework/domain/backtesting/backtest_permutation_test.py +275 -0
- investing_algorithm_framework/domain/backtesting/backtest_run.py +663 -0
- investing_algorithm_framework/domain/backtesting/backtest_summary_metrics.py +162 -0
- investing_algorithm_framework/domain/backtesting/backtest_utils.py +198 -0
- investing_algorithm_framework/domain/backtesting/combine_backtests.py +392 -0
- investing_algorithm_framework/domain/config.py +60 -138
- investing_algorithm_framework/domain/constants.py +23 -34
- investing_algorithm_framework/domain/data_provider.py +334 -0
- investing_algorithm_framework/domain/data_structures.py +42 -0
- investing_algorithm_framework/domain/decimal_parsing.py +40 -0
- investing_algorithm_framework/domain/exceptions.py +51 -1
- investing_algorithm_framework/domain/models/__init__.py +29 -14
- investing_algorithm_framework/domain/models/app_mode.py +34 -0
- investing_algorithm_framework/domain/models/base_model.py +3 -1
- investing_algorithm_framework/domain/models/data/__init__.py +7 -0
- investing_algorithm_framework/domain/models/data/data_source.py +222 -0
- investing_algorithm_framework/domain/models/data/data_type.py +46 -0
- investing_algorithm_framework/domain/models/event.py +35 -0
- investing_algorithm_framework/domain/models/market/__init__.py +5 -0
- investing_algorithm_framework/domain/models/market/market_credential.py +88 -0
- investing_algorithm_framework/domain/models/order/__init__.py +3 -4
- investing_algorithm_framework/domain/models/order/order.py +243 -86
- investing_algorithm_framework/domain/models/order/order_status.py +2 -2
- investing_algorithm_framework/domain/models/order/order_type.py +1 -3
- investing_algorithm_framework/domain/models/portfolio/__init__.py +7 -2
- investing_algorithm_framework/domain/models/portfolio/portfolio.py +134 -1
- investing_algorithm_framework/domain/models/portfolio/portfolio_configuration.py +37 -37
- investing_algorithm_framework/domain/models/portfolio/portfolio_snapshot.py +208 -0
- investing_algorithm_framework/domain/models/position/__init__.py +3 -2
- investing_algorithm_framework/domain/models/position/position.py +29 -0
- investing_algorithm_framework/domain/models/position/position_size.py +41 -0
- investing_algorithm_framework/domain/models/position/{position_cost.py → position_snapshot.py} +16 -8
- 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 +45 -0
- investing_algorithm_framework/domain/models/strategy_profile.py +33 -0
- investing_algorithm_framework/domain/models/time_frame.py +94 -98
- investing_algorithm_framework/domain/models/time_interval.py +33 -0
- investing_algorithm_framework/domain/models/time_unit.py +111 -2
- investing_algorithm_framework/domain/models/tracing/__init__.py +0 -0
- investing_algorithm_framework/domain/models/tracing/trace.py +23 -0
- investing_algorithm_framework/domain/models/trade/__init__.py +11 -0
- investing_algorithm_framework/domain/models/trade/trade.py +389 -0
- investing_algorithm_framework/domain/models/trade/trade_status.py +40 -0
- investing_algorithm_framework/domain/models/trade/trade_stop_loss.py +332 -0
- investing_algorithm_framework/domain/models/trade/trade_take_profit.py +365 -0
- investing_algorithm_framework/domain/order_executor.py +112 -0
- investing_algorithm_framework/domain/portfolio_provider.py +118 -0
- investing_algorithm_framework/domain/services/__init__.py +11 -0
- investing_algorithm_framework/domain/services/market_credential_service.py +37 -0
- investing_algorithm_framework/domain/services/portfolios/__init__.py +5 -0
- investing_algorithm_framework/domain/services/portfolios/portfolio_sync_service.py +9 -0
- investing_algorithm_framework/domain/services/rounding_service.py +27 -0
- investing_algorithm_framework/domain/services/state_handler.py +38 -0
- investing_algorithm_framework/domain/strategy.py +1 -29
- investing_algorithm_framework/domain/utils/__init__.py +16 -4
- investing_algorithm_framework/domain/utils/csv.py +22 -0
- investing_algorithm_framework/domain/utils/custom_tqdm.py +22 -0
- investing_algorithm_framework/domain/utils/dates.py +57 -0
- investing_algorithm_framework/domain/utils/jupyter_notebook_detection.py +19 -0
- investing_algorithm_framework/domain/utils/polars.py +53 -0
- investing_algorithm_framework/domain/utils/random.py +29 -0
- investing_algorithm_framework/download_data.py +244 -0
- investing_algorithm_framework/infrastructure/__init__.py +39 -11
- investing_algorithm_framework/infrastructure/data_providers/__init__.py +36 -0
- investing_algorithm_framework/infrastructure/data_providers/ccxt.py +1152 -0
- investing_algorithm_framework/infrastructure/data_providers/csv.py +568 -0
- 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 +87 -13
- investing_algorithm_framework/infrastructure/models/__init__.py +13 -4
- investing_algorithm_framework/infrastructure/models/decimal_parser.py +14 -0
- investing_algorithm_framework/infrastructure/models/order/__init__.py +2 -2
- investing_algorithm_framework/infrastructure/models/order/order.py +73 -73
- investing_algorithm_framework/infrastructure/models/order/order_metadata.py +44 -0
- investing_algorithm_framework/infrastructure/models/order_trade_association.py +10 -0
- investing_algorithm_framework/infrastructure/models/portfolio/__init__.py +3 -2
- investing_algorithm_framework/infrastructure/models/portfolio/portfolio_snapshot.py +37 -0
- investing_algorithm_framework/infrastructure/models/portfolio/{portfolio.py → sql_portfolio.py} +57 -3
- investing_algorithm_framework/infrastructure/models/position/__init__.py +2 -2
- investing_algorithm_framework/infrastructure/models/position/position.py +16 -11
- investing_algorithm_framework/infrastructure/models/position/position_snapshot.py +23 -0
- investing_algorithm_framework/infrastructure/models/trades/__init__.py +9 -0
- investing_algorithm_framework/infrastructure/models/trades/trade.py +130 -0
- investing_algorithm_framework/infrastructure/models/trades/trade_stop_loss.py +59 -0
- investing_algorithm_framework/infrastructure/models/trades/trade_take_profit.py +55 -0
- investing_algorithm_framework/infrastructure/order_executors/__init__.py +21 -0
- investing_algorithm_framework/infrastructure/order_executors/backtest_oder_executor.py +28 -0
- investing_algorithm_framework/infrastructure/order_executors/ccxt_order_executor.py +200 -0
- investing_algorithm_framework/infrastructure/portfolio_providers/__init__.py +19 -0
- investing_algorithm_framework/infrastructure/portfolio_providers/ccxt_portfolio_provider.py +199 -0
- investing_algorithm_framework/infrastructure/repositories/__init__.py +13 -5
- investing_algorithm_framework/infrastructure/repositories/order_metadata_repository.py +17 -0
- investing_algorithm_framework/infrastructure/repositories/order_repository.py +32 -19
- investing_algorithm_framework/infrastructure/repositories/portfolio_repository.py +2 -2
- investing_algorithm_framework/infrastructure/repositories/portfolio_snapshot_repository.py +56 -0
- investing_algorithm_framework/infrastructure/repositories/position_repository.py +47 -4
- investing_algorithm_framework/infrastructure/repositories/position_snapshot_repository.py +21 -0
- investing_algorithm_framework/infrastructure/repositories/repository.py +85 -31
- investing_algorithm_framework/infrastructure/repositories/trade_repository.py +71 -0
- investing_algorithm_framework/infrastructure/repositories/trade_stop_loss_repository.py +29 -0
- investing_algorithm_framework/infrastructure/repositories/trade_take_profit_repository.py +29 -0
- investing_algorithm_framework/infrastructure/services/__init__.py +9 -2
- investing_algorithm_framework/infrastructure/services/aws/__init__.py +6 -0
- investing_algorithm_framework/infrastructure/services/aws/state_handler.py +193 -0
- investing_algorithm_framework/infrastructure/services/azure/__init__.py +5 -0
- investing_algorithm_framework/infrastructure/services/azure/state_handler.py +158 -0
- investing_algorithm_framework/infrastructure/services/backtesting/__init__.py +9 -0
- investing_algorithm_framework/infrastructure/services/backtesting/backtest_service.py +2596 -0
- investing_algorithm_framework/infrastructure/services/backtesting/event_backtest_service.py +285 -0
- investing_algorithm_framework/infrastructure/services/backtesting/vector_backtest_service.py +468 -0
- investing_algorithm_framework/services/__init__.py +127 -10
- investing_algorithm_framework/services/configuration_service.py +95 -0
- investing_algorithm_framework/services/data_providers/__init__.py +5 -0
- investing_algorithm_framework/services/data_providers/data_provider_service.py +1058 -0
- investing_algorithm_framework/services/market_credential_service.py +40 -0
- investing_algorithm_framework/services/metrics/__init__.py +119 -0
- investing_algorithm_framework/services/metrics/alpha.py +0 -0
- investing_algorithm_framework/services/metrics/beta.py +0 -0
- investing_algorithm_framework/services/metrics/cagr.py +60 -0
- investing_algorithm_framework/services/metrics/calmar_ratio.py +40 -0
- investing_algorithm_framework/services/metrics/drawdown.py +218 -0
- investing_algorithm_framework/services/metrics/equity_curve.py +24 -0
- investing_algorithm_framework/services/metrics/exposure.py +210 -0
- investing_algorithm_framework/services/metrics/generate.py +358 -0
- investing_algorithm_framework/services/metrics/mean_daily_return.py +84 -0
- investing_algorithm_framework/services/metrics/price_efficiency.py +57 -0
- investing_algorithm_framework/services/metrics/profit_factor.py +165 -0
- investing_algorithm_framework/services/metrics/recovery.py +113 -0
- investing_algorithm_framework/services/metrics/returns.py +452 -0
- investing_algorithm_framework/services/metrics/risk_free_rate.py +28 -0
- investing_algorithm_framework/services/metrics/sharpe_ratio.py +137 -0
- investing_algorithm_framework/services/metrics/sortino_ratio.py +74 -0
- investing_algorithm_framework/services/metrics/standard_deviation.py +156 -0
- investing_algorithm_framework/services/metrics/trades.py +473 -0
- investing_algorithm_framework/services/metrics/treynor_ratio.py +0 -0
- investing_algorithm_framework/services/metrics/ulcer.py +0 -0
- investing_algorithm_framework/services/metrics/value_at_risk.py +0 -0
- investing_algorithm_framework/services/metrics/volatility.py +118 -0
- investing_algorithm_framework/services/metrics/win_rate.py +177 -0
- investing_algorithm_framework/services/order_service/__init__.py +9 -0
- investing_algorithm_framework/services/order_service/order_backtest_service.py +178 -0
- investing_algorithm_framework/services/order_service/order_executor_lookup.py +110 -0
- investing_algorithm_framework/services/order_service/order_service.py +826 -0
- investing_algorithm_framework/services/portfolios/__init__.py +16 -0
- investing_algorithm_framework/services/portfolios/backtest_portfolio_service.py +54 -0
- investing_algorithm_framework/services/{portfolio_configuration_service.py → portfolios/portfolio_configuration_service.py} +27 -12
- investing_algorithm_framework/services/portfolios/portfolio_provider_lookup.py +106 -0
- investing_algorithm_framework/services/portfolios/portfolio_service.py +188 -0
- investing_algorithm_framework/services/portfolios/portfolio_snapshot_service.py +136 -0
- investing_algorithm_framework/services/portfolios/portfolio_sync_service.py +182 -0
- investing_algorithm_framework/services/positions/__init__.py +7 -0
- investing_algorithm_framework/services/positions/position_service.py +210 -0
- investing_algorithm_framework/services/positions/position_snapshot_service.py +18 -0
- investing_algorithm_framework/services/repository_service.py +8 -2
- investing_algorithm_framework/services/trade_order_evaluator/__init__.py +9 -0
- investing_algorithm_framework/services/trade_order_evaluator/backtest_trade_oder_evaluator.py +117 -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 +9 -0
- investing_algorithm_framework/services/trade_service/trade_service.py +1099 -0
- 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.25.6.dist-info/METADATA +535 -0
- investing_algorithm_framework-7.25.6.dist-info/RECORD +268 -0
- {investing_algorithm_framework-1.3.1.dist-info → investing_algorithm_framework-7.25.6.dist-info}/WHEEL +1 -2
- investing_algorithm_framework-7.25.6.dist-info/entry_points.txt +3 -0
- investing_algorithm_framework/app/algorithm.py +0 -410
- investing_algorithm_framework/domain/models/market_data/__init__.py +0 -11
- investing_algorithm_framework/domain/models/market_data/asset_price.py +0 -50
- investing_algorithm_framework/domain/models/market_data/ohlcv.py +0 -76
- investing_algorithm_framework/domain/models/market_data/order_book.py +0 -63
- investing_algorithm_framework/domain/models/market_data/ticker.py +0 -92
- investing_algorithm_framework/domain/models/order/order_fee.py +0 -45
- investing_algorithm_framework/domain/models/trading_data_types.py +0 -47
- investing_algorithm_framework/domain/models/trading_time_frame.py +0 -205
- investing_algorithm_framework/domain/singleton.py +0 -9
- investing_algorithm_framework/infrastructure/models/order/order_fee.py +0 -21
- investing_algorithm_framework/infrastructure/models/position/position_cost.py +0 -32
- investing_algorithm_framework/infrastructure/repositories/order_fee_repository.py +0 -15
- investing_algorithm_framework/infrastructure/repositories/position_cost_repository.py +0 -16
- investing_algorithm_framework/infrastructure/services/market_service.py +0 -422
- investing_algorithm_framework/services/market_data_service.py +0 -75
- investing_algorithm_framework/services/order_service.py +0 -464
- investing_algorithm_framework/services/portfolio_service.py +0 -105
- investing_algorithm_framework/services/position_cost_service.py +0 -5
- investing_algorithm_framework/services/position_service.py +0 -50
- investing_algorithm_framework/services/strategy_orchestrator_service.py +0 -219
- investing_algorithm_framework/setup_logging.py +0 -40
- investing_algorithm_framework-1.3.1.dist-info/AUTHORS.md +0 -8
- investing_algorithm_framework-1.3.1.dist-info/METADATA +0 -172
- investing_algorithm_framework-1.3.1.dist-info/RECORD +0 -103
- investing_algorithm_framework-1.3.1.dist-info/top_level.txt +0 -1
- {investing_algorithm_framework-1.3.1.dist-info → investing_algorithm_framework-7.25.6.dist-info}/LICENSE +0 -0
|
@@ -1,464 +0,0 @@
|
|
|
1
|
-
from datetime import datetime
|
|
2
|
-
|
|
3
|
-
from investing_algorithm_framework.domain import OrderType, OrderSide, \
|
|
4
|
-
OperationalException, OrderStatus
|
|
5
|
-
from investing_algorithm_framework.services.repository_service \
|
|
6
|
-
import RepositoryService
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class OrderService(RepositoryService):
|
|
10
|
-
|
|
11
|
-
def __init__(
|
|
12
|
-
self,
|
|
13
|
-
order_repository,
|
|
14
|
-
order_fee_repository,
|
|
15
|
-
market_service,
|
|
16
|
-
position_repository,
|
|
17
|
-
position_cost_repository,
|
|
18
|
-
portfolio_repository,
|
|
19
|
-
portfolio_configuration_service,
|
|
20
|
-
):
|
|
21
|
-
super(OrderService, self).__init__(order_repository)
|
|
22
|
-
self.order_repository = order_repository
|
|
23
|
-
self.order_fee_repository = order_fee_repository
|
|
24
|
-
self.market_service = market_service
|
|
25
|
-
self.position_repository = position_repository
|
|
26
|
-
self.position_cost_repository = position_cost_repository
|
|
27
|
-
self.portfolio_repository = portfolio_repository
|
|
28
|
-
self.portfolio_configuration_service = portfolio_configuration_service
|
|
29
|
-
|
|
30
|
-
def create(self, data, execute=True, validate=True, sync=True):
|
|
31
|
-
portfolio_id = data["portfolio_id"]
|
|
32
|
-
portfolio = self.portfolio_repository.get(portfolio_id)
|
|
33
|
-
|
|
34
|
-
if validate:
|
|
35
|
-
self.validate_order(data, portfolio)
|
|
36
|
-
|
|
37
|
-
order_fee = None
|
|
38
|
-
|
|
39
|
-
if "fee" in data:
|
|
40
|
-
order_fee = data.pop("fee")
|
|
41
|
-
|
|
42
|
-
del data["portfolio_id"]
|
|
43
|
-
symbol = data["target_symbol"]
|
|
44
|
-
|
|
45
|
-
if validate:
|
|
46
|
-
self.validate_order(data, portfolio)
|
|
47
|
-
|
|
48
|
-
position = self._create_position_if_not_exists(symbol, portfolio)
|
|
49
|
-
data["position_id"] = position.id
|
|
50
|
-
order = self.order_repository.create(data)
|
|
51
|
-
order_id = order.id
|
|
52
|
-
|
|
53
|
-
if order_fee:
|
|
54
|
-
order_fee["order_id"] = order_id
|
|
55
|
-
self.order_fee_repository.create(order_fee)
|
|
56
|
-
|
|
57
|
-
if execute:
|
|
58
|
-
portfolio.configuration = self.portfolio_configuration_service\
|
|
59
|
-
.get(portfolio.identifier)
|
|
60
|
-
self.execute_order(order_id, portfolio)
|
|
61
|
-
|
|
62
|
-
if sync:
|
|
63
|
-
if OrderSide.BUY.equals(order.get_side()):
|
|
64
|
-
self._sync_portfolio_with_created_buy_order(order)
|
|
65
|
-
else:
|
|
66
|
-
self._sync_portfolio_with_created_sell_order(order)
|
|
67
|
-
|
|
68
|
-
return order
|
|
69
|
-
|
|
70
|
-
def update(self, object_id, data):
|
|
71
|
-
|
|
72
|
-
if "fee" in data:
|
|
73
|
-
order_fee_data = data.pop("fee")
|
|
74
|
-
|
|
75
|
-
if self.order_fee_repository.exists({"order_id": object_id}):
|
|
76
|
-
order_fee = self.order_fee_repository.find({"order_id": object_id})
|
|
77
|
-
self.order_fee_repository.update(order_fee.id, order_fee_data)
|
|
78
|
-
else:
|
|
79
|
-
order_fee_data["order_id"] = object_id
|
|
80
|
-
self.order_fee_repository.create(order_fee_data)
|
|
81
|
-
|
|
82
|
-
return self.order_repository.update(object_id, data)
|
|
83
|
-
|
|
84
|
-
def get_order_fee(self, order_id):
|
|
85
|
-
return self.order_fee_repository.find({"order": order_id})
|
|
86
|
-
|
|
87
|
-
def execute_order(self, order_id, portfolio):
|
|
88
|
-
self.market_service.initialize(portfolio.configuration)
|
|
89
|
-
order = self.get(order_id)
|
|
90
|
-
|
|
91
|
-
if OrderType.LIMIT.equals(order.get_type()):
|
|
92
|
-
|
|
93
|
-
if OrderSide.BUY.equals(order.get_side()):
|
|
94
|
-
self.market_service.create_limit_buy_order(
|
|
95
|
-
target_symbol=order.get_target_symbol(),
|
|
96
|
-
trading_symbol=order.get_trading_symbol(),
|
|
97
|
-
amount=order.get_amount(),
|
|
98
|
-
price=order.get_price()
|
|
99
|
-
)
|
|
100
|
-
else:
|
|
101
|
-
self.market_service.create_limit_sell_order(
|
|
102
|
-
target_symbol=order.get_target_symbol(),
|
|
103
|
-
trading_symbol=order.get_trading_symbol(),
|
|
104
|
-
amount=order.get_amount(),
|
|
105
|
-
price=order.get_price()
|
|
106
|
-
)
|
|
107
|
-
else:
|
|
108
|
-
if OrderSide.BUY.equals(order.get_side()):
|
|
109
|
-
raise OperationalException("Market buy order not supported")
|
|
110
|
-
else:
|
|
111
|
-
self.market_service.create_market_sell_order(
|
|
112
|
-
target_symbol=order.get_target_symbol(),
|
|
113
|
-
trading_symbol=order.get_trading_symbol(),
|
|
114
|
-
amount=order.get_amount(),
|
|
115
|
-
)
|
|
116
|
-
|
|
117
|
-
self.update(order_id, {"status": OrderStatus.OPEN.value})
|
|
118
|
-
|
|
119
|
-
def validate_order(self, order_data, portfolio):
|
|
120
|
-
|
|
121
|
-
if OrderSide.BUY.equals(order_data["side"]):
|
|
122
|
-
self.validate_buy_order(order_data, portfolio)
|
|
123
|
-
else:
|
|
124
|
-
self.validate_sell_order(order_data, portfolio)
|
|
125
|
-
|
|
126
|
-
if OrderType.LIMIT.equals(order_data["type"]):
|
|
127
|
-
self.validate_limit_order(order_data, portfolio)
|
|
128
|
-
else:
|
|
129
|
-
self.validate_market_order(order_data, portfolio)
|
|
130
|
-
|
|
131
|
-
def validate_sell_order(self, order_data, portfolio):
|
|
132
|
-
if not self.position_repository.exists(
|
|
133
|
-
{
|
|
134
|
-
"symbol": order_data["target_symbol"],
|
|
135
|
-
"portfolio": portfolio.id
|
|
136
|
-
}
|
|
137
|
-
):
|
|
138
|
-
raise OperationalException(
|
|
139
|
-
"Can't add sell order to non existing position"
|
|
140
|
-
)
|
|
141
|
-
|
|
142
|
-
position = self.position_repository\
|
|
143
|
-
.find(
|
|
144
|
-
{
|
|
145
|
-
"symbol": order_data["target_symbol"],
|
|
146
|
-
"portfolio": portfolio.id
|
|
147
|
-
}
|
|
148
|
-
)
|
|
149
|
-
|
|
150
|
-
if position.amount < order_data["amount"]:
|
|
151
|
-
raise OperationalException(
|
|
152
|
-
"Order amount is larger then amount of open position"
|
|
153
|
-
)
|
|
154
|
-
|
|
155
|
-
if not order_data["trading_symbol"] == portfolio.trading_symbol:
|
|
156
|
-
raise OperationalException(
|
|
157
|
-
f"Can't add sell order with target "
|
|
158
|
-
f"symbol {order_data['target_symbol']} to "
|
|
159
|
-
f"portfolio with trading symbol {portfolio.trading_symbol}"
|
|
160
|
-
)
|
|
161
|
-
|
|
162
|
-
@staticmethod
|
|
163
|
-
def validate_buy_order(order_data, portfolio):
|
|
164
|
-
|
|
165
|
-
if not order_data["trading_symbol"] == portfolio.trading_symbol:
|
|
166
|
-
raise OperationalException(
|
|
167
|
-
f"Can't add buy order with trading "
|
|
168
|
-
f"symbol {order_data['trading_symbol']} to "
|
|
169
|
-
f"portfolio with trading symbol {portfolio.trading_symbol}"
|
|
170
|
-
)
|
|
171
|
-
|
|
172
|
-
def validate_limit_order(self, order_data, portfolio):
|
|
173
|
-
|
|
174
|
-
if OrderSide.SELL.equals(order_data["side"]):
|
|
175
|
-
amount = order_data["amount"]
|
|
176
|
-
position = self.position_repository\
|
|
177
|
-
.find(
|
|
178
|
-
{
|
|
179
|
-
"portfolio": portfolio.id,
|
|
180
|
-
"symbol": portfolio.trading_symbol
|
|
181
|
-
}
|
|
182
|
-
)
|
|
183
|
-
if amount > position.amount:
|
|
184
|
-
raise OperationalException(
|
|
185
|
-
f"Order amount: {amount} {portfolio.trading_symbol}, is "
|
|
186
|
-
f"larger then position size: {position.amount} "
|
|
187
|
-
f"{portfolio.trading_symbol} of the portfolio"
|
|
188
|
-
)
|
|
189
|
-
else:
|
|
190
|
-
total_price = order_data["amount"] * order_data["price"]
|
|
191
|
-
unallocated_position = self.position_repository\
|
|
192
|
-
.find(
|
|
193
|
-
{
|
|
194
|
-
"portfolio": portfolio.id,
|
|
195
|
-
"symbol": portfolio.trading_symbol
|
|
196
|
-
}
|
|
197
|
-
)
|
|
198
|
-
amount = unallocated_position.amount
|
|
199
|
-
|
|
200
|
-
if amount < total_price:
|
|
201
|
-
raise OperationalException(
|
|
202
|
-
f"Order total: {total_price} {portfolio.trading_symbol}, is "
|
|
203
|
-
f"larger then unallocated size: {amount} "
|
|
204
|
-
f"{portfolio.trading_symbol} of the portfolio"
|
|
205
|
-
)
|
|
206
|
-
|
|
207
|
-
def validate_market_order(self, order_data, portfolio):
|
|
208
|
-
|
|
209
|
-
if OrderSide.BUY.equals(order_data["side"]):
|
|
210
|
-
|
|
211
|
-
if "amount" not in order_data:
|
|
212
|
-
raise OperationalException(
|
|
213
|
-
f"Market order needs an amount specified in the trading "
|
|
214
|
-
f"symbol {order_data['trading_symbol']}"
|
|
215
|
-
)
|
|
216
|
-
|
|
217
|
-
if order_data['amount'] > portfolio.unallocated:
|
|
218
|
-
raise OperationalException(
|
|
219
|
-
f"Market order amount "
|
|
220
|
-
f"{order_data['amount']}"
|
|
221
|
-
f"{portfolio.trading_symbol.upper()} is larger then "
|
|
222
|
-
f"unallocated {portfolio.unallocated} "
|
|
223
|
-
f"{portfolio.trading_symbol.upper()}"
|
|
224
|
-
)
|
|
225
|
-
else:
|
|
226
|
-
position = self.position_repository\
|
|
227
|
-
.find(
|
|
228
|
-
{
|
|
229
|
-
"symbol": order_data["target_symbol"],
|
|
230
|
-
"portfolio": portfolio.id
|
|
231
|
-
}
|
|
232
|
-
)
|
|
233
|
-
|
|
234
|
-
if position is None:
|
|
235
|
-
raise OperationalException(
|
|
236
|
-
"Can't add market sell order to non existing position"
|
|
237
|
-
)
|
|
238
|
-
|
|
239
|
-
if order_data['amount'] > position.amount:
|
|
240
|
-
raise OperationalException(
|
|
241
|
-
"Sell order amount larger then position size"
|
|
242
|
-
)
|
|
243
|
-
|
|
244
|
-
def check_pending_orders(self):
|
|
245
|
-
pending_orders = self.get_all({"status": OrderStatus.OPEN.value})
|
|
246
|
-
|
|
247
|
-
for order in pending_orders:
|
|
248
|
-
position = self.position_repository.get(order.position_id)
|
|
249
|
-
portfolio = self.portfolio_repository.get(position.portfolio_id)
|
|
250
|
-
portfolio_configuration = self.portfolio_configuration_service\
|
|
251
|
-
.get(portfolio.identifier)
|
|
252
|
-
self.market_service.initialize(portfolio_configuration)
|
|
253
|
-
external_order = self.market_service.get_order(order)
|
|
254
|
-
|
|
255
|
-
if OrderStatus.from_value(external_order.status)\
|
|
256
|
-
.equals(order.status):
|
|
257
|
-
continue
|
|
258
|
-
|
|
259
|
-
updated_order = self.update(order.id, external_order.to_dict())
|
|
260
|
-
|
|
261
|
-
if OrderStatus.CLOSED.equals(updated_order.status):
|
|
262
|
-
|
|
263
|
-
if OrderSide.BUY.equals(updated_order.side):
|
|
264
|
-
self._sync_portfolio_with_executed_buy_order(updated_order)
|
|
265
|
-
else:
|
|
266
|
-
self._sync_portfolio_with_executed_sell_order(updated_order)
|
|
267
|
-
elif OrderStatus.CANCELED.equals(updated_order.status):
|
|
268
|
-
|
|
269
|
-
if OrderSide.BUY.equals(updated_order.side):
|
|
270
|
-
self._sync_portfolio_with_cancelled_buy_order(
|
|
271
|
-
updated_order
|
|
272
|
-
)
|
|
273
|
-
else:
|
|
274
|
-
self._sync_portfolio_with_cancelled_sell_order(
|
|
275
|
-
updated_order
|
|
276
|
-
)
|
|
277
|
-
elif OrderStatus.REJECTED.equals(updated_order.status) \
|
|
278
|
-
or OrderStatus.EXPIRED.equals(updated_order.status):
|
|
279
|
-
|
|
280
|
-
if OrderSide.BUY.equals(updated_order.side):
|
|
281
|
-
self._sync_portfolio_with_failed_buy_order(updated_order)
|
|
282
|
-
else:
|
|
283
|
-
self._sync_portfolio_with_failed_sell_order(updated_order)
|
|
284
|
-
|
|
285
|
-
def _create_position_if_not_exists(self, symbol, portfolio):
|
|
286
|
-
if not self.position_repository.exists(
|
|
287
|
-
{"portfolio": portfolio.id, "symbol": symbol}
|
|
288
|
-
):
|
|
289
|
-
self.position_repository \
|
|
290
|
-
.create({"portfolio_id": portfolio.id, "symbol": symbol})
|
|
291
|
-
position = self.position_repository \
|
|
292
|
-
.find({"portfolio": portfolio.id, "symbol": symbol})
|
|
293
|
-
else:
|
|
294
|
-
position = self.position_repository \
|
|
295
|
-
.find({"portfolio": portfolio.id, "symbol": symbol})
|
|
296
|
-
|
|
297
|
-
return position
|
|
298
|
-
|
|
299
|
-
def _sync_portfolio_with_created_buy_order(self, order):
|
|
300
|
-
position = self.position_repository.get(order.position_id)
|
|
301
|
-
portfolio = self.portfolio_repository.get(position.portfolio_id)
|
|
302
|
-
trading_symbol_position = self.position_repository.find(
|
|
303
|
-
{
|
|
304
|
-
"portfolio": portfolio.id,
|
|
305
|
-
"symbol": portfolio.trading_symbol
|
|
306
|
-
}
|
|
307
|
-
)
|
|
308
|
-
self.portfolio_repository.update(
|
|
309
|
-
portfolio.id,
|
|
310
|
-
{"unallocated": portfolio.unallocated - order.amount * order.price}
|
|
311
|
-
)
|
|
312
|
-
self.position_repository.update(
|
|
313
|
-
trading_symbol_position.id,
|
|
314
|
-
{
|
|
315
|
-
"amount": trading_symbol_position.amount - order.amount * order.price
|
|
316
|
-
}
|
|
317
|
-
)
|
|
318
|
-
|
|
319
|
-
def _sync_portfolio_with_created_sell_order(self, order):
|
|
320
|
-
position = self.position_repository.get(order.position_id)
|
|
321
|
-
self.position_repository.update(
|
|
322
|
-
position.id,
|
|
323
|
-
{
|
|
324
|
-
"amount": position.amount - order.amount
|
|
325
|
-
}
|
|
326
|
-
)
|
|
327
|
-
|
|
328
|
-
def _sync_portfolio_with_executed_buy_order(self, order):
|
|
329
|
-
position = self.position_repository.get(order.position_id)
|
|
330
|
-
position_cost = self.position_cost_repository.create(
|
|
331
|
-
{
|
|
332
|
-
"position_id": position.id,
|
|
333
|
-
"price": order.price,
|
|
334
|
-
"amount": order.amount,
|
|
335
|
-
"created_at": order.created_at,
|
|
336
|
-
}
|
|
337
|
-
)
|
|
338
|
-
self.position_repository.update(
|
|
339
|
-
position.id, {"amount": position.amount + order.amount}
|
|
340
|
-
)
|
|
341
|
-
cost = position_cost.price * position_cost.amount
|
|
342
|
-
portfolio = self.portfolio_repository.get(position.portfolio_id)
|
|
343
|
-
self.portfolio_repository.update(
|
|
344
|
-
portfolio.id,
|
|
345
|
-
{
|
|
346
|
-
"total_cost": portfolio.total_cost + cost,
|
|
347
|
-
}
|
|
348
|
-
)
|
|
349
|
-
|
|
350
|
-
def _sync_portfolio_with_executed_sell_order(self, order):
|
|
351
|
-
position = self.position_repository.get(order.position_id)
|
|
352
|
-
portfolio = self.portfolio_repository.get(position.portfolio_id)
|
|
353
|
-
amount_to_sell = order.amount
|
|
354
|
-
net_gain = 0
|
|
355
|
-
revenue = order.amount * order.price
|
|
356
|
-
total_cost = 0
|
|
357
|
-
|
|
358
|
-
while amount_to_sell > 0:
|
|
359
|
-
position_cost = self.position_cost_repository.find(
|
|
360
|
-
{"position": position.id}
|
|
361
|
-
)
|
|
362
|
-
if position_cost is None:
|
|
363
|
-
break
|
|
364
|
-
|
|
365
|
-
if position_cost.amount > amount_to_sell:
|
|
366
|
-
net_gain += amount_to_sell * \
|
|
367
|
-
(order.price - position_cost.price)
|
|
368
|
-
self.position_cost_repository.update(
|
|
369
|
-
position_cost.id,
|
|
370
|
-
{"amount": position_cost.amount - amount_to_sell}
|
|
371
|
-
)
|
|
372
|
-
amount_to_sell = 0
|
|
373
|
-
else:
|
|
374
|
-
net_gain += position_cost.amount * (order.price - position_cost.price)
|
|
375
|
-
total_cost += position_cost.amount * position_cost.price
|
|
376
|
-
amount_to_sell -= position_cost.amount
|
|
377
|
-
self.position_cost_repository.delete(position_cost.id)
|
|
378
|
-
|
|
379
|
-
# Update the buy order net gain
|
|
380
|
-
buy_order = self.order_repository.find(
|
|
381
|
-
{
|
|
382
|
-
"position": position.id,
|
|
383
|
-
"side": OrderSide.BUY.value,
|
|
384
|
-
"created_at": position_cost.created_at
|
|
385
|
-
}
|
|
386
|
-
)
|
|
387
|
-
|
|
388
|
-
if buy_order is not None:
|
|
389
|
-
self.order_repository.update(
|
|
390
|
-
buy_order.id,
|
|
391
|
-
{
|
|
392
|
-
"net_gain": buy_order.net_gain + net_gain,
|
|
393
|
-
"trade_closed_at": datetime.now(),
|
|
394
|
-
"trade_closing_price": order.price,
|
|
395
|
-
}
|
|
396
|
-
)
|
|
397
|
-
|
|
398
|
-
# Update the portfolio
|
|
399
|
-
self.portfolio_repository.update(
|
|
400
|
-
portfolio.id,
|
|
401
|
-
{
|
|
402
|
-
"unallocated": portfolio.unallocated + revenue,
|
|
403
|
-
"total_net_gain": portfolio.total_net_gain + net_gain,
|
|
404
|
-
"net_size": portfolio.net_size + revenue,
|
|
405
|
-
}
|
|
406
|
-
)
|
|
407
|
-
|
|
408
|
-
def _sync_portfolio_with_cancelled_buy_order(self, order):
|
|
409
|
-
position = self.position_repository.find({"id": order.position_id})
|
|
410
|
-
portfolio = self.portfolio_repository.get(position.portfolio_id)
|
|
411
|
-
|
|
412
|
-
if OrderType.LIMIT.equals(order.type):
|
|
413
|
-
self.portfolio_repository.update(
|
|
414
|
-
portfolio.id,
|
|
415
|
-
{
|
|
416
|
-
"unallocated":
|
|
417
|
-
portfolio.unallocated + order.amount * order.price
|
|
418
|
-
}
|
|
419
|
-
)
|
|
420
|
-
|
|
421
|
-
def _sync_portfolio_with_cancelled_sell_order(self, order):
|
|
422
|
-
position = self.position_repository.find({"id": order.position_id})
|
|
423
|
-
self.position_repository.update(
|
|
424
|
-
position.id,
|
|
425
|
-
{
|
|
426
|
-
"amount": position.amount + order.amount
|
|
427
|
-
}
|
|
428
|
-
)
|
|
429
|
-
|
|
430
|
-
def _sync_portfolio_with_failed_buy_order(self, order):
|
|
431
|
-
position = self.position_repository.find({"id": order.position_id})
|
|
432
|
-
portfolio = self.portfolio_repository.get(position.portfolio_id)
|
|
433
|
-
|
|
434
|
-
if OrderType.LIMIT.equals(order.type):
|
|
435
|
-
self.portfolio_repository.update(
|
|
436
|
-
portfolio.id,
|
|
437
|
-
{
|
|
438
|
-
"unallocated":
|
|
439
|
-
portfolio.unallocated + order.amount * order.price
|
|
440
|
-
}
|
|
441
|
-
)
|
|
442
|
-
|
|
443
|
-
def _sync_portfolio_with_failed_sell_order(self, order):
|
|
444
|
-
position = self.position_repository.find({"id": order.position_id})
|
|
445
|
-
self.position_repository.update(
|
|
446
|
-
position.id,
|
|
447
|
-
{
|
|
448
|
-
"amount": position.amount + order.amount
|
|
449
|
-
}
|
|
450
|
-
)
|
|
451
|
-
|
|
452
|
-
def cancel_order(self, order_id):
|
|
453
|
-
self.check_pending_orders()
|
|
454
|
-
order = self.order_repository.get(order_id)
|
|
455
|
-
|
|
456
|
-
if order is not None:
|
|
457
|
-
|
|
458
|
-
if OrderStatus.OPEN.equals(order.status):
|
|
459
|
-
portfolio = self.portfolio_repository\
|
|
460
|
-
.find({"position": order.position_id})
|
|
461
|
-
portfolio_configuration = self.portfolio_configuration_service\
|
|
462
|
-
.get(portfolio.identifier)
|
|
463
|
-
self.market_service.initialize(portfolio_configuration)
|
|
464
|
-
self.market_service.cancel_order(order_id)
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
from investing_algorithm_framework.services.repository_service \
|
|
2
|
-
import RepositoryService
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
class PortfolioService(RepositoryService):
|
|
6
|
-
|
|
7
|
-
def __init__(
|
|
8
|
-
self,
|
|
9
|
-
market_service,
|
|
10
|
-
position_repository,
|
|
11
|
-
order_service,
|
|
12
|
-
portfolio_repository,
|
|
13
|
-
portfolio_configuration_service,
|
|
14
|
-
):
|
|
15
|
-
self.market_service = market_service
|
|
16
|
-
self.position_repository = position_repository
|
|
17
|
-
self.portfolio_configuration_service = portfolio_configuration_service
|
|
18
|
-
self.order_service = order_service
|
|
19
|
-
super(PortfolioService, self).__init__(portfolio_repository)
|
|
20
|
-
|
|
21
|
-
def find(self, query_params):
|
|
22
|
-
portfolio = self.repository.find(query_params)
|
|
23
|
-
portfolio_configuration = self.portfolio_configuration_service\
|
|
24
|
-
.get(portfolio.identifier)
|
|
25
|
-
portfolio.configuration = portfolio_configuration
|
|
26
|
-
return portfolio
|
|
27
|
-
|
|
28
|
-
def create(self, data):
|
|
29
|
-
unallocated = data.get("unallocated", 0)
|
|
30
|
-
del data["unallocated"]
|
|
31
|
-
portfolio = super(PortfolioService, self).create(data)
|
|
32
|
-
self.position_repository.create(
|
|
33
|
-
{
|
|
34
|
-
"symbol": portfolio.get_trading_symbol(),
|
|
35
|
-
"amount": unallocated,
|
|
36
|
-
"portfolio_id": portfolio.id
|
|
37
|
-
}
|
|
38
|
-
)
|
|
39
|
-
|
|
40
|
-
def sync_portfolios(self):
|
|
41
|
-
|
|
42
|
-
for portfolio in self.get_all():
|
|
43
|
-
portfolio_configuration = self.portfolio_configuration_service\
|
|
44
|
-
.get(portfolio.identifier)
|
|
45
|
-
|
|
46
|
-
self.market_service.initialize(portfolio_configuration)
|
|
47
|
-
balances = self.market_service.get_balance()
|
|
48
|
-
|
|
49
|
-
for symbol in balances:
|
|
50
|
-
if isinstance(balances[symbol], dict):
|
|
51
|
-
if "free" in balances[symbol]:
|
|
52
|
-
|
|
53
|
-
if self.position_repository.exists(
|
|
54
|
-
{"portfolio_id": portfolio.id, "symbol": symbol}
|
|
55
|
-
):
|
|
56
|
-
position = self.position_repository.find(
|
|
57
|
-
{
|
|
58
|
-
"portfolio_id": portfolio.id,
|
|
59
|
-
"symbol": symbol
|
|
60
|
-
}
|
|
61
|
-
)
|
|
62
|
-
self.position_repository.update(
|
|
63
|
-
position.id,
|
|
64
|
-
{"amount": balances[symbol]["free"]}
|
|
65
|
-
)
|
|
66
|
-
else:
|
|
67
|
-
self.position_repository.create(
|
|
68
|
-
{
|
|
69
|
-
"symbol": symbol,
|
|
70
|
-
"amount": balances[symbol]["free"],
|
|
71
|
-
"portfolio_id": portfolio.id
|
|
72
|
-
}
|
|
73
|
-
)
|
|
74
|
-
|
|
75
|
-
for position in self.position_repository.get_all(
|
|
76
|
-
{"portfolio_id": portfolio.id}
|
|
77
|
-
):
|
|
78
|
-
|
|
79
|
-
if position.symbol == portfolio.trading_symbol:
|
|
80
|
-
continue
|
|
81
|
-
|
|
82
|
-
external_orders = self.market_service\
|
|
83
|
-
.get_orders(
|
|
84
|
-
f"{position.symbol}/{portfolio.trading_symbol}",
|
|
85
|
-
since=portfolio_configuration.track_from
|
|
86
|
-
)
|
|
87
|
-
|
|
88
|
-
for external_order in external_orders:
|
|
89
|
-
|
|
90
|
-
if self.order_service.exists(
|
|
91
|
-
{"external_id": external_order.external_id}
|
|
92
|
-
):
|
|
93
|
-
order = self.order_service.find(
|
|
94
|
-
{"external_id": external_order.external_id}
|
|
95
|
-
)
|
|
96
|
-
self.order_service.update(
|
|
97
|
-
order.id, external_order.to_dict()
|
|
98
|
-
)
|
|
99
|
-
else:
|
|
100
|
-
data = external_order.to_dict()
|
|
101
|
-
data["position_id"] = position.id
|
|
102
|
-
data["portfolio_id"] = portfolio.id
|
|
103
|
-
self.order_service.create(
|
|
104
|
-
data, execute=False, validate=False, sync=False
|
|
105
|
-
)
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
from investing_algorithm_framework.services.repository_service import \
|
|
2
|
-
RepositoryService
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
class PositionService(RepositoryService):
|
|
6
|
-
|
|
7
|
-
def __init__(self, repository, market_service):
|
|
8
|
-
super().__init__(repository)
|
|
9
|
-
self.market_service = market_service
|
|
10
|
-
|
|
11
|
-
def sync_positions(self, portfolio, market_service):
|
|
12
|
-
self.market_service.initialize(portfolio.portfolio_configuration)
|
|
13
|
-
balances = self.market_service.balances()
|
|
14
|
-
|
|
15
|
-
# Get trading symbol position
|
|
16
|
-
trading_symbol_position = balances[portfolio.get_trading_symbol()]
|
|
17
|
-
|
|
18
|
-
if portfolio.portfolio_configuration.has_unallocated_limit \
|
|
19
|
-
and trading_symbol_position["free"] > \
|
|
20
|
-
portfolio.portfolio_configuration.max_unallocated:
|
|
21
|
-
self._sync_position(
|
|
22
|
-
portfolio.portfolio_configuration.max_unallocated,
|
|
23
|
-
portfolio.id,
|
|
24
|
-
portfolio.get_trading_symbol(),
|
|
25
|
-
)
|
|
26
|
-
else:
|
|
27
|
-
self._sync_position(
|
|
28
|
-
trading_symbol_position["free"],
|
|
29
|
-
portfolio.id,
|
|
30
|
-
portfolio.get_trading_symbol(),
|
|
31
|
-
)
|
|
32
|
-
|
|
33
|
-
for balance_entry in balances:
|
|
34
|
-
symbol = balance_entry
|
|
35
|
-
|
|
36
|
-
if symbol == portfolio.get_trading_symbol():
|
|
37
|
-
continue
|
|
38
|
-
|
|
39
|
-
if "free" in balances[symbol]:
|
|
40
|
-
self._sync_position(
|
|
41
|
-
balances[symbol]["free"],
|
|
42
|
-
portfolio.id,
|
|
43
|
-
symbol,
|
|
44
|
-
)
|
|
45
|
-
|
|
46
|
-
def _sync_position(self, synced_amount, portfolio_id, symbol):
|
|
47
|
-
position = self.find({"symbol": symbol, "portfolio": portfolio_id})
|
|
48
|
-
|
|
49
|
-
if position.amount != synced_amount:
|
|
50
|
-
self.update(position.id, {"amount": synced_amount})
|