def test_ret(self): # Variables array = np.array([1, 2, 3, 4, 5]) array_2 = np.array([5, 4, 3, 2, 1]) matrix = np.array([array, array_2]).T series = pd.Series(array, index=[5, 7, 8, 10, 11]) time_series = pd.Series(array) df = pd.DataFrame(matrix, columns=['c1', 'c2'], index=[5, 7, 8, 10, 11]) # Input is numpy.ndarray of 1 dimmension => float ans = Calculator.ret(array) self.assertFloat(ans) self.assertEqual(ans, 4) # Input is numpy.ndarray of 2 dimmensions => np.ndarray ans = Calculator.ret(matrix) self.assertArray(ans) self.assertEqual(ans, np.array([4, -0.8])) # Input is pandas.Series => float ans = Calculator.ret(series) self.assertFloat(ans) self.assertEqual(ans, 4) # Input is pandas.TimeSeries => float ans = Calculator.ret(time_series) self.assertFloat(ans) self.assertEqual(ans, 4) # Input is pandas.DataFrame with col parameter => float ans = Calculator.ret(df, col='c1') self.assertFloat(ans) self.assertEqual(ans, 4) # -- ans = Calculator.ret(df, col='c2') self.assertFloat(ans) self.assertEqual(ans, -0.8) # Input is pandas.DataFrame without col parameter => Return pd.Series ans = Calculator.ret(df) self.assertSeries(ans) sol = pd.Series([4, -0.8], index=['c1', 'c2'], name='Total Returns') self.assertEqual(ans, sol)
def test_ret(self): # Variables array = np.array([1,2,3,4,5]) array_2 = np.array([5,4,3,2,1]) matrix = np.array([array, array_2]).T series = pd.Series(array, index=[5,7,8,10,11]) time_series = pd.Series(array) df = pd.DataFrame(matrix, columns=['c1', 'c2'], index=[5,7,8,10,11]) # Input is numpy.ndarray of 1 dimmension => float ans = Calculator.ret(array) self.assertFloat(ans) self.assertEqual(ans, 4) # Input is numpy.ndarray of 2 dimmensions => np.ndarray ans = Calculator.ret(matrix) self.assertArray(ans) self.assertEqual(ans, np.array([4, -0.8])) # Input is pandas.Series => float ans = Calculator.ret(series) self.assertFloat(ans) self.assertEqual(ans, 4) # Input is pandas.TimeSeries => float ans = Calculator.ret(time_series) self.assertFloat(ans) self.assertEqual(ans, 4) # Input is pandas.DataFrame with col parameter => float ans = Calculator.ret(df, col='c1') self.assertFloat(ans) self.assertEqual(ans, 4) # -- ans = Calculator.ret(df, col='c2') self.assertFloat(ans) self.assertEqual(ans, -0.8) # Input is pandas.DataFrame without col parameter => Return pd.Series ans = Calculator.ret(df) self.assertSeries(ans) sol = pd.Series([4, -0.8], index=['c1', 'c2'], name='Total Returns') self.assertEqual(ans, sol)
# Create the data data = [['December, 2004', 31.18], ['January, 2005', 27.00],['February, 2005', 25.91], ['March, 2005', 25.83],['April, 2005', 24.76],['May, 2005', 27.40], ['June, 2005', 25.83],['July, 2005', 26.27],['August, 2005', 24.51], ['September, 2005', 25.05],['October, 2005', 28.28],['November, 2005', 30.45], ['December, 2005', 30.51]] starbucks = pd.DataFrame(data, columns=['Date', 'Value']).set_index('Date')['Value'] ''' Question 1: Using the data in the table, what is the simple monthly return between the end of December 2004 and the end of January 2005? Ans: -13.40% ''' q1 = Calculator.ret(starbucks, pos=1) # q1 = Calculator.R(PV=data[0][1], FV=data[1][1]) # Other option print(1, q1) ''' Question 2: If you invested $10,000 in Starbucks at the end of December 2004, how much would the investment be worth at the end of January 2005? Ans: $8659.39 ''' q2 = Calculator.FV(PV=10000, R=q1) print(2, q2) ''' Question 3: Using the data in the table, what is the continuously compounded monthly return between December 2004 and January 2005? Ans: -14.39% '''
from datetime import datetime import matplotlib.pyplot as plt from finance.utils import Calculator from finance.sim import MarketSimulator # from finance.utils import DataAccess # DataAccess.path = 'data' sim = MarketSimulator() sim.initial_cash = 1000000 sim.load_trades("MarketSimulator_orders.csv") sim.simulate() print(sim.portfolio[0:10]) print('Total Return:', Calculator.ret(sim.portfolio)) print(Calculator.sharpe_ratio(sim.portfolio)) sim.portfolio.plot() # plt.grid(True) plt.show()
plt.show() # Question 9 W0 = 100000 R = stats.norm(loc=0.04, scale=0.09) print(9, W0 * R.ppf(0.01), W0 * R.ppf(0.05)) # Question 10 W0 = 100000 r = stats.norm(loc=0.04, scale=0.09) r_1, r_5 = r.ppf(0.01), r.ppf(0.05) R_1, R_5 = math.exp(r_1) - 1, math.exp(r_5) - 1 print(10, W0 * R_1, W0 * R_5) # Question 11 q11_amzn = Calculator.ret([38.23, 41.29]) # q11_amzn = Calculator.R(PV=38.23, FV=41.29) # Other option q11_cost = Calculator.ret([41.11, 41.74]) print(11, q11_amzn, q11_cost) # Question 12 q12_amzn = Calculator.ret([38.23, 41.29], cc=True) q12_cost = Calculator.ret([41.11, 41.74], cc=True) print(12, q12_amzn, q12_cost) # Question 13 q13_amzn = Calculator.ret([38.23, 41.29], dividends=[0, 0.1]) print(13, q13_amzn, 0.1 / 41.29) print(13, (41.29 + 0.1) / 38.23 - 1, 0.1 / 41.29) # Question 14
plt.show() # Question 9 W0 = 100000 R = stats.norm(loc=0.04, scale=0.09) print(9, W0 * R.ppf(0.01), W0 * R.ppf(0.05)) # Question 10 W0 = 100000 r = stats.norm(loc=0.04, scale=0.09) r_1, r_5 = r.ppf(0.01), r.ppf(0.05) R_1, R_5 = math.exp(r_1) - 1, math.exp(r_5) - 1 print(10, W0 * R_1, W0 * R_5) # Question 11 q11_amzn = Calculator.ret([38.23, 41.29]) # q11_amzn = Calculator.R(PV=38.23, FV=41.29) # Other option q11_cost = Calculator.ret([41.11, 41.74]) print(11, q11_amzn, q11_cost) # Question 12 q12_amzn = Calculator.ret([38.23, 41.29], cc=True) q12_cost = Calculator.ret([41.11, 41.74], cc=True) print(12, q12_amzn, q12_cost) # Question 13 q13_amzn = Calculator.ret([38.23, 41.29], dividends=[0, 0.1]) print(13, q13_amzn, 0.1/41.29) print(13, (41.29 + 0.1)/38.23 - 1, 0.1/41.29) # Question 14
da = DataAccess() # Question 1 symbols = ['SBUX'] start_date = datetime(1993, 3, 31) end_date = datetime(2008, 3, 31) fields = 'adjusted_close' data = da.get_data(symbols, start_date, end_date, fields) monthly = data.asfreq('M', method='ffill') monthly.plot() plt.title('Montly Data') plt.draw() # Question 2 and 3 total_return = Calculator.ret(data) q2 = Calculator.FV(PV=10000, R=total_return) print(2, q2) # Question 3 q3 = Calculator.ann_ret(R=total_return, m=1/15) print(3, q3) # Question 4 monthly_ln = monthly.apply(np.log) monthly_ln.plot() plt.title('Montly Natural Logarithm') plt.draw() # Question 5 monthly_returns = Calculator.returns(monthly)
da = DataAccess() # Question 1 symbols = ['SBUX'] start_date = datetime(1993, 3, 31) end_date = datetime(2008, 3, 31) fields = "Adj Close" data = da.get_data(symbols, start_date, end_date, fields) monthly = data.asfreq('M', method='ffill') monthly.plot() plt.title('Montly Data') plt.draw() # Question 2 and 3 total_return = Calculator.ret(data) q2 = Calculator.FV(PV=10000, R=total_return) print(2, q2) # Question 3 q3 = Calculator.ann_ret(R=total_return, m=1 / 15) print(3, q3) # Question 4 monthly_ln = monthly.apply(np.log) monthly_ln.plot() plt.title('Montly Natural Logarithm') plt.draw() # Question 5 monthly_returns = Calculator.returns(monthly)
data = [['December, 2004', 31.18], ['January, 2005', 27.00], ['February, 2005', 25.91], ['March, 2005', 25.83], ['April, 2005', 24.76], ['May, 2005', 27.40], ['June, 2005', 25.83], ['July, 2005', 26.27], ['August, 2005', 24.51], ['September, 2005', 25.05], ['October, 2005', 28.28], ['November, 2005', 30.45], ['December, 2005', 30.51]] starbucks = pd.DataFrame(data, columns=['Date', 'Value']).set_index('Date')['Value'] ''' Question 1: Using the data in the table, what is the simple monthly return between the end of December 2004 and the end of January 2005? Ans: -13.40% ''' q1 = Calculator.ret(starbucks, pos=1) # q1 = Calculator.R(PV=data[0][1], FV=data[1][1]) # Other option print(1, q1) ''' Question 2: If you invested $10,000 in Starbucks at the end of December 2004, how much would the investment be worth at the end of January 2005? Ans: $8659.39 ''' q2 = Calculator.FV(PV=10000, R=q1) print(2, q2) ''' Question 3: Using the data in the table, what is the continuously compounded monthly return between December 2004 and January 2005? Ans: -14.39% ''' q3 = Calculator.ret(starbucks, pos=1, cc=True)