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.
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 //
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:
if (Close[0] > Open[0]) { Print("Code has entered If statement. Close: "+ Close[0] + " Open: " + Open[0]); // Do something }
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.
protected override void OnStateChange() { if (State == State.SetDefaults) { TraceOrders = true; } }
Draw.Dot(this, "tag1", true, 0, Low[0] - TickSize, Brushes.Red);
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
Leave a comment: