def run_strategy(data_provider: DataProvider) -> Tuple[float, str]: """ Returns the strategy end result and checksum of the preloaded data. """ start_date = str_to_date("2017-01-01") end_date = str_to_date("2018-01-01") settings = get_test_settings() session_builder = BacktestTradingSessionBuilder(data_provider, settings, PDFExporter(settings), ExcelExporter(settings)) session_builder.set_backtest_name('SPY w. stop ' + str(SpxWithStopLoss.percentage)) session_builder.set_initial_cash(1000000) session_builder.set_frequency(Frequency.DAILY) session_builder.set_commission_model(IBCommissionModel) session_builder.set_data_provider(data_provider) session_builder.set_monitor_settings(BacktestMonitorSettings.no_stats()) ts = session_builder.build(start_date, end_date) ts.use_data_preloading(SpxWithStopLoss.ticker, RelativeDelta(days=40)) SpxWithStopLoss(ts) ts.start_trading() data_checksum = ts.get_preloaded_data_checksum() actual_end_value = ts.portfolio.portfolio_eod_series()[-1] return actual_end_value, data_checksum
def setUp(self): dates = DatetimeIndex(start='2014-01-01', freq='d', periods=10) returns = np.arange(0, 1, 0.1) self.test_series = QFSeries(index=dates, data=returns) reversed_returns = returns[::-1] self.test_series_reversed = QFSeries(index=dates, data=reversed_returns) self.test_data_frame = concat( [self.test_series, self.test_series_reversed], axis=1, join='inner') settings = get_test_settings() self.xl_exporter = ExcelExporter(settings=settings)
def run_strategy(data_provider: DataProvider) -> Tuple[float, str]: """ Returns the strategy end result and checksum of the preloaded data. """ model_tickers = [BloombergFutureTicker("Corn", "C {} Comdty", 1, 10, 1)] start_date = str_to_date('2003-05-30') end_date = str_to_date('2009-01-01') initial_risk = 0.006 # ----- build trading session ----- # settings = get_test_settings() session_builder = BacktestTradingSessionBuilder(data_provider, settings, PDFExporter(settings), ExcelExporter(settings)) session_builder.set_backtest_name('Simple Futures Strategy') session_builder.set_position_sizer(InitialRiskPositionSizer, initial_risk=initial_risk) session_builder.set_frequency(Frequency.DAILY) session_builder.set_data_provider(data_provider) session_builder.set_monitor_settings(BacktestMonitorSettings.no_stats()) ts = session_builder.build(start_date, end_date) # ----- build models ----- # model = SimpleFuturesModel(fast_time_period=50, slow_time_period=100, risk_estimation_factor=3, data_provider=ts.data_handler) model_tickers_dict = {model: model_tickers} # ----- start trading ----- # strategy = AlphaModelStrategy(ts, model_tickers_dict, use_stop_losses=False) OnBeforeMarketOpenSignalGeneration(strategy) ts.use_data_preloading(model_tickers) ts.start_trading() data_checksum = ts.get_preloaded_data_checksum() actual_end_value = ts.portfolio.portfolio_eod_series()[-1] return actual_end_value, data_checksum
class TestExcelExport(TestCaseWithFileOutput): _tmp_dir = join(dirname(__file__), 'tmp') _templates_dir = join(dirname(__file__), 'dummies') comparator = ExcelFilesComparator() def setUp(self): dates = DatetimeIndex(start='2014-01-01', freq='d', periods=10) returns = np.arange(0, 1, 0.1) self.test_series = QFSeries(index=dates, data=returns) reversed_returns = returns[::-1] self.test_series_reversed = QFSeries(index=dates, data=reversed_returns) self.test_data_frame = concat( [self.test_series, self.test_series_reversed], axis=1, join='inner') settings = get_test_settings() self.xl_exporter = ExcelExporter(settings=settings) def tearDown(self): self.clear_tmp_dir() def tmp_dir(self): return self._tmp_dir def templates_dir(self): return self._templates_dir def test_exporting_series_to_the_new_file(self): new_file_path = self.tmp_file_path(SINGLE_SHEET_ONE_SERIES) expected_file_path = self.template_file_path(SINGLE_SHEET_ONE_SERIES) self.xl_exporter.export_container(self.test_series, new_file_path) self.comparator.assert_the_same(expected_file_path, new_file_path) def test_exporting_series_to_the_existing_file(self): """ Take a workbook with one worksheet and later add a new series to this worksheet without removing the other data within the worksheet. """ file_to_modify_path = self.copy_template_to_tmp( SINGLE_SHEET_ONE_SERIES) expected_file_path = self.template_file_path(SINGLE_SHEET_TWO_SERIES) self.xl_exporter.export_container(self.test_series_reversed, file_to_modify_path, starting_cell='C1') self.comparator.assert_the_same(expected_file_path, file_to_modify_path) def test_exporting_series_to_the_new_worksheet(self): """ Take the existing workbook, add a new worksheet and save there some data. """ file_to_modify_path = self.copy_template_to_tmp( SINGLE_SHEET_ONE_SERIES) expected_file_path = self.template_file_path(TWO_SHEETS_TWO_SERIES) self.xl_exporter.export_container(self.test_series_reversed, file_to_modify_path, sheet_name='Sheet2') self.comparator.assert_the_same(expected_file_path, file_to_modify_path) def test_exporting_series_to_the_existing_worksheet(self): file_to_modify_path = self.copy_template_to_tmp(TWO_SHEETS_TWO_SERIES) expected_file_path = self.template_file_path(TWO_SHEETS_THREE_SERIES) self.xl_exporter.export_container(self.test_series, file_to_modify_path, sheet_name='Sheet2', starting_cell='C1') self.comparator.assert_the_same(expected_file_path, file_to_modify_path) def test_exporting_dataframe(self): new_file_path = self.tmp_file_path(SINGLE_SHEET_ONE_DATA_FRAME) expected_file_path = self.template_file_path( SINGLE_SHEET_ONE_DATA_FRAME) self.xl_exporter.export_container(self.test_data_frame, new_file_path) self.comparator.assert_the_same(expected_file_path, new_file_path) def test_exporting_dataframe_custom_index(self): new_file_path = self.tmp_file_path( SINGLE_SHEET_CUSTOM_INDEX_DATA_FRAME) expected_file_path = self.template_file_path( SINGLE_SHEET_CUSTOM_INDEX_DATA_FRAME) df = QFDataFrame( { "Test": [1, 2, 3, 4, 5], "Test2": [10, 20, 30, 40, 50] }, ["A", "B", "C", "D", "E"]) self.xl_exporter.export_container(df, new_file_path, starting_cell='A10', include_column_names=True) self.comparator.assert_the_same(expected_file_path, new_file_path) def test_exporting_single_cells(self): new_file_path = self.tmp_file_path(SINGLE_CELLS) expected_file_path = self.template_file_path(SINGLE_CELLS) self.xl_exporter.write_cell(new_file_path, "A1", "Testing") self.xl_exporter.write_cell(new_file_path, "D6", 123456) self.xl_exporter.write_cell(new_file_path, "G12", 3.1423456) self.comparator.assert_the_same(expected_file_path, new_file_path)