Exemplo n.º 1
0
import history
import functions
import matplotlib.pyplot as plt
import numpy as np
# List of symbols
symbols = ['AAPL']
# Create your data frame
df = history.get_csv_data(symbols, '01/01/2007', '01/01/2017')
# Fill all null values
functions.fill_null_values(df)
# Get daily returns
daily_returns = functions.compute_daily_returns(df)
# Generate histogram
daily_returns.hist(bins=20)
mean_spy = daily_returns['SPY'].mean()
std_spy = daily_returns['SPY'].std()

plt.axvline(mean_spy, color="w", linestyle="dashed", linewidth=2)
plt.axvline(std_spy, color="r", linestyle="dashed", linewidth=2)
plt.axvline(-std_spy, color="r", linestyle="dashed", linewidth=2)

# mean_aapl = daily_returns['AAPL'].mean()
# std_aapl = daily_returns['AAPL'].std()
#
# plt.axvline(mean_aapl, color='w', linestyle='dashed', linewidth=2)
# plt.axvline(std_aapl, color='r', linestyle='dashed',linewidth=2)
# plt.axvline(-std_aapl, color='r', linestyle='dashed',linewidth=2)

daily_returns['SPY'].hist(bins=20, label='SPY')
daily_returns['AAPL'].hist(bins=20, label='AAPL')
Exemplo n.º 2
0
import pandas as pd
import matplotlib.pyplot as plt
import scipy.optimize as spo
import numpy as np
import history

df = history.get_csv_data(['AAPL'])
# df['timestamp'] = pd.date_range('2000-1-1', periods=200, freq='D')

print df
import history
import functions
import numpy as np
import math

start_date = "2009-01-01"
end_date = "2012-12-31"
symbols = ['SPY', 'XOM', 'GOOG', 'GLD']
start_value = 1000000
allocations = np.array([0.4, 0.4, 0.1, 0.1])

df = history.get_csv_data(symbols, start_date, end_date)
daily_returns = functions.compute_daily_returns(df)
normed = functions.compute_normed(df)

allocated = functions.position_values(normed, allocations)
position_values = np.array(allocated)
portfolio_values = position_values.sum(axis=1)
daily_returns = daily_returns[1:]
cumulative_returns = (portfolio_values[-1] / portfolio_values[1]) - 1
avg_daily_returns = daily_returns.mean()
std_daily_returns = daily_returns.std()
k = math.sqrt(252)
sharpe_ratio = k * avg_daily_returns / std_daily_returns
print sharpe_ratio
import history as h
import functions
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

start_date = "2009-01-01"
end_date = "2012-12-31"

dates = pd.date_range(start_date, end_date)
symbols = ['SPY', 'XOM', 'GLD']
df = h.get_csv_data(symbols, start_date, end_date)
daily_returns = functions.compute_daily_returns(df)

# IF the beta value is greater that means that that stock is more reactive to the market.
# Scatter plot SPY vs XOM
daily_returns.plot(kind='scatter', x='SPY', y='XOM')
beta, alpha = np.polyfit(daily_returns['SPY'], daily_returns['XOM'], 1)
plt.plot(daily_returns['SPY'],
         beta * daily_returns['SPY'] + alpha,
         '-',
         color='r')
print "XOM Alpha", alpha
print "XOM Beta", beta
plt.show()
# Scatter plot SPY vs GLD
daily_returns.plot(kind='scatter', x='SPY', y='GLD')
beta, alpha = np.polyfit(daily_returns['SPY'], daily_returns['GLD'], 1)
plt.plot(daily_returns['SPY'],
         beta * daily_returns['SPY'] + alpha,
         '-',