I'm learning how to do backtesting in Python using Pandas. I'm learning how to use Moving Average Crossover. I've generated signals to buy or to sell already. But I'm not sure where to go from there? Let's say if I have the initial capital of
$100,000 what would be my final % return
I realise that this is a very basic question but I can't seem to wrap my head around it yet.
This is what I have it so far.
import datetime import pandas as pd from pandas_datareader import data, wb import numpy as np import Quandl import matplotlib.pylab as pylab %matplotlib inline start_date = datetime.datetime(2009,1,1) end_date = datetime.datetime(2014,1,1) amzn = data.DataReader("AMZN", "yahoo", start_date, end_date) def generate_signals(self): # Create DataFrame and initialise signal series to zero signals = pd.DataFrame(index=amzn.index) signals['signal'] = 0 # Create the short/long simple moving averages signals['short_mavg'] = pd.rolling_mean(amzn['Adj Close'], 40, min_periods=1) signals['long_mavg'] = pd.rolling_mean(amzn['Adj Close'], 100, min_periods=1) # When the short SMA exceeds the long SMA, set the ‘signals’ Series to 1 (else 0) signals['signal'][40:] = np.where(signals['short_mavg'][40:] > signals['long_mavg'][100:], 1, 0) # Take the difference of the signals in order to generate actual trading orders signals['positions'] = signals['signal'].diff() return signals
I've taken the code from https://s3.amazonaws.com/quantstart/media/powerpoint/an-introduction-to-backtesting.pdf
Portfolio part doesn't run for me so I'm trying to figure out what's supposed to happen in the actual backtesting.