def upd_portfolio(self): signal = self.upd_signal() trade_number = 1 trade = None if signal == 'buy': #Create Trade trade = td.Trade(name = "Trade"+str(trade_number), type = "Eq", notional = 1) #Add to portfolio self.portfolio.add_trade(trade) #update cash for trade md_slice = md.market_data_slice(self.market_data, self.time) self.portfolio.trades[0].notional -= trade.value(md_slice) elif signal == 'sell': trade = td.Trade(name = "Trade"+str(trade_number), type = "Eq", notional = -1) #Add to portfolio self.portfolio.add_trade(trade) #update cash for trade md_slice = md.market_data_slice(self.market_data, self.time) self.portfolio.trades[0].notional -= trade.value(md_slice)
def upd_portfolio(self): #Update portfolio by making delta neutral delta = self.upd_signal() #upd portfolio name = "Hedge" + str(self.time) thisHedge = td.TradeEquity(name = name, notional = - delta, price_series_label = 'underlying') self.portfolio.add_trade(thisHedge) #update cash for trade md_slice = md.market_data_slice(self.market_data, self.time) self.portfolio.trades[0].notional -= thisHedge.value(md_slice)
def DeltaHedgeVanillaCallEg(): import numpy as np import pandas as pd import Portfolio as pf import GenerateData as gd import matplotlib.pyplot as plt 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()