def test_close_open_positions_with_open_no_force_all_write(self): # close values DataFrame cv_df = pd.read_csv('./data/sample_data.csv', parse_dates=True, index_col=0)[['close']] # input_data DataFrame id_df = pd.read_csv('./data/sample_data.csv', parse_dates=True, index_col=0) ts = TradingSimulation(input_data_index=id_df.index, close_values=cv_df, max_exposure=None, short_exposure_factor=1.5) ts._portfolio = pd.read_csv( './data/portfolio_simulation_data_all_open_ten_rounds.csv', parse_dates=True, index_col=0).to_numpy(dtype=np.float64, copy=True) portfolio_expected_result = pd.read_csv( './data/portfolio_simulation_data_all_open_ten_rounds.csv', parse_dates=True, index_col=0).to_numpy(dtype=np.float64, copy=True) portfolio_expected_result[0, 1] = 2.0 portfolio_expected_result[2, 1] = 2.0 portfolio_expected_result[5, 1] = 2.0 portfolio_expected_result[5, 2] = 40.00 ts._simulation_data['exposure'].iat[8] = 100.0 ts._simulation_data['earnings'].iat[8] = 200.0 ts._portfolio[5, 2] = 40.00 ts._close_values[9, 0] = 24.0 earnings, closed_exposure = ts._closeOpenPositions(i_index=9) exposure_expected = 20.50 + 22.50 + 40.00 earnings_expected = (2 * 24.0 - 20.5 - 22.5) + \ ((40.00 / 1.5) - 1 * 24.0) self.assertEqual(earnings, earnings_expected) self.assertEqual(closed_exposure, exposure_expected) np.testing.assert_equal( ts._simulation_data['exposure'].iat[9], ts._simulation_data['exposure'].iat[8] - exposure_expected) np.testing.assert_equal( ts._simulation_data['earnings'].iat[9], ts._simulation_data['earnings'].iat[8] + earnings_expected) np.testing.assert_equal(ts._portfolio, portfolio_expected_result)
def test_close_simulation(self): # close values DataFrame cv_df = pd.read_csv('./data/sample_data.csv', parse_dates=True, index_col=0)[['close']] # input_data DataFrame id_df = pd.read_csv('./data/sample_data.csv', parse_dates=True, index_col=0) ts = TradingSimulation(input_data_index=id_df.index, close_values=cv_df, max_exposure=None, short_exposure_factor=1.5) self.maxDiff = None statistics_expected_result = { 'number_of_trading_days': 3169, 'number_of_buy_signals': 1297, 'number_of_ignored_buy_signals': 1029, 'number_of_sell_signals': 1152, 'number_of_ignored_sell_signals': 741, 'last_stock_value': 140.41, 'last_exposure': 3268.0, 'last_open_long_positions': 397, 'last_open_short_positions': 396, 'last_portfolio_value': 3368.0, 'last_earnings': 3468.0, 'final_balance': 3568.0 } ts._simulation_data = pd.read_csv( './data/simulation_data_full_with_actions.csv', parse_dates=True, index_col=0) ts._portfolio = pd.read_csv( './data/portfolio_simulation_data_full.csv', parse_dates=True, index_col=0).to_numpy(dtype=np.float64, copy=True) sd_result, st_result = ts.closeSimulation() pd.testing.assert_frame_equal(sd_result, ts._simulation_data) self.assertDictEqual(st_result, statistics_expected_result)
def test_calculate_simulation_statistics_ten_simulation_rounds(self): # close values DataFrame cv_df = pd.read_csv('./data/sample_data.csv', parse_dates=True, index_col=0)[['close']] # input_data DataFrame id_df = pd.read_csv('./data/sample_data.csv', parse_dates=True, index_col=0) ts = TradingSimulation(input_data_index=id_df.index, close_values=cv_df, max_exposure=None, short_exposure_factor=1.5) statistics_expected_result = { 'number_of_trading_days': 10, 'number_of_buy_signals': 4, 'number_of_ignored_buy_signals': 1, 'number_of_sell_signals': 3, 'number_of_ignored_sell_signals': 2, 'last_stock_value': 34.37, 'last_exposure': 109.0, 'last_open_long_positions': 2, 'last_open_short_positions': 3, 'last_portfolio_value': 209.0, 'last_earnings': 309.0, 'final_balance': 409.0 } ts._simulation_data = pd.read_csv( './data/simulation_data_with_actions_ten_rounds.csv', parse_dates=True, index_col=0) ts._portfolio = pd.read_csv( './data/portfolio_simulation_data_ten_rounds.csv', parse_dates=True, index_col=0).to_numpy(dtype=np.float64, copy=True) ts._calculateSimulationStatistics() self.assertDictEqual(ts._statistics, statistics_expected_result)
def test_close_open_positions_none_open_no_force_all_write(self): # close values DataFrame cv_df = pd.read_csv('./data/sample_data.csv', parse_dates=True, index_col=0)[['close']] # input_data DataFrame id_df = pd.read_csv('./data/sample_data.csv', parse_dates=True, index_col=0) ts = TradingSimulation(input_data_index=id_df.index, close_values=cv_df, max_exposure=None, short_exposure_factor=1.5) ts._portfolio = pd.read_csv( './data/portfolio_simulation_data_none_open_ten_rounds.csv', parse_dates=True, index_col=0).to_numpy(dtype=np.float64, copy=True) portfolio_expected_result = pd.read_csv( './data/portfolio_simulation_data_none_open_ten_rounds.csv', parse_dates=True, index_col=0).to_numpy(dtype=np.float64, copy=True) ts._simulation_data['exposure'].iat[8] = 100.0 ts._simulation_data['earnings'].iat[8] = 200.0 earnings, closed_exposure = ts._closeOpenPositions(i_index=9) self.assertEqual(earnings, 0.0) self.assertEqual(closed_exposure, 0.0) np.testing.assert_equal(ts._simulation_data['exposure'].iat[9], 100.0) np.testing.assert_equal(ts._simulation_data['earnings'].iat[9], 200.0) np.testing.assert_equal(ts._portfolio, portfolio_expected_result)
def test_calculate_portfolio_value(self): # close values DataFrame cv_df = pd.read_csv('./data/sample_data.csv', parse_dates=True, index_col=0)[['close']] # input_data DataFrame id_df = pd.read_csv('./data/sample_data.csv', parse_dates=True, index_col=0) ts = TradingSimulation(input_data_index=id_df.index, close_values=cv_df, max_exposure=None, short_exposure_factor=1.5) ts._portfolio = pd.read_csv( './data/portfolio_simulation_data_ten_rounds.csv', parse_dates=True, index_col=0).to_numpy(dtype=np.float64, copy=True) value = ts._calculatePortfolioValue(i_index=9) self.assertEqual(value, 34.37 * (2 - 3))