Adding leverage in manual backtesting

On the course, getting started with algotrading, a moving average cross over strategy was discussed and also how to check performance. I want to get an insight of how to add  (e.g 5X, 10X) so that the metrics (cumulative return) would be more realistic if one chooses to trade with leverage

Hello Obimba,



So in section 9 unit 6 we have a notebook to create this strategy. 



In cell 7, we have the following code:

 

# Calculate the returns by multiplying the signal with daily price change
returns = data['signal']*data['Adj Close'].pct_change()

You can set whatever leverage you want to incorporate to it. You just need to do the following change:

 

# Calculate the returns by multiplying the signal with daily price change
returns = data['signal']*data['Adj Close'].pct_change()*leverage

Where leverage can be 2, 5, or 10, etc.



When you want to create the cumulative returns, you just apply the same code you have in cell 9:

 

cumret = (returns+1).cumprod()

So, as you see, the only change is made in the returns code.



I hope this helps,



Thanks and regards,



José Carlos