# Tag Archives: MuPAD # Steps from Mupad to custom Matlab M function

Steps from Mupad to custom Matlab M function

Here are the steps involved to create an M function from a Mupad session:

(This uses calculating the basis used in futures trading of Cash Price – Future Price)

http://www.mathworks.com/help/symbolic/create-matlab-functions-from-mupad-expressions.html

>> notebook_handle = mupad;
>> basis = getVar(notebook_handle,’basis’)    <–note: do keep Mupad open in order for this to work!!

basis =

FC – FP

>> h = matlabFunction(S)
Undefined function or variable ‘S’.

>> h = matlabFunction(basis)

h =

@(FC,FP)FC-FP

>> basis = matlabFunction(basis,’file’, ‘basis.m’);

Here is the resulting M function basis.m:

function basis = basis(FC,FP)
%BASIS
% BASIS = BASIS(FC,FP)

% This function was generated by the Symbolic Math Toolbox version 6.2.
% 26-Jul-2015 21:13:44

basis = FC-FP;

Join my FREE newsletter as I apply these tools to my automated algo trading

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! # Highlevel intro to Matlab Mupad

Highlevel intro to Matlab Mupad

Just like IPtyhon notebook but more expansive (I think)

Join my FREE newsletter to learn about this powerful tool

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! # 1st step workflow to HFT with Matlab Mupad Over the last week I have been mentioning about this new workflow

from a visual scientific notation trading idea all the way through to a

code generated set of C or C++ set of files for HFT trading potential.

I have compiled two separate videos to showcase this workflow:

1st step workflow to HFT with Matlab Mupad

You can download my entire sample workflow project here:

Workflow From Simulink Visual to C CPP HFT potential pt2

As you know, I have been working on this futures and options trading

course out of UCS Davis University. The next set of steps involving

building out a trading system revolving around all these algorithms

and trading logic. All iterations of this source code will be made

available to my Quant Elite members.

# Matlab Simulink algo for Apple IOS?!?!

As I was doing my most recent research on the technology, I was quite

surprised to find this:

Whoa! Matlab Simulink algo for Apple IOS?!?!

I will even host a Youtube Hangout out to take your questions on this workflow described

above!

Bryan

P.S. Have you seen my latest AK-47 trading system. All the source code has been

posted for all my Quant Elite members.

Get the source code for this here.

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! # 1st step workflow to HFT with Matlab Mupad

1st step workflow to HFT with Matlab Mupad

As hinted at here

https://quantlabs.net/blog/2015/07/workflow-quant-algo-from-research-paper-to-matlab-to-c-c-or-fpga-hdl-for-ultra-lowest-latency-hft/

NOTE: There is a second half with Simulink demos here:

https://quantlabs.net/blog/2015/07/download-workflow-from-simulink-visual-to-c-cpp-hft-potential-pt2/

Video description below

The first step is up on for Mupad with the following steps:

(I can verify the second options work as demoed in the video below)

http://www.mathworks.com/help/symbolic/create-matlab-functions-from-mupad-expressions.html

http://www.mathworks.com/videos/using-symbolic-computations-to-develop-efficient-algorithms-and-system-models-81703.html?s_iid=disc_rw_sym_cta1

These are the first set of important steps.

Join my FREE newsletter to learn more about how implement this ‘Best workflow to HFT’

Overall workflow:

Demo of Mupad to custom Matlab M function

]

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! # Matlab Mupad code for Yin Yang Volatility?

Matlab Mupad code for Yin Yang Volatility?

An example shot from

For equation 33: I tried to use the following potential partial Mupad code:

Use 217335851-Mupad-User-Manual.pdf

Sum example could be: sum(exp(x)^(-x), x = 0..infinity)

xbar=stats:mean(x) p8

L := [1, 1, 1, 1, 1, 100.0]:

meanL = stats::mean(L);

nops(x)  is  number of operands

Attempt for Yin Yang volatility in Mupad:

xbar = stats::mean(x)

xSumTerm := x -> _plus(x – xbar \$ i=1..(nops(x)-1));  <- did not like _plus

xSumTerm := x -> (x – xbar \$ i=1..(nops(x)-1));

This may work:

`xbar = stats::mean(x)`

xfun:=x->x-xbar

sum(xfun, i=0..10)

sum(xfun^2, i=0..(m-1))

Join my FREE newsletter to see if this code makes sense

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! # Matlab MuPAD for scientific notation hand coding

Matlab MuPAD for scientific notation hand coding

Instead of wasting time on guessing how to hand coding these equations, I have decided to focus on MuPAD to bang this out. As a newbie, it can be frustrating let alone if it id being done right. I think this is best path. The other advantage I don’t have to pester people to accomplish this hand coding process.

https://quantlabs.net/blog/2014/04/new-workflow-qaunt-algo-from-research-paper-to-simulink-to-c-c-or-fpga-hdl-for-ultra-lowest-latency-hft/

http://www.codeitive.com/0yNePVkjWe/how-to-implement-this-function-in-mupad-matlab.html

Webinar intro: http://www.mathworks.com/videos/using-symbolic-computations-to-develop-efficient-algorithms-and-system-models-81703.html

Join my FREE newsletter to learn how I plan to implement this

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! # How to use integral and dot product within Matlab Mupad

How to use integral and dot product within Matlab Mupad

As I work on this Deutsche Bank Forex (aka DB FX) strategy, here are some useful links for Mupad:

http://www.mathworks.com/help/symbolic/mupad_ug/if-mupad-cannot-compute-an-integral.html

http://www.mathworks.com/help/symbolic/mupad_ref/linalg-scalarproduct.html

Join my FREE newsletter to learn other tips within Matlab

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! # How to convert Mupad function to Matlab M function within Mupad notebook

How to convert Mupad function to Matlab M function within Mupad notebook

This has been updated it looks like for Matlab 2014!

From within Mupad Notebook:
fprint(Unquoted, Text, “cartesian.m”, generate::MATLAB(S)):

http://www.mathworks.com/help/symbolic/create-matlab-functions-from-mupad-expressions.html
Join my FREE newsletter to learn more Matlab tips

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! # Amazon suggested these books on Matlab Mupad? Why? Get lessons for free

 Science & Math Books See even more similar items

Join my FREE newsletter to see what kind of Mupad resources we suggest

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! # Research paper algo for call or put options to Matlab Mupad converted to clean C or C++ Demo Intro

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!