def build_trd(self): cols = [ "CPX", "PPX", "CDL", "PDL", "GAM", "VEG", "CTH", "PTH", "VOL", "CHS", "PHS", "CEP", "PEP", "VOM", "VAN" ] opt, stk = self.join_search(cols) opt_LEG = opt[:, 2] opt_TNR = opt[:, 5].astype(int) opt_SID = np.where(opt[:, 0] < 0, -1, 1) opt_RGT = opt[:, 1] opt_SIZ = opt[:, 0].astype(int) opt_STR = opt[:, 4] opt_PPX = np.where(opt_RGT == 1, opt[:, 6], opt[:, 7]) * -opt_SIZ opt_DEL = np.where(opt_RGT == 1, opt[:, 8], opt[:, 9]) * opt_SIZ opt_GAM = opt[:, 10] * opt_SIZ opt_VEG = opt[:, 11] * opt_SIZ opt_THT = np.where(opt_RGT == 1, opt[:, 12], opt[:, 13]) * opt_SIZ opt_VOL = opt[:, 14] opt_SPR = np.where(opt_RGT == 1, opt[:, 15], opt[:, 16]) * -abs(opt_SIZ) opt_EEP = np.where(opt_RGT == 1, opt[:, 17], opt[:, 18]) * -opt_SIZ opt_VOM = opt[:, 19] * opt_SIZ opt_VAN = opt[:, 20] * opt_SIZ opt_out = np.array([ opt_LEG, opt_TNR, opt_SID, opt_RGT, opt_SIZ, opt_STR, opt_PPX, opt_DEL, opt_GAM, opt_VEG, opt_THT, opt_VOM, opt_VAN, opt_VOL, opt_SPR, opt_EEP ]) stk_LEG = stk[:, 1] stk_TNR = np.zeros(stk.shape[0]).astype(int) stk_SID = np.where(stk[:, 4] < 0, -1, 1) stk_RGT = stk[:, 0] stk_SIZ = stk[:, 4].astype(int) stk_nan = np.zeros(stk.shape[0]) stk_PPX = np.where(stk_LEG == 1, self.get_raw(0, "SPT", 1)[0], self.get_raw(0, "SPT", 0)[0]) * -stk_SIZ stk_DEL = stk[:, 4].astype(float) stk_out = np.array([ stk_LEG, stk_TNR, stk_SID, stk_RGT, stk_SIZ, stk_nan, stk_PPX, stk_DEL, stk_nan, stk_nan, stk_nan, stk_nan, stk_nan, stk_nan, stk_nan, stk_nan ]) out = np.vstack([opt_out.T, stk_out.T]) out = utils.sort_array(out, (1, 3, 0)) head = [ "LEG", "TNR", "SID", "RGT", "SIZ", "STR", "PPX", "DEL", "GAM", "VEG", "THT", "VOM", "VAN", "VOL", "SPR", "EEP" ] dtyp = [ int, int, int, int, int, float, float, float, float, float, float, float, float, float, float, float ] dtype = np.dtype([(x, y) for x, y in zip(head, dtyp)]) return struct(out, dtype=dtype)
def pop(self,leg=0): rs,fs = self.rs,self.fs if leg==0: rs[fct_cols][:,0] = self.params.loc[rs["IDX"][:,0],fct_cols].to_records(index=False) rs[fct_cols][:,1] = self.params.loc[rs["IDX"][:,0],fct_cols].to_records(index=False) self.rs = rs self.build_raw(0) else: rs[fct_cols[2:]][:,1] = struct( #Have to convert to unstructured array for broadcasting across cols of the structure (unstruct(rs[fct_cols[2:]][:,0]) * (1 + unstruct(fs[fct_cols[2:]][rs["IDX"][:,0]]))), dtype = (rs[fct_cols[2:]][:,1].dtype) ) rs[fct_cols[:2]][:,1] = struct( (unstruct(rs[fct_cols[:2]][:,0]) - unstruct(fs[fct_cols[:2]][rs["IDX"][:,0]])), dtype = (rs[fct_cols[:2]][:,1].dtype) ) self.rs = rs self.build_raw(1)
def build_pay(self): self.prg_pay.val += 0.1 cols = ["CDD","BOR","DIV","FRT","YLD","VOL","CH0","CH1","CKH", "PH0","PH1","PKH","CPX","CEP","CHS","PPX","PEP","PHS"] opt,stk = self.join_search(cols) x = np.zeros((opt.shape[0],2)) #pct move, spot st = np.hstack([x,opt]) #Expanding dimension for broadcasting delta vals across the depth st = np.repeat(st[np.newaxis,:,:],self.pct_moves.shape[0],axis=0) st[:,:,0] = np.repeat(self.pct_moves[:,np.newaxis],st.shape[1],axis=1) st[:,:,1] = self.get_raw(0,"SPT",0)[0] * (1 + st[:,:,0]) #Scls = Sopn * (1 + d%) st = st.reshape((st.shape[0]*st.shape[1]),st.shape[2]); self.prg_pay.val += 0.1 head = ["PCT","SPT","SIZ","RGT","LEG","IDX","STR","TNR"] + cols dtype = np.dtype([(x,float) for x in head]) st = struct(st,dtype=dtype) o = np.where(st["LEG"]==0)[0] #open idx c = np.where(st["LEG"]==1)[0] #cls idx st["CDD"] = st["CDD"]/base st["TNR"] = st["TNR"]/base st["CKH"][c] = st["CKH"][c] * (1 + st["PCT"][c]) st["PKH"][c] = st["PKH"][c] * (1 + st["PCT"][c]) qargs = (st["SPT"][c],st["TNR"][c],st["DIV"][c],st["CDD"][c],st["FRT"][c],) st["YLD"][c] = (pricer.dsc2yld(*qargs) + st["BOR"][c]) #to adjust borrow opx = np.where(st["RGT"][o]==1,st["CPX"][o],st["PPX"][o]) * -st["SIZ"][o] oep = np.where(st["RGT"][o]==1,st["CEP"][o],st["PEP"][o]) * -st["SIZ"][o] ohs = np.where(st["RGT"][o]==1,st["CHS"][o],st["PHS"][o]) * -abs(st["SIZ"][o]); self.prg_pay.val += 0.1 cargs = ("C",st["SPT"][c],st["STR"][c],st["TNR"][c],st["VOL"][c],st["FRT"][c], st["YLD"][c],st["CH0"][c],st["CH1"][c],st["CKH"][c]) pargs = ("P",st["SPT"][c],st["STR"][c],st["TNR"][c],st["VOL"][c],st["FRT"][c], st["YLD"][c],st["PH0"][c],st["PH1"][c],st["PKH"][c]) cpx,cep = np.where( #Calc close px st["RGT"][c]==1, pricer.ThreadPool(*cargs).run(), pricer.ThreadPool(*pargs).run() ) * -st["SIZ"][c]; self.prg_pay.val += 0.1 chs = np.where( #Calc close spread cost st["RGT"][c]==1, pricer.halfspread(st["STR"][c],st["CH0"][c],st["CH1"][c],st["CKH"][c],"C"), pricer.halfspread(st["STR"][c],st["PH0"][c],st["PH1"][c],st["PKH"][c],"P") ) * -abs(st["SIZ"][c]); self.prg_pay.val += 0.1 cpf = np.where( #Calc payoff st["RGT"][c]==1, np.maximum(st["SPT"][c]-st["STR"][c],0), np.maximum(st["STR"][c]-st["SPT"][c],0) ) * -st["SIZ"][c]; self.prg_pay.val += 0.1 #New array, expand back to higher dimension by pct move liq = np.stack([opx,oep,ohs,cpx,cep,chs],axis=1) #Liquidation liq = np.reshape(liq,(self.pct_moves.shape[0],int(opt.shape[0]/2),liq.shape[1])) exr = np.stack([opx,oep,ohs,cpf],axis=1) #Exercise exr = np.reshape(exr,(self.pct_moves.shape[0],int(opt.shape[0]/2),exr.shape[1])); self.prg_pay.val += 0.1 if len(stk) != 0: #Spot returns, to add funding to short opn_size = stk[np.where(stk[:,1]==0)[0],4] cls_size = stk[np.where(stk[:,1]==1)[0],4] spr = np.zeros((self.pct_moves.shape[0],2)) #2 cols: open & close cash flows spr[:,0] = (self.get_raw(0,"SPT",0)[0] * -opn_size) spr[:,1] = (self.get_raw(0,"SPT",0)[0] * (1 + self.pct_moves) * -cls_size) totspr = spr[:,0] + spr[:,1] else: totspr = np.zeros((self.pct_moves.shape[0],)) ps = self.ps; self.prg_pay.val += 0.1 liq = np.sum(liq,axis=1) #Liquidation returns exr = np.sum(exr,axis=1) #Exercise returns ps["LQPX"] = liq[:,0] + liq[:,3] ps["LQEP"] = liq[:,1] + liq[:,4] ps["LQHS"] = liq[:,2] + liq[:,5] ps["EXPX"] = exr[:,0] + exr[:,3] ps["EXEP"] = exr[:,1] ps["EXHS"] = exr[:,2] ps["SPTR"] = totspr self.ps = ps; self.prg_pay.val += 0.1