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

Help develope Indicator

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

    Help develope Indicator

    Hi there,

    would somebody be so kind to develop an indicator following the simple rules explained in the attached "trading rules.pgn" that plots a line like in the attached "output.pgn", basically the plot is digital, its plot 0 or 1.

    It's a simple filter for trend mode that uses the custom built ADX.

    thanks got your help
    Attached Files

    #2
    Hello sburtt,

    I will leave this thread open if any of our Forum Members would like to help with this.

    Please note that we have an extensive Help Guide that will help you get started with building a NinjaScript Indicator that you may view at the following link.
    JCNinjaTrader Customer Service

    Comment


      #3
      Originally posted by sburtt View Post
      Hi there,

      would somebody be so kind to develop an indicator following the simple rules explained in the attached "trading rules.pgn" that plots a line like in the attached "output.pgn", basically the plot is digital, its plot 0 or 1.

      It's a simple filter for trend mode that uses the custom built ADX.

      thanks got your help
      Condition 4 is not clear. Do you mean: "Stays above 10 for 3 out of 4 bars"? "Rises from below 10 ..." implies that it must be oscillating from below 10 to above 10. (It cannot rise above, unless it starts from below). Is that really what you mean?

      Comment


        #4
        Koganam, I am not 100% sure but I would interpret as following:

        if CrossAbove(ADX(14),10,1) && at least 3 out of 4 following bars close above 10.

        rules:

        1. If ADX crosses above 25, then the market is trending.
        2. If ADX crosses below 20, then the market is consolidating.
        3. If ADX crosses below 45 from above, then the market is consolidating.
        4. If ADX rises from below 10 on 3 out of 4 days, then the market will start
        to trend.
        5. If a trend is based on rule 4, it remains in effect until the 5 day
        difference in ADX is less than 0.

        This is some metastocks code found on the web, not sure it reflects perfectly the rules:


        periods:=Input("Periods?",1,63,14);
        If((ADX(periods)>25 AND (BarsSince(Cross(45,ADX(periods))) >
        BarsSince(Cross(ADX(periods),25)))) OR (ADX(periods) > 10 AND
        Ref(ADX(periods),-4)<10 AND (ADX(periods)-Ref(ADX(periods),-5)>0)), 1,
        If(ADX(periods)<20 OR ((BarsSince(Cross(45,ADX(periods))) <
        BarsSince(Cross(ADX(periods),25))) AND ADX(periods) < 45),-1,0))
        Last edited by sburtt; 03-05-2013, 03:04 PM.

        Comment


          #5
          Originally posted by sburtt View Post
          Koganam, I am not 100% sure but I would interpret as following:

          if CrossAbove(ADX(14),10,1) && at least 3 out of 4 following bars close above 10.

          rules:

          1. If ADX crosses above 25, then the market is trending.
          2. If ADX crosses below 20, then the market is consolidating.
          3. If ADX crosses below 45 from above, then the market is consolidating.
          4. If ADX rises from below 10 on 3 out of 4 days, then the market will start
          to trend.
          5. If a trend is based on rule 4, it remains in effect until the 5 day
          difference in ADX is less than 0.

          This is some metastocks code found on the web, not sure it reflects perfectly the rules:


          periods:=Input("Periods?",1,63,14);
          If((ADX(periods)>25 AND (BarsSince(Cross(45,ADX(periods))) >
          BarsSince(Cross(ADX(periods),25)))) OR (ADX(periods) > 10 AND
          Ref(ADX(periods),-4)<10 AND (ADX(periods)-Ref(ADX(periods),-5)>0)), 1,
          If(ADX(periods)<20 OR ((BarsSince(Cross(45,ADX(periods))) <
          BarsSince(Cross(ADX(periods),25))) AND ADX(periods) < 45),-1,0))
          Well, your interpretation, right or not, is perfectly clear. However, that would have to have been written like so:
          4. If ADX rises from below 10 and is above 10 for 3 out of the next 4 days, then the market will start to trend.
          in which case I would not have asked for clarification.

          Sorry, but I never did quite use MetaStock because I was not willing to spend so much money on a charting package, so I cannot even begin to interpret the MetaStock code.

          Comment


            #6
            Originally posted by sburtt View Post
            Koganam, I am not 100% sure but I would interpret as following:

            if CrossAbove(ADX(14),10,1) && at least 3 out of 4 following bars close above 10.

            rules:

            1. If ADX crosses above 25, then the market is trending.
            2. If ADX crosses below 20, then the market is consolidating.
            3. If ADX crosses below 45 from above, then the market is consolidating.
            4. If ADX rises from below 10 on 3 out of 4 days, then the market will start
            to trend.
            5. If a trend is based on rule 4, it remains in effect until the 5 day
            difference in ADX is less than 0.

            This is some metastocks code found on the web, not sure it reflects perfectly the rules:


            periods:=Input("Periods?",1,63,14);
            If((ADX(periods)>25 AND (BarsSince(Cross(45,ADX(periods))) >
            BarsSince(Cross(ADX(periods),25)))) OR (ADX(periods) > 10 AND
            Ref(ADX(periods),-4)<10 AND (ADX(periods)-Ref(ADX(periods),-5)>0)), 1,
            If(ADX(periods)<20 OR ((BarsSince(Cross(45,ADX(periods))) <
            BarsSince(Cross(ADX(periods),25))) AND ADX(periods) < 45),-1,0))
            Your spec is still incomplete. You have specified what and how you want to analyze the price data, but you have not defined what inputs and outputs you want to use/see, so it would not be possible to develop an indicator from your description. .

            Comment


              #7
              you are correct, the way it is written is misleading. I'm reading a book and started to test a few ideas, the author however is very sloppy in the way he wrights.

              Forget about the metastocks code, do you think you could give me a few hints on the script to you use for this indicator?

              Basically indicator will plot +1 when in trend and -1 when in consolidation mode. It could be worth trying to develop, maybe it could work well as a trend filter.

              Comment


                #8
                Originally posted by sburtt View Post
                you are correct, the way it is written is misleading. I'm reading a book and started to test a few ideas, the author however is very sloppy in the way he wrights.

                Forget about the metastocks code, do you think you could give me a few hints on the script to you use for this indicator?

                Basically indicator will plot +1 when in trend and -1 when in consolidation mode. It could be worth trying to develop, maybe it could work well as a trend filter.
                OK. So now you have specified two of the outputs. What about the inputs?

                Comment


                  #9
                  Originally posted by koganam View Post
                  OK. So now you have specified two of the outputs. What about the inputs?
                  input would be the ADX of the close, ADX is a custom builtin indicator in NT7 usually used with a period of 14, so basically the input would be ADX(Close,14)

                  Comment


                    #10
                    Originally posted by sburtt View Post
                    input would be the ADX of the close, ADX is a custom builtin indicator in NT7 usually used with a period of 14, so basically the input would be ADX(Close,14)
                    Well, then, so what you really mean is that the indicator should process based on ADX(14), in an absolute sense, so there will be no User Input parameter? IOW, nothing to configure, other than the Plot properties, which are really an output anyway, not an input?

                    Comment


                      #11
                      Originally posted by koganam View Post
                      Well, then, so what you really mean is that the indicator should process based on ADX(14), in an absolute sense, so there will be no User Input parameter? IOW, nothing to configure, other than the Plot properties, which are really an output anyway, not an input?
                      correct no input parameter. or maybe we could have the ADX period as a parameter, but that is something we can easily add after, if needed

                      Comment


                        #12
                        Originally posted by sburtt View Post
                        correct no input parameter. or maybe we could have the ADX period as a parameter, but that is something we can easily add after, if needed
                        Unfortunately, your spec is still incomplete, as it leaves out important details of what to do between the crossovers and also when a trend is starting (Condition 4 & 5). Also, even though you say to use a fixed ADX period, it is so restrictive that I created a user parameter to specify the period.

                        However, purely to show how you would take a spec and code it, you can look at the stuff below:
                        Code:
                               #region Variables
                                private int  lookBackPeriod = 14; // Default setting for lookBackPeriod
                                private bool IsTrending = false;
                                private bool TrendStarting = false;
                                private int  ADXAbove10Count = -1;
                                private int  ADXAbove10TestEndBar = -1;
                                #endregion
                                /// <summary>
                                /// This method is used to configure the indicator and is called once before any bar data is loaded.
                                /// </summary>
                                protected override void Initialize()
                                {
                                    Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
                                    Overlay    = false;
                                }
                                /// <summary>
                                /// Called on each bar update event (incoming tick)
                                /// </summary>
                                protected override void OnBarUpdate()
                                {
                           if (CurrentBar < 5) return; //per Condition 5.
                         
                        //   1. If ADX crosses above 25, then the market is trending.
                           if (CrossAbove(ADX(this.LookBackPeriod), 25 ,1)) IsTrending = true;
                         
                        //   2. If ADX crosses below 20, then the market is consolidating.
                           if (CrossBelow(ADX(this.LookBackPeriod), 20, 1)) IsTrending = false;
                         
                        //   3. If ADX crosses below 45 from above, then the market is consolidating.
                           if (CrossBelow(ADX(this.LookBackPeriod), 45, 1)) IsTrending = false;
                         
                        //   4. If ADX rises from below 10, and is above 10, on 3 out of 4 days, then the market will start
                        //   to trend.
                           if (CrossAbove(ADX(this.LookBackPeriod), 10, 1))
                           {
                            this.ADXAbove10Count = 0;
                            this.ADXAbove10TestEndBar = CurrentBar + 4;
                           }
                         
                           if (CurrentBar < this.ADXAbove10TestEndBar)
                           {
                            if (ADX(this.LookBackPeriod)[0] > 10) this.ADXAbove10Count++; //how many days is ADX above 10?    
                           }
                         
                           if (CurrentBar == this.ADXAbove10TestEndBar)
                           {
                            if (this.ADXAbove10Count >= 3) this.TrendStarting = true; //so what?
                            this.ADXAbove10Count = -1; //reset
                            this.ADXAbove10TestEndBar = -1; //reset
                           }
                         
                        //   5. If a trend is based on rule 4, it remains in effect until the 5 day
                        //   difference in ADX is less than 0.
                           if (this.TrendStarting && (ADX(this.LookBackPeriod)[0] < ADX(this.LookBackPeriod)[5]))
                           {
                            this.TrendStarting = false;
                           }
                         
                           //So far, nothing has been done with the supposition that the trend is starting
                         
                        //   Trending Market: Plot is 1
                        //   Consolidating Market: Plot is -1
                         
                           double Plotvalue = IsTrending ? 1 : -1;
                                    Plot0.Set(Plotvalue);
                                }
                                #region Properties
                                [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
                                [XmlIgnore()]  // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
                                public DataSeries Plot0
                                {
                                    get { return Values[0]; }
                                }
                                [Description("")]
                                [GridCategory("Parameters")]
                                public int LookBackPeriod
                                {
                                    get { return lookBackPeriod; }
                                    set { lookBackPeriod = Math.Max(1, value); }
                                }
                                #endregion
                        Last edited by koganam; 03-06-2013, 03:05 AM. Reason: Corrected punctuation.

                        Comment


                          #13
                          Originally posted by koganam View Post
                          Unfortunately, your spec is still incomplete, as it leaves out important details of what to do between the crossovers and also when a trend is starting (Condition 4 & 5). Also, even though you say to use a fixed ADX period, it is so restrictive that I created a user parameter to specify the period.
                          First of all thanks.

                          Many traders will use ADX readings above 25 to suggest that the trend's strength is strong enough for trend trading strategies. This is supposed to be an alternative version that plots -1 or 1 according to the conditions.

                          When trend is starting plot should be = 1
                          Regarding what to do between crossovers, would it be possible to add something like this to rule 1:

                          && BarsSince(Cross(45,ADX(periods)) > BarsSince(Cross(ADX(periods),25)

                          And to rule 3 this:

                          && BarsSince(Cross(45,ADX(periods)) < BarsSince(Cross(ADX(periods),25))

                          I think at this point we should have all specs covered

                          Comment


                            #14
                            Originally posted by sburtt View Post
                            First of all thanks.

                            Many traders will use ADX readings above 25 to suggest that the trend's strength is strong enough for trend trading strategies. This is supposed to be an alternative version that plots -1 or 1 according to the conditions.

                            When trend is starting plot should be = 1
                            Regarding what to do between crossovers, would it be possible to add something like this to rule 1:

                            && BarsSince(Cross(45,ADX(periods)) > BarsSince(Cross(ADX(periods),25)

                            And to rule 3 this:

                            && BarsSince(Cross(45,ADX(periods)) < BarsSince(Cross(ADX(periods),25))

                            I think at this point we should have all specs covered
                            Sorry, you are using non-NT Standard code in mathematical constructs, so I do not understand what you mean. You can try to write those conditions in English.

                            Even then from this point onward, I will probably only come back with questions to help you clarify what you spec and then code it yourself, rather than code it for you.

                            I have pretty much, in the code so far, shown you how to take each line of a spec and code it. You just need to follow the same procedure, so at to code any conditions that you want to add.

                            Comment


                              #15
                              Thanks, i really appreciated your help.

                              I will fix the last part. Only would you know how to code in ninja script the following conditionality i would like to add to rule 1:

                              the bar count since ADX(14) crossed above 25 is < than the bars since ADX(14) crossed below 45

                              Again thanks

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by bsbisme, Yesterday, 02:08 PM
                              1 response
                              15 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by prdecast, Today, 06:07 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post prdecast  
                              Started by i019945nj, 12-14-2023, 06:41 AM
                              3 responses
                              60 views
                              0 likes
                              Last Post i019945nj  
                              Started by TraderBCL, Today, 04:38 AM
                              2 responses
                              18 views
                              0 likes
                              Last Post TraderBCL  
                              Started by martin70, 03-24-2023, 04:58 AM
                              14 responses
                              106 views
                              0 likes
                              Last Post martin70  
                              Working...
                              X