Wealth-Lab Wiki

API ChartStyles Community Components Community Indicators IndexDefinitions Knowledge Base Misc Optimizers PosSizers Providers Standard Indicators TASC Traders Tips TASCIndicators Tutorial Videos Visualizers
RSS

Navigation


Quick Search
»
Advanced Search »


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

RSS
by Dion Kurczek

Wealth-Lab version 4.x contained an integrated debugger that allowed you to set up breakpoints, step through your code, and examine variable values as your ChartScript executed. Since Wealth-Lab 4.x was based on an interpreted scripting engine, the debugger that was provided was less than ideal. Problems occurred if you used "include files" in your ChartScripts, or called user defined functions in the code. Also, the interpreted nature of the scripting meant that the debugger had to resort to stopping a thread in order to "debug" the script, and the result was sometimes instable.

In Wealth-Lab 6, you have the capability of writing Strategies (the new name for our old ChartScripts) 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.
  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 Script-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.

Important Disclaimer: The information provided by Wealth-Lab is strictly for informational purposes and is not to be construed as advice or solicitation to buy or sell any security.  The owner of Wealth-Lab.com assumes no liability resulting from the use of the material contained herein for investment purposes. By using this web site, you agree to the terms of this disclaimer and our Terms of Use.

Used under license from FMR Corp. Copyright 2008 FMR Corp. All rights reserved.


ScrewTurn Wiki. Some of the icons created by FamFamFam.