13.08.2022 Views

advanced-algorithmic-trading

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

424

produce a final truth column where both conditions are met.

Since these are written with Python True and False operators they need to be converted to

+1 and -1 integers, which the final two lines carry out.

Note that in the following code the "up/down factor" is commented out in favour of the

directional change, but is still kept in the same column name of UpDown. If you wish to use the

"up/down factor" instead of the directional change as response, you will need to uncomment out

ts["UpDown"] = down_tot & up_tot and comment out the line below it:

# Determine if the stock has gone up at least by

# ’up_down_factor’ x ’percent_factor’ and down no more

# then ’percent_factor’

up = up_down_factor*percent_factor

down = percent_factor

# Create the list of True/False entries for each date

# as to whether the up/down logic is true

down_cols = [

ts["Lookforward%s" % str(i+1)] > -down

for i in range(0, lookforward_minutes)

]

up_cols = [

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

for i in range(0, lookforward_minutes)

]

# Carry out the bitwise and, as well as bitwise or

# for the down and up logic

down_tot = down_cols[0]

for c in down_cols[1:]:

down_tot = down_tot & c

up_tot = up_cols[0]

for c in up_cols[1:]:

up_tot = up_tot | c

#ts["UpDown"] = down_tot & up_tot

ts["UpDown"] = np.sign(ts["Lookforward1"])

# Convert True/False into 1 and 0

ts["UpDown"] = ts["UpDown"].astype(int)

ts["UpDown"].replace(to_replace=0, value=-1, inplace=True)

return ts

This function carries out the data preparation work of the script. In the __main__ function

the above function is called for the AREX data.

n_estimators controls the number of decision trees to use in the RandomForestClassifier,

while n_jobs controls the number of CPU cores to use for the training.

Make sure to change your path from csv_filepath = "/path/to/your/AREX.csv" to the

path where your AREX (or other) data resides before running this script:

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

Saved successfully!

Ooh no, something went wrong!