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

Any help ? struggle to code this price channel in NT8

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

    Any help ? struggle to code this price channel in NT8

    I'm tithing 2 weeks to code this in NT8 , but no result yet , any help?

    its a simple price channel with fixed Deviation = 20 ticks or something...

    for (int i = Bars;i>=0; i--){
    LineHigh[i] = High[i]+Deviation;
    LineLow[i] = Low[i]-Deviation;


    if ( (High[i] < LineHigh[i]) && (Low[i] > LineLow[i]) )
    {
    LineHigh[i] = LineHigh[i+1];
    LineLow[i] = LineLow[i+1];
    }
    if ( High[i] > LineHigh[i+1] )
    {
    LineHigh[i] = High[i];
    LineLow[i] = LineHigh[i]-Deviation*2;
    }
    if (Low[i] < LineLow[i+1] )
    {
    LineLow[i] = Low[i];
    LineHigh[i] = LineLow[i]+Deviation*2;
    }
    }

    I can't adopt price series to get this done.
    Thanks in advance

    #2
    Hello Lyubomir Kratinov,

    Thank you for the post, and welcome to the NinjaTrader forum.

    I see you are looping through all of the bars on the chart. NinjaTrader will do this for you automatically when you load your indicator onto the cart, I call it the "historical run". All you have to do is put your logic in OnBarUpdate and NinjaTrader will run that logic for each bar of the chart (with OnBarClose). For an example of this look at the source code for a simple indicator like the ATR. All of the OnBarUpdate logic is processed when the indicator is loaded, so you see the historical values of ATR.

    Please try moving the logic from within the FOR loop and place that within OnBarUpdate. Also replace any 'i' indexes with a '0' referencing the most current bar.

    I can take a look at the full code as well if you would like.

    I look forward to hearing of your results.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Here is the code : // but not works as well

      protected override void OnBarUpdate()
      {
      //Add your custom indicator logic here.
      //
      // this should init the first bar
      LineHigh[0] = High[0]+Deviation/2;
      LineLow[0] = Low[0]-Deviation/2;

      // then keep the hi and low line until price break any side
      if ( (High[0] < LineHigh[1]) && (Low[0] > LineLow[1]) )
      {
      LineHigh[0] = LineHigh[1];
      LineLow[0] = LineLow[1];
      }
      // if the price break High then update BOTH line to the direction keepeng the same deviation
      if ( High[0] > LineHigh[1] )
      {
      // update current LineHigh value with the new high
      // update current LowLine value
      LineHigh[0] = High[0];
      LineLow[0] = LineHigh[0]-Deviation; //keep the same distance
      }
      // do the opposite of price brake high
      if (Low[0] < LineLow[1] )
      {
      LineLow[0] = Low[0];
      LineHigh[0] = LineLow[0]+Deviation;
      }

      // it does't work when I try to update LineHigh and LineLow values
      // it works if I disable /* ---- */ part from the code
      // I have rtied to use private series as well but same result



      }
      Here is a full code

      FixPriceChannel.zip


      Comment


        #4
        It should looks like :

        Comment


          #5
          Hello Lyubomir Kratinov,

          Thank you for the reply.

          I added a CurrentBars check at the beginning of OnBarUpdate and your plots worked after that. Remember to check the Log tab of your Control Center if you apply an indicator to your chart and receive no results CurrenBars counts upward from the leftmost bar of the chart, so the problem was you were accessing LineHigh[1] when the script only had bar 0 to work with.

          protected override void OnBarUpdate()
          {
          if(CurrentBar < 1)
          return;

          //rest of the code.

          Please let me know if I can assist further.
          Chris L.NinjaTrader Customer Service

          Comment


            #6
            wow awesome , Thanks a lot for that, its absolutely clear now how the data series works..... Thanks

            Comment


              #7
              well it's too early to be so happy - the code ' works ' it plots the line now , but its look like to loose some data somewhere. I will explore the code tomorrow, but the logic is right for me. The same logic (code) works on MT4 and NT7 . here is a picture on what indicator draws :
              Click image for larger version

Name:	FixPriceChannel1.png
Views:	860
Size:	40.2 KB
ID:	1044759

              Comment


                #8
                Hello Lyubomir,

                Thanks for the reply.

                Could you please post the NT7 code so I can take a look?

                I look forward to your reply.
                Chris L.NinjaTrader Customer Service

                Comment


                  #9
                  Hello Chris,

                  Thanks a lot for your support and help. I'm not sure what is wrong with the original code ?? but I find the way to fix wrong drawing.
                  I've added min , max, and trend data series for calculation and now everything is OK. Here you are the working code (NT8) for your references :
                  FixPriceChannel_working.zip

                  Since this is my entry point to NT8 programming I'll be more than happy if you can support me in the futures

                  Best Regards

                  Comment


                    #10
                    This indicator is awesome! I love it!
                    Though I've never coded C# and hardly code at all I accomplished it to add:

                    private double Deviation1;
                    Deviation1 = Deviation1 * Bars.Instrument.MasterInstrument.TickSize;

                    in order to be able to use the indicator on instruments like CL, ES or GC which don't have 1 tick as one 1.00 like YM for e.g.

                    After trading a while with it I recognized that it misses every 20th wick or so in its calculation. There seems to be no regularity. Just rarely it ignores a wick but it does it sometimes, unfortunately.
                    Lyubomir Kratinov did you already solved this or do you guys have an idea what might be the matter?

                    Comment


                      #11
                      Bar highs and lows are missed sometimes, because of basing the high and low price tests on close, instead of high and low. These two changes will fix the problem:
                      Code:
                      //            if (Close[0] > aaMax[1])
                                  if (High[0] > aaMax[1])
                                      aaTrend[0] = 1;
                      
                      //            if (Close[0] < aaMin[1])
                                  if (Low[0] < aaMin[1])
                                      aaTrend[0] = -1;

                      Comment


                        #12
                        Indeed that fixed it. Very nice. Thank you Lector!
                        Makes sense with the Close vs High/Low

                        Comment


                          #13
                          Even though the indicator works properly with those changes, it wastes lots of memory due to poor design. A much more efficient version is attached.

                          HLchannel.zip
                          Last edited by caveat_lector; 06-12-2020, 03:38 PM.

                          Comment


                            #14
                            Great.

                            I digged a little into the code and added an edge area by 2 additional lines and a trend indicating background.
                            Attached Files
                            Last edited by seykool; 06-17-2020, 11:43 AM. Reason: Now with the correct file. No idea how it could have been empty. Thanks for noting.

                            Comment


                              #15
                              seykool, the file you posted above contains "Using declarations" and "NinjaScript generated code" regions, but no indicator code.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by 00nevest, Today, 02:27 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post 00nevest  
                              Started by Jonafare, 12-06-2012, 03:48 PM
                              5 responses
                              3,986 views
                              0 likes
                              Last Post rene69851  
                              Started by Fitspressorest, Today, 01:38 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post Fitspressorest  
                              Started by Jonker, Today, 01:19 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post Jonker
                              by Jonker
                               
                              Started by futtrader, Today, 01:16 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post futtrader  
                              Working...
                              X