def setUp(self): # setup market data# # First setup data as list data = np.array([1, 0.05, 100, 0.1]) data.resize(1, 4) # Then converst to types needed for pricing columns = ["zero", "rate", "underlying", "vol"] data = pd.DataFrame(data, columns=columns) self.md_for_option = md.market_data(data) self.testdir = "/home/phcostello/Documents/workspace/FinancePython/strategy_tester/test/" self.SP500 = pickle.load(open(self.testdir + "SP500.pkl")) self.md_SP500 = md.market_data(self.SP500)
def setUp(self): #setup market data# #First setup data as list data = np.array([1,0.05,100,0.1]) data.resize(1,4) #Then converst to types needed for pricing columns = ["zero", "rate", "underlying", "vol"] data = pd.DataFrame(data, columns = columns) md_cash = md.market_data(data) #need to add to self to use in test functions self.md_slice = md.market_data_slice(md_cash,time_index=0)
def test_addTechnical(self): '''Test that adding simple technical indicators''' md = market_data(self.SP500) #Test simple moving average sma = pd.rolling_mean(self.SP500['Adj_Close'], 10) md.addSMA('Adj_Close','sma',10) self.assertTrue((sma[10:] == md['sma'][10:]).all(), 'Add simple ma wrong') #Test adding exponetially weighted moving average works ema = pd.ewma(self.SP500['Adj_Close'], 10) md.addEMA('Adj_Close','ema', win_length=10) self.assertTrue((ema[10:] == md['ema'][10:]).all()) #Test adding rolling stdev average works stdev = pd.rolling_std(self.SP500['Adj_Close'], 10) md.addSDev('Adj_Close','stdev', win_length=10) self.assertTrue((stdev[11:] == md['stdev'][11:]).all())
def setUp(self): r = 0.05 vol = 0.1 S = 100.0 #setup market data# #First setup data as list data = np.array([r,S,vol]) data.resize(1,3) #Then convert to types needed for pricing columns = ["rate", "underlying", "vol"] data = pd.DataFrame(data, columns = columns) self.md1 = md.market_data(data) #need to add to self to use in test functions self.md_slice = md.market_data_slice(self.md1,time_index=0) #Setup vanilla option trade self.tradeCall = td.TradeVanillaEuroCall(name = "Call", notional = 0, strike = 100, expiry = 0.5) #Setup portfolio self.tradeEquity = td.TradeEquity(name = "Equity", notional = 0, price_series_label = "underlying") self.port1 = pf.Portfolio("port1")
#class MyClass(object): # ''' # classdocs # ''' # # # def __init__(selfparams): # ''' # Constructor # ''' import strategy_tester.market_data as md import GenerateData as gd import matplotlib.pyplot as plt import pandas as pd paths = [ gd.GenerateLogNormalTS(100, 0.05, 0.1, 100).get_data() for i in range(0,100)] path1 = gd.GenerateLogNormalTS(100, 0.05, 0.1, 100).get_data() path1 = pd.DataFrame(path1) md1 = md.market_data(path1) #[plt.plot(path) for path in paths ] #plt.show()
K = 100.0 #setup market data# #First setup data as list data = np.array([r,S,vol]) data.resize(1,3) #Then convert to types needed for pricing columns = ["rate", "underlying", "vol"] data = pd.DataFrame(data, columns = columns) md1 = md.market_data(data) #need to add to self to use in test functions md_slice = md.market_data_slice(md1,time_index=0) #Setup vanilla option trade tradeCall = td.TradeVanillaEuroCall(name = "Call", notional = 1, strike = 100, expiry = 0.5) price = tradeCall.price(md_slice) print "price = ", price delta = tradeCall.delta(md_slice) print "delta = ", delta
def test_MD_readcsv_constructor(self): #Make sure that not constructing from allowable type throws error with self.assertRaises(NameError): market_data(self.AORD)
def DeltaHedgeVanillaCallEg(): steps = 3000 stepsize = 1.0/steps r = 0.05 dividend = 0.0 vol = 0.2 S0 = 50.0 t0 = 0.0 expiry = 1.0 K = 50.0 #setup market data# #Generate Series rseries = steps*[r] dividendseries = steps*[dividend] volseries = steps*[vol] underlyingSeries = gd.GenerateLogNormalTS(S0, mu=0.03, covariance=vol, stepsize=stepsize,steps=steps-1).get_data() data2 = [rseries,dividendseries,volseries, underlyingSeries] data2 = np.array(data2) data2.shape data2 = data2.transpose() data2[1,:] columns = ['rate','dividend','vol','underlying'] data = pd.DataFrame(data2, columns = columns) data.index = list(np.arange(0,steps,dtype='float64')/steps) md1 = md.market_data(data) #need to add to self to use in test functions md_slice = md.market_data_slice(md1,time_index=0) md_slice.data tradeUnderlying = td.TradeEquity('underlying', notional= 0, price_series_label = 'underlying') tradeCall = td.TradeVanillaEuroCall(name = "Call", notional = 0, strike = K, expiry = expiry) price = tradeCall.price(md_slice) print "price = ", price delta = tradeCall.delta(md_slice) print "delta = ", delta #Setup portfolio #First initialise trade type but empty portfolio port1 = pf.Portfolio("port1") port1.add_trade(tradeUnderlying) port1.add_trade(tradeCall) #Second initialise starting value initPort = {'Call':1} port1.adjustNotional(initPort) delta = tradeCall.delta(md_slice) print "delta", delta trade = {'underlying':-delta} port1.adjustNotional(trade) port1Slice = pf.PortfolioSlice(portfolio = port1, market_data= md1, time_index = 0) initHedgPort = {'Call':1, "underlying":-delta} port1Slice.adjustCash(initHedgPort) #addsome cash MoreCash = {'Cash':1} port1.adjustNotional(MoreCash) prt1Val = port1Slice.value() print "Portfolio Value" , prt1Val prt1Del = port1Slice.delta() print "Portfolio Del" , prt1Del ts_deltaHedge = tsc.Delta_Hedging(market_data = md1, portfolio = port1, initial_time_index = 0, stepsize = stepsize) ts_deltaHedge.run_strategy() outfile = open('VanillaCallDelta_strat.pkl','wb') pickle.dump(ts_deltaHedge,outfile) outfile.close() print ts_deltaHedge.result.head(20) print ts_deltaHedge.result.tail(20) print ts_deltaHedge.portfolio.get_notional()
K = 100.0 #setup market data# #First setup data as list data = np.array([r,S,vol]) data.resize(1,3) #Then convert to types needed for pricing columns = ["rate", "underlying", "vol"] data = pd.DataFrame(data, columns = columns) md_cash = md.market_data(data) #need to add to self to use in test functions md_slice = md.market_data_slice(md_cash,time_index=0) #Setup vanilla option trade tradeCall = TradeVanillaEuroCall(name = "Call", notional = 1, strike = 100, expiry = 0.5) price = tradeCall.price(md_slice) print "price = ", price delta = tradeCall.delta(md_slice) print "delta = ", delta
#Need to add params to importr as xts and quantmod have conflicts xts = importr("xts", robject_translations = {".subset.xts": "_subset_xts2", "to.period": "to_period2"}) quantmod = importr('quantmod', robject_translations = {"skeleton.TA": "skeleton_TA2"}) #if __name__ == '__main__': # pass #Read Data AORD = ro.r('read.table("~/Documents/R/StrategyTester/Data/AORD.csv",header=T, sep=",")') AORD = zoo.as_zoo(AORD)#, order_by = ro.r.rownames(AORD)) bool(zoo.is_zoo(AORD)) type(AORD) #Read into ST MarketData type MD1 = md.market_data(AORD) MD1.names[0] type(MD1.core_data) #Plotting with matlibplot df1 = MD1.core_data['AORD.Close'] type(df1) df1.plot() plt.show() MD1.core_data['AORD.Close'][0] MD1.core_data.ix[0] MD1.core_data.index #Take slice of of MD MDSlice1 = md.market_data_slice(MD1,1)