Research paper  algo for call or put options  to Matlab Mupad converted to clean C or C++ Demo Intro
The following is a working derivative of (done in Matlab 2014a)
All source with MUPAD MN, and C resource files included  options
http://www.mathworks.com/products/symbolic/code-examples.html?file=/products/demos/symbolictlbx/exotic_options/exotic-options-from-symbolic-modeling-to-c-code.html
Other video highlights:
At APPROXIMATELY 15:00, I show how to convert from Mupad to a Simulink Function Block for your Simulink
At APPROXIMATELY 21:00, I show how to convert from Mupad to a custom Matlab M function which can also be implemented into a Simulink model
Follow instruction how to move the symbolic expression from Mupad to:
Matlab M function:
http://www.mathworks.com/help/symbolic/create-matlab-functions-from-mupad-expressions.html
Simulink block:
http://www.mathworks.com/help/symbolic/create-matlab-function-blocks-from-mupad-expressions.html
 Join my FREE newsletter to see how we proceed with this new workflow
————–
The Mupad Notebook file (RelativeOptionModel.mn) contains:
N:=stats::normalCDF(0,1)
F:=S1/S2*exp((b1 – b2 + sigma2^2 – rho*sigma1*sigma2)*T)
sigmahat:=sqrt(sigma1^2+sigma2^2-2*rho*sigma1*sigma2):
d1:=(ln(F/X)+sigmahat^2*T/2)/(sigmahat*sqrt(T))
d2:=d1-sigmahat*sqrt(T)
Call:=exp(-r*T)*(F*N(d1)-X*N(d2))
Put:=exp(-r*T)*(F*N(d2)-X*N(d1))
S1
S2
S1
———–
Matlab command history:
>> clear all
>> model=’RelativeOptionModel.mn’
model =
RelativeOptionModel.mn
>> nb = mupad(model);
>> evaluateMuPADNotebook(nb)
ans =
1
>> getVar(nb,’S1′)
ans =
S1
>> S1=getVar(nb,’S1′)
S1 =
S1
>> Put=getVar(nb,’Put)
Put=getVar(nb,’Put)
|
Error: String is not terminated properly.
>> Put=getVar(nb,’Put’)
Put =
-exp(-T*r)*(X*(erf((2^(1/2)*(log((S1*exp(T*(sigma2^2 – rho*sigma1*sigma2 + b1 – b2)))/(S2*X)) + (T*(sigma1^2 – 2*rho*sigma1*sigma2 + sigma2^2))/2))/(2*T^(1/2)*(sigma1^2 – 2*rho*sigma1*sigma2 + sigma2^2)^(1/2)))/2 + 1/2) + (S1*exp(T*(sigma2^2 – rho*sigma1*sigma2 + b1 – b2))*(erf((2^(1/2)*(T^(1/2)*(sigma1^2 – 2*rho*sigma1*sigma2 + sigma2^2)^(1/2) – (log((S1*exp(T*(sigma2^2 – rho*sigma1*sigma2 + b1 – b2)))/(S2*X)) + (T*(sigma1^2 – 2*rho*sigma1*sigma2 + sigma2^2))/2)/(T^(1/2)*(sigma1^2 – 2*rho*sigma1*sigma2 + sigma2^2)^(1/2))))/2)/2 – 1/2))/S2)
>> Call=getVar(nb,’Call’)
Call =
exp(-T*r)*(X*(erf((2^(1/2)*(T^(1/2)*(sigma1^2 – 2*rho*sigma1*sigma2 + sigma2^2)^(1/2) – (log((S1*exp(T*(sigma2^2 – rho*sigma1*sigma2 + b1 – b2)))/(S2*X)) + (T*(sigma1^2 – 2*rho*sigma1*sigma2 + sigma2^2))/2)/(T^(1/2)*(sigma1^2 – 2*rho*sigma1*sigma2 + sigma2^2)^(1/2))))/2)/2 – 1/2) + (S1*exp(T*(sigma2^2 – rho*sigma1*sigma2 + b1 – b2))*(erf((2^(1/2)*(log((S1*exp(T*(sigma2^2 – rho*sigma1*sigma2 + b1 – b2)))/(S2*X)) + (T*(sigma1^2 – 2*rho*sigma1*sigma2 + sigma2^2))/2))/(2*T^(1/2)*(sigma1^2 – 2*rho*sigma1*sigma2 + sigma2^2)^(1/2)))/2 + 1/2))/S2)
>> call_ROP = matlabFunction(Call,’file’,’Call_ROP.m’);
>> put_ROP = matlabFunction(Call,’file’,’Put_ROP.m’);
—–
Some Code Generation to C (generates yucky MEX file)
>> codegen Call_ROPÂ -args {0.07,130.00,100.0,1.0,0.3,0.4,0.25,0.5,0.05,0.03}
>> codegen Put_ROPÂ -args {0.07,130.00,100.0,1.0,0.3,0.4,0.25,0.5,0.05,0.03}
Using ccode
>> ccode(Call,’file’,’CallC’)
Warning: Function “erf” is not verified to be a valid C function.
Warning: Function “erf” is not verified to be a valid C function.
Join my FREE newsletter to see how we proceed with this new workflow
NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!