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

Multi-Timeframe Moving Average

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

    Multi-Timeframe Moving Average

    Hello,

    How would I go about creating a moving average indicator so that if price breaks below a moving average the indicator would automatically plot the next moving average below price with say each plot changing by a minimum of 200 ticks, whilst also leaving the one price has just broken below on the chart? No more than say 3 MA plots would be on the chart at any time.

    Thank you.

    #2
    Hello,

    Thank you for the post.

    You can control the plots brush color of the SMA based on defined conditions. The plots will still hold values, but they will not be visible on the chart.

    Code:
     protected override void OnStateChange()
      {
       if (State == State.SetDefaults)
       {
                                     ...
        AddPlot(Brushes.Orange, "SMA0");
        AddPlot(Brushes.Transparent, "SMA1");
       }
       else if (State == State.Configure)
       {
       }
      }
    
      protected override void OnBarUpdate()
      {
       SMA0[0] = SMA(Close, 20)[0];
           SMA1[0] = SMA(Close, 10)[0];
       
        if(CrossBelow(SMA(Close, 20), Close, 1))
        {
         PlotBrushes[1][0] = Brushes.Blue;
        }
        
        if(CrossAbove(SMA(Close, 20), Close, 1))
        {
         PlotBrushes[1][0] = Brushes.Transparent;
        }
    
    //                              Here is an alternative way to plot.
    //    if(SMA(Close, 20)[0] > Close[0])
    //    {
    //     PlotBrushes[1][0] = Brushes.Blue;
    //    }
        
    //    if(SMA(Close, 20)[0] < Close[0])
    //    {
    //     PlotBrushes[1][0] = Brushes.Transparent;
    //    }
      }
    
      [Browsable(false)]
      [XmlIgnore]
      public Series<double> SMA0
      {
       get { return Values[0]; }
      }
    
      [Browsable(false)]
      [XmlIgnore]
      public Series<double> SMA1
      {
       get { return Values[1]; }
      }
    https://ninjatrader.com/support/help...crossbelow.htm - CrossAbove()
    https://ninjatrader.com/support/help...lotbrushes.htm - PlotBrushes[][]

    Please let us know if we may be of any further assistance.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Thanks for that ChrisL, it has worked great.

      Is there an easy way to draw a text label at the end of the plot for ease of identification? This thread asked for something so that this could be easily implemented, but I can't see that anything was done. https://ninjatrader.com/support/forum/showthread.php?t=79467
      Cheers!

      Comment


        #4
        Hello.

        Thank you for the reply.

        You can do something similar to the script attached. I used a boolean flag to tell if the plot is switching from transparent to blue. The text tends to get in the way, but this is the general idea. I will leave any improvements that can be made as an exercise for the reader.

        Here is the help guide link to Draw.Text():


        For instructions on importing this script please click here:


        If we may be of any further assistance, please let us know.
        Attached Files
        Chris L.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by DJ888, Yesterday, 06:09 PM
        2 responses
        9 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by jeronymite, 04-12-2024, 04:26 PM
        3 responses
        40 views
        0 likes
        Last Post jeronymite  
        Started by bill2023, Today, 08:51 AM
        2 responses
        16 views
        0 likes
        Last Post bill2023  
        Started by sidlercom80, 10-28-2023, 08:49 AM
        167 responses
        2,260 views
        0 likes
        Last Post jeronymite  
        Started by warreng86, 11-10-2020, 02:04 PM
        7 responses
        1,362 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Working...
        X