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 a condition

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

    Bars since a condition

    I need to plot the number of bars since a particular condition. What key words are available for this? I found some key words: CurrentBar, BarCount, GotBars. I would like a list of any other key words related to counting bars.

    Would a statement like this work?
    If(RSI(close,14,3)> StochasticsFast(3,14), BarCount[10])
    Any other suggestions?

    #2
    BurtOD,

    Not sure where you got some of those terms. To count bars you have CurrentBar. To find a particular bar index you have GetBar().
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      I am confused. How do I get bars since a particular condition?

      Comment


        #4
        You can save out CurrentBar at the time it was true and then compare it to the most recent CurrentBar. Run the math and you have how many bars ago it occurred.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Bars Since

          Josh, ref your suggestion: "You can save out CurrentBar at the time it was true and then compare it to the most recent CurrentBar. Run the math and you have how many bars ago it occurred. " Great suggestion just what I need.

          I am using the following code:
          protected
          overridevoid OnBarUpdate()
          {
          // Use this method for calculating your indicator values. Assign a value to each
          // plot below by replacing 'Close[0]' with your own formula.
          int BarsSince = 0;
          int TriggerBar = 0;
          if (CCI(14)[0] > 100 && BarsSince == 0)
          {
          TriggerBar = CurrentBar -
          1;
          BarsSince = CurrentBar - TriggerBar;
          }
          elseif (CCI(14)[0] > 100 && BarsSince > 0)
          {BarsSince = CurrentBar - TriggerBar;}
          elseif (CCI(14)[0] > 100 != true)
          {
          BarsSince =
          0;
          TriggerBar =
          0;
          }
          PlotTB.Set(TriggerBar);
          PlotCB.Set(CurrentBar);
          PlotBS.Set(BarsSince);
          My problem is Triggerbar updates at every bar as opposed to saving that initial value that I can later compare to CurrentBar. How do I "save out" as you say?
          Thanks,
          Burt

          Comment


            #6
            You need to limit your if-condition to only be true once. You can do this with a bool variable

            Code:
            if (CCI... > 100 && conditionSet == false)
            {
                 TriggerBar =...;
                 conditionSet = true;
            }
            Then you simply reset the bool when you want to start using the trade condition again.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              I dont get it (truth is that a few hours ago I saw for the first time this ninja trader language), I just need a simple bars since a event function.

              For Tradestation I managed to soleve it in a few minutes like this:
              if CCI(14) > 100 and CCI(14) 1 bar ago < 100 then
              TriggerBar = BarNumber;
              if BarNumber >= TriggerBar then
              BarsSince = CurrentBar-TriggerBar;

              With Amibroker is much more easy:
              Barssince(CCI(14)[0] > 100 and ref(CCI(14),-1) < 100)

              But with Ninja I struggle for 4 hours and cant save that darn Trigger bar, God why do you people make this softwares for expert programmers and not for simple traders

              Can someone please, create this code from start to end coz, I do not know what I miss, should be simple.

              Also how to replicate Valuewhen function from amibroker?
              Last edited by Paul79; 02-06-2010, 08:10 AM.

              Comment


                #8
                Originally posted by Paul79 View Post
                For Tradestation I managed to soleve it in a few minutes like this:
                if CCI(14) > 100 and CCI(14) 1 bar ago < 100 then
                TriggerBar = BarNumber;
                if BarNumber >= TriggerBar then
                BarsSince = CurrentBar-TriggerBar;
                Hi there, in NinjaScript the code is very similar:
                Code:
                if (CCI(14)[0] > 100 && CCI(14)[1] < 100)
                    TriggerBar = CurrentBar;
                
                if (CurrentBar >= TriggerBar)
                    BarsSince = CurrentBar - TriggerBar;
                AustinNinjaTrader Customer Service

                Comment


                  #9
                  Austin thanks for the answer but it`s not working, I tried that and skewed it in all possible ways but cant get the right answer.Here is the code

                  protected override void OnBarUpdate()
                  {
                  // Use this method for calculating your indicator values. Assign a value to each
                  // plot below by replacing 'Close[0]' with your own formula.

                  int TriggerBar=0;
                  int BarsSince=0;
                  if (CCI(14)[0] > 100 && CCI(14)[1] < 100)
                  TriggerBar = CurrentBar;
                  if (CurrentBar >= TriggerBar)
                  BarsSince = CurrentBar - TriggerBar;

                  Plot0.Set(BarsSince);
                  }


                  The above code does nothing more than counting bars and when the condition is true (when CCI crosses 100) the output is zero since Currentbar=Triggerbar, as i said the rest of plot is just Currentbar plot. Perhaps I am missing something...?

                  Comment


                    #10
                    Is really nobody going to help on this forum or everyone is just as "pro" as I am? By the time i will finish learning this programming language i could start developing software within Visual Studio c++ and stop trading, really nobody is seeing that this is not for traders but for expert programmers and one has really nothing to do with the other. You people at Ninja company should really peek a little at tradestation easylanguage or amibroker AFL or metastock MSFL or other softwares made for traders and not for programmers. Unbelieveble in 2 full days I did not manage to solve this simple thing that took me 5 minutes on other platforms.
                    Last edited by Paul79; 02-07-2010, 08:39 AM.

                    Comment


                      #11
                      Paul,

                      notice that the following declarations are reset to zero every time OnBarUpdate() is invoked:

                      int TriggerBar=0;
                      int BarsSince=0;


                      I guess you intended to put these statements into the variables section of your indicator?

                      Regards
                      Ralph

                      Comment


                        #12
                        ... I personally would prefer to code something like this:

                        int BarsSince;

                        protected override void OnBarUpdate()
                        {
                        BarsSince = CrossAbove(CCI(14), 100, 1) ? 0 : BarsSince++;
                        Plot0.Set(BarsSince);
                        }

                        Regards
                        Ralph

                        Comment


                          #13
                          Yeah Ralph, I`ve figure that 1 hour ago..where have you been for 2 days . I managed to solve barssince function and now I try to solve valuewhen function and highestsince (lowestsince). It should be a little easier now. It is good that at least functions can be created to be use anywhere trough "UserdefinedMethods". Thank you for the answer.

                          Comment


                            #14
                            Originally posted by Paul79 View Post
                            ..where have you been for 2 days ...
                            was just ignorant, happens sometime.

                            Comment


                              #15
                              Originally posted by Paul79 View Post
                              ...I try to solve valuewhen function and highestsince (lowestsince)...
                              Consider to use something like this: HigestBar(<High,Low,Open,Close>, <lookback period>)

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by rtwave, 04-12-2024, 09:30 AM
                              2 responses
                              20 views
                              0 likes
                              Last Post rtwave
                              by rtwave
                               
                              Started by tsantospinto, 04-12-2024, 07:04 PM
                              5 responses
                              67 views
                              0 likes
                              Last Post tsantospinto  
                              Started by cre8able, Today, 03:20 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post cre8able  
                              Started by Fran888, 02-16-2024, 10:48 AM
                              3 responses
                              49 views
                              0 likes
                              Last Post Sam2515
                              by Sam2515
                               
                              Started by martin70, 03-24-2023, 04:58 AM
                              15 responses
                              115 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Working...
                              X