Skip to content

Commit 7fd447e

Browse files
committed
Add jupyter notebook check
1 parent 36653ff commit 7fd447e

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

investing_algorithm_framework/app/app.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,8 +630,6 @@ def run_backtest(
630630
output_directory=None,
631631
algorithm: Algorithm = None
632632
) -> BacktestReport:
633-
print(self.name)
634-
print(self.algorithm.name)
635633
"""
636634
Run a backtest for an algorithm. This method should be called when
637635
running a backtest.

investing_algorithm_framework/services/backtesting/backtest_service.py

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
r"backtest-end-date_\d{4}-\d{2}-\d{2}:\d{2}:\d{2}_"
2323
r"created-at_\d{4}-\d{2}-\d{2}:\d{2}:\d{2}\.json$"
2424
)
25+
BACKTEST_REPORT_DIRECTORY_PATTERN = (
26+
r"^report_\w+_backtest-start-date_\d{4}-\d{2}-\d{2}:\d{2}:\d{2}_"
27+
r"backtest-end-date_\d{4}-\d{2}-\d{2}:\d{2}:\d{2}_"
28+
r"created-at_\d{4}-\d{2}-\d{2}:\d{2}:\d{2}$"
29+
)
2530

2631

2732
def validate_algorithm_name(name, illegal_chars=r"[\/:*?\"<>|]"):
@@ -690,7 +695,27 @@ def save_report(
690695
algorithm,
691696
output_directory: str
692697
) -> None:
693-
output_directory = os.path.join(output_directory, algorithm.name)
698+
output_directory = self.create_report_directory(
699+
report, output_directory, algorithm.name
700+
)
701+
702+
703+
if self.is_running_in_notebook():
704+
strategys = algorithm.strategies
705+
706+
for strategy in strategys:
707+
self.save_strategy(strategy, output_directory)
708+
else:
709+
# Copy over all files in the strategy directory
710+
# to the output directory
711+
strategy_directory = os.path.dirname(
712+
inspect.getfile(algorithm.__class__)
713+
)
714+
strategy_files = os.listdir(strategy_directory)
715+
716+
717+
718+
694719
collected_imports = set()
695720
class_definitions = []
696721
self.write_report_to_json(report, output_directory)
@@ -838,3 +863,44 @@ def create_report_file_path(
838863
f"{backtest_end_date}_created-at_{created_at}{extension}"
839864
)
840865
return file_path
866+
867+
@staticmethod
868+
def create_report_directory(
869+
report, output_directory, algorithm_name
870+
) -> str:
871+
"""
872+
Function to create a directory for a backtest report.
873+
874+
Args:
875+
report: BacktestReport - The backtest report to create a
876+
directory for.
877+
output_directory: str - The directory to store the backtest
878+
report file.
879+
algorithm_name: str - The name of the algorithm to
880+
create a directory for.
881+
882+
Returns:
883+
directory_path: str The directory path for the
884+
backtest report file.
885+
"""
886+
887+
backtest_start_date = report.backtest_start_date \
888+
.strftime(DATETIME_FORMAT_BACKTESTING)
889+
backtest_end_date = report.backtest_end_date \
890+
.strftime(DATETIME_FORMAT_BACKTESTING)
891+
created_at = report.created_at.strftime(DATETIME_FORMAT_BACKTESTING)
892+
directory_path = os.path.join(
893+
output_directory,
894+
f"{algorithm_name}_backtest-start-date_"
895+
f"{backtest_start_date}_backtest-end-date_"
896+
f"{backtest_end_date}_created-at_{created_at}"
897+
)
898+
return directory_path
899+
900+
def is_running_in_notebook():
901+
try:
902+
# Jupyter-specific modules
903+
from IPython import get_ipython
904+
return get_ipython() is not None
905+
except ImportError:
906+
return False

0 commit comments

Comments
 (0)