Skip to content

Instantly share code, notes, and snippets.

@davetromp
Created November 18, 2012 16:29
Show Gist options
  • Save davetromp/4106099 to your computer and use it in GitHub Desktop.
Save davetromp/4106099 to your computer and use it in GitHub Desktop.
Get data from yahoo finance
#!/bin/bash
# This will be a little script to pull price data from Yahoo finance in order to run a trading stategy on it.
# this function will be called to collect all needed input variabels
function SetVars(){
echo ticker?
read ticker
echo "timeframe (d/w/m)?" # yahoo provide daily, weekly and monthly ohlc prices
read timeframe
echo "begin date (mm dd yyyy)?"
read beginM beginD beginY
let bMonth=$beginM-1 # yahoo uses 0 for januari up to 11 for december
echo "end date (mm dd yyyy)?"
read endM endD endY
let eMonth=$endM-1 # same as for begin month
}
# this function will be called to download a csv file with the price data from Yahoo finance
# it takes variables from SetVars().
function GetData(){
wget -O pricedata.csv "http://ichart.finance.yahoo.com/table.csv?s=$ticker&d=$eMonth&e=$endD&f=$endY&g=$timeframe&a=$bMonth&b=$beginD&c=$beginY&ignore=.csv pricedata.csv"
awk 'NR>1' pricedata.csv > data.csv # get data without header and put in data.csv
rm pricedata.csv # we do not need the original file with header anymore
}
# this function will loop throug the data file line by line
function loop(){
while read line
do
echo "$line"
done < data.csv | grep -B 9 "2012-08-06" #this will get 9 lines before and including the line with this date. We will be using this to get sets of data to calculate Moving averages etc.
}
SetVars
GetData
loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment