Programming | How can I debug my trading strategies in Wealth-Lab?

Modified on 2017/08/30 12:14 by Eugene — Categorized as: Knowledge Base

by Dion Kurczek

In Wealth-Lab 6, you have the capability of writing Strategies in any .NET development platform, including Microsoft Visual Studio. You can leverage the full power of the Visual Studio debugger to step through your Strategy as it executes within Wealth-Lab, and use the powerful variable evaluator tools that come with the Visual Studio environment.

To debug Strategies in Visual Studio, you will need to create the Strategies in a Class Library within Visual Studio, instead of using the Wealth-Lab built-in Editor. When you are satisfied that the Strategies are working correctly, you can simply leave the resulting Class Library assembly in the Wealth-Lab folder, or copy the Strategy code into new script-based Strategies using Wealth-Lab.

Follow the steps below to debug your Strategy in Visual Studio:

  1. Create a new Class Library project in Visual Studio. You can use any .NET language (Visual Basic, C#, C++, etc.) to develop the code.
  2. Add a reference to the main WealthLab assembly, which contains all of the core objects needed by custom Strategies.
    • Select Project, Add Reference from the menu.
    • Select the Browse tab, then navigate to the Wealth-Lab installation folder.
    • Select WealthLab.dll and click OK.
  3. Create a new "Class" in your Class Library Project.
  4. Add "using WealthLab" to the list of using statements.
  5. Derive the class from WealthScript. At this point you can use Visual Studio's code completion to automatically generate a stub for the required "Execute" method.
  6. In the same source code file (or a different file if desired) create a new class derived from StrategyHelper. A StrategyHelper is required for each Strategy that you create. Wealth-Lab examines your assembly for the StrategyHelpers that it contains, and uses them to determine what Strategies to display and load. Name the helper class the same name as your Strategy's class name, with the word "Helper" appended. Use code completion to provide stubs for the required properties. The Strategy Helper class returns properties that describe details about the Strategy that are needed by Wealth-Lab. Implement the properties in the code of your helper class.
    • For the Guid property, return a Guid object after generating a new Guid in Visual Studio using the Tools/Create Guid menu item. Example:

      return new Guid("DC891C92-F211-42d1-927C-36A6ECB64CD6");

    • For the WealthScriptType property, return a pointer to the Type of the main Strategy class. Example:

      return typeof(DailyBuyer); //Replace "DailyBuyer" with the name of your Strategy class

  7. If you plan to use the precompiled Strategy Library in Wealth-Lab directly, instead of eventually copying the code of the Strategy into a new Strategy that you create and saving it directly in Wealth-Lab, you should provide a description of the assembly. The description will appear when the Strategy Library is selected in the Strategy Explorer. To provide the description, double click the Properties item in the Visual Studio Solution Explorer under the Project, then click the "Assembly Info..." button in the Application tab. Put the description in the Description field.
  8. Implement the Execute method of the Strategy, just as if you were doing this in the Wealth-Lab Editor. Complete any additional implementation of the class, like a constructor, or other additional methods it might contain.
    • DO NOT leave any stub with throw new NotImplementedException() in a getter method! This will crash Wealth-Lab on startup. Make sure to implement all the overridden methods.
  9. Set the Output Path of the project, in the Build tab of the Project's Properties, to the Wealth-Lab installation directory. This will cause the resulting assemblies to be built into the Wealth-Lab folder, and this be visible to Wealth-Lab. You must be running Visual Studio as administrator for this.
  10. Build the class library until it completed without an error. Verify that the resulting files are generated in the Wealth-Lab installation folder.
  11. Place whatever breakpoints you want in the source code for debugging purposes.
  12. Run Wealth-Lab. As "Edit and Continue" will not work when VS is attached to process, see below for an alternative procedure.
  13. In Visual Studio, select Debug/Attach to Process from the menu. Find and select the Wealth-Lab process.
  14. In Wealth-Lab, select "File/Open Strategy" or click the Open toolbar button. You should see your Precompiled Strategy Library appear as an item in the list, with a special icon attached:
    Image
  15. Select the Strategy and click OK to open the chart. Select a symbol to execute the Strategy.
  16. At this point, Visual Studio will break at whatever break point you selected, and allow you to single step through the code line by line. You can evaluate variables and use all of the other advanced debugging capabilities that Visual Studio offers:
    Image
  17. When finished with the debugging session, select Debug/Terminate all from Visual Studio. You will note that Wealth-Lab closes at this point.
  18. Make any changes you need in the source code.
  19. Repeat steps 10 through 18 as required until you are satisfied that the Strategy is running correctly.
  20. At this point you have two options:
    • Leave the Strategy in its Precompiled form and use it in Wealth-Lab as is. You lose the ability to edit the source code in the Wealth-Lab Editor. But you can return to Visual Studio to edit the Strategy at a later time.
    • Create a new code-based Strategy in Wealth-Lab, and copy and paste the code into the Editor. At this point you are letting the Wealth-Lab editor take over to make any future changes. You cannot debug the newly saved Script-based Strategy in Visual Studio, although you can always revert back to the original version you created there.

Edit and Continue

by Eugene

The "Edit and Continue" feature of Visual Studio lets you edit your Strategy code on-the-fly while Wealth-Lab is running. Modern VS versions like Visual Studio 2015 Community edition introduce E&C for 64-bit projects (previous versions supported only 32-bit Wealth-Lab). E&C kicks in after a breakpoint has triggered in your Strategy.

The original article implies that you "Attach to Process". Unfortunately, it's not compatible with "Edit and Continue". To enjoy E&C you have to take an alternate route.

After you have successfully completed steps 1 to 9 of this tutorial, an extra step is required to change the project's startup behavior. In "Start Action" tab (Project - Project options), choose "Start external program", navigate to Wealth-Lab Pro (Developer) 6 installation folder and select WealthLabPro.exe (WealthLabDev.exe):


Image

The paths: 
C:\Program Files\Fidelity Investments\Wealth-Lab Pro 6\WealthLabPro.exe
C:\Program Files\MS123\Wealth-Lab Developer 6\WealthLabDev.exe

Here's what you will need to do instead of steps #12 and #13 above:


#12. When you click "Debug" (F5) next time, Wealth-Lab will start automatically. Move on to opening your Precompiled Strategy Library (step #14) or, to avoid duplicate actions, save your layout as the Default Workspace in WL. In that case, go right to step #16.

When debugging the library is finished, you may roll back the change by deleting the Default Workspace file:
c:\Users\Windows username\AppData\Roaming\Fidelity Investments\WealthLabPro or WealthLabDev \1.0.0.0\Data\Workspaces\Default.ws


#13. (n/a). Skip it.