13.08.2022 Views

advanced-algorithmic-trading

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

258

"""

# Obtain stock information from Yahoo Finance

ts = web.DataReader(

symbol, "yahoo", start_date, end_date

)

# Create the new lagged DataFrame

tslag = pd.DataFrame(index=ts.index)

tslag["Today"] = ts["Adj Close"]

tslag["Volume"] = ts["Volume"]

# Create the shifted lag series of

# prior trading period close values

for i in range(0,lags):

tslag["Lag%s" % str(i+1)] = ts["Adj Close"].shift(i+1)

# Create the returns DataFrame

tsret = pd.DataFrame(index=tslag.index)

tsret["Volume"] = tslag["Volume"]

tsret["Today"] = tslag["Today"].pct_change()*100.0

# Create the lagged percentage returns columns

for i in range(0,lags):

tsret["Lag%s" % str(i+1)] = tslag[

"Lag%s" % str(i+1)

].pct_change()*100.0

tsret = tsret[tsret.index >= start_date]

return tsret

if __name__ == "__main__":

# Set the random seed, number of estimators

# and the "step factor" used to plot the graph of MSE

# for each method

random_state = 42

n_jobs = 1 # Parallelisation factor for bagging, random forests

n_estimators = 1000

step_factor = 10

axis_step = int(n_estimators/step_factor)

# Download ten years worth of Amazon

# adjusted closing prices

start = datetime.datetime(2006, 1, 1)

end = datetime.datetime(2015, 12, 31)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!