Downloading Custom Data for Build Alpha using Python
Happy Friday!
For this Free
Friday edition, I am going to do something new. I am going to make this
slightly educational and give away some code.
I get tons of
questions every week, but they mainly fall into two categories. The
first question is in regards to adding custom data to Build Alpha.
You can add intraday data, weekly data, custom bar type data, sentiment
data, or even simple single stock data. The second question is in
regards to using or learning Python.
In this post, I
will attempt to “kill two birds with one stone” and show a simple
Python code to download stock data from the Yahoo Finance API.
In fact, we will use Python to pull and save data ready formatted for Build Alpha for all 30 Dow stocks in less than 30 seconds.
You can view the entire script later in this code or in the video below.
The first few lines are simple to import statements pulling public code that we can reuse including the popular pandas library.
import pandas as pd
from datetime import datetime
from pandas_datareader import data
I then define a
function that downloads the data using the built-in DataReader function
of the pandas_datareader library. I also adjust the open, high, low and
close prices by the split ratio at every bar. This ensures we have a
consistent time series if a stock has undergone a split, for example. **Please
note other checks could be recommended like verifying high > open
and high > close and high > low, but I have left these up to Yahoo
in this post**. I then end the function returning a pandas data
frame that contains our downloaded data. This get_data function will be
valuable later in the code.
def get_data(symbol, start_date, end_date):
dat = data.DataReader(symbol, "yahoo", start_date, end_date)
dat['Ratio'] = dat['Adj Close'] / dat['Close']
dat['Open'] = dat['Open'] * dat['Ratio']
dat['High'] = dat['High'] * dat['Ratio']
dat['Low'] = dat['Low'] * dat['Ratio']
dat['Close'] = dat['Close'] * dat['Ratio']
return dat
I then go ahead and put all 30 dow tickers
in a Python list named DJIA. I also go ahead and create our start and
end dates in which we desire to download data.
DJIA=["AAPL","AXP","BA","CAT","CSCO","CVX","KO","DD","XOM","GE","GS","HD","IBM","INTC","JNJ","JPM","MCD","MMM","MRK","MSFT",
"NKE","PFE","PG","TRV","UNH","UTX","V","VZ","WMT","DIS"]
start = datetime(2007,1,1)
end = datetime.today()
Finally, and
the guts of this code, I loop through all 30 of our tickers calling the
get_data function on each one of them. After downloading the first one,
AAPL in our case, I open a file named AAPL.csv
and then loop through the downloaded price series retrieved from our
get_data function. I then write each bar to the file appropriately named
AAPL.csv. I then close the AAPL.csv
file before downloading the second symbol, AXP in our case. This
process is repeated for each and every symbol. The result is 30 seconds
to download 30 stocks worth of data! Each symbol’s data is saved in a
file named Symbol.csv.
for ticker in DJIA:
DF = get_data(ticker,start,end)
fh = open("%s.csv" % ticker,'w+')
for i,date in enumerate(DF.index):
fh.write("%s,%.2f,%.2f,%.2f,%.2f,%dn" %
(date.strftime('%Y%m%d'),DF['Open'][i],DF['High'][i],DF['Low'][i],DF['Close'][i],DF['Volume'][i]))
fh.close()
Now to the second part. Using this data in BuildAlpha
is as simple as clicking on settings and searching for your desired
file. I’ve attached a photo below that shows how the trader/money
manager can now run tests on the newly downloaded AAPL data using the
symbol “User Defined 1”. Pictures below for clarity.
I’m showing a
strategy created for $AAPL stock, but it is only to prove this Python
code and Build Alpha feature work. There is major selection bias
creating a strategy on a stock that has basically been in a major
uptrend for 90%+ of its existence. That being said, and in a later post,
I will show a new Build Alpha feature that allows you to test
strategies across different symbols to make sure the strategy holds up
on both correlated and uncorrelated securities. Either way here is the
AAPL strategy.
Buy Rules:
1.Today’s Low > Open of 3 Day’s Ago
2.Today’s 14 Period Stochastics > Yesterday’s 14 Period Stochastic
3. Today’s Upper Keltner Channel > Yesterday’s Upper Keltner Channel
2.Today’s 14 Period Stochastics > Yesterday’s 14 Period Stochastic
3. Today’s Upper Keltner Channel > Yesterday’s Upper Keltner Channel
Exit Rules:
1. Two Day Maximum Hold
2. 1.00 * 20 Period ATR Stop Loss
I like this strategy because it is convex.
We limit the downside, but let the market give us as much as possible
in 2 days. Below is the equity graph with the highlighted part being out
of sample and based on 1 share as this is just for demonstration
purposes!
I hope this makes life easier for some of you and as always… Happy Friday,
Dave
PS: This is all extra. You do not need to know Python (or any programming) to use BuildAlpha. This is just for advanced users that want additional help.
I really liked your Information. Keep up the good work. Best Automated Trading Platform
ReplyDelete