• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Debugging your NinjaScript Code

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    Debugging your NinjaScript Code

    Applies to NinjaTrader 7

    Debugging can be a frustrating and time-consuming task. In order to make the most of your time, it is best to proceed in a methodical manner. The first step you should do is to strip your code down into simple code segments. You want to start your debugging at a point where you know the code works as expected. From there you can then add more layers of complexity. With each additional layer, you want to ensure it works as expected before adding more layers.

    To begin the process of stripping down your code you can either make a new temporary NinjaScript and copy over only the key relevant code segments or you can comment out segments that are not vital to the test.

    To comment out code segments you can either press the "Comment selection" button on the top toolbar in the NinjaScript editor or type "//" in front of the line. To mass comment code segments, you can use your mouse cursor and select multiple lines and press the "Comment selection" button as well. To uncomment code, remove the "//" or select the line and press the "Uncomment selection" button.

    Commenting code segments is also useful if you wanted to just temporarily check if your code compiles. When your NinjaScript editor pops up with errors you can click on the error message and it will bring you to the erroneous line. After you comment out the erroneous lines you should be able to compile.

    The most common method you can use to ensure your code works as expected is through the use of the Print() command.

    Example:
    Code:
    if (Close[0] > Open[0])
    {
         Print("Code has entered If statement. Close: "+ Close[0] + " Open: " + Open[0]);
         // Do something
    }
    Placing Print() commands throughout your code logic allows you to trace where you are exactly. You can see where your code goes and where it does not go by viewing the output from the Output Window. Coupled with reading the error log from the Log tab in the Control Center, you can pinpoint where your code is crashing or locking up and make changes accordingly.

    Debugging orders can be a bit harder though because you cannot discern the behavior state of your orders through the Print() command easily. In addition to Print(), you can use TraceOrders to help you decipher what is happening under the hood for orders. TraceOrders will print information into the Output Window that will contain details about your orders.

    Code:
    protected override void Initialize()
    {
         TraceOrders = true;
    }
    When debugging stop or limit orders it can also be extremely useful to draw dots along your chart of the actual stop/limit prices. This way you can visually see where your orders are at and if they should have been filled or not.
    Code:
    DrawDot("Stop" + CurrentBar, true, 0, stopPrice, Color.Orange);
    Sometimes your code will compile and run fine, but it will produce inaccurate results. To check for accuracy, you can use Print() along with the Data Box to compare values. If you are doing a complex mathematical calculation, you can print the value at every step to ensure the calculations are as expected.

    External references can also be very useful when debugging. They are great for gaining insight into syntax and usage of various methods. Searching Google or MSDN can provide useful examples and code snippets you can adapt to use with your own code.

    Some useful resources:
    C# Station
    Microsoft Developer Network
    The Code Project
    Last edited by NinjaTrader_Jesse; 06-03-2015, 07:27 AM.
    JoshNinjaTrader Customer Service

    #2
    Applies to NinjaTrader 8

    Debugging can be a frustrating and time-consuming task. In order to make the most of your time, it is best to proceed in a methodical manner. The first step you should do is to strip your code down into simple code segments. You want to start your debugging at a point where you know the code works as expected. From there you can then add more layers of complexity. With each additional layer, you want to ensure it works as expected before adding more layers.

    To begin the process of stripping down your code you can either make a new temporary NinjaScript and copy over only the key relevant code segments or you can comment out segments that are not vital to the test.

    To comment out code segments you can either press the "Comment selection" button on the top toolbar in the NinjaScript editor or type "//" in front of the line. To mass comment code segments, you can use your mouse cursor and select multiple lines and press the "Comment selection" button as well. To uncomment code, remove the "//" or select the line and press the "Uncomment selection" button.

    Code:
    double limitPrice = 0; //You can comment after working code
    // double stopPrice = 0; 
    // you can also comment out whole lines to prevent it from being used by using //
    Commenting code segments is also useful if you wanted to just temporarily check if your code compiles. When your NinjaScript editor pops up with errors you can click on the error message and it will bring you to the erroneous line. After you comment out the erroneous lines you should be able to compile.

    The most common method you can use to ensure your code works as expected is through the use of the Print() command. The Print command will print the supplied value to the New -> NinjaScript Output window.

    Example:
    Code:
    if (Close[0] > Open[0])
    {
         Print("Code has entered If statement. Close: "+ Close[0] + " Open: " + Open[0]);
         // Do something
    }
    Placing Print() commands throughout your code logic allows you to trace where you are exactly. You can see where your code goes and where it does not go by viewing the output from the Output Window. Coupled with reading the error log from the Log tab in the Control Center, you can pinpoint where your code is crashing or locking up and make changes accordingly.

    Debugging orders can be a bit harder though because you cannot discern the behavior state of your orders through the Print() command easily. In addition to Print(), you can use TraceOrders to help you decipher what is happening under the hood for orders. TraceOrders will print information into the Output Window that will contain details about your orders.

    Code:
    protected override void OnStateChange()
    {
         if (State == State.SetDefaults)
         {
              TraceOrders = true;
         }
    }
    When debugging stop or limit orders it can also be extremely useful to draw dots along your chart of the actual stop/limit prices. This way you can visually see where your orders are at and if they should have been filled or not. Draw.Dot

    Code:
    Draw.Dot(this, "tag1", true, 0, Low[0] - TickSize, Brushes.Red);
    Sometimes your code will compile and run fine, but it will produce inaccurate results. To check for accuracy, you can use Print() along with the Data Box to compare values. If you are doing a complex mathematical calculation, you can print the value at every step to ensure the calculations are as expected.

    External references can also be very useful when debugging. They are great for gaining insight into syntax and usage of various methods. Searching Google or MSDN can provide useful examples and code snippets you can adapt to use with your own code.

    Some useful resources:
    NinjaScript Debugging
    CSharp Essentials
    C# Station
    Microsoft Developer Network
    The Code Project
    Last edited by NinjaTrader_Jesse; 06-03-2015, 11:58 AM.
    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by mpower, 05-27-2011, 01:16 PM
    4 responses
    2,586 views
    0 likes
    Last Post nkhoi
    by nkhoi
     
    Started by brucerobinson, Today, 02:43 AM
    5 responses
    31 views
    0 likes
    Last Post brucerobinson  
    Started by 2Look4me, Today, 09:33 AM
    6 responses
    39 views
    0 likes
    Last Post NinjaTrader_PatrickH  
    Started by calebsandfort, Today, 02:11 PM
    2 responses
    10 views
    0 likes
    Last Post calebsandfort  
    Started by traderpards, Today, 02:16 PM
    1 response
    6 views
    0 likes
    Last Post NinjaTrader_Jim  
    Working...
    X