Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Problem with trying to create Divergence indicator using bwFractal

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

    Problem with trying to create Divergence indicator using bwFractal

    Hello Everyone

    I have written a couple of indicators (NT8) to try to get used to Ninjascipt but I am having problems with this one, and I cant find any indicators that I can copy from. I am starting with a bwFractal indicator to test Divergence against the MACD. I am trying to improve on the D3Spotter Divergence Indicator to later use in a strategy, so I need to write it myself..

    When I put print lines and a counter in the bwFractal Indicator (Bottom of the Highs calculation) I see the enormous amount of data being created which I want to use. I have also created a resize array and printed that too to see that it worked. All this is happening fine with each loop. But as soon as I want to manipulate any of the data (or add a loop) whilst it is looping the indicator stops output and doesn't print the Fractals.

    What I want to do is take say the first 30 Fractal Highs and probably use an array for storage, work out which Fractal Highs I want to use for Divergence against the MACD, test and plot the Divergence on the chart. I dont need to plot the divergence on the MACD because I can check that via the Databox. Then I want to add the next Fractal High and drop off the first Fractal High and do the same with those 30 Fractal Highs. As I wanted to test one additional Fractal high at a time (30 in total but add one / drop one) I was thinking the perfect time to do this was as the indicator was looping but it doesn't seem possible (to me).

    Manipulating the data is not a big problem. My problem is being able to access the data so I can manipulate it. I don't know if I have to work out a way to read the data from the chart after the indicator has plotted all the fractals or if there is a special way of getting access to the data whilst it loops. I would need to do the same process with the Fractal Lows too.

    Does anyone have any suggestions on the method I need to use.

    Thanks. Ray

    #2
    Hello Ray12345,

    Thanks for your post.

    I would suggest looking at the last code section of the Highs and the Lows as this is where the values are drawn from.

    For example lastLow and lastLowBar and Low[period] and CurrentBar-period are the points the rays are drawn with.

    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hi Paul

      Thank you, but that is the point where I have been stuck the last few weeks. I need to 1. wait for 30 fractals to loop / load and then 2. start working on which fractals I will use for divergence back to the first fractal, then 3. do the indicator comparison, 4. print the divergence line, then 5. move the 31st fractal in and the 1st fractal out, and do the same again, while the indicator loops without interrupting it.

      At the moment anything I try to do is stopping the loop running and the fractals plotting because I am disrupting the fractal loop. I was just wondering if there was a simple way to do what I wanted to do for 1. and 2.

      Regards
      Ray

      Comment


        #4
        when you say it stops output, do you mean it throws an error and stops working?

        if you are saving fractals into an array, at some point in your main loop, you'll probably need to call a separate method to loop through your array of fractals.
        the first test in that separate method will be to test if the count of array items is > 30 (goal#1)

        goal#2 - basica loop starting at the array count and counting down to 0.

        goal#3 - measure the slope between fractal values, and grabbing the bar index of those fractals, then getting the value of MACD at those bar indexes, getting the slope of MACD between those 2 points, comparing the slopes,

        goal#4 - if slope diff is < X, plot a dot or make sound, etc.

        goal#5 - if index of data point in fractal array is > 30, remove that data point from array.

        Comment


          #5
          Hi Balltrader

          I am working it out. I didn't have enough counters and prints for counters in loops to see why arrays were running out of range.

          Thank you for your input.

          Comment


            #6
            Hi Everyone again

            I have removed the array problems with counters (and Print) not pushing the arrays out of bounds and I have got the maths working. The problem was just simple logic and syntax errors.

            I have got to the stage where I want to add the MACD for its data but not for plotting. I can just use the MACD as a separate indicator and the data box to check if my maths is correct.

            I have added everything from the MACD indicator to the bwFractal indicator, above and below "OnBarUpdate()" with no compile or bwFractal execution errors. Inside "OnBarUpdate()" I can't combine the MACD which begins with the current bar at 0 and the bwFractals which starts with the current bar at 10. Whichever way I arrange it I dont get compile errors, but I do get an execution error "Error on calling 'OnBarUpdate' method on bar 0 (or bar 10); Index was outside bounds of the array".

            I assume it is something simple that needs fixing, but I can't find a similar example of another indicator that uses an idicator within it, that does the same. Amalgamation of the "OnBarUpdate()" without one corrupting the other is beyond me, unless I should be adding the MACD in a different way.

            bwFractal "OnBarUpdate()" (Fractals code is similar)

            protected override void OnBarUpdate()
            {

            if (CurrentBar < 10)
            return;

            if (IsFirstTickOfBar)
            barcounter++;

            isHighPivot(2,upcolor);

            isLowPivot(2,downcolor);
            }

            MACD "OnBarUpdate()"

            protected override void OnBarUpdate()
            {
            double input0 = Input[0];

            if (CurrentBar == 0)
            {
            fastEma[0] = input0;
            slowEma[0] = input0;
            Value[0] = 0;
            Avg[0] = 0;
            Diff[0] = 0;
            }
            else
            {
            double fastEma0 = constant1 * input0 + constant2 * fastEma[1];
            double slowEma0 = constant3 * input0 + constant4 * slowEma[1];
            double macd = fastEma0 - slowEma0;
            double macdAvg = constant5 * macd + constant6 * Avg[1];

            fastEma[0] = fastEma0;
            slowEma[0] = slowEma0;
            Value[0] = macd;
            Avg[0] = macdAvg;
            Diff[0] = macd - macdAvg;
            }
            }

            Comment


              #7
              Hello Ray12345,

              Generally most indicators will call on other indicators rather than taking their code over completely. You should be able to just call the MACD from the BWFractals like the following:

              Code:
              double value = MACD(12, 26, 9)[0];


              I would otherwise need to see what you specifically made in the customized script to better understand why you may be seeing this error. If possible can you attach your modified script? You can find the file in the folder Documents\NinjaTrader 8\bin\Custom\Indicators


              I look forward to being of further assistance.
              JesseNinjaTrader Customer Service

              Comment


                #8
                Hi Jesse

                Thank you. I figured there must have been an easier way. Enjoy New Year.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by agclub, 04-21-2024, 08:57 PM
                5 responses
                32 views
                0 likes
                Last Post agclub
                by agclub
                 
                Started by ESHunter, Today, 08:06 PM
                2 responses
                16 views
                0 likes
                Last Post ESHunter  
                Started by ETFVoyageur, 05-07-2024, 07:05 PM
                19 responses
                150 views
                0 likes
                Last Post ETFVoyageur  
                Started by ETFVoyageur, Yesterday, 10:13 PM
                3 responses
                26 views
                0 likes
                Last Post ETFVoyageur  
                Started by ETFVoyageur, Yesterday, 12:52 AM
                3 responses
                33 views
                0 likes
                Last Post ETFVoyageur  
                Working...
                X