investing-algorithm-framework 1.5__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 +192 -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 +29 -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 +2220 -379
- 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/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/strategy.py +867 -60
- 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 +3 -2
- investing_algorithm_framework/app/web/controllers/positions.py +2 -2
- investing_algorithm_framework/app/web/create_app.py +4 -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 +40 -7
- investing_algorithm_framework/dependency_container.py +100 -47
- investing_algorithm_framework/domain/__init__.py +97 -30
- 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 +59 -136
- investing_algorithm_framework/domain/constants.py +18 -37
- investing_algorithm_framework/domain/data_provider.py +334 -0
- investing_algorithm_framework/domain/data_structures.py +42 -0
- investing_algorithm_framework/domain/exceptions.py +51 -1
- investing_algorithm_framework/domain/models/__init__.py +26 -19
- investing_algorithm_framework/domain/models/app_mode.py +34 -0
- 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 +198 -65
- 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 +6 -2
- investing_algorithm_framework/domain/models/portfolio/portfolio.py +98 -3
- investing_algorithm_framework/domain/models/portfolio/portfolio_configuration.py +37 -43
- investing_algorithm_framework/domain/models/portfolio/portfolio_snapshot.py +108 -11
- investing_algorithm_framework/domain/models/position/__init__.py +2 -1
- investing_algorithm_framework/domain/models/position/position.py +20 -0
- investing_algorithm_framework/domain/models/position/position_size.py +41 -0
- investing_algorithm_framework/domain/models/position/position_snapshot.py +0 -2
- 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 +19 -141
- 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 +66 -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 +15 -5
- 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 +37 -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 +86 -12
- investing_algorithm_framework/infrastructure/models/__init__.py +7 -3
- investing_algorithm_framework/infrastructure/models/order/__init__.py +2 -2
- investing_algorithm_framework/infrastructure/models/order/order.py +53 -53
- 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 +1 -1
- investing_algorithm_framework/infrastructure/models/portfolio/portfolio_snapshot.py +8 -2
- investing_algorithm_framework/infrastructure/models/portfolio/{portfolio.py → sql_portfolio.py} +17 -6
- investing_algorithm_framework/infrastructure/models/position/position_snapshot.py +3 -1
- 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 +10 -4
- investing_algorithm_framework/infrastructure/repositories/order_metadata_repository.py +17 -0
- investing_algorithm_framework/infrastructure/repositories/order_repository.py +16 -5
- investing_algorithm_framework/infrastructure/repositories/portfolio_repository.py +2 -2
- investing_algorithm_framework/infrastructure/repositories/position_repository.py +11 -0
- investing_algorithm_framework/infrastructure/repositories/repository.py +84 -30
- 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 -4
- 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 +123 -15
- investing_algorithm_framework/services/configuration_service.py +77 -11
- 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/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.5.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 -630
- investing_algorithm_framework/domain/models/backtest_profile.py +0 -414
- 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 -105
- 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/trade.py +0 -78
- investing_algorithm_framework/domain/models/trading_data_types.py +0 -47
- investing_algorithm_framework/domain/models/trading_time_frame.py +0 -223
- investing_algorithm_framework/domain/singleton.py +0 -9
- investing_algorithm_framework/domain/utils/backtesting.py +0 -82
- investing_algorithm_framework/infrastructure/models/order/order_fee.py +0 -21
- investing_algorithm_framework/infrastructure/repositories/order_fee_repository.py +0 -15
- investing_algorithm_framework/infrastructure/services/market_backtest_service.py +0 -360
- investing_algorithm_framework/infrastructure/services/market_service.py +0 -410
- investing_algorithm_framework/infrastructure/services/performance_service.py +0 -192
- investing_algorithm_framework/services/backtest_service.py +0 -268
- investing_algorithm_framework/services/market_data_service.py +0 -77
- investing_algorithm_framework/services/order_backtest_service.py +0 -122
- investing_algorithm_framework/services/order_service.py +0 -752
- investing_algorithm_framework/services/portfolio_service.py +0 -164
- investing_algorithm_framework/services/portfolio_snapshot_service.py +0 -68
- investing_algorithm_framework/services/position_cost_service.py +0 -5
- investing_algorithm_framework/services/position_service.py +0 -63
- investing_algorithm_framework/services/strategy_orchestrator_service.py +0 -225
- investing_algorithm_framework-1.5.dist-info/AUTHORS.md +0 -8
- investing_algorithm_framework-1.5.dist-info/METADATA +0 -230
- investing_algorithm_framework-1.5.dist-info/RECORD +0 -119
- investing_algorithm_framework-1.5.dist-info/top_level.txt +0 -1
- /investing_algorithm_framework/{infrastructure/services/performance_backtest_service.py → app/reporting/tables/stop_loss_table.py} +0 -0
- /investing_algorithm_framework/services/{position_snapshot_service.py → positions/position_snapshot_service.py} +0 -0
- {investing_algorithm_framework-1.5.dist-info → investing_algorithm_framework-7.25.6.dist-info}/LICENSE +0 -0
|
@@ -1,752 +0,0 @@
|
|
|
1
|
-
import logging
|
|
2
|
-
from datetime import datetime
|
|
3
|
-
from queue import PriorityQueue
|
|
4
|
-
|
|
5
|
-
from investing_algorithm_framework.domain import OrderType, OrderSide, \
|
|
6
|
-
OperationalException, OrderStatus
|
|
7
|
-
from investing_algorithm_framework.services.repository_service \
|
|
8
|
-
import RepositoryService
|
|
9
|
-
|
|
10
|
-
logger = logging.getLogger("investing_algorithm_framework")
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
class OrderService(RepositoryService):
|
|
14
|
-
|
|
15
|
-
def __init__(
|
|
16
|
-
self,
|
|
17
|
-
order_repository,
|
|
18
|
-
order_fee_repository,
|
|
19
|
-
market_service,
|
|
20
|
-
position_repository,
|
|
21
|
-
portfolio_repository,
|
|
22
|
-
portfolio_configuration_service,
|
|
23
|
-
portfolio_snapshot_service,
|
|
24
|
-
):
|
|
25
|
-
super(OrderService, self).__init__(order_repository)
|
|
26
|
-
self.order_repository = order_repository
|
|
27
|
-
self.order_fee_repository = order_fee_repository
|
|
28
|
-
self.market_service = market_service
|
|
29
|
-
self.position_repository = position_repository
|
|
30
|
-
self.portfolio_repository = portfolio_repository
|
|
31
|
-
self.portfolio_configuration_service = portfolio_configuration_service
|
|
32
|
-
self.portfolio_snapshot_service = portfolio_snapshot_service
|
|
33
|
-
|
|
34
|
-
def create(self, data, execute=True, validate=True, sync=True):
|
|
35
|
-
portfolio_id = data["portfolio_id"]
|
|
36
|
-
portfolio = self.portfolio_repository.get(portfolio_id)
|
|
37
|
-
|
|
38
|
-
if validate:
|
|
39
|
-
self.validate_order(data, portfolio)
|
|
40
|
-
|
|
41
|
-
order_fee = None
|
|
42
|
-
|
|
43
|
-
if "fee" in data:
|
|
44
|
-
order_fee = data.pop("fee")
|
|
45
|
-
|
|
46
|
-
del data["portfolio_id"]
|
|
47
|
-
symbol = data["target_symbol"]
|
|
48
|
-
|
|
49
|
-
if validate:
|
|
50
|
-
self.validate_order(data, portfolio)
|
|
51
|
-
|
|
52
|
-
position = self._create_position_if_not_exists(symbol, portfolio)
|
|
53
|
-
data["position_id"] = position.id
|
|
54
|
-
data["remaining"] = data["amount"]
|
|
55
|
-
data["status"] = OrderStatus.CREATED.value
|
|
56
|
-
order = self.order_repository.create(data)
|
|
57
|
-
order_id = order.id
|
|
58
|
-
|
|
59
|
-
if order_fee:
|
|
60
|
-
order_fee["order_id"] = order_id
|
|
61
|
-
self.order_fee_repository.create(order_fee)
|
|
62
|
-
|
|
63
|
-
if sync:
|
|
64
|
-
if OrderSide.BUY.equals(order.get_order_side()):
|
|
65
|
-
self._sync_portfolio_with_created_buy_order(order)
|
|
66
|
-
else:
|
|
67
|
-
self._sync_portfolio_with_created_sell_order(order)
|
|
68
|
-
|
|
69
|
-
self.create_snapshot(portfolio.id, created_at=order.created_at)
|
|
70
|
-
|
|
71
|
-
if execute:
|
|
72
|
-
portfolio.configuration = self.portfolio_configuration_service\
|
|
73
|
-
.get(portfolio.identifier)
|
|
74
|
-
self.execute_order(order_id, portfolio)
|
|
75
|
-
|
|
76
|
-
return order
|
|
77
|
-
|
|
78
|
-
def update(self, object_id, data):
|
|
79
|
-
previous_order = self.order_repository.get(object_id)
|
|
80
|
-
trading_symbol_position = self.position_repository.find(
|
|
81
|
-
{
|
|
82
|
-
"id": previous_order.position_id,
|
|
83
|
-
"symbol": previous_order.trading_symbol
|
|
84
|
-
}
|
|
85
|
-
)
|
|
86
|
-
portfolio = self.portfolio_repository.get(
|
|
87
|
-
trading_symbol_position.portfolio_id
|
|
88
|
-
)
|
|
89
|
-
|
|
90
|
-
if "fee" in data:
|
|
91
|
-
order_fee_data = data.pop("fee")
|
|
92
|
-
|
|
93
|
-
if order_fee_data is not None:
|
|
94
|
-
if self.order_fee_repository.exists({"order_id": object_id}):
|
|
95
|
-
order_fee = self.order_fee_repository\
|
|
96
|
-
.find({"order_id": object_id})
|
|
97
|
-
self.order_fee_repository\
|
|
98
|
-
.update(order_fee.id, order_fee_data)
|
|
99
|
-
else:
|
|
100
|
-
order_fee_data["order_id"] = object_id
|
|
101
|
-
self.order_fee_repository.create(order_fee_data)
|
|
102
|
-
|
|
103
|
-
new_order = self.order_repository.update(object_id, data)
|
|
104
|
-
filled_difference = new_order.get_filled() - previous_order.get_filled()
|
|
105
|
-
|
|
106
|
-
if filled_difference:
|
|
107
|
-
|
|
108
|
-
if OrderSide.BUY.equals(new_order.get_order_side()):
|
|
109
|
-
self._sync_with_buy_order_filled(previous_order, new_order)
|
|
110
|
-
else:
|
|
111
|
-
self._sync_with_sell_order_filled(previous_order, new_order)
|
|
112
|
-
|
|
113
|
-
if "status" in data:
|
|
114
|
-
|
|
115
|
-
if OrderStatus.CANCELED.equals(new_order.get_status()):
|
|
116
|
-
|
|
117
|
-
if OrderSide.BUY.equals(new_order.get_order_side()):
|
|
118
|
-
self._sync_with_buy_order_cancelled(new_order)
|
|
119
|
-
else:
|
|
120
|
-
self._sync_with_sell_order_cancelled(new_order)
|
|
121
|
-
|
|
122
|
-
if OrderStatus.EXPIRED.equals(new_order.get_status()):
|
|
123
|
-
|
|
124
|
-
if OrderSide.BUY.equals(new_order.get_order_side()):
|
|
125
|
-
self._sync_with_buy_order_expired(new_order)
|
|
126
|
-
else:
|
|
127
|
-
self._sync_with_sell_order_expired(new_order)
|
|
128
|
-
|
|
129
|
-
if OrderStatus.REJECTED.equals(new_order.get_status()):
|
|
130
|
-
|
|
131
|
-
if OrderSide.BUY.equals(new_order.get_order_side()):
|
|
132
|
-
self._sync_with_buy_order_rejected(new_order)
|
|
133
|
-
else:
|
|
134
|
-
self._sync_with_sell_order_expired(new_order)
|
|
135
|
-
|
|
136
|
-
if "updated_at" in data:
|
|
137
|
-
created_at = data["updated_at"]
|
|
138
|
-
else:
|
|
139
|
-
created_at = datetime.now()
|
|
140
|
-
|
|
141
|
-
self.create_snapshot(portfolio.id, created_at=created_at)
|
|
142
|
-
return new_order
|
|
143
|
-
|
|
144
|
-
def get_order_fee(self, order_id):
|
|
145
|
-
return self.order_fee_repository.find({"order": order_id})
|
|
146
|
-
|
|
147
|
-
def execute_order(self, order_id, portfolio):
|
|
148
|
-
self.market_service.initialize(portfolio.configuration)
|
|
149
|
-
order = self.get(order_id)
|
|
150
|
-
|
|
151
|
-
try:
|
|
152
|
-
if OrderType.LIMIT.equals(order.get_order_type()):
|
|
153
|
-
|
|
154
|
-
if OrderSide.BUY.equals(order.get_order_side()):
|
|
155
|
-
self.market_service.create_limit_buy_order(
|
|
156
|
-
target_symbol=order.get_target_symbol(),
|
|
157
|
-
trading_symbol=order.get_trading_symbol(),
|
|
158
|
-
amount=order.get_amount(),
|
|
159
|
-
price=order.get_price()
|
|
160
|
-
)
|
|
161
|
-
else:
|
|
162
|
-
self.market_service.create_limit_sell_order(
|
|
163
|
-
target_symbol=order.get_target_symbol(),
|
|
164
|
-
trading_symbol=order.get_trading_symbol(),
|
|
165
|
-
amount=order.get_amount(),
|
|
166
|
-
price=order.get_price()
|
|
167
|
-
)
|
|
168
|
-
else:
|
|
169
|
-
if OrderSide.BUY.equals(order.get_order_side()):
|
|
170
|
-
raise OperationalException("Market buy order not supported")
|
|
171
|
-
else:
|
|
172
|
-
self.market_service.create_market_sell_order(
|
|
173
|
-
target_symbol=order.get_target_symbol(),
|
|
174
|
-
trading_symbol=order.get_trading_symbol(),
|
|
175
|
-
amount=order.get_amount(),
|
|
176
|
-
)
|
|
177
|
-
|
|
178
|
-
order = self.update(
|
|
179
|
-
order_id,
|
|
180
|
-
{
|
|
181
|
-
"status": OrderStatus.OPEN.value,
|
|
182
|
-
"remaining": order.remaining,
|
|
183
|
-
"updated_at": datetime.now()
|
|
184
|
-
}
|
|
185
|
-
)
|
|
186
|
-
return order
|
|
187
|
-
except Exception as e:
|
|
188
|
-
logger.error("Error executing order: {}".format(e))
|
|
189
|
-
return self.update(
|
|
190
|
-
order_id,
|
|
191
|
-
{
|
|
192
|
-
"status": OrderStatus.REJECTED.value,
|
|
193
|
-
"updated_at": datetime.now()
|
|
194
|
-
}
|
|
195
|
-
)
|
|
196
|
-
|
|
197
|
-
def validate_order(self, order_data, portfolio):
|
|
198
|
-
|
|
199
|
-
if OrderSide.BUY.equals(order_data["order_side"]):
|
|
200
|
-
self.validate_buy_order(order_data, portfolio)
|
|
201
|
-
else:
|
|
202
|
-
self.validate_sell_order(order_data, portfolio)
|
|
203
|
-
|
|
204
|
-
if OrderType.LIMIT.equals(order_data["order_type"]):
|
|
205
|
-
self.validate_limit_order(order_data, portfolio)
|
|
206
|
-
else:
|
|
207
|
-
self.validate_market_order(order_data, portfolio)
|
|
208
|
-
|
|
209
|
-
def validate_sell_order(self, order_data, portfolio):
|
|
210
|
-
if not self.position_repository.exists(
|
|
211
|
-
{
|
|
212
|
-
"symbol": order_data["target_symbol"],
|
|
213
|
-
"portfolio": portfolio.id
|
|
214
|
-
}
|
|
215
|
-
):
|
|
216
|
-
raise OperationalException(
|
|
217
|
-
"Can't add sell order to non existing position"
|
|
218
|
-
)
|
|
219
|
-
|
|
220
|
-
position = self.position_repository\
|
|
221
|
-
.find(
|
|
222
|
-
{
|
|
223
|
-
"symbol": order_data["target_symbol"],
|
|
224
|
-
"portfolio": portfolio.id
|
|
225
|
-
}
|
|
226
|
-
)
|
|
227
|
-
|
|
228
|
-
if position.get_amount() < order_data["amount"]:
|
|
229
|
-
raise OperationalException(
|
|
230
|
-
"Order amount is larger then amount of open position"
|
|
231
|
-
)
|
|
232
|
-
|
|
233
|
-
if not order_data["trading_symbol"] == portfolio.trading_symbol:
|
|
234
|
-
raise OperationalException(
|
|
235
|
-
f"Can't add sell order with target "
|
|
236
|
-
f"symbol {order_data['target_symbol']} to "
|
|
237
|
-
f"portfolio with trading symbol {portfolio.trading_symbol}"
|
|
238
|
-
)
|
|
239
|
-
|
|
240
|
-
@staticmethod
|
|
241
|
-
def validate_buy_order(order_data, portfolio):
|
|
242
|
-
|
|
243
|
-
if not order_data["trading_symbol"] == portfolio.trading_symbol:
|
|
244
|
-
raise OperationalException(
|
|
245
|
-
f"Can't add buy order with trading "
|
|
246
|
-
f"symbol {order_data['trading_symbol']} to "
|
|
247
|
-
f"portfolio with trading symbol {portfolio.trading_symbol}"
|
|
248
|
-
)
|
|
249
|
-
|
|
250
|
-
def validate_limit_order(self, order_data, portfolio):
|
|
251
|
-
|
|
252
|
-
if OrderSide.SELL.equals(order_data["order_side"]):
|
|
253
|
-
amount = order_data["amount"]
|
|
254
|
-
position = self.position_repository\
|
|
255
|
-
.find(
|
|
256
|
-
{
|
|
257
|
-
"portfolio": portfolio.id,
|
|
258
|
-
"symbol": order_data["target_symbol"]
|
|
259
|
-
}
|
|
260
|
-
)
|
|
261
|
-
if amount > position.get_amount():
|
|
262
|
-
raise OperationalException(
|
|
263
|
-
f"Order amount: {amount} {position.symbol}, is "
|
|
264
|
-
f"larger then position size: {position.get_amount()} "
|
|
265
|
-
f"{position.symbol} of the portfolio"
|
|
266
|
-
)
|
|
267
|
-
else:
|
|
268
|
-
total_price = order_data["amount"] * order_data["price"]
|
|
269
|
-
unallocated_position = self.position_repository\
|
|
270
|
-
.find(
|
|
271
|
-
{
|
|
272
|
-
"portfolio": portfolio.id,
|
|
273
|
-
"symbol": portfolio.trading_symbol
|
|
274
|
-
}
|
|
275
|
-
)
|
|
276
|
-
amount = unallocated_position.get_amount()
|
|
277
|
-
|
|
278
|
-
if amount < total_price:
|
|
279
|
-
raise OperationalException(
|
|
280
|
-
f"Order total: {total_price} {portfolio.trading_symbol}, is "
|
|
281
|
-
f"larger then unallocated size: {portfolio.unallocated} "
|
|
282
|
-
f"{portfolio.trading_symbol} of the portfolio"
|
|
283
|
-
)
|
|
284
|
-
|
|
285
|
-
def validate_market_order(self, order_data, portfolio):
|
|
286
|
-
|
|
287
|
-
if OrderSide.BUY.equals(order_data["order_side"]):
|
|
288
|
-
|
|
289
|
-
if "amount" not in order_data:
|
|
290
|
-
raise OperationalException(
|
|
291
|
-
f"Market order needs an amount specified in the trading "
|
|
292
|
-
f"symbol {order_data['trading_symbol']}"
|
|
293
|
-
)
|
|
294
|
-
|
|
295
|
-
if order_data['amount'] > portfolio.unallocated:
|
|
296
|
-
raise OperationalException(
|
|
297
|
-
f"Market order amount "
|
|
298
|
-
f"{order_data['amount']}"
|
|
299
|
-
f"{portfolio.trading_symbol.upper()} is larger then "
|
|
300
|
-
f"unallocated {portfolio.unallocated} "
|
|
301
|
-
f"{portfolio.trading_symbol.upper()}"
|
|
302
|
-
)
|
|
303
|
-
else:
|
|
304
|
-
position = self.position_repository\
|
|
305
|
-
.find(
|
|
306
|
-
{
|
|
307
|
-
"symbol": order_data["target_symbol"],
|
|
308
|
-
"portfolio": portfolio.id
|
|
309
|
-
}
|
|
310
|
-
)
|
|
311
|
-
|
|
312
|
-
if position is None:
|
|
313
|
-
raise OperationalException(
|
|
314
|
-
"Can't add market sell order to non existing position"
|
|
315
|
-
)
|
|
316
|
-
|
|
317
|
-
if order_data['amount'] > position.get_amount():
|
|
318
|
-
raise OperationalException(
|
|
319
|
-
"Sell order amount larger then position size"
|
|
320
|
-
)
|
|
321
|
-
|
|
322
|
-
def check_pending_orders(self):
|
|
323
|
-
pending_orders = self.get_all({"status": OrderStatus.OPEN.value})
|
|
324
|
-
logger.info(f"Checking {len(pending_orders)} open orders")
|
|
325
|
-
|
|
326
|
-
for order in pending_orders:
|
|
327
|
-
position = self.position_repository.get(order.position_id)
|
|
328
|
-
portfolio = self.portfolio_repository.get(position.portfolio_id)
|
|
329
|
-
portfolio_configuration = self.portfolio_configuration_service\
|
|
330
|
-
.get(portfolio.identifier)
|
|
331
|
-
self.market_service.initialize(portfolio_configuration)
|
|
332
|
-
external_order = self.market_service.get_order(order)
|
|
333
|
-
self.update(order.id, external_order.to_dict())
|
|
334
|
-
|
|
335
|
-
def _create_position_if_not_exists(self, symbol, portfolio):
|
|
336
|
-
if not self.position_repository.exists(
|
|
337
|
-
{"portfolio": portfolio.id, "symbol": symbol}
|
|
338
|
-
):
|
|
339
|
-
self.position_repository \
|
|
340
|
-
.create({"portfolio_id": portfolio.id, "symbol": symbol})
|
|
341
|
-
position = self.position_repository \
|
|
342
|
-
.find({"portfolio": portfolio.id, "symbol": symbol})
|
|
343
|
-
else:
|
|
344
|
-
position = self.position_repository \
|
|
345
|
-
.find({"portfolio": portfolio.id, "symbol": symbol})
|
|
346
|
-
|
|
347
|
-
return position
|
|
348
|
-
|
|
349
|
-
def _sync_portfolio_with_created_buy_order(self, order):
|
|
350
|
-
position = self.position_repository.get(order.position_id)
|
|
351
|
-
portfolio = self.portfolio_repository.get(position.portfolio_id)
|
|
352
|
-
size = order.get_amount() * order.get_price()
|
|
353
|
-
trading_symbol_position = self.position_repository.find(
|
|
354
|
-
{
|
|
355
|
-
"portfolio": portfolio.id,
|
|
356
|
-
"symbol": portfolio.trading_symbol
|
|
357
|
-
}
|
|
358
|
-
)
|
|
359
|
-
self.portfolio_repository.update(
|
|
360
|
-
portfolio.id, {"unallocated": portfolio.get_unallocated() - size}
|
|
361
|
-
)
|
|
362
|
-
self.position_repository.update(
|
|
363
|
-
trading_symbol_position.id,
|
|
364
|
-
{
|
|
365
|
-
"amount": trading_symbol_position.get_amount() - size
|
|
366
|
-
}
|
|
367
|
-
)
|
|
368
|
-
|
|
369
|
-
def _sync_portfolio_with_created_sell_order(self, order):
|
|
370
|
-
position = self.position_repository.get(order.position_id)
|
|
371
|
-
self.position_repository.update(
|
|
372
|
-
position.id,
|
|
373
|
-
{
|
|
374
|
-
"amount": position.get_amount() - order.get_amount()
|
|
375
|
-
}
|
|
376
|
-
)
|
|
377
|
-
|
|
378
|
-
def cancel_order(self, order_id):
|
|
379
|
-
self.check_pending_orders()
|
|
380
|
-
order = self.order_repository.get(order_id)
|
|
381
|
-
|
|
382
|
-
if order is not None:
|
|
383
|
-
|
|
384
|
-
if OrderStatus.OPEN.equals(order.status):
|
|
385
|
-
portfolio = self.portfolio_repository\
|
|
386
|
-
.find({"position": order.position_id})
|
|
387
|
-
portfolio_configuration = self.portfolio_configuration_service\
|
|
388
|
-
.get(portfolio.identifier)
|
|
389
|
-
self.market_service.initialize(portfolio_configuration)
|
|
390
|
-
self.market_service.cancel_order(order_id)
|
|
391
|
-
|
|
392
|
-
def _sync_with_buy_order_filled(self, previous_order, current_order):
|
|
393
|
-
filled_difference = current_order.get_filled() - \
|
|
394
|
-
previous_order.get_filled()
|
|
395
|
-
filled_size = filled_difference * current_order.get_price()
|
|
396
|
-
|
|
397
|
-
if filled_difference <= 0:
|
|
398
|
-
return
|
|
399
|
-
|
|
400
|
-
# Update position
|
|
401
|
-
position = self.position_repository.get(current_order.position_id)
|
|
402
|
-
self.position_repository.update(
|
|
403
|
-
position.id,
|
|
404
|
-
{
|
|
405
|
-
"amount": position.get_amount() + filled_difference,
|
|
406
|
-
"cost": position.get_cost() + filled_size,
|
|
407
|
-
}
|
|
408
|
-
)
|
|
409
|
-
|
|
410
|
-
# Update portfolio
|
|
411
|
-
portfolio = self.portfolio_repository.get(position.portfolio_id)
|
|
412
|
-
self.portfolio_repository.update(
|
|
413
|
-
portfolio.id,
|
|
414
|
-
{
|
|
415
|
-
"total_cost": portfolio.get_total_cost() + filled_size,
|
|
416
|
-
"total_trade_volume": portfolio.get_total_trade_volume()
|
|
417
|
-
+ filled_size,
|
|
418
|
-
}
|
|
419
|
-
)
|
|
420
|
-
|
|
421
|
-
def _sync_with_sell_order_filled(self, previous_order, current_order):
|
|
422
|
-
filled_difference = current_order.get_filled() - \
|
|
423
|
-
previous_order.get_filled()
|
|
424
|
-
filled_size = filled_difference * current_order.get_price()
|
|
425
|
-
|
|
426
|
-
if filled_difference <= 0:
|
|
427
|
-
return
|
|
428
|
-
|
|
429
|
-
# Get position
|
|
430
|
-
position = self.position_repository.get(current_order.position_id)
|
|
431
|
-
|
|
432
|
-
# Update the portfolio
|
|
433
|
-
portfolio = self.portfolio_repository.get(position.portfolio_id)
|
|
434
|
-
self.portfolio_repository.update(
|
|
435
|
-
portfolio.id,
|
|
436
|
-
{
|
|
437
|
-
"unallocated": portfolio.get_unallocated() + filled_size,
|
|
438
|
-
"total_trade_volume": portfolio.get_total_trade_volume()
|
|
439
|
-
+ filled_size,
|
|
440
|
-
}
|
|
441
|
-
)
|
|
442
|
-
|
|
443
|
-
# Update the trading symbol position
|
|
444
|
-
trading_symbol_position = self.position_repository.find(
|
|
445
|
-
{
|
|
446
|
-
"symbol": portfolio.trading_symbol,
|
|
447
|
-
"portfolio": portfolio.id
|
|
448
|
-
}
|
|
449
|
-
)
|
|
450
|
-
self.position_repository.update(
|
|
451
|
-
trading_symbol_position.id,
|
|
452
|
-
{
|
|
453
|
-
"amount": trading_symbol_position.get_amount()
|
|
454
|
-
+ filled_size
|
|
455
|
-
}
|
|
456
|
-
)
|
|
457
|
-
|
|
458
|
-
self._close_trades(filled_difference, current_order)
|
|
459
|
-
|
|
460
|
-
def _sync_with_buy_order_cancelled(self, order):
|
|
461
|
-
remaining = order.get_amount() - order.get_filled()
|
|
462
|
-
size = remaining * order.get_price()
|
|
463
|
-
|
|
464
|
-
# Add the remaining amount to the portfolio
|
|
465
|
-
portfolio = self.portfolio_repository.find(
|
|
466
|
-
{
|
|
467
|
-
"position": order.position_id
|
|
468
|
-
}
|
|
469
|
-
)
|
|
470
|
-
self.portfolio_repository.update(
|
|
471
|
-
portfolio.id,
|
|
472
|
-
{
|
|
473
|
-
"unallocated": portfolio.get_unallocated() + size
|
|
474
|
-
}
|
|
475
|
-
)
|
|
476
|
-
|
|
477
|
-
# Add the remaining amount to the trading symbol position
|
|
478
|
-
trading_symbol_position = self.position_repository.find(
|
|
479
|
-
{
|
|
480
|
-
"symbol": portfolio.trading_symbol,
|
|
481
|
-
"portfolio": portfolio.id
|
|
482
|
-
}
|
|
483
|
-
)
|
|
484
|
-
self.position_repository.update(
|
|
485
|
-
trading_symbol_position.id,
|
|
486
|
-
{
|
|
487
|
-
"amount": trading_symbol_position.get_amount() + remaining
|
|
488
|
-
}
|
|
489
|
-
)
|
|
490
|
-
|
|
491
|
-
def _sync_with_sell_order_cancelled(self, order):
|
|
492
|
-
remaining = order.get_amount() - order.get_filled()
|
|
493
|
-
|
|
494
|
-
# Add the remaining back to the position
|
|
495
|
-
position = self.position_repository.get(order.position_id)
|
|
496
|
-
self.position_repository.update(
|
|
497
|
-
position.id,
|
|
498
|
-
{
|
|
499
|
-
"amount": position.get_amount() + remaining
|
|
500
|
-
}
|
|
501
|
-
)
|
|
502
|
-
|
|
503
|
-
def _sync_with_buy_order_failed(self, order):
|
|
504
|
-
remaining = order.get_amount() - order.get_filled()
|
|
505
|
-
size = remaining * order.get_price()
|
|
506
|
-
|
|
507
|
-
# Add the remaining amount to the portfolio
|
|
508
|
-
portfolio = self.portfolio_repository.find(
|
|
509
|
-
{
|
|
510
|
-
"position": order.position_id
|
|
511
|
-
}
|
|
512
|
-
)
|
|
513
|
-
self.portfolio_repository.update(
|
|
514
|
-
portfolio.id,
|
|
515
|
-
{
|
|
516
|
-
"unallocated": portfolio.get_unallocated() + size
|
|
517
|
-
}
|
|
518
|
-
)
|
|
519
|
-
|
|
520
|
-
# Add the remaining amount to the trading symbol position
|
|
521
|
-
trading_symbol_position = self.position_repository.find(
|
|
522
|
-
{
|
|
523
|
-
"symbol": portfolio.trading_symbol,
|
|
524
|
-
"portfolio": portfolio.id
|
|
525
|
-
}
|
|
526
|
-
)
|
|
527
|
-
self.position_repository.update(
|
|
528
|
-
trading_symbol_position.id,
|
|
529
|
-
{
|
|
530
|
-
"amount": trading_symbol_position.get_amount() + remaining
|
|
531
|
-
}
|
|
532
|
-
)
|
|
533
|
-
|
|
534
|
-
def _sync_with_sell_order_failed(self, order):
|
|
535
|
-
remaining = order.get_amount() - order.get_filled()
|
|
536
|
-
|
|
537
|
-
# Add the remaining back to the position
|
|
538
|
-
position = self.position_repository.get(order.position_id)
|
|
539
|
-
self.position_repository.update(
|
|
540
|
-
position.id,
|
|
541
|
-
{
|
|
542
|
-
"amount": position.get_amount() + remaining
|
|
543
|
-
}
|
|
544
|
-
)
|
|
545
|
-
|
|
546
|
-
def _sync_with_buy_order_expired(self, order):
|
|
547
|
-
remaining = order.get_amount() - order.get_filled()
|
|
548
|
-
size = remaining * order.get_price()
|
|
549
|
-
|
|
550
|
-
# Add the remaining amount to the portfolio
|
|
551
|
-
portfolio = self.portfolio_repository.find(
|
|
552
|
-
{
|
|
553
|
-
"position": order.position_id
|
|
554
|
-
}
|
|
555
|
-
)
|
|
556
|
-
self.portfolio_repository.update(
|
|
557
|
-
portfolio.id,
|
|
558
|
-
{
|
|
559
|
-
"unallocated": portfolio.get_unallocated() + size
|
|
560
|
-
}
|
|
561
|
-
)
|
|
562
|
-
|
|
563
|
-
# Add the remaining amount to the trading symbol position
|
|
564
|
-
trading_symbol_position = self.position_repository.find(
|
|
565
|
-
{
|
|
566
|
-
"symbol": portfolio.trading_symbol,
|
|
567
|
-
"portfolio": portfolio.id
|
|
568
|
-
}
|
|
569
|
-
)
|
|
570
|
-
self.position_repository.update(
|
|
571
|
-
trading_symbol_position.id,
|
|
572
|
-
{
|
|
573
|
-
"amount": trading_symbol_position.get_amount() + remaining
|
|
574
|
-
}
|
|
575
|
-
)
|
|
576
|
-
|
|
577
|
-
def _sync_with_sell_order_expired(self, order):
|
|
578
|
-
remaining = order.get_amount() - order.get_filled()
|
|
579
|
-
|
|
580
|
-
# Add the remaining back to the position
|
|
581
|
-
position = self.position_repository.get(order.position_id)
|
|
582
|
-
self.position_repository.update(
|
|
583
|
-
position.id,
|
|
584
|
-
{
|
|
585
|
-
"amount": position.get_amount() + remaining
|
|
586
|
-
}
|
|
587
|
-
)
|
|
588
|
-
|
|
589
|
-
def _sync_with_buy_order_rejected(self, order):
|
|
590
|
-
remaining = order.get_amount() - order.get_filled()
|
|
591
|
-
size = remaining * order.get_price()
|
|
592
|
-
|
|
593
|
-
# Add the remaining amount to the portfolio
|
|
594
|
-
portfolio = self.portfolio_repository.find(
|
|
595
|
-
{
|
|
596
|
-
"position": order.position_id
|
|
597
|
-
}
|
|
598
|
-
)
|
|
599
|
-
self.portfolio_repository.update(
|
|
600
|
-
portfolio.id,
|
|
601
|
-
{
|
|
602
|
-
"unallocated": portfolio.get_unallocated() + size
|
|
603
|
-
}
|
|
604
|
-
)
|
|
605
|
-
|
|
606
|
-
# Add the remaining amount to the trading symbol position
|
|
607
|
-
trading_symbol_position = self.position_repository.find(
|
|
608
|
-
{
|
|
609
|
-
"symbol": portfolio.trading_symbol,
|
|
610
|
-
"portfolio": portfolio.id
|
|
611
|
-
}
|
|
612
|
-
)
|
|
613
|
-
self.position_repository.update(
|
|
614
|
-
trading_symbol_position.id,
|
|
615
|
-
{
|
|
616
|
-
"amount": trading_symbol_position.get_amount() + remaining
|
|
617
|
-
}
|
|
618
|
-
)
|
|
619
|
-
|
|
620
|
-
def _sync_with_sell_order_rejected(self, order):
|
|
621
|
-
remaining = order.get_amount() - order.get_filled()
|
|
622
|
-
|
|
623
|
-
# Add the remaining back to the position
|
|
624
|
-
position = self.position_repository.get(order.position_id)
|
|
625
|
-
self.position_repository.update(
|
|
626
|
-
position.id,
|
|
627
|
-
{
|
|
628
|
-
"amount": position.get_amount() + remaining
|
|
629
|
-
}
|
|
630
|
-
)
|
|
631
|
-
|
|
632
|
-
def _close_trades(self, amount_to_close, sell_order):
|
|
633
|
-
matching_buy_orders = self.order_repository.get_all(
|
|
634
|
-
{
|
|
635
|
-
"position": sell_order.position_id,
|
|
636
|
-
"order_side": OrderSide.BUY.value,
|
|
637
|
-
"status": OrderStatus.CLOSED.value
|
|
638
|
-
}
|
|
639
|
-
)
|
|
640
|
-
matching_buy_orders = [
|
|
641
|
-
buy_order for buy_order in matching_buy_orders
|
|
642
|
-
if buy_order.get_trade_closed_at() is None
|
|
643
|
-
]
|
|
644
|
-
order_queue = PriorityQueue()
|
|
645
|
-
|
|
646
|
-
for order in matching_buy_orders:
|
|
647
|
-
order_queue.put(order)
|
|
648
|
-
|
|
649
|
-
total_net_gain = 0
|
|
650
|
-
total_cost = 0
|
|
651
|
-
|
|
652
|
-
while amount_to_close > 0 and not order_queue.empty():
|
|
653
|
-
buy_order = order_queue.get()
|
|
654
|
-
|
|
655
|
-
if buy_order.get_trade_closed_amount() != None:
|
|
656
|
-
available_to_close = buy_order.get_filled() \
|
|
657
|
-
- buy_order.get_trade_closed_amount()
|
|
658
|
-
else:
|
|
659
|
-
available_to_close = buy_order.get_filled()
|
|
660
|
-
|
|
661
|
-
if amount_to_close >= available_to_close:
|
|
662
|
-
to_be_closed = available_to_close
|
|
663
|
-
remaining = amount_to_close - to_be_closed
|
|
664
|
-
cost = buy_order.get_price() * to_be_closed
|
|
665
|
-
net_gain = (sell_order.get_price() - buy_order.get_price()) \
|
|
666
|
-
* to_be_closed
|
|
667
|
-
amount_to_close = remaining
|
|
668
|
-
self.order_repository.update(
|
|
669
|
-
buy_order.id,
|
|
670
|
-
{
|
|
671
|
-
"trade_closed_amount": buy_order.get_filled(),
|
|
672
|
-
"trade_closed_at": sell_order.get_updated_at(),
|
|
673
|
-
"trade_closed_price": sell_order.get_price(),
|
|
674
|
-
"net_gain": buy_order.get_net_gain() + net_gain
|
|
675
|
-
}
|
|
676
|
-
)
|
|
677
|
-
else:
|
|
678
|
-
to_be_closed = amount_to_close
|
|
679
|
-
net_gain = (sell_order.get_price() - buy_order.get_price()) \
|
|
680
|
-
* to_be_closed
|
|
681
|
-
cost = buy_order.get_price() * amount_to_close
|
|
682
|
-
self.order_repository.update(
|
|
683
|
-
buy_order.id,
|
|
684
|
-
{
|
|
685
|
-
"trade_closed_amount": buy_order.get_trade_closed_amount() + to_be_closed,
|
|
686
|
-
"trade_closed_price": sell_order.get_price(),
|
|
687
|
-
"net_gain": buy_order.get_net_gain() + net_gain
|
|
688
|
-
}
|
|
689
|
-
)
|
|
690
|
-
amount_to_close = 0
|
|
691
|
-
|
|
692
|
-
total_net_gain += net_gain
|
|
693
|
-
total_cost += cost
|
|
694
|
-
|
|
695
|
-
position = self.position_repository.get(sell_order.position_id)
|
|
696
|
-
|
|
697
|
-
# Update portfolio
|
|
698
|
-
portfolio = self.portfolio_repository.get(position.portfolio_id)
|
|
699
|
-
self.portfolio_repository.update(
|
|
700
|
-
portfolio.id,
|
|
701
|
-
{
|
|
702
|
-
"total_net_gain": portfolio.get_total_net_gain() + total_net_gain,
|
|
703
|
-
"total_cost": portfolio.get_total_cost() - total_cost,
|
|
704
|
-
"net_size": portfolio.get_net_size() + total_net_gain
|
|
705
|
-
}
|
|
706
|
-
)
|
|
707
|
-
# Update the position
|
|
708
|
-
position = self.position_repository.get(sell_order.position_id)
|
|
709
|
-
self.position_repository.update(
|
|
710
|
-
position.id,
|
|
711
|
-
{
|
|
712
|
-
"cost": position.get_cost() - total_cost
|
|
713
|
-
}
|
|
714
|
-
)
|
|
715
|
-
|
|
716
|
-
# Update the sell order
|
|
717
|
-
self.order_repository.update(
|
|
718
|
-
sell_order.id,
|
|
719
|
-
{
|
|
720
|
-
"trade_closed_amount": sell_order.get_filled(),
|
|
721
|
-
"trade_closed_at": sell_order.get_updated_at(),
|
|
722
|
-
"trade_closed_price": sell_order.get_price(),
|
|
723
|
-
"net_gain": sell_order.get_net_gain() + total_net_gain
|
|
724
|
-
}
|
|
725
|
-
)
|
|
726
|
-
|
|
727
|
-
def create_snapshot(self, portfolio_id, created_at=None):
|
|
728
|
-
|
|
729
|
-
if created_at is None:
|
|
730
|
-
created_at = datetime.utcnow()
|
|
731
|
-
|
|
732
|
-
portfolio = self.portfolio_repository.get(portfolio_id)
|
|
733
|
-
pending_orders = self.get_all(
|
|
734
|
-
{
|
|
735
|
-
"order_side": OrderSide.BUY.value,
|
|
736
|
-
"status": OrderStatus.OPEN.value,
|
|
737
|
-
"portfolio_id": portfolio.id
|
|
738
|
-
}
|
|
739
|
-
)
|
|
740
|
-
created_orders = self.get_all(
|
|
741
|
-
{
|
|
742
|
-
"order_side": OrderSide.BUY.value,
|
|
743
|
-
"status": OrderStatus.CREATED.value,
|
|
744
|
-
"portfolio_id": portfolio.id
|
|
745
|
-
}
|
|
746
|
-
)
|
|
747
|
-
return self.portfolio_snapshot_service.create_snapshot(
|
|
748
|
-
portfolio,
|
|
749
|
-
pending_orders=pending_orders,
|
|
750
|
-
created_orders=created_orders,
|
|
751
|
-
created_at=created_at
|
|
752
|
-
)
|