Predict S&P Using Google Colab & Facebook Prophet

A few years ago Facebook decided to open source Prophet. This is their analytics algorithm that uses an additive model to fit non-linear data with seasonality. I started to wonder, “If this algorithm were in place in March when the stock market’s crashed what would it have advised?” So I decided to give it a spin.

Let’s assume you had a significant amount of money invested in S&P index funds on March 23, 2020. Since the beginning of the year, you would have lost 31% of your money. At this point you might be thinking, “Oh shit, what do I do, sell, buy, hold?”. A lot of investors would panic and sell. The market thrives and two psychologies, fear and greed.

But let’s take an analytics approach to this problem. What would Facebook’s Prophet algorithm advise you do to? Here is how you can approach that problem.

The first thing I did was fire up Google Colab. The entire notebook can be found here.

The first part of this code uses a Python DataReader to pull SPY from Yahoo Finance. I created an end date of 3/23/2020. This only gives Facebook’s Prophet access to data up until this point. We are then going to have it predict where it thinks the price would be today 8/20/2020 without feeding it any future data.

# Python
import pandas as pd
from fbprophet import Prophet

from pandas_datareader import data as web
import datetime

import pandas as pd
import matplotlib.pyplot as plt

stock = 'spy'
endDate = datetime.datetime(2020, 3, 23)
#start_date = (datetime.datetime.now() - datetime.timedelta(days=2000)).strftime("%m-%d-%Y")
start_date = (endDate - datetime.timedelta(days=2000)).strftime("%m-%d-%Y")
#print(start_date)
df = web.DataReader(stock, data_source='yahoo', start=start_date,end=endDate)
#date is the index so you need to make it a column
df["Date"] = df.index

import matplotlib.pyplot as plt

plt.plot(df['Close'])
df.head()
df.tail()

The next part of code renames the imported columns from “Date” to “ds” and “Close” to “y”. DS and Y are the two variables that Prophet will be looking for.

# Python
df = df.rename(columns={"Date": "ds", "Close": "y"}, errors="raise")

This next part of the code starts to set up Prophet. The only variable you should be concerned with is 151. This is telling the algorithm to look out 151 days. Then it forecasts three variables yhat, yhat_lower, and yhat_upper. Yhat is the predicted price with upper and lower being the bounds in which it assumes the price will fall in.

# Python
m = Prophet()
m.fit(df)
future = m.make_future_dataframe(periods=151)
forecast = m.predict(future)
forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()

You can see that it predicts the closing price for tomorrow will be 331.52. Remember it is only using data from March 23rd to make this calculation. Given the wild gyrations in the market, this is extremely close to being accurate. The SPY closed today at 338. Prophet predicted it would close at 331. It was off by 2%. This is using a 5-month look ahead forecasting model.

Here is the visual representation of what that looks like.

from fbprophet.plot import plot_plotly, plot_components_plotly

plot_plotly(m, forecast)

And more charts…

plot_components_plotly(m, forecast)

Finally here is the visualization of the predicted price vs. the actual price chart.

Memory handler errors in MT4 while importing history files or csv files

I no longer trade using Metatrader. I noticed users kept hitting an invalid link on my website to this search phrase. So the purpose of this post is to catch those invalid links related to Metatrader. I would recommend learning Python and custom developing your algorithms. It will be much more rewarding.

If you have issues with Memory handler errors in MT4 it is because you’re trying to convert too many symbols at the same time. Close all of your charts and only leave open the pair you’re working with. When you’re done close it and go on to the next. This will get rid of the memory errors that MT4 complains about once terminal.exe starts to use more than 1GB of memory.

Attached are zip files I had or that I was working on for Metatrader. I don’t know if they’re up to date, if they work, or support them in any form.

Monitor Account Balance and Send Email Alerts in Metatrader When Account Drops Below Threshold

I no longer trade using Metatrader. I noticed users kept hitting an invalid link on my website to this search phrase. So the purpose of this post is to catch those invalid links related to Metatrader. I would recommend learning Python and custom developing your algorithms. It will be much more rewarding.

I had a strategy that I programmed awhile ago that completely drained my account. The strategy itself was fine. In fact it was doing so good I decided to apply it from some of my currency pairs to XAU/USD. After applying I caught a flight to LAX to pick up a Porsche I had found online. I had never traded gold before but judging from the backtest it should have performed relatively well. The problem was I did not anticipate the large spread on gold. The spread was enough to send my strategy constantly opening and closing positions. The buy rules and sell rules were being met because of the huge spread. Once I landed I had picked up the Porsche and headed to my hotel, not realizing at the time I had left my briefcase and laptop in the back of the taxi that I caught at LAX. When I arrived at my hotel I checked my phone only to realize one of my accounts was down around 70%, only 30% of my capital remaining. I immediately freaked out and started my investigation. It was then I realized the large spread in gold was constantly opening and closing positions. Costing me the spread every 5 minutes or so. I called Oanda as I then realized I lost my laptop I could not shut off the robot. Closing the trades manually from my phone only made the account drain faster.

Now you see my reason for writing this code. The purpose of it is to grab your account balance at a specific time and then if it drops below a threshold to send an email notification. This code is in mqh format. Which is an include file. The reason for this is so that it can be applied to any expert advisor.


//+------------------------------------------------------------------+
//| DailyBalanceCheck.mq4 |
//| Copyright © 2011, Jeremy R. Whittaker |
//| http://www.JeremyWhittaker.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Jeremy R. Whittaker"
#property link "http://www.JeremyWhittaker.com"

double todaysBalance, dDouble;
int iHandle, iErrorCode;
extern string sFileName=”dailyBalance.csv”;
int iDecimalPlaces=2;
int sendWarningCount, sendBalanceCount;

void dailyBalanceCheck(){
int count;
double onePercent;

if(todaysBalance<1){
fnReadFile();
todaysBalance=dDouble;
}

if(Hour()==16 && Minute()==59){
count=0;
sendWarningCount=0;
sendBalanceCount=0;
}

if(Hour()==1700 && Minute()==0){
if(count==0){
todaysBalance=AccountBalance();
dDouble=todaysBalance;
fnWriteFile();
if(sendBalanceCount<1){
SendMail(“Account Update”,”Good evening Mr. Whittaker todays balance is “+todaysBalance+”. Your current account equity is “+AccountEquity());
Print(“Sending Balance Email”);
sendBalanceCount=sendBalanceCount+1;
}
count=count+1;
}
}

onePercent=todaysBalance*0.01;

if(AccountEquity() SendMail(“Warning account equity down 1%”,”Account Balance at 5PM was “+todaysBalance+”. Now it is “+AccountEquity());
Print(“Sending Warning Email”);
sendWarningCount=sendWarningCount+1;
}
return(false);
}

//+——————————————————————+
bool fnReadFile()
{
iHandle = FileOpen(sFileName,FILE_CSV|FILE_READ,’;’);
if(iHandle < 1)
{
iErrorCode = GetLastError();
if (iErrorCode == 4103)
Print(“File not found”);
else
Print(“Error reading file: “,iErrorCode);
return(false);
}
dDouble = StrToDouble(FileReadString(iHandle));
FileClose(iHandle);
return(true);
}

//+------------------------------------------------------------------+
bool fnWriteFile()
{
iHandle = FileOpen(sFileName,FILE_CSV|FILE_WRITE,';');
if(iHandle < 1)
{
iErrorCode = GetLastError();
Print("Error updating file: ",iErrorCode);
return(false);
}
FileWrite(iHandle,DoubleToStr(dDouble,iDecimalPlaces));
FileClose(iHandle);
return(true);
}
//+------------------------------------------------------------------+

Attached are zip files I had or that I was working on for Metatrader. I don’t know if they’re up to date, if they work, or support them in any form.

Whittaker hourly ATR indicator for Metatrader

I no longer trade using Metatrader. I noticed users kept hitting an invalid link on my website to this search phrase. So the purpose of this post is to catch those invalid links related to Metatrader. I would recommend learning Python and custom developing your algorithms. It will be much more rewarding.

Attached are zip files I had or that I was working on for Metatrader. I don’t know if they’re up to date, if they work, or support them in any form.