Skip to content

Instantly share code, notes, and snippets.

@mmourafiq
Created December 19, 2012 23:55
Show Gist options
  • Save mmourafiq/4341853 to your computer and use it in GitHub Desktop.
Save mmourafiq/4341853 to your computer and use it in GitHub Desktop.
Calculating the sharp ratio for AAPL
import scipy
from yahoo_finance import YahooFinance
VALUE_TYPE = {'Date': 0, 'Open': 1, 'High': 2, 'Low': 3, 'Close': 4, 'Volume': 5, 'Adj_Close': 6}
start_date = "20121101"
end_date = "20121201"
yf = YahooFinance("AAPL")
for (symb, data) in yf.get_historical_prices(start_date, end_date): #get historical data from yahoo finance
data.reverse() #reverse the time series
data.pop() #get rid of the header
"""
our data look like this :
['Date' 'Open' 'High' 'Low' 'Close' 'Volume' 'Adj Clos']
['2012-11-01' '598.22' '603.00' '594.17' '596.54' '12903500' '593.8']
['2012-11-02' '595.89' '596.95' '574.75' '576.80' '21406200' '574.1']
['2012-11-05' '583.52' '587.77' '577.60' '584.62' '18897700' '581.9']
['2012-11-06' '590.23' '590.74' '580.09' '582.85' '13389900' '580.2']
['2012-11-07' '573.84' '574.54' '555.75' '558.00' '28344600' '558.0']
['2012-11-08' '560.63' '562.23' '535.29' '537.75' '37719500' '537.7']
['2012-11-09' '540.42' '554.88' '533.72' '547.06' '33211200' '547.0']
['2012-11-12' '554.15' '554.50' '538.65' '542.83' '18421500' '542.8']
['2012-11-13' '538.91' '550.48' '536.36' '542.90' '19033900' '542.9']
['2012-11-14' '545.50' '547.45' '536.18' '536.88' '17041800' '536.8']
['2012-11-15' '537.53' '539.50' '522.62' '525.62' '28211100' '525.6']
['2012-11-16' '525.20' '530.00' '505.75' '527.68' '45246200' '527.6']
['2012-11-19' '540.71' '567.50' '539.88' '565.73' '29404200' '565.7']
['2012-11-20' '571.91' '571.95' '554.58' '560.91' '22955500' '560.9']
['2012-11-21' '564.25' '567.37' '556.60' '561.70' '13321500' '561.7']
['2012-11-23' '567.17' '572.00' '562.60' '571.50' '9743800' '571.5']
['2012-11-26' '575.90' '590.00' '573.71' '589.53' '22520700' '589.5']
['2012-11-27' '589.55' '590.42' '580.10' '584.78' '19047500' '584.7']
['2012-11-28' '577.27' '585.80' '572.26' '582.94' '18602300' '582.9']
['2012-11-29' '590.22' '594.25' '585.25' '589.36' '18382100' '589.3']
['2012-11-30' '586.79' '588.40' '582.68' '585.28' '13975700' '585.2']
"""
#since we are only interested by the Adjusted Closing Price
adj_prices = scipy.array(map(lambda x : float(x[VALUE_TYPE['Adj_Close']]), data))
>> [ 593.8 574.1 581.9 580.2 558. 537.7 547. 542.8 542.9 536.8
525.6 527.6 565.7 560.9 561.7 571.5 589.5 584.7 582.9 589.3
585.2]
total_return = (adj_prices[-1]/adj_prices[0]) - 1 #calculate the total return for apple this month
>> -0.014482990906
daily_return = (adj_prices[1:]/adj_prices[0:-1]) - 1 #calculate the daily_return
>> [-0.03317615 0.01358648 -0.00292146 -0.03826267 -0.03637993 0.01729589
-0.00767824 0.00018423 -0.01123596 -0.02086438 0.00380518 0.0722138
-0.00848506 0.00142628 0.01744704 0.03149606 -0.00814249 -0.0030785
0.01097958 -0.00695741]
avg_daily_return = scipy.mean(daily_return, 0) #average daily retuen
>> -0.000437386088059
std_daily_return = scipy.std(daily_return, 0) #deviation fo daily return
>> 0.0243327696358
sharp_ratio = scipy.sqrt(250) * (avg_daily_return / std_daily_return) # sharp ratio
>> -0.284212663794
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment