# "Backtesting" from trading signals and historical prices not functioning properly

How does one go about properly backtesting and visualising their strategy using their trading signals and historical prices where the trading signals are 1 for long, 0 for no position -1 for short?

Obviously the end result should look something like this, this visual originates from a simple moving average strategy from "Python for Finance"

My strategy is not the same however since the trading signals are just 1s and -1s I would expect to be able to get a similar result, however when I try using the following code, I get the following graph, and I have absolutely no idea why.

data['XONRETURNS'] = np.log(data['XON'] / data['XON'].shift(1))
data['XONSTRATEGY'] = (data['POSITIONXON'].shift(1)) * (data['XONRETURNS'])
ax = data[['XONRETURNS', 'XONSTRATEGY']].cumsum().apply(np.exp).plot(figsize=(10, 6))


XON is simply the raw price data and XON strategy is using the trading signals i.e. POSITIONXON.

My suspicion is that either the returns or something relating to taking the log(or not) is causing this issue?

General answers on backtesting from trading signals will also be very helpful so that I can start from scratch if need be.

Many thanks!

• 1. Debug by looking at simple cases. For example, make every data['POSITIONXON'] equal to 1, what happens then? 2. Have you checked that every POSITIONXON is 1,0 or -1? I strongly suspect you have some other values in there 3. Looks like the green line plunges to 1 (which is exp(0)) at every calendar month end. Which suggests a data/input issue of some kind. Look at the data and krank through some calculations manually. Apr 22 at 9:48