/* PAYOFF.G Procedure to obtain payoff,profit and return profiles to various option strategies, which depends upon value of underlying at expiration Format: {pay,profit,return}=payoff(xmat,cmat,svals) txm txm txm mxk mxk tx1 Optional: _ncont Number of contracts involved (conformable with xmat&cmat) txm (default=ones for all contracts) where: xmat strike values (positive for calls, negative for puts) cvec option prices (positive for purchases, negative for sales) svals range of stock prices to evaluates profiles on m is the number of stategies to consider k is the number of options involved in the strategy (can be padded with zeros for passing strategies involving diff. numbers of options) t is the number of underlying asset values to consider _cop Global output - cost of each position */ proc(3)=payoff(xmat,cmat,svals); local t,m,payoff,profit,ret,i,nopt,bsdir; #include payoff.dec; t=rows(svals); m=rows(xmat); payoff=zeros(t,m); profit=zeros(t,m); ret=zeros(t,m); _cop=zeros(m,1); if _ncont==-99; _ncont=(abs(xmat).>0); endif; i=1; do while i le m; nopt=sumc(xmat[i,.]'.ne 0); _cop[i]=sumc((_ncont[i,1:nopt].*cmat[i,1:nopt])'); /* cost of position */ bsdir=(cmat[i,1:nopt].>0)-(cmat[i,1:nopt].<0); /* buy (+1) sell (-1) */ payoff[.,i]=sumc(((bsdir).*(xmat[i,1:nopt].>0).*(_ncont[i,1:nopt]).* (svals.>xmat[i,1:nopt]).*(svals-xmat[i,1:nopt]))'); payoff[.,i]=payoff[.,i]+sumc((bsdir.*(xmat[i,1:nopt].<0).* (_ncont[i,1:nopt]).*(svals.