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

    #16
    Thanks Ralph but what i mean by highestsince is different (this is from amibroker AFL and this is what i want):

    AFL Function Reference - HIGHESTSINCE HIGHESTSINCE
    - highest value since condition met





    SYNTAX highestsince( EXPRESSION, ARRAY, Nth = 1 ) RETURNS ARRAY FUNCTION Returns the highest ARRAY value since EXPRESSION was true on theNth most recent occurrence. EXAMPLE highestsince( Cross( macd(), 0 ), Close, 1 ) returns the highest close price since macd() has crossed above zero.
    Also there is valuewhen

    AFL Function Reference - VALUEWHEN VALUEWHEN
    - get value of the array when condition met





    SYNTAX valuewhen(EXPRESSION, ARRAY, n = 1) RETURNS ARRAY FUNCTION Returns the value of the ARRAY when the EXPRESSION was true on the n -th most recent occurrence. Note: this function allows also 0 and negative values for n - this enables referencing future EXAMPLE valuewhen( cross( close, ma(close,5) ) ,macd(), 1)
    so basically the above ready made function I search. In the end i will probably manage to solve them and if i define them within "UserdefinedMethods" I should be able to use them in any indicator. what I do not understand is why they are not already defined, should be extremely easy for a programmer but for me its pretty difficult to learn c++ and I dont need C++ to trade. That is why I said that Ninja programmers should make this easier for traders by defining such functions because I personally am really not interested in learning computer languages.

    PS:
    Ralph if you can solve them fast and put them here I wont mind

    Comment


      #17
      Something like this?
      Note that BarsSince starts with 1 for this application.

      int BarsSince;
      int hBar;

      protected override void OnBarUpdate()
      {
      BarsSince = CrossAbove(CCI(14), 100, 1) ? 1 : BarsSince++;
      hBar = HigestBar(<High,Low,Open,Close>, BarsSince);
      }

      Regards
      Ralph

      Comment


        #18
        Not quite, Highestbar method (function returns number of bars ago highest price value occured for lookback period and I need the highest high for the data series since the event took place. Besides that highestsince should be able to return more than last highest high. Its hard to explain but this says all:

        highestsince( EXPRESSION, ARRAY, Nth = 1 )

        Returns the highest ARRAY value since EXPRESSION was true on theNth most recent occurrence. Like for example finding the highest high of the close price since second last time when CCI(14) crosses 100 would be

        highestsince(CrossAbove(CCI(14)[0],100,1),Close,2)

        ...just as a example if it would be defined, so basically thats what I want to create and then define with "UserDefinedMethods" so I can replace CCI and Close with any other indicators or prices.

        Thanks anyway Ralph.

        Comment


          #19
          The List hBars should contain the bar numbers of the highest bar after each crossover event (not tested).

          Regards
          Ralph


          List<int> hBars = new List<int>();
          int lastIndex = -1;
          double lastHigh;

          protected override void OnBarUpdate()
          {
          if (CurrentBar < 2) return;
          if (FirstTickOfBar)
          {
          if (CCI(14)[1] > 100 && CCI(14)[2] < 100)
          {
          hBars.Add(CurrentBar - 1);
          lastHigh = High[1];
          lastIndex++;
          }
          else if (lastIndex >= 0)
          {
          if (High[1] > lastHigh)
          {
          lastHigh = High[1];
          hBars[lastIndex] = CurrentBar - 1;
          }
          }
          }

          Comment


            #20
            Thanks Ralph but its not what I want. Thanks anyway. However here`s a new problem: i have defined barssince function (method) within "UserDefinedMethods" so I can use it with any custom indicator but the problem is that it only works once. I mean if i try to use it 2 times in the same custom indicator then it messes up everything and does not give the right result.

            here is the code used in "UserDefinedMethods"

            partial class Indicator
            {
            int trigger;
            public int barssince(bool array)
            {
            if (array==true)
            {
            trigger=CurrentBar;
            }
            if(CurrentBar>trigger)
            {
            return(CurrentBar-trigger);
            }
            else
            {
            return(0);
            }
            }
            }


            the above code defines a function barssince(event) that will count the number of bars since event was true.

            It works like this barssince(CrossAbove(CCI(14),100,1)) this will count the number of bars after each cross of CCI14 above 100 then will reset to zero and count again at the next cross. It works fine alone but if I try to add another barssince like this barssince(CrossBelow(CCI(14),-100,1)) in the same custom indicators then it does not give the right result. see the code bellow;

            double a;
            double b;


            protected override void OnBarUpdate()
            {
            if(CurrentBar>0)
            {
            a=barssince(CrossAbove(CCI(14),100,1));
            }
            if(CurrentBar>0)
            {
            b=barssince(CrossBelow(CCI(14),-100,1));
            }
            Plot0.Set(a);
            Plot1.Set(b);
            }


            Maybe someone knows why and how to solve it? Maybe there is something I did not do right in "UserDefinedMethods" and because of that it messes up everything when used twice in the same custom indicator?

            ...help???

            Comment


              #21
              It seems, I post i answer, however sometimes the simpliest solutions are the hardest to see. I just added another barssince named barssince1 in to the userdefined methods like this

              partial class Indicator
              {
              int trigger;
              public int barssince(bool array)
              {
              if (array==true)
              {
              trigger=CurrentBar;
              }
              if(CurrentBar>trigger)
              {
              return(CurrentBar-trigger);
              }
              else
              {
              return(0);
              }
              }
              int trigger1;
              public int barssince1(bool array1)
              {
              if (array1==true)
              {
              trigger1=CurrentBar;
              }
              if(CurrentBar>trigger1)
              {
              return(CurrentBar-trigger1);
              }
              else
              {
              return(0);
              }
              }
              }

              Comment


                #22
                Exactly Paul,

                you need one for CrossAbove and one for CrossBelow. A better approach would be to implement barssince() as a class. Then you could instantiate class instances as often as you desire and use them independently without the need to duplicate code.

                Regards
                Ralph

                Comment


                  #23
                  what is that Ralph? I mean..what is a class? and how to implement it?. If you mean to have a single barssince(event) method that works anywhere like a new function then that will be great. If you know how to do it then maybe we could add a few new functions to Ninja to make it a little easier. i have no ideea what a class means but I supose that is what you meant.

                  Comment


                    #24
                    An indicator is implemented as a class for instance.
                    First you design a class.
                    Second you instantiate an instance of your class in your application and then you can access the class' public methods and properties.

                    However, since this concept is unknown to you lets consider this task from another side: Why even implement it as a function? I mean the functionality required is as simple as this:

                    int BarsSinceCrossAbove;

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

                    Regards
                    Ralph

                    Comment


                      #25
                      Ralph, the below code does not work, however I solved barssince and it would be truly nice to implement it as a class from what I understand. I am not used with all the "nuts and bolts" C++ requires and honestly dont want to know them but I know very well to code in languages like Metstock`s or easylanguage or amibroker`s. Implementing functions like barssince(event) as a class so they could be used as easy as a SMA for example would be great and I think would not be such a big deal for someone that is a programmer. I dont understand why ninja people want to make programmers out of traders. Now I have to solve that highestsince function (method) and again I am stuck...by the time I will finish I could start to write a new software myself and I trade not develop software...LOL. Thanks for your help.

                      Comment


                        #26
                        Paul79, jumping in here...did you check into the MRO / LRO methods supplied per default for those tasks?
                        BertrandNinjaTrader Customer Service

                        Comment


                          #27
                          Bertrand I have no clue what MRO / LRO or whatever means, and about jumping in here, I expected to solve some simple problems in less than 1 hour as i did with whatever other software for traders BUT Ninja that took me 2 days to solve barssince(event)... "nuts and bolts" as I was saying. Now if you are kind enough to explain MRO /LRO and enlighten me that would be great else thanks for mention I will search them myself.

                          Comment


                            #28
                            Oh..Bertrand you sure you know how to talk with customers? 995$ paid for the software its not a small amount and I hate when people are rude after I paid money.

                            Comment


                              #29
                              For that matter your MRO /LRO Bertrand has nothing to do with solving methods like valuewhen, highestsince or others and pretty much makes no difference, barssince I solve it already...after 2 days.

                              Comment


                                #30
                                Hi Paul, here is an implementation of the trigger bar counter as a separate class (HelperClass). In HelperClassTest you can see how this class is applied 2 times (up- and down-counter). Just use File->Utilities->NinjaScriptImport to install.

                                The little picture depicts the HelperClassTest in action. The green line counts the bars after the down trigger events, the orange line accordingly vice versa.

                                However, with this example you can see which way to go if using this approach. You should try to understand this concept before starting own developments, otherwise you won't get happy with that.

                                Regards
                                Ralph
                                Attached Files

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by The_Sec, Today, 02:29 PM
                                1 response
                                4 views
                                0 likes
                                Last Post NinjaTrader_Jesse  
                                Started by jeronymite, 04-12-2024, 04:26 PM
                                2 responses
                                30 views
                                0 likes
                                Last Post NinjaTrader_BrandonH  
                                Started by Mindset, 05-06-2023, 09:03 PM
                                10 responses
                                265 views
                                0 likes
                                Last Post NinjaTrader_BrandonH  
                                Started by michi08, 10-05-2018, 09:31 AM
                                5 responses
                                743 views
                                0 likes
                                Last Post NinjaTrader_ChelseaB  
                                Started by tsantospinto, 04-12-2024, 07:04 PM
                                4 responses
                                63 views
                                0 likes
                                Last Post aligator  
                                Working...
                                X