@@ -498,6 +498,9 @@ def _initialize_algorithm_for_backtest(self, algorithm):
498498 if portfolio_configuration_service .count () == 0 :
499499 raise OperationalException ("No portfolios configured" )
500500
501+ # Initialize all portfolios that are registered
502+
503+
501504 strategy_orchestrator_service = \
502505 self .container .strategy_orchestrator_service ()
503506 market_credential_service = self .container .market_credential_service ()
@@ -518,17 +521,6 @@ def _initialize_algorithm_for_backtest(self, algorithm):
518521 trade_service = self .container .trade_service (),
519522 )
520523
521- # Create all portfolios
522- portfolio_configuration_service = self .container \
523- .portfolio_configuration_service ()
524- portfolio_configurations = portfolio_configuration_service .get_all ()
525- portfolio_service = self .container .portfolio_service ()
526-
527- for portfolio_configuration in portfolio_configurations :
528- portfolio_service .create_portfolio_from_configuration (
529- portfolio_configuration
530- )
531-
532524 def run (
533525 self ,
534526 payload : dict = None ,
@@ -774,20 +766,24 @@ def get_portfolio_configurations(self):
774766
775767 def run_backtest (
776768 self ,
777- algorithm ,
778769 backtest_date_range : BacktestDateRange ,
770+ initial_amount = None ,
779771 pending_order_check_interval = None ,
780- output_directory = None
772+ output_directory = None ,
773+ algorithm : Algorithm = None
781774 ) -> BacktestReport :
782775 """
783776 Run a backtest for an algorithm. This method should be called when
784777 running a backtest.
785778
786779 Parameters:
787- algorithm: The algorithm to run a backtest for (instance of
788- Algorithm)
789780 backtest_date_range: The date range to run the backtest for
790781 (instance of BacktestDateRange)
782+ initial_amount: The initial amount to start the backtest with.
783+ This will be the amount of trading currency that the backtest
784+ portfolio will start with.
785+ algorithm: The algorithm to run a backtest for (instance of
786+ Algorithm)
791787 pending_order_check_interval: str - pending_order_check_interval:
792788 The interval at which to check pending orders (e.g. 1h, 1d, 1w)
793789 output_directory: str - The directory to
@@ -796,8 +792,11 @@ def run_backtest(
796792 Returns:
797793 Instance of BacktestReport
798794 """
799- logger .info ("Initializing backtest" )
800- self .algorithm = algorithm
795+ if algorithm is not None :
796+ self .algorithm = algorithm
797+
798+ if self .algorithm is None :
799+ raise OperationalException ("No algorithm registered" )
801800
802801 self ._initialize_app_for_backtest (
803802 backtest_date_range = backtest_date_range ,
@@ -807,18 +806,20 @@ def run_backtest(
807806 self ._initialize_algorithm_for_backtest (
808807 algorithm = self .algorithm
809808 )
809+
810810 backtest_service = self .container .backtest_service ()
811- configuration_service = self .container .configuration_service ()
812- config = configuration_service .get_config ()
813- backtest_service .resource_directory = config [RESOURCE_DIRECTORY ]
814811
815812 # Run the backtest with the backtest_service and collect the report
816813 report = backtest_service .run_backtest (
817- algorithm = self .algorithm , backtest_date_range = backtest_date_range
814+ algorithm = self .algorithm ,
815+ initial_amount = initial_amount ,
816+ backtest_date_range = backtest_date_range
818817 )
819818 backtest_report_writer_service = self .container \
820819 .backtest_report_writer_service ()
821820
821+ config = self .container .configuration_service ().get_config ()
822+
822823 if output_directory is None :
823824 output_directory = os .path .join (
824825 config [RESOURCE_DIRECTORY ], "backtest_reports"
0 commit comments