Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Filling a space between two sma with color

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

    Filling a space between two sma with color

    Hello. I need help with indicator written by chatgpt.
    Thera are two sma's, high and low. I want the space between thems to be filled with color.
    Unfortunatelly this functionality doesn't work.
    I am not a programmer, I would be gratefull if someone could fix this code.

    Code:
    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class TwoSmaHighLowIndicator : Indicator
        {
            private SMA smaHigh;
            private SMA smaLow;
    
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="SMA Period for High", Order=1, GroupName="Parameters")]
            public int PeriodHigh { get; set; }
    
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="SMA Period for Low", Order=2, GroupName="Parameters")]
            public int PeriodLow { get; set; }
    
            [XmlIgnore]
            [Display(Name="Fill Color", Order=3, GroupName="Parameters")]
            public Brush FillColor { get; set; }
    
            [Browsable(false)]
            public string FillColorSerializable
            {
                get { return Serialize.BrushToString(FillColor); }
                set { FillColor = Serialize.StringToBrush(value); }
            }
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description = @"Indicator drawing two SMA moving averages based on High and Low prices and filling the area between them with a color.";
                    Name = "TwoSmaHighLowIndicator";
                    Calculate = Calculate.OnEachTick;  // Ensure the indicator works on each tick
                    IsOverlay = true;
                    
                    // Default periods for SMA
                    PeriodHigh = 14;
                    PeriodLow = 14;
    
                    // Default fill color
                    FillColor = Brushes.LightBlue;
    
                    // Adding plot lines
                    AddPlot(Brushes.Blue, "SMA High");
                    AddPlot(Brushes.Red, "SMA Low");
                }
                else if (State == State.DataLoaded)
                {
                    smaHigh = SMA(High, PeriodHigh);
                    smaLow = SMA(Low, PeriodLow);
                }
            }
    
            protected override void OnBarUpdate()
            {
                // Ensure there are enough bars for calculations
                if (CurrentBar < Math.Max(PeriodHigh, PeriodLow))
                    return;
    
                // Setting values for SMA
                Values[0][0] = smaHigh[0];
                Values[1][0] = smaLow[0];
    
                // Filling the area between SMAs
                // Drawing the area from the previous bar to the current one
                Draw.Region(this, "Region" + CurrentBar, CurrentBar - 1, CurrentBar, smaHigh, smaLow, FillColor, FillColor, 0);
            }
    
            #region Properties
            [Browsable(false)]
            [XmlIgnore]
            public Series<double> SmaHigh
            {
                get { return Values[0]; }
            }
    
            [Browsable(false)]
            [XmlIgnore]
            public Series<double> SmaLow
            {
                get { return Values[1]; }
            }
            #endregion
        }
    }

    #2
    Hello ryju81,

    Thank you for your post.

    Here are some existing indicators on the forum which use Draw.Region():

    DrawRegionwithMultipleMovingAverage indicator will help users to see the region between High/Open and Close/Low of the moving average. As a result, users can compare and use the most preferable moving average in their coming strategy plan. This indicator includes all the moving averages such as DEMA,EMA,HMA,KAMA&#8230;&#8230;, Ribbon. ** Some of the moving averages will give you [&#8230;]


    KeltnerChannelwColor is an NT8 replacement for the NT7/NT6.5 KeltnerChannelwColor indicator. It adds a Draw.Region() drawing object to the Keltner Channel indicator with options to change the region&apos;s color and opacity.


    To understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

    In the strategy add prints (outside of any conditions) that print the date time of the bar and all values compared in every condition that places an order.

    The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very important to include a text label for each value and for each comparison operator in the print to understand what is being compared in the condition sets.

    The debugging print output should clearly show what the condition is, what time the conditions are being compared, all values being compared, and how they are being compared.

    Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

    I am happy to assist you with analyzing the output from the output window.

    Run or backtest the script and when the output from the output window appears save this by right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output text file to your reply.


    Below is a link to a support article that demonstrates using informative prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.



    If you are not wanting to create or debug your script yourself with our guidance through email, you can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our NinjaTrader Ecosystem team follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request.

    The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The companies and services listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem, LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.

    Let me know if I may be of further assistance.

    Comment


      #3
      Thank Yiou Gaby V!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      646 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      367 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      107 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      569 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      573 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X