Created
December 9, 2012 22:49
-
-
Save davetromp/4247349 to your computer and use it in GitHub Desktop.
This bash script calculates the sharpratio on yahoo finance data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# this script will read in a ohlc file from yahoo finance line by line | |
# putting every line in a list / array as an element | |
# and then do some basic calculations on the numbers | |
# we will calculate the absolute return, the avarage daily differences, | |
# the standard deviation of the daily differences and finally the sharp ratio | |
fname=$1 | |
readin(){ | |
while read line | |
do | |
filearray+=("$line") | |
done < $fname | |
} | |
readout(){ | |
# initialize some variables we need later for calculations in the loop | |
absret=0 | |
dailydifsq=0 | |
sumofdailydifsq=0 | |
stdev=0 | |
# now let's loop through the array | |
# skip index zero because we use it as base: we cannot compare it to index -1 there is no. | |
for ((i=1; i < ${#filearray[*]}; i++)) | |
do | |
#echo $i | |
daybefore=`(echo ${filearray[$i-1]}) | awk -F , '{print $7}'` | |
today=`(echo ${filearray[$i]}) | awk -F , '{print $7}'` | |
dailydif=$(echo "scale=4;(($today-$daybefore)/$daybefore)*100"|bc) | |
echo "daily difference is $dailydif %" | |
absret=$(echo "scale=4;$absret+$dailydif"|bc) #sum of daily dif = abs return | |
echo "abs return sofar is $absret" | |
dailydifsq=$(echo "scale=4;$dailydif*$dailydif"|bc) # needed for calculating the stdev | |
sumofdailydifsq=$(echo "scale=4;$sumofdailydifsq+$dailydifsq"|bc) # we wanna have the sum of all squared daily difs | |
done | |
avdd=$(echo "scale=4;$absret/$i"|bc) #average dailydiff | |
stdev=$(echo "scale=4;sqrt($sumofdailydifsq/$i)"|bc) # here we use the sum of all daily difs, devide it by the count, and then take the square root. | |
sharpratio=$(echo "scale=4;($avdd/$stdev)*sqrt(252)"|bc) | |
#sharpratio is the avarage daily dif devided by the stdev and then multiplied by the sqrt of 252 for dauly pricing | |
echo "abs return has been $absret%" | |
echo "av daily diff has been $avdd%" | |
echo "stdev of daily diff has been $stdev" | |
echo "the sharpratio has been $sharpratio" | |
} | |
readin | |
readout |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment