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_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)