Example #1
0
    def test_1_6(self):
        daily_returns = self.get_daily_returns()
        stocks().plot_data(daily_returns,
                           title='Daily returns')  #, ylabel='Daily returns')

        daily_returns.hist(bins=20)
        plt.show()

        daily_returns['SPY'].hist(bins=20, label='SPY')
        daily_returns['XOM'].hist(bins=20, label='XOM')
        daily_returns['GLD'].hist(bins=20, label='GLD')
        plt.legend(loc='upper right')
        plt.show()
Example #2
0
 def test_1_7_sharp_rate(self):
     #Sharper rate
     s = stocks()
     df = s.get_datas(['SPY', 'MSFT'],
                      pd.date_range('2012-01-01', '2012-12-30'))
     daily_returns = s.compute_daily_returns(df)
     s.plot_data(daily_returns)
Example #3
0
 def get_daily_returns(self):
     s = stocks()
     dates = pd.date_range('2009-01-01', '2012-12-31')
     symbols = ['SPY', 'XOM', 'GLD']
     df = s.get_datas(symbols, dates)
     #self.plot_data(df)
     return s.compute_daily_returns(df)
Example #4
0
 def test_cache(self):
     """
     need implement a cache for visited stocks
     :return:
     """
     s = stocks()
     s.get_data("MSFT")
     self.assertTrue(s.is_cached("MSFT"))
Example #5
0
 def test_incompletdata_01_05(self):
     s = stocks()
     s.get_data('JAMN',
                start=datetime(2005, 12, 31),
                end=datetime(2014, 12, 07))
     spy = s.get_data('SPY')
     print('-----------------')
     print(spy)
     pass
Example #6
0
 def pullData(self, stock):
     """
     use pandas datareader to read history stock data from yahoo
     :param stock: stock symbol
     :return: None
     """
     s = stocks()
     currstock = s.get_data(stock)
     print(len(currstock))
Example #7
0
def build_stocks(symbols):
    from stocks import stocks

    stock = stocks()

    for i in symbols:
        stock.add(i)

    return stock
Example #8
0
 def test_1_7_exercises(self):
     #portfolio values, cumulative return, average daily return, sharp_rate
     allocations = {'SPY': 0.4, 'XOM': 0.4, 'MSFT': 0.1, 'IBM': 0.1}
     buy_date = datetime(2012, 1, 1)
     s = stocks()
     dates = pd.date_range('2012-01-01', '2012-12-20')
     df = s.get_datas(allocations.keys(), dates)
     df = df.div(
         df.ix[0]
     )  #http://stackoverflow.com/questions/12007406/python-pandas-divide-dataframe-by-first-row
     df = 1000000 * df.multiply([0.4, 0.4, 0.1, 0.1])  #allocations
     daily_port_vals = df.sum(axis=1)
     s.plot_data(daily_port_vals)
Example #9
0
 def test_plot_spy(self):
     s = stocks()
     spydata = s.get_data('SPY')
     #print(type(spydata))
     print(spydata.axes)
     #spydata.plot()
     #plt.show()
     dates = pd.date_range('2012-01-01', '2012-12-31')
     df = pd.DataFrame(index=dates)
     print(df.axes)
     spydata.rename(columns={'Adj Close': 'SPY'})
     df.join(spydata)
     spydata.to_csv('data/{}.csv'.format('SPY'))
     spydata.plot()
     plt.show()
Example #10
0
 def assess_portfolio(self, sd, ed, syms, allocs, sv, rfr, sf, gen_plot):
     """
     http://quantsoftware.gatech.edu/MC1-Project-1#API_specification
     :param sd: A datetime object that represents the start date
     :param ed: A datetime object that represents the end date
     :param symb: A list of symbols that make up the portfolio (note that your code should support any symbol in the data directory)
     :param allocs: A list of allocations to the stocks, must sum to 1.0
     :param sv: Start value of the portfolio
     :param rfr: The risk free return per sample period for the entire date range (a single number, not an array).
     :param sf: Sampling frequency per year
     :param gen_plot: If True, create a plot named plot.png
     :return: (cr, adr, sddr, sr, ev)
             cr: Cumulative return
             adr: Average period return (if sf == 252 this is daily return)
             sddr: Standard deviation of daily return
             sr: Sharpe ratio
             ev: End value of portfolio
     """
     symbols = list(syms)
     s = stocks()
     dates = pd.date_range(sd,ed)
     df = s.get_datas(syms,dates)
     df = df.div(df.ix[0])
     df = df.multiply([1.0] + allocs)
     df['portfolio'] = df[symbols].sum(axis=1)
     df.drop(symbols, axis=1, inplace=True)
     #s.plot_data(df)
     cr = df['portfolio'][-1] - 1
     adr = cr / len(df)
     df = df * sv
     #s.plot_data(df)
     daily_return = s.compute_daily_returns(df)
     sddr = daily_return.std()['portfolio']
     sr = np.sqrt(sf) * (daily_return['portfolio'].mean() - rfr) / sddr
     ev = df['portfolio'][-1]
     return (cr, adr, sddr, sr, ev)
Example #11
0
 def test_get_cmcm(self):
     s = stocks()
     s.get_data('CMCM')
     self.assertTrue(s.is_cached('CMCM'))
Example #12
0
 def test_data_to_csv(self):
     import os
     s = stocks()
     s.get_data_to_csv("MSFT")
     self.assertTrue(os.path.isfile(s.symbol_to_path("MSFT")))
 def test1(self):
     self.assertEqual(stocks.stocks([9.5, 3.4, 3.3, 5.6, 7.0], 50), [3.3, 7.0])
 def test3(self):
     self.assertEqual(stocks.stocks([100.3, 4.5, 34.5, 23.4, 6.8, 19.2], 0.7), [])
 def test2(self):
     self.assertEqual(stocks.stocks([2.9, 3.0, 5.0, 3.4, 2.2, 4.4], 3), [2.2, 4.4])