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.

423

The next steps consist of filtering the DataFrame via starting and ending dates as well as

dropping the non-essential columns. The shift method is used to create all of the future and

historical minutely lags. The rows with generated NaN values are dropped. All of the prices are

then converted into returns using the pct_change method. Once again the rows with generated

NaN values are dropped:

# Filter on start/end dates

if start is not None:

ts = ts[ts.index >= start]

if end is not None:

ts = ts[ts.index <= end]

# Drop the non-essential columns

ts.drop(

[

"Open", "Low", "High",

"Volume", "OpenInterest"

],

axis=1, inplace=True

)

# Create the lookback and lookforward shifts

for i in range(0, lookback_minutes):

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

for i in range(0, lookforward_minutes):

ts["Lookforward%s" % str(i+1)] = ts["Close"].shift(-(i+1))

ts.dropna(inplace=True)

# Adjust all of these values to be percentage returns

ts["Lookback0"] = ts["Close"].pct_change()*100.0

for i in range(0, lookback_minutes):

ts["Lookback%s" % str(i+1)] = ts[

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

].pct_change()*100.0

for i in range(0, lookforward_minutes):

ts["Lookforward%s" % str(i+1)] = ts[

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

].pct_change()*100.0

ts.dropna(inplace=True)

The following code carries out the calculation of the "up/down factor". It creates a list of up

and down truth columns for each historical or future lag.

The "down" columns are iterated over and bitwise-added (&) to generate a final column,

which is True only when all columns have not dropped below the down percent factor.

The "up" columns use bitwise-or (|) to generate a final column, which is True when at least

one of the columns exceed the up percent factor. These two columns are then bitwise-added to

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

Saved successfully!

Ooh no, something went wrong!