Using cumprod() to caclulate equity curve in Python
Using cumprod() to caclulate equity curve in Python
I am having difficulty calculating a compounding return using cumprod(). It starts compounding from the very beginning, but I only want it to start when 'Final_Order' equals buy and stop when 'Final_Order equal sell, and then reset again on the next buy order. Please see the example below.
Sample data below shows the output I expect to see for column 'Backtest'.
Time Adj_Price Final_Order Backtest
0 7 nan 1000
1 6 nan 1000
2 5 Buy 1000
3 7 Buy 1400
4 8 Sell 1600
5 6 Sell 1600
6 4 Buy 1600
7 5 Buy 2000
8 7 Buy 2800
9 9 Sell 3600
10 7 Sell 3600
11 7 Sell 3600
12 6 Sell 3600
Below are calculations for 'Backtest'.
Below is the code I am working with.
data['Backtest'] = np.where(data['Final_Order'] == 'Buy',
((1 + data['Adj
Close'].pct_change(1)).cumprod())*1000,
data['Backtest'].ffill())
Should say 'Backtest', not benchmark. I have made the correction
– mark
30 mins ago
t9
is sell. How do you get 3600??– Onyambu
26 mins ago
t9
@Onyambu It is based on the previous day's 'Final_Order'. The price is based on Closing price, so I technically wouldnt sell until the following morning. In the IF statements, you'll see that Final_Order = Buy(t8).
– mark
21 mins ago
where are you getting the else values from? or the values you are using to compute eg 1000,1400,1600 etc where do you get them from?
– Onyambu
10 mins ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What is benchmark?
– Mad Physicist
31 mins ago