def __init__(self, portfolio, market_data, time_index): ''' Constructor ''' self.md_slice = md.market_data_slice(market_data, time_index) self.portfolio = portfolio self.time_index = time_index
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 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")
#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 #Setup portfolio tradeEquity = td.TradeEquity(name = "Equity", notional = 1,
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()
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) print MDSlice1.data print MDSlice1.time_stamp #Create Trades and value trade_eq = td.Trade("TestEq","Eq", 100) trade_cash = td.Trade("TestEq","Cash", 100) trade_eq.price(MDSlice1) trade_cash.price(MDSlice1) trade_eq.value(MDSlice1) trade_cash.value(MDSlice1) #Create Portfolio and value port_test = pt.Portfolio("port_test") port_test.add_trade(trade_eq)