Free Friday #15 – Downloading Custom Data for Build Alpha using Python
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']
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.
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]))
Now to the second part. Using this data in Build Alpha 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.
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
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,
PS: This is all extra. You do not need to know Python (or any programming) to use Build Alpha. This is just for advanced users that want additional help.
- Free Friday March 2017 Update
- Free Friday 14 and 14a – MultiMarket Portfolio
- Noise Test – Explained
- Free Friday 12 – Noise Test
- Free Friday February 2017 Update
- What is E-Ratio
- Free Friday 11 – Free Friday 11 – Sample Size
- Free Friday 10 – Sizing with Monte Carlo
- Free Friday 9 – Intermarket Signals
- Free Friday January 2017 Update
- Free Friday 8 – Randomized Monte Carlo
Thanks for reading,