Im writing a dollar cost averaging strategy. The code works fine but the trades are done in a reverse chronological order. the dataframe is not reversed.
0,3,3750,3754,92.254997,92.07,-0.554993,-0.002005,2024-05-21 00:00:00+02:00,2024-05-27 00:00:00+02:00,6 days 00:00:00
1,3,3745,3754,91.330002,92.07,2.219994,0.008102,2024-05-14 00:00:00+02:00,2024-05-27 00:00:00+02:00,13 days 00:00:00
2,3,3740,3754,90.815002,92.07,3.764992,0.013819,2024-05-07 00:00:00+02:00,2024-05-27 00:00:00+02:00,20 days 00:00:00
3,3,3736,3754,90.010002,92.07,6.179993,0.022886,2024-04-30 00:00:00+02:00,2024-05-27 00:00:00+02:00,27 days 00:00:00
4,3,3731,3754,88.839996,92.07,9.690010,0.036358,2024-04-23 00:00:00+02:00,2024-05-27 00:00:00+02:00,34 days 00:00:00
5,3,3726,3754,89.345001,92.07,8.174995,0.030500,2024-04-16 00:00:00+02:00,2024-05-27 00:00:00+02:00,41 days 00:00:00
6,3,3721,3754,90.370003,92.07,5.099991,0.018812,2024-04-09 00:00:00+02:00,2024-05-27 00:00:00+02:00,48 days 00:00:00
7,3,3713,3754,90.754997,92.07,3.945007,0.014490,2024-03-26 00:00:00+01:00,2024-05-27 00:00:00+02:00,61 days 23:00:00
8,3,3708,3754,89.300003,92.07,8.309990,0.031019,2024-03-19 00:00:00+01:00,2024-05-27 00:00:00+02:00,68 days 23:00:00
9,3,3703,3754,88.730003,92.07,10.019989,0.037642,2024-03-12 00:00:00+01:00,2024-05-27 00:00:00+02:00,75 days 23:00:00
10,3,3698,3754,88.714996,92.07,10.065010,0.037818,2024-03-05 00:00:00+01:00,2024-05-27 00:00:00+02:00,82 days 23:00:00
Behavior
Im writing a dollar cost averaging strategy. The code works fine but the trades are done in a reverse chronological order. the dataframe is not reversed.
ticker_symbol = "IWDA.AS"
Get the data
ticker_data = yf.Ticker(ticker_symbol) df = ticker_data.history(period="max")
class DCA(Strategy):
Initialize the backtest with the dataframe 'df'
bt = Backtest(df, DCA, cash=10000000, commission=0, trade_on_close=False, exclusive_orders=False)
Run the backtest
output = bt.run() print(output)
trades = output["_trades"] trades
0,3,3750,3754,92.254997,92.07,-0.554993,-0.002005,2024-05-21 00:00:00+02:00,2024-05-27 00:00:00+02:00,6 days 00:00:00 1,3,3745,3754,91.330002,92.07,2.219994,0.008102,2024-05-14 00:00:00+02:00,2024-05-27 00:00:00+02:00,13 days 00:00:00 2,3,3740,3754,90.815002,92.07,3.764992,0.013819,2024-05-07 00:00:00+02:00,2024-05-27 00:00:00+02:00,20 days 00:00:00 3,3,3736,3754,90.010002,92.07,6.179993,0.022886,2024-04-30 00:00:00+02:00,2024-05-27 00:00:00+02:00,27 days 00:00:00 4,3,3731,3754,88.839996,92.07,9.690010,0.036358,2024-04-23 00:00:00+02:00,2024-05-27 00:00:00+02:00,34 days 00:00:00 5,3,3726,3754,89.345001,92.07,8.174995,0.030500,2024-04-16 00:00:00+02:00,2024-05-27 00:00:00+02:00,41 days 00:00:00 6,3,3721,3754,90.370003,92.07,5.099991,0.018812,2024-04-09 00:00:00+02:00,2024-05-27 00:00:00+02:00,48 days 00:00:00 7,3,3713,3754,90.754997,92.07,3.945007,0.014490,2024-03-26 00:00:00+01:00,2024-05-27 00:00:00+02:00,61 days 23:00:00 8,3,3708,3754,89.300003,92.07,8.309990,0.031019,2024-03-19 00:00:00+01:00,2024-05-27 00:00:00+02:00,68 days 23:00:00 9,3,3703,3754,88.730003,92.07,10.019989,0.037642,2024-03-12 00:00:00+01:00,2024-05-27 00:00:00+02:00,75 days 23:00:00 10,3,3698,3754,88.714996,92.07,10.065010,0.037818,2024-03-05 00:00:00+01:00,2024-05-27 00:00:00+02:00,82 days 23:00:00