Skip to content

Commit

Permalink
Add sql trade model
Browse files Browse the repository at this point in the history
  • Loading branch information
MDUYN committed Jan 15, 2025
1 parent f5c97ef commit e7d0710
Show file tree
Hide file tree
Showing 10 changed files with 562 additions and 695 deletions.
31 changes: 15 additions & 16 deletions investing_algorithm_framework/dependency_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from investing_algorithm_framework.app.algorithm import Algorithm
from investing_algorithm_framework.infrastructure import SQLOrderRepository, \
SQLPositionRepository, SQLPortfolioRepository, \
SQLPortfolioSnapshotRepository, \
SQLPortfolioSnapshotRepository, SQLTradeRepository, \
SQLPositionSnapshotRepository, PerformanceService, CCXTMarketService
from investing_algorithm_framework.services import OrderService, \
PositionService, PortfolioService, StrategyOrchestratorService, \
Expand Down Expand Up @@ -40,6 +40,7 @@ class DependencyContainer(containers.DeclarativeContainer):
portfolio_snapshot_repository = providers.Factory(
SQLPortfolioSnapshotRepository
)
trade_repository = providers.Factory(SQLTradeRepository)
market_service = providers.Factory(
CCXTMarketService,
market_credential_service=market_credential_service,
Expand All @@ -65,6 +66,17 @@ class DependencyContainer(containers.DeclarativeContainer):
portfolio_repository=portfolio_repository,
position_repository=position_repository,
)
position_service = providers.Factory(
PositionService,
repository=position_repository,
market_service=market_service,
market_credential_service=market_credential_service,
order_repository=order_repository,
)
trade_service = providers.Factory(
TradeService,
trade_repository=trade_repository,
)
order_service = providers.Factory(
OrderService,
configuration_service=configuration_service,
Expand All @@ -74,14 +86,8 @@ class DependencyContainer(containers.DeclarativeContainer):
market_service=market_service,
market_credential_service=market_credential_service,
portfolio_configuration_service=portfolio_configuration_service,
portfolio_snapshot_service=portfolio_snapshot_service
)
position_service = providers.Factory(
PositionService,
repository=position_repository,
market_service=market_service,
market_credential_service=market_credential_service,
order_repository=order_repository,
portfolio_snapshot_service=portfolio_snapshot_service,
trade_service=trade_service,
)
portfolio_service = providers.Factory(
PortfolioService,
Expand All @@ -94,13 +100,6 @@ class DependencyContainer(containers.DeclarativeContainer):
portfolio_configuration_service=portfolio_configuration_service,
portfolio_snapshot_service=portfolio_snapshot_service,
)
trade_service = providers.Factory(
TradeService,
portfolio_repository=portfolio_repository,
order_service=order_service,
market_data_source_service=market_data_source_service,
position_service=position_service,
)
portfolio_sync_service = providers.Factory(
PortfolioSyncService,
trade_service=trade_service,
Expand Down
8 changes: 5 additions & 3 deletions investing_algorithm_framework/infrastructure/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from .database import setup_sqlalchemy, Session, \
create_all_tables
from .models import SQLPortfolio, SQLOrder, SQLPosition, \
SQLPortfolioSnapshot, SQLPositionSnapshot, \
SQLPortfolioSnapshot, SQLPositionSnapshot, SQLTrade, \
CCXTOHLCVBacktestMarketDataSource, CCXTOrderBookMarketDataSource, \
CCXTTickerMarketDataSource, CCXTOHLCVMarketDataSource, \
CSVOHLCVMarketDataSource, CSVTickerMarketDataSource
from .repositories import SQLOrderRepository, SQLPositionRepository, \
SQLPortfolioRepository, \
SQLPortfolioRepository, SQLTradeRepository, \
SQLPortfolioSnapshotRepository, SQLPositionSnapshotRepository
from .services import PerformanceService, CCXTMarketService, \
AzureBlobStorageStateHandler
Expand All @@ -21,6 +21,7 @@
"setup_sqlalchemy",
"Session",
"SQLPortfolio",
"SQLTrade",
"SQLOrder",
"SQLPosition",
"PerformanceService",
Expand All @@ -35,5 +36,6 @@
"CSVTickerMarketDataSource",
"CCXTOHLCVBacktestMarketDataSource",
"CCXTOrderBookMarketDataSource",
"AzureBlobStorageStateHandler"
"AzureBlobStorageStateHandler",
"SQLTradeRepository"
]
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .order import SQLOrder
from .portfolio import SQLPortfolio, SQLPortfolioSnapshot
from .position import SQLPosition, SQLPositionSnapshot
from .trade import SQLTrade

__all__ = [
"SQLOrder",
Expand All @@ -18,4 +19,5 @@
"CCXTOHLCVMarketDataSource",
"CSVTickerMarketDataSource",
"CSVOHLCVMarketDataSource",
"SQLTrade"
]
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
# from sqlalchemy import Column, Integer, String, DateTime, Float
# from sqlalchemy.orm import relationship
from sqlalchemy import Column, Integer, String, DateTime, Float
from sqlalchemy.orm import relationship

# from investing_algorithm_framework.domain import PortfolioSnapshot
# from investing_algorithm_framework.infrastructure.database import SQLBaseModel
# from investing_algorithm_framework.infrastructure.models.model_extension \
# import SQLAlchemyModelExtension
from investing_algorithm_framework.domain import PortfolioSnapshot
from investing_algorithm_framework.infrastructure.database import SQLBaseModel
from investing_algorithm_framework.infrastructure.models.model_extension \
import SQLAlchemyModelExtension


# class SQLPortfolioSnapshot(
# PortfolioSnapshot, SQLBaseModel, SQLAlchemyModelExtension
# ):
# __tablename__ = "portfolio_snapshots"
# id = Column(Integer, primary_key=True)
# portfolio_id = Column(String, nullable=False)
# trading_symbol = Column(String, nullable=False)
# pending_value = Column(Float, nullable=False, default=0)
# unallocated = Column(Float, nullable=False, default=0)
# total_net_gain = Column(Float, nullable=False, default=0)
# total_revenue = Column(Float, nullable=False, default=0)
# total_cost = Column(Float, nullable=False, default=0)
# cash_flow = Column(Float, nullable=False, default=0)
# created_at = Column(DateTime, nullable=False, default=0)
# position_snapshots = relationship(
# "SQLPositionSnapshot",
# back_populates="portfolio_snapshot",
# lazy="dynamic",
# cascade="all,delete",
# )
class SQLPortfolioSnapshot(
PortfolioSnapshot, SQLBaseModel, SQLAlchemyModelExtension
):
__tablename__ = "portfolio_snapshots"
id = Column(Integer, primary_key=True)
portfolio_id = Column(String, nullable=False)
trading_symbol = Column(String, nullable=False)
pending_value = Column(Float, nullable=False, default=0)
unallocated = Column(Float, nullable=False, default=0)
total_net_gain = Column(Float, nullable=False, default=0)
total_revenue = Column(Float, nullable=False, default=0)
total_cost = Column(Float, nullable=False, default=0)
cash_flow = Column(Float, nullable=False, default=0)
created_at = Column(DateTime, nullable=False, default=0)
position_snapshots = relationship(
"SQLPositionSnapshot",
back_populates="portfolio_snapshot",
lazy="dynamic",
cascade="all,delete",
)
Loading

0 comments on commit e7d0710

Please sign in to comment.