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

# bars since last occurence...

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

    # bars since last occurence...

    Dear All,

    I am chewing my way through Ninja Script codeng and making some progress at last. I have a indicator that simply plots +1 and -1 for given conditions which works fine.

    Second step is to find out how many bars ago the last occurence of +1 in the indicator was. In Metastock, this is a standard formula 'BarsSince(condition)' and I am looking for th NS equivalent.

    In the attachment, the chart plots bars green when the indicator plots +1. The lowest indicator is my attempt to count # of bars between each green bar for which the coding is below. However, this not working as I expected.

    Between occurence 0 and occurence -1 as plotted on the chart, there are 22 bars in between, indicator plots 19.

    Between occurence -1 and occurence -2 as plotted on the chart, there are 38 bars in between, indicator plots 16.

    Pretty sure this is doable but not quite sure ho to get there.

    Any thoughts on this?
    Thanks,
    leduc



    Coding of indicator
    {
    if (CurrentBar == 0)
    Plot0.Set(
    0);
    else
    {
    double dblPrevClose = Close[1];
    double dblAtrVal = ATR(AtrLength)[1];
    double dblTriggerUp = dblPrevClose + dblAtrVal;
    double dblTriggerDown = dblPrevClose - dblAtrVal;
    if (CrossAbove(Close, dblTriggerUp,1))
    {

    int intNumBarsBack = CountIf(delegate {return CrossAbove(Close, dblTriggerUp,1);}, 999);
    Plot0.Set(intNumBarsBack);
    Attached Files

    #2
    Hello,

    You could take this approach:

    if(...condition here...)
    {
    //do something
    myConditionBar = CurrentBar; //record the current bar when the condition was met
    }

    Then use CurrentBar minus the myConditionBar to get the spread, in bars, between when your condition was met and the current bar now.

    Give that a try and post your code if you have issues. This link may be helpful:
    Last edited by NinjaTrader_Ben; 04-11-2010, 08:26 PM.
    DenNinjaTrader Customer Service

    Comment


      #3
      Hi,

      I tried the BarsSinceSession and a For loop to get this to work...

      It took me hours to go through this and the conclusion is that I need a method to hold a previous variable somwhere.

      Current coding is attached in the screen print.

      Idea is to loop over all price bars, finding occurrences of indicator values being equal to 1, corresponding with the green arrows in the chart in the attachement. This works in the sence that bars having the occurence are accurately marked.

      Problem with the current coding is that the values in all variables are constantly reset which I understand since after the loop is over, the variables are initialized again.... This can be viewed in the ouput where variable b is always 0 while it should hold the value from the first loop

      Basically, I need to know how I can keep variables alive outside the OnBarUpdateMethod for this to have a chance of working.

      I hope by the way that someone will point out a buildin function to reference previous indicator values because this beginning to look more complex than need be.

      Thanks,
      leduc
      Attached Files

      Comment


        #4
        leduc,

        Instead of declaring your variables in OnBarUpdate() please put it into the Variables region of your code above Initialize(). From there it will persist its values.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          pfff, and off course I should have figured that one out.....7,5 hours on a plane staring at C# screen makes you not see the obvious I guess.

          With that last remark, it is now working as expected!!!

          While we are on the subject, I want the indicator to also plot a second line for reverse signals. I extended the coding by adding a new plot but it keeps complaining about not being able to recognize "Plot1"

          This line was there
          Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));

          This is what I added later.
          Add(new Plot(Color.FromKnownColor(KnownColor.Black), PlotStyle.Line, "Plot1"));


          When trying to plot a signal, I get the error below:
          The name 'Plot1' does not exist within current context.

          This must be simple I would say. I have added the full coding in the picture.

          Thanks,
          leduc
          Attached Files

          Comment


            #6
            leduc, did you add your new Plot to the Properties section, too?
            BertrandNinjaTrader Customer Service

            Comment


              #7
              ehh, no I did not

              I should pay more attention to all the hidden sections do doubt.

              I have added some coding to the properties section and now it works!!

              Thanks for all the help, I will post the revised coding with some explanations when I am done for later reference.

              Thanks all,
              leduc

              Comment


                #8
                You're welcome, great you got it figured out.
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Frustrated...

                  mmm, not having much luck with that second plot.

                  I revised the coding and if I do not use the else statement, things for 1 plot are OK.

                  Using the second statement wipes out the first plot and the one that does show does not do what it should do...

                  It must be in the nested statements somewhere but I tried all kinds of combinations and I have not managed to at least plot 2 lines.

                  What gives? Coding is in the attachement
                  Attached Files

                  Comment


                    #10
                    leduc,

                    Unfortunately you will have to step through your code slowly and debug it. I suggest you use Print() statements throughout your code and try to track it along each step of the way and see where it breaks down.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      Found the reason for the not plotting.

                      Values for both plots were set to 0 where each plot seems to need its own (unique value)..



                      #region Properties
                      [
                      public DataSeries Plot0
                      {
                      get { return Values[0]; }
                      }
                      public DataSeries Plot1
                      {
                      get {return Values[1];} // This was Values[0];
                      }
                      #endregion


                      Originally posted by NinjaTrader_Josh View Post
                      leduc,

                      Unfortunately you will have to step through your code slowly and debug it. I suggest you use Print() statements throughout your code and try to track it along each step of the way and see where it breaks down.

                      Comment


                        #12
                        Solved!!

                        Actually, solved it entirly with much simpler code!!

                        Thanks all,
                        leduc
                        Attached Files

                        Comment


                          #13
                          mmm, ran into something that I did not expect.

                          it seems that the indicator was doing fine but since a new day (session) started, the output is all screwed up, dropping the count to -700....

                          Can anyone explain this one?

                          leduc
                          Attached Files

                          Comment


                            #14
                            leduc,

                            Suggest you print the values of your indicator calculations on session break. You can use something like Bars.FirstBarOfSession to find the session breaks.
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15


                              Dear All,

                              Fixed this one finaly today.... problem was with the barssincesession variable declared in the wrong spot.

                              Attached is new coding. The event declared under x can be replaced with alternative coding.

                              Regards,
                              leduc
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by helpwanted, Today, 03:06 AM
                              1 response
                              7 views
                              0 likes
                              Last Post sarafuenonly123  
                              Started by Brevo, Today, 01:45 AM
                              0 responses
                              7 views
                              0 likes
                              Last Post Brevo
                              by Brevo
                               
                              Started by aussugardefender, Today, 01:07 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post aussugardefender  
                              Started by pvincent, 06-23-2022, 12:53 PM
                              14 responses
                              242 views
                              0 likes
                              Last Post Nyman
                              by Nyman
                               
                              Started by TraderG23, 12-08-2023, 07:56 AM
                              9 responses
                              385 views
                              1 like
                              Last Post Gavini
                              by Gavini
                               
                              Working...
                              X