Topic: Custom Indicators: Tracing Techniques

To code an indicator, you have to be able to "trace". This means seeing what variables are in the lines of your code. First step, trace values in the calculation to be sure they are correct at each step. This thread is to discuss some tracing techniques. I have a couple, please feel welcome to add your favorites.

Re: Custom Indicators: Tracing Techniques

Technique 1: adTrace and Display in Dynamic Values

I use a text editor, this is a simple technique for one or more values, and is quick.

1. create an array to keep the values for each iBar iteration
2. set adTrace[iBar] to value
3. add or modify a component in the Component = new IndicatorComp[]; section
4. run and mouse over bars to see values

This example uses ADX indicator, renamed and with "CustomIndicator = true".

1. create an array to keep the values for each iBar iteration

At the start of Calculate, you should Reading the Parameters, then Calculation. Usually there are some arrays created. In ADX, it looks like:

            double[] DIPos = new double[Bars];
            double[] DINeg = new double[Bars];

            for (int bar = 1; bar < Bars; bar++)

Be sure you add your array before the "for" line. I call my array "adTrace", and any more I add 1, like "adTrace1", "adTrace2", etc. To add:

            double[] DIPos = new double[Bars];
            double[] DINeg = new double[Bars];
            double[] adTrace = new double[Bars];

            for (int bar = 1; bar < Bars; bar++)

2. set adTrace[iBar] (or adTrace[bar]) to value

If you have unexpected values, usually one variable at some step is getting an unexpected value. Sometimes it's not set, so it's 0; the sign may be wrong; it's getting the price value from the wrong bar, etc. Many reasons, you just want to check one.
In ADX, I see variable "deltaHigh" is used, but not displayed. I want to check deltaHigh is what I expect.

Find these lines:

                if (trueRange < Point)
                    trueRange = Point;
 
                double deltaHigh = High[bar] - High[bar - 1];
                double deltaLow  = Low[bar - 1] - Low[bar];
 
                if (deltaHigh > 0 && deltaHigh > deltaLow)
                    DIPos[bar] = 100 * deltaHigh / trueRange;

Add adTrace[bar]:

                if (trueRange < Point)
                    trueRange = Point;
 
                double deltaHigh = High[bar] - High[bar - 1];
                double deltaLow  = Low[bar - 1] - Low[bar];

                adTrace[bar] = deltaHigh;
 
                if (deltaHigh > 0 && deltaHigh > deltaLow)
                    DIPos[bar] = 100 * deltaHigh / trueRange;

3. add or modify a component in the Component = new IndicatorComp[]; section
Put adTrace in a Component, then you can see the trace values on the chart, the same as any other indicator value.
Look for this line:

            // Saving the components
            Component = new IndicatorComp[5];

Increase the number in [] by one:

            // Saving the components
            Component = new IndicatorComp[6];

Copy and Paste one of the Component blocks. Change the number in [] to previous step - 1  (eg, 6 - 1 = 5)

            Component[0] = new IndicatorComp();
            Component[0].CompName   = "ADX";
            Component[0].DataType   = IndComponentType.IndicatorValue;
            Component[0].ChartType  = IndChartType.Line;
            Component[0].ChartColor = Color.Blue;
            Component[0].FirstBar   = firstBar;
            Component[0].Value      = ADX;

            //  Copy and Paste Component[0], then change [0] to [5] 
            Component[5] = new IndicatorComp();
            Component[5].CompName   = "ADX";
            Component[5].DataType   = IndComponentType.IndicatorValue;
            Component[5].ChartType  = IndChartType.Line;
            Component[5].ChartColor = Color.Blue;
            Component[5].FirstBar   = firstBar;
            Component[5].Value      = ADX;

Change the properties as set CompName to "adTrace", set Value to adTrace, and ChartType to .NoChart:

            Component[5] = new IndicatorComp();
            Component[5].CompName   = "adTrace";
            Component[5].DataType   = IndComponentType.IndicatorValue;
            Component[5].ChartType  = IndChartType.NoChart;
            Component[5].ChartColor = Color.Blue;
            Component[5].FirstBar   = firstBar;
            Component[5].Value      = adTrace;

(if there is "ShowDynamicInfo", be sure it is set to "true")



4. run and mouse over bars to see values

Save your file. Recompile Custom Indcators (Ctrl + I   or Tools > Custom Indicators > Reload the Custom Indicators).
Add your indicator to a strategy.
Open the Chart.
Mouse over bars, and review Indicator Values on the right.

http://s12.postimage.org/jdb3urpax/ad_Trace.jpg
(img also attached below)

Now you can be sure deltaHigh is not always 0; manually calculate the bar's high - previous bar's high and verify they match; use common sense it is a reasonable number, like a few pips, not a wild value like 23,342,011.3489348.

Post's attachments

adTrace.jpg 181.95 kb, file has never been downloaded. 

You don't have the permssions to download the attachments of this post.

Re: Custom Indicators: Tracing Techniques

If you don't want to copy and paste a Component, or if your Components are already at 6, as:

            // Saving the components
            Component = new IndicatorComp[6];

Then, choose a Component that has .DataType   = IndComponentType.IndicatorValue; and set it's value to adTrace. Choose one that you are not interested in for tracing or debugging.

Re: Custom Indicators: Tracing Techniques

Technique 2: Pop Up Alert Box  (post 1 of 2 posts)

Sometimes I want to see more than one value at a time. Or, if the indicator is crashing, I want to see how far it gets. Or, I want to see the trace at one particular bar. In these cases, I use a pop up alert, it's helpful.

1. import Alert Box
2. create a string to display info
3. add MessageBox call
4. add if conditions
5. run
6. flags for crashes


1. import Alert Box

At the top of the indicator, look for the lines with "using ...". They're usually at the top, under the comments.
In ADX, they're at lines 8 and 9.

using System;
using System.Drawing;

Add the import statement "using System.Windows.Forms;" as:

using System;
using System.Drawing;
using System.Windows.Forms;

2. create a string to display info

Create a string variable, then set it's value to whatever you want to see. Most of the variables in the indicator are int, double, bool, or date, so you have to call .ToString().
I add a label, makes it easier to read. Example from ADX:

                double deltaHigh = High[bar] - High[bar - 1];
                double deltaLow  = Low[bar - 1] - Low[bar];
                // label, then the value to trace
                string s = "deltaHigh = " + deltaHigh.ToString();

If it is an array, with [], use iBar or bar, in brackets:

                double deltaHigh = High[bar] - High[bar - 1];
                double deltaLow  = Low[bar - 1] - Low[bar];
                // label, then the value to trace
                string s = "High at " + bar.ToString() + " = " + High[bar].ToString();

Example with 2 variables, DIPos and DINeg at each bar:

                if (deltaLow > 0 && deltaLow > deltaHigh)
                    DINeg[bar] = 100 * deltaLow / trueRange;
                else
                    DINeg[bar] = 0;

                string s = bar.ToString() + ":  DIPos: " + DIPos[bar].ToString() + "; ";
                s += "DINeg: " + DINeg[bar].ToString();
                // will look like:   19011: DIPos: .0034; DINeg: -.0019

3. add MessageBox call
Add one line after setting string variable. String variable goes first.

                string s = bar.ToString() + ":  DIPos: " + DIPos[bar].ToString() + "; ";
                s += "DINeg: " + DINeg[bar].ToString();
                MessageBox.Show(s, "Add your own title here", MessageBoxButtons.OK);

There are other types of MessageBox you can use. I Use .OK because it's easy.



4. add if conditions
If you add your alert in the main for loop, the alert will popup on each bar. If you have 10,000 bars, it will pop up 10,000 times. It's better to focus on one bar you want, add an if condition, it only pops up once. This example only shows info on bar 1999.

                string s = bar.ToString() + ":  DIPos: " + DIPos[bar].ToString() + "; ";
                s += "DINeg: " + DINeg[bar].ToString();

        if (bar == 1999)
        {
              MessageBox.Show(s, Add your own title here", MessageBoxButtons.OK);
        }


5. run
When you add your indicator, the Alert boxes will pop up. If you change a parameter, it will pop up. I think there are 3 that will pop up in total.

If you forgot the if clause in step 4. and don't want to click OK 30,000 times, I don't know. I go to Task Manager to close FSB, add the if clause to my indicator, then start over.


6. flags for crashes
In next post

Re: Custom Indicators: Tracing Techniques

Technique 2: Pop Up Alert Box  (post 2 of 2 posts)


6. flags for crashes
Sometimes you get a crash or an error message that is not clear. In this case, it's helpful to use flags to find what line the error happens. I use the MessageBox technique, with string variable set to "A", "B", "C", "D", etc. If I see MessageBox with "B", then the error, I know it is some line between "B" and "C".

            // verify can enter main for loop
            MessageBox.Show("A", "Add your title here", MessageBoxButtons.OK);

            for (int bar = 1; bar < Bars; bar++)
            {
                double trueRange = Math.Max(High[bar], Close[bar - 1]) - Math.Min(Low[bar], Close[bar - 1]);
 
                if (trueRange < Point)
                    trueRange = Point;
                // verify can pass setting trueRange
                MessageBox.Show("B", "Add your title here", MessageBoxButtons.OK); 

                double deltaHigh = High[bar] - High[bar - 1];
                double deltaLow  = Low[bar - 1] - Low[bar];

                // verify can pass setting deltaHigh and deltaLow
                MessageBox.Show("C", "Add your title here", MessageBoxButtons.OK); 

                if (deltaHigh > 0 && deltaHigh > deltaLow)
                    DIPos[bar] = 100 * deltaHigh / trueRange;
                else
                    DIPos[bar] = 0;
 
                // verify can pass setting DIPos
                MessageBox.Show("D", "Add your title here", MessageBoxButtons.OK); 

                if (deltaLow > 0 && deltaLow > deltaHigh)
                    DINeg[bar] = 100 * deltaLow / trueRange;
                else
                    DINeg[bar] = 0;

                // verify can pass setting DIPos
                MessageBox.Show("E", "Add your title here", MessageBoxButtons.OK); 

            }

            // If you see F after a few boxes, for loop is probably exiting early
            MessageBox.Show("F", "Add your title here", MessageBoxButtons.OK); 

I did not add the if clauses for a bar; usually a bad line will throw an error the first time it's run. If that does not find it, then you can add the if clauses for greater than.

if (bar > 1000) {

Change the bar number to figure out what bar is causing the problem (Or cut off data in Data Horizon).

Re: Custom Indicators: Tracing Techniques

Technique 3: Use Visual Studio

This is the most detailed way to trace, but it takes the most work and uses Visual Studio. You can download FSB source code from github (see another thread for that).

Add you custom indicator to the Indicators folder.

Remove "CustomIndicator = true".

Open Solution Explorer, open Indicator Base > Indicator Store.cs

Add to the method AddOriginalIndicators:

        static void AddOriginalIndicators()
        {
            // add with your indicator's name and class
            originalIndicators.Add("ADX 1 krog's example", new ADX_1(SlotTypes.NotDefined));
            originalIndicators.Add("Accelerator Oscillator", new Accelerator_Oscillator(SlotTypes.NotDefined));

Add your data to the Data folder for your Visual Studio FSB project. Then you can add breakpoints to your indicator and gets lots of advanced tracing functionality from Visual Studio.

Re: Custom Indicators: Tracing Techniques

dr.B wrote:

Thanks Krog for all this work again smile

I will have to play with this to fully understand it
when i look at coding, i tend to follow the logic but the structure or the grammar of it is just eluding me (for now)
i will try not to ask for too much of your time, as it seems like you are helping everyone... i like the Gann project you are working on, it looks promising. smile
one question,  is it possible to set the a stop loss to an indicator value?  what i have in mind is to use Ross Hook value as a sl and add extra few pips for protection

Moved to new thread:
http://forexsb.com/forum/topic/2822/usi … stop-loss/

Re: Custom Indicators: Tracing Techniques

You can use Export->Indicators from FSB main menu to export a spreadsheet with all bar data and indicator data. You can also use the Command Console to see indicator values for a chosen bar.