44
55from investing_algorithm_framework .services import ConfigurationService , \
66 MarketCredentialService , OrderService , PortfolioConfigurationService , \
7- PortfolioService , PositionService , TradeService , DataProviderService
7+ PortfolioService , PositionService , TradeService , DataProviderService , \
8+ TradeStopLossService , TradeTakeProfitService
89from investing_algorithm_framework .domain import OrderStatus , OrderType , \
910 OrderSide , OperationalException , Portfolio , RoundingService , \
10- BACKTESTING_FLAG , INDEX_DATETIME , TradeRiskType , Order , \
11- Position , Trade , TradeStatus , MarketCredential
11+ BACKTESTING_FLAG , INDEX_DATETIME , Order , \
12+ Position , Trade , TradeStatus , MarketCredential , TradeStopLoss , \
13+ TradeTakeProfit
1214
1315logger = logging .getLogger ("investing_algorithm_framework" )
1416
@@ -29,6 +31,8 @@ def __init__(
2931 order_service : OrderService ,
3032 market_credential_service : MarketCredentialService ,
3133 trade_service : TradeService ,
34+ trade_stop_loss_service : TradeStopLossService ,
35+ trade_take_profit_service : TradeTakeProfitService ,
3236 data_provider_service : DataProviderService
3337 ):
3438 self .configuration_service : ConfigurationService = \
@@ -42,6 +46,10 @@ def __init__(
4246 market_credential_service
4347 self .data_provider_service : DataProviderService = data_provider_service
4448 self .trade_service : TradeService = trade_service
49+ self .trade_stop_loss_service : TradeStopLossService = \
50+ trade_stop_loss_service
51+ self .trade_take_profit_service : TradeTakeProfitService = \
52+ trade_take_profit_service
4553
4654 @property
4755 def config (self ):
@@ -1372,9 +1380,10 @@ def add_stop_loss(
13721380 self ,
13731381 trade : Trade ,
13741382 percentage : float ,
1375- trade_risk_type = TradeRiskType . FIXED ,
1383+ trailing : bool = False ,
13761384 sell_percentage : float = 100 ,
1377- ):
1385+ created_at : datetime = None ,
1386+ ) -> TradeStopLoss :
13781387 """
13791388 Function to add a stop loss to a trade.
13801389
@@ -1397,30 +1406,34 @@ def add_stop_loss(
13971406 of the open price that the stop loss should
13981407 be set at. This must be a positive
13991408 number, e.g. 5 for 5%, or 10 for 10%.
1400- trade_risk_type (TradeRiskType ): The type of the stop
1401- loss, fixed or trailing
1409+ trailing (bool ): Whether the stop loss should be trailing
1410+ or fixed.
14021411 sell_percentage (float): float representing the
14031412 percentage of the trade that should be sold if the
14041413 stop loss is triggered
1414+ created_at: datetime: The date and time when the stop loss
1415+ was created. If not specified, the current date and time
1416+ will be used.
14051417
14061418 Returns:
14071419 None
14081420 """
1409- self .trade_service .add_stop_loss (
1421+ return self .trade_service .add_stop_loss (
14101422 trade ,
14111423 percentage = percentage ,
1412- trade_risk_type = trade_risk_type ,
1424+ trailing = trailing ,
14131425 sell_percentage = sell_percentage ,
1426+ created_at = created_at ,
14141427 )
1415- return self .trade_service .get (trade .id )
14161428
14171429 def add_take_profit (
14181430 self ,
14191431 trade : Trade ,
14201432 percentage : float ,
1421- trade_risk_type = TradeRiskType . FIXED ,
1433+ trailing : bool = False ,
14221434 sell_percentage : float = 100 ,
1423- ) -> None :
1435+ created_at : datetime = None ,
1436+ ) -> TradeTakeProfit :
14241437 """
14251438 Function to add a take profit to a trade. This function will add a
14261439 take profit to the specified trade. If the take profit is triggered,
@@ -1445,22 +1458,25 @@ def add_take_profit(
14451458 of the open price that the stop loss should
14461459 be set at. This must be a positive
14471460 number, e.g. 5 for 5%, or 10 for 10%.
1448- trade_risk_type (TradeRiskType ): The type of the stop
1449- loss, fixed or trailing
1461+ trailing (bool ): Whether the take profit should be trailing
1462+ or fixed.
14501463 sell_percentage (float): float representing the
14511464 percentage of the trade that should be sold if the
14521465 stop loss is triggered
1466+ created_at: datetime: The date and time when the take profit
1467+ was created. If not specified, the current date and time
1468+ will be used.
14531469
14541470 Returns:
14551471 None
14561472 """
1457- self .trade_service .add_take_profit (
1473+ return self .trade_service .add_take_profit (
14581474 trade ,
14591475 percentage = percentage ,
1460- trade_risk_type = trade_risk_type ,
1476+ trailing = trailing ,
14611477 sell_percentage = sell_percentage ,
1478+ created_at = created_at ,
14621479 )
1463- return self .trade_service .get (trade .id )
14641480
14651481 def close_trade (self , trade , precision = None ) -> None :
14661482 """
@@ -1665,3 +1681,45 @@ def get_trading_symbol(self, portfolio_id=None):
16651681 portfolio = self .portfolio_service .get (portfolio_id )
16661682
16671683 return portfolio .trading_symbol
1684+
1685+ def get_take_profits (
1686+ self , triggered : bool = None
1687+ ) -> List [TradeTakeProfit ]:
1688+ """
1689+ Function to get all take profits. If the triggered parameter
1690+ is specified, the function will return all take profits that
1691+ match the triggered status.
1692+
1693+ Args:
1694+ triggered (bool): The triggered status of the take profits
1695+
1696+ Returns:
1697+ List[TradeTakeProfit]: A list of take profits
1698+ """
1699+ query_params = {}
1700+
1701+ if triggered is not None :
1702+ query_params ["triggered" ] = triggered
1703+
1704+ return self .trade_take_profit_service .get_all (query_params )
1705+
1706+ def get_stop_losses (
1707+ self , triggered : bool = None
1708+ ) -> List [TradeStopLoss ]:
1709+ """
1710+ Function to get all stop losses. If the triggered parameter
1711+ is specified, the function will return all stop losses that
1712+ match the triggered status.
1713+
1714+ Args:
1715+ triggered (bool): The triggered status of the stop losses
1716+
1717+ Returns:
1718+ List[TradeStopLoss]: A list of stop losses
1719+ """
1720+ query_params = {}
1721+
1722+ if triggered is not None :
1723+ query_params ["triggered" ] = triggered
1724+
1725+ return self .trade_stop_loss_service .get_all (query_params )
0 commit comments