Build a strategy with Matlab
Author: hsingh
Creation Date: 9/11/2012 10:36 PM
profile picture

hsingh

#1
I am trying to create a strategy using Matlab. I have been using it for a month and a half and I have gotten familiar with the user interface, now I need help coming up with a simple strategy that I can execute. Can someone please help me?
profile picture

Eugene

#2
Matlab is not Wealth-Lab. Look for Matlab help on Matlab forums.
profile picture

Partha

#3
Hey Yuri and Eugene

First of all great to have you expert folks helping guys like me implement trading systems.... and I am currently going through Yuri's course in Udemy..very helpful!!

I have a question to both of you on custom extension... I need to price options dynamically and I can easily get historical volatility from WL pro but getting implied volatility is a challenge as WL does not have a native method to do that yet...Eugene did gave me a C# code from the internet the other day but I also have MATLABfinancial toolbox which has built in functions to compute the implied volatility...is there any way to extend the capability of WL pro to take advantage of these features in MATLAB....I found this on MATLAB website which allows MATLAB to be called from native C# applications

https://www.mathworks.com/help/matlab/matlab_external/call-matlab-function-from-c-client.html

the last paragraph says:

"From your C# client program, add a reference to your project to the MATLAB COM object. For example, in Microsoft® Visual Studio®, open your project. From the Project menu, select Add Reference. Select the COM tab in the Add Reference dialog box. Select the MATLAB application. Refer to your vendor documentation for details."

Is it possible to do this in WL pro?
profile picture

Eugene

#4
Hi Partha,

Firstly, your post was moved here from an announcement topic started by Yuri to market his courses and services. Please agree with me on that asking utterly specific questions such as this would be suboptimal in a thread like that (btw, where Yuri left a URL to reach him directly). Now on to your question.

Unlike Visual Studio, Wealth-Lab does not have any possibility to add a reference to a COM object. Which means that you cannot select the "COM tab" in it (there ain't any). I see two potential workarounds here, and both require Visual Studio:

1) create a .NET wrapper library
2) create a compiled Strategy library

The first workaround implies that you use VS to add that COM reference (legacy not supported in WL) to a blank Class Library project and then come up with some methods that call ML and return results to WL. So the idea is that your "recipient" WL Strategy would call that "proxy" method from your wrapper DLL, outsourcing the COM interop job to the "donor" (wrapper library). I'm not familiar with ML but if all you need is to call matlab.Feval like in your example (or something), that should suffice. Disclaimer: I haven't tested it and cannot guarantee that it would work.

The other workaround appears to be a quite simple and surefire way. Write your Strategy not in the built-in Editor but directly in Visual Studio, leveraging its capabilities including the powerful debugger and ability to add COM references. No wrapper required. The Community edition is free to use. Here's what you need to know to get the ball rolling:

1. Creating a WealthScript Strategy in VS: How can I debug my trading strategies in Wealth-Lab?
2. Sample code of a fully functional Strategy library: Download source code of Strategies.ActiveTrader library

So as not to repeat myself, there's no shortage of advices and troubleshooting suggestions on our forum. Good luck with your venture!
profile picture

Partha

#5
Thanks Eugene,

I like your suggestions and sorry I posted in Yuri's post.....

Cone also helped me yesterday with his implied volatility code....I have some commercial ideas for WL pro (no it is not WL strategy related) that I would like to discuss with you and Cone privately....should I raise a ticket?
profile picture

Eugene

#6
Yes, please create a support ticket.
profile picture

superticker

#7
There's an alternative solution to Post# 4 (COM approach). If you are only going to access a couple MatLab functions and you don't need to interact with these functions on the MatLab side, then another approach is to use a code generator to produce C++ code from the MatLab M-file that can be subsequently compiled with Visual Studio into a DLL library. You can then access all these functions from this portable DLL library.

The advantages of this approach is that it has much faster execution and it has a much smaller footprint. With the COM approach, you always need MatLab running when you're running Wealth-Lab. The disadvantage is that you always have to recompile when you change something on the MatLab side.

The MathWorks (MatLab author) sells a C++ code generator for M-files, but there are a few third-party vendors that do the same and their code generators produce tighter code for the embedded system market. (It's a common practice to write the high-level signal processing part in MatLab, then convert it to C++ to execute on the embedded instrument side.)
profile picture

Partha

#8
thanks superticker
profile picture

superticker

#9
Perhaps this is obvious, but if you only have one M-file to convert, rather than employing a C++ code generator, I would simply do the conversion yourself manually. And you'll find you can do a much better job than any code generator because you'll be able to combine some of the MatLab vector operations into a single FOR loop. And if you run into a linear system that needs solving (or any low-level numerical analysis; e.g. single-value decomposition, SVD), you can just call Math.Net for that. See the discussion about using Math.Net with WL: https://www.wealth-lab.com/Forum/Posts/Using-MathNet-Numerics-38646 It's a great package and even includes statistical functions, signal processing (separate download), and a Kalman filter. I use it for regression fitting all the time, which is part of my WL strategy. Happy computing to you ...
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).