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.

373

value would represent data not known at the time of the prediction.

In order to account for this we simply need to move the predicted value one day ahead. I

have found this to be more straightforward using Python. To keep things simple, I’ve kept it to

pure Python, by not using any special libraries.

Here is the short script that carries this procedure out. Make sure to run it in the same

directory as the forecasts.csv file:

if __name__ == "__main__":

# Open the forecasts CSV file and read in the lines

forecasts = open("forecasts.csv", "r").readlines()

# Run through the list and lag the forecasts by one

old_value = 1

new_list = []

for f in forecasts[1:]:

strpf = f.replace(’"’,’’).strip()

new_str = "%s,%s\n" % (strpf, old_value)

newspl = new_str.strip().split(",")

final_str = "%s,%s\n" % (newspl[0], newspl[2])

final_str = final_str.replace(’"’,’’)

old_value = f.strip().split(’,’)[1]

new_list.append(final_str)

# Output the updated forecasts CSV file

out = open("forecasts_new.csv", "w")

for n in new_list:

out.write(n)

At this point we now have the corrected indicator file stored in forecasts_new.csv. If you

purchased the book+source option you will find the file in the appropriate directory in the zip

package.

26.3 Strategy Results

Now that we have generated our indicator CSV file we need to compare its performance to "Buy

& Hold".

We firstly read in the indicator from the CSV file and store it as spArimaGarch:

> spArimaGarch = as.xts(

> read.zoo(

> file="forecasts_new.csv", format="%Y-%m-%d", header=F, sep=","

> )

> )

We then create an intersection of the dates for the ARIMA+GARCH forecasts and the original

set of returns from the S&P500. We can then calculate the returns for the ARIMA+GARCH

strategy by multiplying the forecast sign (+ or -) with the return itself:

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

Saved successfully!

Ooh no, something went wrong!