investing-algorithm-framework 7.19.14__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of investing-algorithm-framework might be problematic. Click here for more details.

Files changed (260) hide show
  1. investing_algorithm_framework/__init__.py +197 -0
  2. investing_algorithm_framework/app/__init__.py +47 -0
  3. investing_algorithm_framework/app/algorithm/__init__.py +7 -0
  4. investing_algorithm_framework/app/algorithm/algorithm.py +239 -0
  5. investing_algorithm_framework/app/algorithm/algorithm_factory.py +114 -0
  6. investing_algorithm_framework/app/analysis/__init__.py +15 -0
  7. investing_algorithm_framework/app/analysis/backtest_data_ranges.py +121 -0
  8. investing_algorithm_framework/app/analysis/backtest_utils.py +107 -0
  9. investing_algorithm_framework/app/analysis/permutation.py +116 -0
  10. investing_algorithm_framework/app/analysis/ranking.py +297 -0
  11. investing_algorithm_framework/app/app.py +2204 -0
  12. investing_algorithm_framework/app/app_hook.py +28 -0
  13. investing_algorithm_framework/app/context.py +1667 -0
  14. investing_algorithm_framework/app/eventloop.py +590 -0
  15. investing_algorithm_framework/app/reporting/__init__.py +27 -0
  16. investing_algorithm_framework/app/reporting/ascii.py +921 -0
  17. investing_algorithm_framework/app/reporting/backtest_report.py +349 -0
  18. investing_algorithm_framework/app/reporting/charts/__init__.py +19 -0
  19. investing_algorithm_framework/app/reporting/charts/entry_exist_signals.py +66 -0
  20. investing_algorithm_framework/app/reporting/charts/equity_curve.py +37 -0
  21. investing_algorithm_framework/app/reporting/charts/equity_curve_drawdown.py +74 -0
  22. investing_algorithm_framework/app/reporting/charts/line_chart.py +11 -0
  23. investing_algorithm_framework/app/reporting/charts/monthly_returns_heatmap.py +70 -0
  24. investing_algorithm_framework/app/reporting/charts/ohlcv_data_completeness.py +51 -0
  25. investing_algorithm_framework/app/reporting/charts/rolling_sharp_ratio.py +79 -0
  26. investing_algorithm_framework/app/reporting/charts/yearly_returns_barchart.py +55 -0
  27. investing_algorithm_framework/app/reporting/generate.py +185 -0
  28. investing_algorithm_framework/app/reporting/tables/__init__.py +11 -0
  29. investing_algorithm_framework/app/reporting/tables/key_metrics_table.py +217 -0
  30. investing_algorithm_framework/app/reporting/tables/stop_loss_table.py +0 -0
  31. investing_algorithm_framework/app/reporting/tables/time_metrics_table.py +80 -0
  32. investing_algorithm_framework/app/reporting/tables/trade_metrics_table.py +147 -0
  33. investing_algorithm_framework/app/reporting/tables/trades_table.py +75 -0
  34. investing_algorithm_framework/app/reporting/tables/utils.py +29 -0
  35. investing_algorithm_framework/app/reporting/templates/report_template.html.j2 +154 -0
  36. investing_algorithm_framework/app/stateless/__init__.py +35 -0
  37. investing_algorithm_framework/app/stateless/action_handlers/__init__.py +84 -0
  38. investing_algorithm_framework/app/stateless/action_handlers/action_handler_strategy.py +8 -0
  39. investing_algorithm_framework/app/stateless/action_handlers/check_online_handler.py +15 -0
  40. investing_algorithm_framework/app/stateless/action_handlers/run_strategy_handler.py +40 -0
  41. investing_algorithm_framework/app/stateless/exception_handler.py +40 -0
  42. investing_algorithm_framework/app/strategy.py +675 -0
  43. investing_algorithm_framework/app/task.py +41 -0
  44. investing_algorithm_framework/app/web/__init__.py +5 -0
  45. investing_algorithm_framework/app/web/controllers/__init__.py +13 -0
  46. investing_algorithm_framework/app/web/controllers/orders.py +20 -0
  47. investing_algorithm_framework/app/web/controllers/portfolio.py +20 -0
  48. investing_algorithm_framework/app/web/controllers/positions.py +18 -0
  49. investing_algorithm_framework/app/web/create_app.py +20 -0
  50. investing_algorithm_framework/app/web/error_handler.py +59 -0
  51. investing_algorithm_framework/app/web/responses.py +20 -0
  52. investing_algorithm_framework/app/web/run_strategies.py +4 -0
  53. investing_algorithm_framework/app/web/schemas/__init__.py +12 -0
  54. investing_algorithm_framework/app/web/schemas/order.py +12 -0
  55. investing_algorithm_framework/app/web/schemas/portfolio.py +22 -0
  56. investing_algorithm_framework/app/web/schemas/position.py +15 -0
  57. investing_algorithm_framework/app/web/setup_cors.py +6 -0
  58. investing_algorithm_framework/cli/__init__.py +0 -0
  59. investing_algorithm_framework/cli/cli.py +207 -0
  60. investing_algorithm_framework/cli/deploy_to_aws_lambda.py +499 -0
  61. investing_algorithm_framework/cli/deploy_to_azure_function.py +718 -0
  62. investing_algorithm_framework/cli/initialize_app.py +603 -0
  63. investing_algorithm_framework/cli/templates/.gitignore.template +178 -0
  64. investing_algorithm_framework/cli/templates/app.py.template +18 -0
  65. investing_algorithm_framework/cli/templates/app_aws_lambda_function.py.template +48 -0
  66. investing_algorithm_framework/cli/templates/app_azure_function.py.template +14 -0
  67. investing_algorithm_framework/cli/templates/app_web.py.template +18 -0
  68. investing_algorithm_framework/cli/templates/aws_lambda_dockerfile.template +22 -0
  69. investing_algorithm_framework/cli/templates/aws_lambda_dockerignore.template +92 -0
  70. investing_algorithm_framework/cli/templates/aws_lambda_readme.md.template +110 -0
  71. investing_algorithm_framework/cli/templates/aws_lambda_requirements.txt.template +2 -0
  72. investing_algorithm_framework/cli/templates/azure_function_function_app.py.template +65 -0
  73. investing_algorithm_framework/cli/templates/azure_function_host.json.template +15 -0
  74. investing_algorithm_framework/cli/templates/azure_function_local.settings.json.template +8 -0
  75. investing_algorithm_framework/cli/templates/azure_function_requirements.txt.template +3 -0
  76. investing_algorithm_framework/cli/templates/data_providers.py.template +17 -0
  77. investing_algorithm_framework/cli/templates/env.example.template +2 -0
  78. investing_algorithm_framework/cli/templates/env_azure_function.example.template +4 -0
  79. investing_algorithm_framework/cli/templates/market_data_providers.py.template +9 -0
  80. investing_algorithm_framework/cli/templates/readme.md.template +135 -0
  81. investing_algorithm_framework/cli/templates/requirements.txt.template +2 -0
  82. investing_algorithm_framework/cli/templates/run_backtest.py.template +20 -0
  83. investing_algorithm_framework/cli/templates/strategy.py.template +124 -0
  84. investing_algorithm_framework/create_app.py +54 -0
  85. investing_algorithm_framework/dependency_container.py +155 -0
  86. investing_algorithm_framework/domain/__init__.py +148 -0
  87. investing_algorithm_framework/domain/backtesting/__init__.py +21 -0
  88. investing_algorithm_framework/domain/backtesting/backtest.py +503 -0
  89. investing_algorithm_framework/domain/backtesting/backtest_date_range.py +96 -0
  90. investing_algorithm_framework/domain/backtesting/backtest_evaluation_focuss.py +242 -0
  91. investing_algorithm_framework/domain/backtesting/backtest_metrics.py +459 -0
  92. investing_algorithm_framework/domain/backtesting/backtest_permutation_test.py +275 -0
  93. investing_algorithm_framework/domain/backtesting/backtest_run.py +435 -0
  94. investing_algorithm_framework/domain/backtesting/backtest_summary_metrics.py +162 -0
  95. investing_algorithm_framework/domain/backtesting/combine_backtests.py +280 -0
  96. investing_algorithm_framework/domain/config.py +111 -0
  97. investing_algorithm_framework/domain/constants.py +83 -0
  98. investing_algorithm_framework/domain/data_provider.py +334 -0
  99. investing_algorithm_framework/domain/data_structures.py +42 -0
  100. investing_algorithm_framework/domain/decimal_parsing.py +40 -0
  101. investing_algorithm_framework/domain/exceptions.py +112 -0
  102. investing_algorithm_framework/domain/models/__init__.py +43 -0
  103. investing_algorithm_framework/domain/models/app_mode.py +34 -0
  104. investing_algorithm_framework/domain/models/base_model.py +25 -0
  105. investing_algorithm_framework/domain/models/data/__init__.py +7 -0
  106. investing_algorithm_framework/domain/models/data/data_source.py +214 -0
  107. investing_algorithm_framework/domain/models/data/data_type.py +46 -0
  108. investing_algorithm_framework/domain/models/event.py +35 -0
  109. investing_algorithm_framework/domain/models/market/__init__.py +5 -0
  110. investing_algorithm_framework/domain/models/market/market_credential.py +88 -0
  111. investing_algorithm_framework/domain/models/order/__init__.py +6 -0
  112. investing_algorithm_framework/domain/models/order/order.py +384 -0
  113. investing_algorithm_framework/domain/models/order/order_side.py +36 -0
  114. investing_algorithm_framework/domain/models/order/order_status.py +37 -0
  115. investing_algorithm_framework/domain/models/order/order_type.py +30 -0
  116. investing_algorithm_framework/domain/models/portfolio/__init__.py +9 -0
  117. investing_algorithm_framework/domain/models/portfolio/portfolio.py +169 -0
  118. investing_algorithm_framework/domain/models/portfolio/portfolio_configuration.py +93 -0
  119. investing_algorithm_framework/domain/models/portfolio/portfolio_snapshot.py +208 -0
  120. investing_algorithm_framework/domain/models/position/__init__.py +4 -0
  121. investing_algorithm_framework/domain/models/position/position.py +68 -0
  122. investing_algorithm_framework/domain/models/position/position_snapshot.py +47 -0
  123. investing_algorithm_framework/domain/models/snapshot_interval.py +45 -0
  124. investing_algorithm_framework/domain/models/strategy_profile.py +33 -0
  125. investing_algorithm_framework/domain/models/time_frame.py +153 -0
  126. investing_algorithm_framework/domain/models/time_interval.py +124 -0
  127. investing_algorithm_framework/domain/models/time_unit.py +149 -0
  128. investing_algorithm_framework/domain/models/tracing/__init__.py +0 -0
  129. investing_algorithm_framework/domain/models/tracing/trace.py +23 -0
  130. investing_algorithm_framework/domain/models/trade/__init__.py +13 -0
  131. investing_algorithm_framework/domain/models/trade/trade.py +388 -0
  132. investing_algorithm_framework/domain/models/trade/trade_risk_type.py +34 -0
  133. investing_algorithm_framework/domain/models/trade/trade_status.py +40 -0
  134. investing_algorithm_framework/domain/models/trade/trade_stop_loss.py +267 -0
  135. investing_algorithm_framework/domain/models/trade/trade_take_profit.py +303 -0
  136. investing_algorithm_framework/domain/order_executor.py +112 -0
  137. investing_algorithm_framework/domain/portfolio_provider.py +118 -0
  138. investing_algorithm_framework/domain/positions/__init__.py +4 -0
  139. investing_algorithm_framework/domain/positions/position_size.py +41 -0
  140. investing_algorithm_framework/domain/services/__init__.py +11 -0
  141. investing_algorithm_framework/domain/services/market_credential_service.py +37 -0
  142. investing_algorithm_framework/domain/services/portfolios/__init__.py +5 -0
  143. investing_algorithm_framework/domain/services/portfolios/portfolio_sync_service.py +9 -0
  144. investing_algorithm_framework/domain/services/rounding_service.py +27 -0
  145. investing_algorithm_framework/domain/services/state_handler.py +38 -0
  146. investing_algorithm_framework/domain/stateless_actions.py +7 -0
  147. investing_algorithm_framework/domain/strategy.py +44 -0
  148. investing_algorithm_framework/domain/utils/__init__.py +27 -0
  149. investing_algorithm_framework/domain/utils/csv.py +104 -0
  150. investing_algorithm_framework/domain/utils/custom_tqdm.py +22 -0
  151. investing_algorithm_framework/domain/utils/dates.py +57 -0
  152. investing_algorithm_framework/domain/utils/jupyter_notebook_detection.py +19 -0
  153. investing_algorithm_framework/domain/utils/polars.py +53 -0
  154. investing_algorithm_framework/domain/utils/random.py +41 -0
  155. investing_algorithm_framework/domain/utils/signatures.py +17 -0
  156. investing_algorithm_framework/domain/utils/stoppable_thread.py +26 -0
  157. investing_algorithm_framework/domain/utils/synchronized.py +12 -0
  158. investing_algorithm_framework/download_data.py +108 -0
  159. investing_algorithm_framework/infrastructure/__init__.py +50 -0
  160. investing_algorithm_framework/infrastructure/data_providers/__init__.py +36 -0
  161. investing_algorithm_framework/infrastructure/data_providers/ccxt.py +1143 -0
  162. investing_algorithm_framework/infrastructure/data_providers/csv.py +568 -0
  163. investing_algorithm_framework/infrastructure/data_providers/pandas.py +599 -0
  164. investing_algorithm_framework/infrastructure/database/__init__.py +10 -0
  165. investing_algorithm_framework/infrastructure/database/sql_alchemy.py +120 -0
  166. investing_algorithm_framework/infrastructure/models/__init__.py +16 -0
  167. investing_algorithm_framework/infrastructure/models/decimal_parser.py +14 -0
  168. investing_algorithm_framework/infrastructure/models/model_extension.py +6 -0
  169. investing_algorithm_framework/infrastructure/models/order/__init__.py +4 -0
  170. investing_algorithm_framework/infrastructure/models/order/order.py +124 -0
  171. investing_algorithm_framework/infrastructure/models/order/order_metadata.py +44 -0
  172. investing_algorithm_framework/infrastructure/models/order_trade_association.py +10 -0
  173. investing_algorithm_framework/infrastructure/models/portfolio/__init__.py +4 -0
  174. investing_algorithm_framework/infrastructure/models/portfolio/portfolio_snapshot.py +37 -0
  175. investing_algorithm_framework/infrastructure/models/portfolio/sql_portfolio.py +114 -0
  176. investing_algorithm_framework/infrastructure/models/position/__init__.py +4 -0
  177. investing_algorithm_framework/infrastructure/models/position/position.py +63 -0
  178. investing_algorithm_framework/infrastructure/models/position/position_snapshot.py +23 -0
  179. investing_algorithm_framework/infrastructure/models/trades/__init__.py +9 -0
  180. investing_algorithm_framework/infrastructure/models/trades/trade.py +130 -0
  181. investing_algorithm_framework/infrastructure/models/trades/trade_stop_loss.py +40 -0
  182. investing_algorithm_framework/infrastructure/models/trades/trade_take_profit.py +41 -0
  183. investing_algorithm_framework/infrastructure/order_executors/__init__.py +21 -0
  184. investing_algorithm_framework/infrastructure/order_executors/backtest_oder_executor.py +28 -0
  185. investing_algorithm_framework/infrastructure/order_executors/ccxt_order_executor.py +200 -0
  186. investing_algorithm_framework/infrastructure/portfolio_providers/__init__.py +19 -0
  187. investing_algorithm_framework/infrastructure/portfolio_providers/ccxt_portfolio_provider.py +199 -0
  188. investing_algorithm_framework/infrastructure/repositories/__init__.py +21 -0
  189. investing_algorithm_framework/infrastructure/repositories/order_metadata_repository.py +17 -0
  190. investing_algorithm_framework/infrastructure/repositories/order_repository.py +96 -0
  191. investing_algorithm_framework/infrastructure/repositories/portfolio_repository.py +30 -0
  192. investing_algorithm_framework/infrastructure/repositories/portfolio_snapshot_repository.py +56 -0
  193. investing_algorithm_framework/infrastructure/repositories/position_repository.py +66 -0
  194. investing_algorithm_framework/infrastructure/repositories/position_snapshot_repository.py +21 -0
  195. investing_algorithm_framework/infrastructure/repositories/repository.py +299 -0
  196. investing_algorithm_framework/infrastructure/repositories/trade_repository.py +71 -0
  197. investing_algorithm_framework/infrastructure/repositories/trade_stop_loss_repository.py +23 -0
  198. investing_algorithm_framework/infrastructure/repositories/trade_take_profit_repository.py +23 -0
  199. investing_algorithm_framework/infrastructure/services/__init__.py +7 -0
  200. investing_algorithm_framework/infrastructure/services/aws/__init__.py +6 -0
  201. investing_algorithm_framework/infrastructure/services/aws/state_handler.py +113 -0
  202. investing_algorithm_framework/infrastructure/services/azure/__init__.py +5 -0
  203. investing_algorithm_framework/infrastructure/services/azure/state_handler.py +158 -0
  204. investing_algorithm_framework/services/__init__.py +132 -0
  205. investing_algorithm_framework/services/backtesting/__init__.py +5 -0
  206. investing_algorithm_framework/services/backtesting/backtest_service.py +651 -0
  207. investing_algorithm_framework/services/configuration_service.py +96 -0
  208. investing_algorithm_framework/services/data_providers/__init__.py +5 -0
  209. investing_algorithm_framework/services/data_providers/data_provider_service.py +850 -0
  210. investing_algorithm_framework/services/market_credential_service.py +40 -0
  211. investing_algorithm_framework/services/metrics/__init__.py +114 -0
  212. investing_algorithm_framework/services/metrics/alpha.py +0 -0
  213. investing_algorithm_framework/services/metrics/beta.py +0 -0
  214. investing_algorithm_framework/services/metrics/cagr.py +60 -0
  215. investing_algorithm_framework/services/metrics/calmar_ratio.py +40 -0
  216. investing_algorithm_framework/services/metrics/drawdown.py +181 -0
  217. investing_algorithm_framework/services/metrics/equity_curve.py +24 -0
  218. investing_algorithm_framework/services/metrics/exposure.py +210 -0
  219. investing_algorithm_framework/services/metrics/generate.py +358 -0
  220. investing_algorithm_framework/services/metrics/mean_daily_return.py +83 -0
  221. investing_algorithm_framework/services/metrics/price_efficiency.py +57 -0
  222. investing_algorithm_framework/services/metrics/profit_factor.py +165 -0
  223. investing_algorithm_framework/services/metrics/recovery.py +113 -0
  224. investing_algorithm_framework/services/metrics/returns.py +452 -0
  225. investing_algorithm_framework/services/metrics/risk_free_rate.py +28 -0
  226. investing_algorithm_framework/services/metrics/sharpe_ratio.py +137 -0
  227. investing_algorithm_framework/services/metrics/sortino_ratio.py +74 -0
  228. investing_algorithm_framework/services/metrics/standard_deviation.py +157 -0
  229. investing_algorithm_framework/services/metrics/trades.py +500 -0
  230. investing_algorithm_framework/services/metrics/treynor_ratio.py +0 -0
  231. investing_algorithm_framework/services/metrics/ulcer.py +0 -0
  232. investing_algorithm_framework/services/metrics/value_at_risk.py +0 -0
  233. investing_algorithm_framework/services/metrics/volatility.py +97 -0
  234. investing_algorithm_framework/services/metrics/win_rate.py +177 -0
  235. investing_algorithm_framework/services/order_service/__init__.py +9 -0
  236. investing_algorithm_framework/services/order_service/order_backtest_service.py +178 -0
  237. investing_algorithm_framework/services/order_service/order_executor_lookup.py +110 -0
  238. investing_algorithm_framework/services/order_service/order_service.py +826 -0
  239. investing_algorithm_framework/services/portfolios/__init__.py +16 -0
  240. investing_algorithm_framework/services/portfolios/backtest_portfolio_service.py +54 -0
  241. investing_algorithm_framework/services/portfolios/portfolio_configuration_service.py +75 -0
  242. investing_algorithm_framework/services/portfolios/portfolio_provider_lookup.py +106 -0
  243. investing_algorithm_framework/services/portfolios/portfolio_service.py +188 -0
  244. investing_algorithm_framework/services/portfolios/portfolio_snapshot_service.py +136 -0
  245. investing_algorithm_framework/services/portfolios/portfolio_sync_service.py +182 -0
  246. investing_algorithm_framework/services/positions/__init__.py +7 -0
  247. investing_algorithm_framework/services/positions/position_service.py +210 -0
  248. investing_algorithm_framework/services/positions/position_snapshot_service.py +18 -0
  249. investing_algorithm_framework/services/repository_service.py +40 -0
  250. investing_algorithm_framework/services/trade_order_evaluator/__init__.py +9 -0
  251. investing_algorithm_framework/services/trade_order_evaluator/backtest_trade_oder_evaluator.py +132 -0
  252. investing_algorithm_framework/services/trade_order_evaluator/default_trade_order_evaluator.py +66 -0
  253. investing_algorithm_framework/services/trade_order_evaluator/trade_order_evaluator.py +41 -0
  254. investing_algorithm_framework/services/trade_service/__init__.py +3 -0
  255. investing_algorithm_framework/services/trade_service/trade_service.py +1083 -0
  256. investing_algorithm_framework-7.19.14.dist-info/LICENSE +201 -0
  257. investing_algorithm_framework-7.19.14.dist-info/METADATA +459 -0
  258. investing_algorithm_framework-7.19.14.dist-info/RECORD +260 -0
  259. investing_algorithm_framework-7.19.14.dist-info/WHEEL +4 -0
  260. investing_algorithm_framework-7.19.14.dist-info/entry_points.txt +3 -0
@@ -0,0 +1,120 @@
1
+ import logging
2
+
3
+ from sqlalchemy import create_engine, StaticPool
4
+ from sqlalchemy import inspect
5
+ from sqlalchemy.orm import DeclarativeBase, sessionmaker
6
+
7
+ from investing_algorithm_framework.domain import SQLALCHEMY_DATABASE_URI, \
8
+ OperationalException
9
+
10
+ Session = sessionmaker()
11
+ logger = logging.getLogger("investing_algorithm_framework")
12
+
13
+
14
+ class SQLAlchemyAdapter:
15
+
16
+ def __init__(self, app):
17
+ self._app = app
18
+ if SQLALCHEMY_DATABASE_URI not in app.config \
19
+ or app.config[SQLALCHEMY_DATABASE_URI] is None:
20
+ raise OperationalException("SQLALCHEMY_DATABASE_URI not set")
21
+
22
+ global Session
23
+ engine = create_engine(
24
+ app.config[SQLALCHEMY_DATABASE_URI],
25
+ connect_args={'check_same_thread': False},
26
+ poolclass=StaticPool
27
+ )
28
+ Session.configure(bind=engine)
29
+
30
+
31
+ def setup_sqlalchemy(app, throw_exception_if_not_set=True):
32
+
33
+ try:
34
+ SQLAlchemyAdapter(app)
35
+ except OperationalException as e:
36
+ if throw_exception_if_not_set:
37
+ raise e
38
+
39
+ return app
40
+
41
+
42
+
43
+ class SQLBaseModel(DeclarativeBase):
44
+ pass
45
+
46
+
47
+ def create_all_tables():
48
+ SQLBaseModel.metadata.create_all(bind=Session().bind)
49
+
50
+
51
+ from sqlalchemy import event
52
+ from sqlalchemy.orm import mapper
53
+ from datetime import timezone
54
+
55
+ def clear_db(db_uri):
56
+ """
57
+ Clear the database by dropping all tables.
58
+ This is useful for testing purposes.
59
+
60
+ Args:
61
+ db_uri (str): The database URI to connect to.
62
+
63
+ Returns:
64
+ None
65
+ """
66
+ # Drop all tables before deleting file
67
+ try:
68
+ engine = create_engine(db_uri)
69
+ inspector = inspect(engine)
70
+ if inspector.get_table_names():
71
+ logger.info("Dropping all tables in backtest database")
72
+ SQLBaseModel.metadata.drop_all(bind=engine)
73
+ except Exception as e:
74
+ logger.error(f"Error dropping tables: {e}")
75
+
76
+ # # Clear mappers (if using classical mappings)
77
+ # try:
78
+ # clear_mappers()
79
+ # except Exception:
80
+ # pass # ignore if not needed
81
+
82
+
83
+ @event.listens_for(mapper, "load")
84
+ def attach_utc_timezone_on_load(target, context):
85
+ """
86
+ For each model instance loaded from the database,
87
+ this function will check if the `created_at` and `updated_at`
88
+ attributes are timezone-naive and, if so, will set them to UTC.
89
+
90
+ Its documented in the contributing guide (https://coding-kitties.github
91
+ .io/investing-algorithm-framework/Contributing%20Guide/contributing)
92
+ that each datetime attribute should be utc timezone-aware.
93
+
94
+ Args:
95
+ target: The model instance being loaded from the database.
96
+ context: The context in which the event is being handled.
97
+
98
+ Returns:
99
+ None
100
+ """
101
+ # This will apply to every model instance loaded from the DB
102
+ if hasattr(target, "created_at"):
103
+ dt = getattr(target, "created_at")
104
+ if dt and dt.tzinfo is None:
105
+ target.created_at = dt.replace(tzinfo=timezone.utc)
106
+
107
+ if hasattr(target, "updated_at"):
108
+ dt = getattr(target, "updated_at")
109
+ if dt and dt.tzinfo is None:
110
+ target.updated_at = dt.replace(tzinfo=timezone.utc)
111
+
112
+ if hasattr(target, "closed_at"):
113
+ dt = getattr(target, "closed_at")
114
+ if dt and dt.tzinfo is None:
115
+ target.closed_at = dt.replace(tzinfo=timezone.utc)
116
+
117
+ if hasattr(target, "opened_at"):
118
+ dt = getattr(target, "opened_at")
119
+ if dt and dt.tzinfo is None:
120
+ target.opened_at = dt.replace(tzinfo=timezone.utc)
@@ -0,0 +1,16 @@
1
+ from .order import SQLOrder, SQLOrderMetadata
2
+ from .portfolio import SQLPortfolio, SQLPortfolioSnapshot
3
+ from .position import SQLPosition, SQLPositionSnapshot
4
+ from .trades import SQLTrade, SQLTradeStopLoss, SQLTradeTakeProfit
5
+
6
+ __all__ = [
7
+ "SQLOrder",
8
+ "SQLPosition",
9
+ "SQLPortfolio",
10
+ "SQLPositionSnapshot",
11
+ "SQLPortfolioSnapshot",
12
+ "SQLTrade",
13
+ "SQLTradeStopLoss",
14
+ "SQLTradeTakeProfit",
15
+ "SQLOrderMetadata",
16
+ ]
@@ -0,0 +1,14 @@
1
+ from decimal import Decimal, getcontext
2
+
3
+
4
+ def count_number_of_decimals(value) -> int:
5
+ value = str(value)
6
+ if "." in value:
7
+ return len(value) - value.index(".") - 1
8
+ else:
9
+ return 0
10
+
11
+
12
+ def parse_decimal(value) -> Decimal:
13
+ getcontext().prec = count_number_of_decimals(value)
14
+ return Decimal(value)
@@ -0,0 +1,6 @@
1
+ class SQLAlchemyModelExtension:
2
+
3
+ def update(self, data):
4
+
5
+ for attr, value in data.items():
6
+ setattr(self, attr, value)
@@ -0,0 +1,4 @@
1
+ from .order import SQLOrder
2
+ from .order_metadata import SQLOrderMetadata
3
+
4
+ __all__ = ["SQLOrder", "SQLOrderMetadata"]
@@ -0,0 +1,124 @@
1
+ import logging
2
+ from datetime import datetime, timezone
3
+
4
+ from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Float
5
+ from sqlalchemy.orm import relationship
6
+
7
+ from investing_algorithm_framework.domain import OrderType, \
8
+ OrderSide, Order, OrderStatus
9
+ from investing_algorithm_framework.infrastructure.database import SQLBaseModel
10
+ from investing_algorithm_framework.infrastructure.models.model_extension \
11
+ import SQLAlchemyModelExtension
12
+ from investing_algorithm_framework.infrastructure.models.\
13
+ order_trade_association import order_trade_association
14
+
15
+ logger = logging.getLogger("investing_algorithm_framework")
16
+
17
+
18
+ def utcnow():
19
+ return datetime.now(tz=timezone.utc)
20
+
21
+
22
+ class SQLOrder(Order, SQLBaseModel, SQLAlchemyModelExtension):
23
+ """
24
+ SQLOrder model based on the Order domain model.
25
+ """
26
+ __tablename__ = "orders"
27
+ id = Column(Integer, primary_key=True, unique=True)
28
+ external_id = Column(Integer)
29
+ target_symbol = Column(String)
30
+ trading_symbol = Column(String)
31
+ order_side = Column(String, nullable=False, default=OrderSide.BUY.value)
32
+ order_type = Column(String, nullable=False, default=OrderType.LIMIT.value)
33
+ trades = relationship(
34
+ 'SQLTrade', secondary=order_trade_association, back_populates='orders'
35
+ )
36
+ price = Column(Float)
37
+ amount = Column(Float)
38
+ remaining = Column(Float, default=None)
39
+ filled = Column(Float, default=None)
40
+ cost = Column(Float, default=0)
41
+ status = Column(String, default=OrderStatus.CREATED.value)
42
+ position_id = Column(Integer, ForeignKey('positions.id'))
43
+ position = relationship("SQLPosition", back_populates="orders")
44
+ created_at = Column(DateTime(timezone=True), default=utcnow)
45
+ updated_at = Column(
46
+ DateTime(timezone=True), default=utcnow, onupdate=utcnow
47
+ )
48
+ order_fee = Column(Float, default=None)
49
+ order_fee_currency = Column(String, default=None)
50
+ order_fee_rate = Column(Float, default=None)
51
+ sell_order_metadata_id = Column(Integer, ForeignKey('orders.id'))
52
+ order_metadata = relationship(
53
+ 'SQLOrderMetadata', back_populates='order'
54
+ )
55
+
56
+ def update(self, data):
57
+
58
+ if "status" in data and data["status"] is not None:
59
+ data["status"] = OrderStatus.from_value(data["status"]).value
60
+
61
+ super().update(data)
62
+
63
+ @staticmethod
64
+ def from_order(order):
65
+ return SQLOrder(
66
+ external_id=order.external_id,
67
+ amount=order.get_amount(),
68
+ filled=order.get_filled(),
69
+ remaining=order.get_remaining(),
70
+ price=order.price,
71
+ order_type=order.get_order_type(),
72
+ order_side=order.get(),
73
+ status=order.get_status(),
74
+ target_symbol=order.get_target_symbol(),
75
+ trading_symbol=order.get_trading_symbol(),
76
+ created_at=order.get_created_at(),
77
+ updated_at=order.get_updated_at(),
78
+ )
79
+
80
+ @staticmethod
81
+ def from_ccxt_order(ccxt_order):
82
+ """
83
+ Create an Order object from a CCXT order object
84
+
85
+ Args:
86
+ ccxt_order: CCXT order object
87
+
88
+ Returns:
89
+ Order: Order object
90
+ """
91
+ status = OrderStatus.from_value(ccxt_order["status"])
92
+ target_symbol = ccxt_order.get("symbol").split("/")[0]
93
+ trading_symbol = ccxt_order.get("symbol").split("/")[1]
94
+ ccxt_fee = ccxt_order.get("fee", None)
95
+ order_fee = None
96
+ order_fee_rate = None
97
+ order_fee_currency = None
98
+
99
+ if ccxt_fee is not None:
100
+ order_fee = ccxt_fee.get("cost", None)
101
+ order_fee_rate = ccxt_fee.get("rate", None)
102
+ order_fee_currency = ccxt_fee.get("currency", None)
103
+
104
+ return Order(
105
+ external_id=ccxt_order.get("id", None),
106
+ target_symbol=target_symbol,
107
+ trading_symbol=trading_symbol,
108
+ price=ccxt_order.get("price", None),
109
+ amount=ccxt_order.get("amount", None),
110
+ status=status,
111
+ order_type=ccxt_order.get("type", None),
112
+ order_side=ccxt_order.get("side", None),
113
+ filled=ccxt_order.get("filled", None),
114
+ remaining=ccxt_order.get("remaining", None),
115
+ cost=ccxt_order.get("cost", None),
116
+ order_fee=order_fee,
117
+ order_fee_rate=order_fee_rate,
118
+ order_fee_currency=order_fee_currency,
119
+ created_at=ccxt_order.get("datetime", None),
120
+ )
121
+
122
+ def __lt__(self, other):
123
+ # Define the less-than comparison based on created_at attribute
124
+ return self.created_at < other.created_at
@@ -0,0 +1,44 @@
1
+ import logging
2
+
3
+ from sqlalchemy import Column, Integer, ForeignKey, Float
4
+ from sqlalchemy.orm import relationship
5
+
6
+ from investing_algorithm_framework.infrastructure.database import SQLBaseModel
7
+ from investing_algorithm_framework.infrastructure.models.model_extension \
8
+ import SQLAlchemyModelExtension
9
+
10
+ logger = logging.getLogger("investing_algorithm_framework")
11
+
12
+
13
+ class SQLOrderMetadata(SQLBaseModel, SQLAlchemyModelExtension):
14
+ __tablename__ = "sql_order_metadata"
15
+ id = Column(Integer, primary_key=True, unique=True)
16
+ order_id = Column(Integer, ForeignKey('orders.id'))
17
+ order = relationship('SQLOrder', back_populates='order_metadata')
18
+ trade_id = Column(Integer)
19
+ stop_loss_id = Column(Integer)
20
+ take_profit_id = Column(Integer)
21
+ amount = Column(Float)
22
+ amount_pending = Column(Float)
23
+
24
+ def __init__(
25
+ self,
26
+ order_id,
27
+ amount,
28
+ amount_pending,
29
+ trade_id=None,
30
+ stop_loss_id=None,
31
+ take_profit_id=None,
32
+ ):
33
+ self.order_id = order_id
34
+ self.trade_id = trade_id
35
+ self.stop_loss_id = stop_loss_id
36
+ self.take_profit_id = take_profit_id
37
+ self.amount = amount
38
+ self.amount_pending = amount_pending
39
+
40
+ def __repr__(self):
41
+ return f"<SQLOrderMetadata(id={self.id}, order_id={self.order_id}, " \
42
+ f"trade_id={self.trade_id}, stop_loss_id={self.stop_loss_id}, "\
43
+ f"take_profit_id={self.take_profit_id}, amount={self.amount}, "\
44
+ f"amount_pending={self.amount_pending})>"
@@ -0,0 +1,10 @@
1
+ from sqlalchemy import Table, Column, Integer, ForeignKey
2
+ from investing_algorithm_framework.infrastructure.database import SQLBaseModel
3
+
4
+ # Association table
5
+ order_trade_association = Table(
6
+ 'order_trade', # Table name
7
+ SQLBaseModel.metadata,
8
+ Column('order_id', Integer, ForeignKey('orders.id'), primary_key=True),
9
+ Column('trade_id', Integer, ForeignKey('trades.id'), primary_key=True)
10
+ )
@@ -0,0 +1,4 @@
1
+ from .sql_portfolio import SQLPortfolio
2
+ from .portfolio_snapshot import SQLPortfolioSnapshot
3
+
4
+ __all__ = ['SQLPortfolio', "SQLPortfolioSnapshot"]
@@ -0,0 +1,37 @@
1
+ from sqlalchemy import Column, Integer, String, DateTime, Float
2
+ from sqlalchemy.orm import relationship
3
+
4
+ from investing_algorithm_framework.domain import PortfolioSnapshot
5
+ from investing_algorithm_framework.infrastructure.database import SQLBaseModel
6
+ from investing_algorithm_framework.infrastructure.models.model_extension \
7
+ import SQLAlchemyModelExtension
8
+
9
+
10
+ class SQLPortfolioSnapshot(
11
+ PortfolioSnapshot, SQLBaseModel, SQLAlchemyModelExtension
12
+ ):
13
+ """
14
+ SQLAlchemy model for portfolio snapshots.
15
+
16
+ Portfolio snapshots represent the state of a portfolio at a specific
17
+ point in time.
18
+ """
19
+ __tablename__ = "portfolio_snapshots"
20
+ id = Column(Integer, primary_key=True)
21
+ portfolio_id = Column(String, nullable=False)
22
+ trading_symbol = Column(String, nullable=False)
23
+ pending_value = Column(Float, nullable=False, default=0)
24
+ unallocated = Column(Float, nullable=False, default=0)
25
+ net_size = Column(Float, nullable=False, default=0)
26
+ total_net_gain = Column(Float, nullable=False, default=0)
27
+ total_revenue = Column(Float, nullable=False, default=0)
28
+ total_cost = Column(Float, nullable=False, default=0)
29
+ total_value = Column(Float, nullable=False, default=0)
30
+ cash_flow = Column(Float, nullable=False, default=0)
31
+ created_at = Column(DateTime, nullable=False, default=0)
32
+ position_snapshots = relationship(
33
+ "SQLPositionSnapshot",
34
+ back_populates="portfolio_snapshot",
35
+ lazy="dynamic",
36
+ cascade="all,delete",
37
+ )
@@ -0,0 +1,114 @@
1
+ from datetime import datetime, timezone
2
+
3
+ from sqlalchemy import Column, Integer, String, DateTime, Float, Boolean
4
+ from sqlalchemy import UniqueConstraint
5
+ from sqlalchemy.orm import relationship
6
+ from sqlalchemy.orm import validates
7
+
8
+ from investing_algorithm_framework.domain import Portfolio
9
+ from investing_algorithm_framework.infrastructure.database import SQLBaseModel
10
+ from investing_algorithm_framework.infrastructure.models.model_extension \
11
+ import SQLAlchemyModelExtension
12
+
13
+
14
+ class SQLPortfolio(Portfolio, SQLBaseModel, SQLAlchemyModelExtension):
15
+ __tablename__ = "portfolios"
16
+ id = Column(Integer, primary_key=True)
17
+ identifier = Column(String, nullable=False, unique=True)
18
+ trading_symbol = Column(String, nullable=False)
19
+ realized = Column(Float, nullable=False, default=0)
20
+ total_revenue = Column(Float, nullable=False, default=0)
21
+ total_cost = Column(Float, nullable=False, default=0)
22
+ total_net_gain = Column(Float, nullable=False, default=0)
23
+ total_trade_volume = Column(Float, nullable=False, default=0)
24
+ net_size = Column(Float, nullable=False, default=0)
25
+ unallocated = Column(Float, nullable=False, default=0)
26
+ initial_balance = Column(Float, nullable=True)
27
+ market = Column(String, nullable=False)
28
+ positions = relationship(
29
+ "SQLPosition",
30
+ back_populates="portfolio",
31
+ lazy="dynamic",
32
+ cascade="all,delete",
33
+ )
34
+ created_at = Column(DateTime, nullable=False)
35
+ updated_at = Column(DateTime, nullable=False)
36
+ initialized = Column(Boolean, nullable=False, default=False)
37
+
38
+ __table_args__ = (
39
+ UniqueConstraint(
40
+ 'trading_symbol',
41
+ 'identifier',
42
+ name='_trading_currency_identifier_uc'
43
+ ),
44
+ )
45
+
46
+ @validates('trading_symbol')
47
+ def _write_once(self, key, value):
48
+ existing = getattr(self, key)
49
+
50
+ if existing is not None:
51
+ raise ValueError("{} is write-once".format(key))
52
+ return value
53
+
54
+ def __init__(
55
+ self,
56
+ trading_symbol,
57
+ market,
58
+ unallocated,
59
+ initialized,
60
+ initial_balance=None,
61
+ identifier=None,
62
+ created_at=None,
63
+ updated_at=None,
64
+ ):
65
+
66
+ if identifier is None:
67
+ identifier = market
68
+
69
+ if created_at is None:
70
+ created_at = datetime.now(tz=timezone.utc)
71
+
72
+ if updated_at is None:
73
+ updated_at = datetime.now(tz=timezone.utc)
74
+
75
+ super().__init__(
76
+ trading_symbol=trading_symbol,
77
+ market=market,
78
+ identifier=identifier,
79
+ net_size=unallocated,
80
+ unallocated=unallocated,
81
+ realized=0,
82
+ total_revenue=0,
83
+ total_cost=0,
84
+ created_at=created_at,
85
+ updated_at=updated_at,
86
+ initialized=initialized,
87
+ initial_balance=initial_balance,
88
+ )
89
+
90
+ def update(self, data):
91
+
92
+ if "net_size" in data:
93
+ self.net_size = data.pop("net_size")
94
+
95
+ if "unallocated" in data:
96
+ self.unallocated = data.pop("unallocated")
97
+
98
+ if "realized" in data:
99
+ self.realized = data.pop("realized")
100
+
101
+ if "total_revenue" in data:
102
+ self.total_revenue = data.pop("total_revenue")
103
+
104
+ if "total_trade_volume" in data:
105
+ self.total_trade_volume = data.pop("total_trade_volume")
106
+
107
+ if "total_cost" in data:
108
+ self.total_cost = data.pop("total_cost")
109
+
110
+ if "total_net_gain" in data:
111
+ self.total_net_gain = data.pop("total_net_gain")
112
+
113
+ super().update(data)
114
+ return self
@@ -0,0 +1,4 @@
1
+ from .position import SQLPosition
2
+ from .position_snapshot import SQLPositionSnapshot
3
+
4
+ __all__ = ["SQLPosition", "SQLPositionSnapshot"]
@@ -0,0 +1,63 @@
1
+ from sqlalchemy import Column, Integer, String, ForeignKey, Float
2
+ from sqlalchemy import UniqueConstraint
3
+ from sqlalchemy.orm import relationship, validates
4
+
5
+ from investing_algorithm_framework.domain import Position
6
+ from investing_algorithm_framework.infrastructure.database import SQLBaseModel
7
+ from investing_algorithm_framework.infrastructure.models.model_extension \
8
+ import SQLAlchemyModelExtension
9
+
10
+
11
+ class SQLPosition(SQLBaseModel, Position, SQLAlchemyModelExtension):
12
+ __tablename__ = "positions"
13
+ id = Column(Integer, primary_key=True, unique=True)
14
+ symbol = Column(String)
15
+ amount = Column(Float)
16
+ cost = Column(Float)
17
+ orders = relationship(
18
+ "SQLOrder",
19
+ back_populates="position",
20
+ lazy="dynamic",
21
+ cascade="all, delete-orphan"
22
+ )
23
+ portfolio_id = Column(Integer, ForeignKey('portfolios.id'))
24
+ portfolio = relationship("SQLPortfolio", back_populates="positions")
25
+ __table_args__ = (
26
+ UniqueConstraint(
27
+ 'symbol', 'portfolio_id', name='_symbol_portfolio_uc'
28
+ ),
29
+ )
30
+
31
+ def __init__(
32
+ self,
33
+ symbol,
34
+ amount=0,
35
+ cost=0,
36
+ portfolio_id=None,
37
+ ):
38
+ super(SQLPosition, self).__init__()
39
+ self.symbol = symbol
40
+ self.amount = amount
41
+ self.portfolio_id = portfolio_id
42
+ self.cost = cost
43
+
44
+ @validates('id', 'symbol')
45
+ def _write_once(self, key, value):
46
+ existing = getattr(self, key)
47
+ if existing is not None:
48
+ raise ValueError("{} is write-once".format(key))
49
+ return value
50
+
51
+ def update(self, data):
52
+
53
+ if 'amount' in data:
54
+ self.amount = data.pop('amount')
55
+
56
+ if 'cost' in data:
57
+ self.cost = data.pop('cost')
58
+
59
+ super(SQLPosition, self).update(data)
60
+
61
+ @property
62
+ def ccxt_symbol(self):
63
+ return f"{self.symbol}/{self.portfolio.trading_symbol}"
@@ -0,0 +1,23 @@
1
+ from sqlalchemy import Column, Integer, String, ForeignKey, Float
2
+ from sqlalchemy.orm import relationship
3
+
4
+ from investing_algorithm_framework.domain import PositionSnapshot
5
+ from investing_algorithm_framework.infrastructure.database import SQLBaseModel
6
+ from investing_algorithm_framework.infrastructure.models.model_extension \
7
+ import SQLAlchemyModelExtension
8
+
9
+
10
+ class SQLPositionSnapshot(
11
+ SQLBaseModel, PositionSnapshot, SQLAlchemyModelExtension
12
+ ):
13
+ __tablename__ = "position_snapshots"
14
+ id = Column(Integer, primary_key=True, unique=True)
15
+ symbol = Column(String)
16
+ amount = Column(Float)
17
+ cost = Column(Float)
18
+ portfolio_snapshot_id = Column(
19
+ Integer, ForeignKey('portfolio_snapshots.id')
20
+ )
21
+ portfolio_snapshot = relationship(
22
+ "SQLPortfolioSnapshot", back_populates="position_snapshots"
23
+ )
@@ -0,0 +1,9 @@
1
+ from .trade import SQLTrade
2
+ from .trade_stop_loss import SQLTradeStopLoss
3
+ from .trade_take_profit import SQLTradeTakeProfit
4
+
5
+ __all__ = [
6
+ "SQLTrade",
7
+ "SQLTradeStopLoss",
8
+ "SQLTradeTakeProfit",
9
+ ]