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

Sma

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

    Sma

    I would like to code a SMA(5) line which initiates from the lowest High of the last 9 bars.

    This is what I've been able to do so far:

    double barhigh9 = 0;
    barhigh9 =MIN(High,9)[0];

    What I want to do now is to use the data derived from barhigh9 and calculate SMA of barhigh9 and the high of the 4 bars that immediately precede it. Could you point me in the right direction?
    Last edited by kaywai; 01-24-2010, 11:25 PM.

    #2
    Hello,

    You will need to store that in an IDataSeries:


    Then use it in place of the Close value DataSeries below:
    SMA(Close,5)
    DenNinjaTrader Customer Service

    Comment


      #3
      Ben, it's doing pretty much what I want it to do now.

      Is it possible to add a bit of complexity to it? For example, If I want the "Purple" line to stop extending once the price bars move below it.

      I've attached the code to make things clearer for you.
      Attached Files

      Comment


        #4
        Kay, you would need to add a condition then only Set the dataseries then (fill it with your values), if the conditions negates itself, just don't set a value then.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Bertrand, this is what I have now.

          if (CurrentBar<13) return;
          double barlows = MAX(Low,13)[0];

          What I want to add now before setting the DataSeries is if there is no higher low than the past 13 bars stop calculating data until a low higher than the lows of the past 13 bars is formed.

          Not sure how to write that. Would you mind helping?

          Comment


            #6
            Kay, you could try something like this, referring to the posted MA1 here -

            Code:
             
            protected override void OnBarUpdate()
            {
            if (CurrentBar < 13) return;
            
            myDataSeriesHi.Set(MAX(Low,13)[0]);
            myDataSeriesLo.Set(MIN(High,13)[0]);
            
            if (High[0] > High[13])
            AvgR.Set(SMA(myDataSeriesHi,Period)[0]);
            
            if (Low[0] < Low[13])
            AvgA.Set(SMA(myDataSeriesLo, Period)[0]);
            
            }
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Thanks Bertrand. The SMA Lines are still printing when Higher Highs are formed and Lower Lows are formed. Trying to get those lines to calculate/print ONLY when Higher Lows and Lower Highs over the last 13 bars are formed.

              Comment


                #8
                I might have misunderstood you then, try it the other way round for your conditions -

                Code:
                 
                protected override void OnBarUpdate()
                {
                if (CurrentBar < 13) return;
                
                myDataSeriesHi.Set(MAX(Low,13)[0]);
                myDataSeriesLo.Set(MIN(High,13)[0]);
                
                if (High[0] > High[13])
                AvgA.Set(SMA(myDataSeriesLo,Period)[0]);
                
                if (Low[0] < Low[13])
                AvgR.Set(SMA(myDataSeriesHi, Period)[0]);
                
                }
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Hi Bertrand, I tried that before I replied earlier. Apologies for not mentioning it. I've attached a chart so that you can visualize what i'm talking about. From the chart, during the uptrend, the purple line should not appear during the uptrend as Higher Highs are being formed while the Green line is behaving correctly. However when lower lows are formed, the green line should stop extending and the purple line should start plotting as Lower Highs are being formed.
                  Attached Files

                  Comment


                    #10
                    Suggest you bring up the data box and mouse over several of the plots. Are both plots being set to some value on every bar or just one plot?
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      Not quite sure what you are saying.
                      The green line, which plots SMA(5) based on highest lows over the past 13 bars, is designed to be active during uptrends while the purple line plots SMA(5) based on the lowest highs over the past 13 bars.

                      What I'm trying to achieve here is when low[0] is no longer a 13-day-high low to have the green line cease plotting and only recommence plotting once a new 13-day-high low is recorded. The opposite is applicable for the highs.

                      The problem now is I can't get the green or purple line to stay silent when they are suppose to.

                      Comment


                        #12
                        kaywai, which exact code are you using here? The changed OnBarUpdate() I posted? This would set the plots only if the conditions are true...
                        BertrandNinjaTrader Customer Service

                        Comment


                          #13
                          Hi Bertrand, I've attached the code and the gif file for your reference. I tried your code and also tried modifying your recommended lines to:-

                          protectedoverridevoid OnBarUpdate()
                          {
                          if (CurrentBar<13) return;

                          myDataSeriesHi.Set(MAX(Low,
                          13)[0]);
                          myDataSeriesLo.Set(MIN(High,
                          13)[0]);

                          if (High[0] > MIN(High,13)[0]);
                          AvgR.Set(SMA(myDataSeriesLo, Period)[
                          0]);

                          if (Low[0] < MAX(Low,13)[0]);
                          AvgA.Set(SMA(myDataSeriesHi, Period)[
                          0]);
                          }

                          The gif file and script is as per your advice.
                          Attached Files

                          Comment


                            #14
                            Hi Bertrand, Sent this to you earlier this morning...please take a look. Thanks!

                            Comment


                              #15
                              Kay, please check into the attached script, which implements my earlier post yesterday. Would this work for you?
                              Attached Files
                              BertrandNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by stafe, 04-15-2024, 08:34 PM
                              7 responses
                              31 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by adeelshahzad, Today, 03:54 AM
                              4 responses
                              30 views
                              0 likes
                              Last Post adeelshahzad  
                              Started by merzo, 06-25-2023, 02:19 AM
                              10 responses
                              823 views
                              1 like
                              Last Post NinjaTrader_ChristopherJ  
                              Started by frankthearm, Today, 09:08 AM
                              5 responses
                              17 views
                              0 likes
                              Last Post NinjaTrader_Clayton  
                              Started by jeronymite, 04-12-2024, 04:26 PM
                              3 responses
                              43 views
                              0 likes
                              Last Post jeronymite  
                              Working...
                              X