Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

NT 8 Comparing Indicators to Past Value

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

    NT 8 Comparing Indicators to Past Value

    Hello,

    In NT 7 in a strategy it was simple to compare historical data of indicator to current data for example:

    --------------------------------------

    if(SMA(14)[0] < SMA(14)[1]
    {
    BarColor = Color.Red;
    }

    --------------------------------------

    In NT 8 I am trying to replicate the strategy but I am having no luck, the code compile but when I enable the strategy it disable itself right away

    --------------------------------------
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class Testing : Strategy
    {
    private SMA SMA1;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "Testing";
    Calculate = Calculate.OnPriceChange;

    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = false;
    }
    else if (State == State.Configure)
    {
    SMA1 = SMA(14);
    }
    }

    protected override void OnBarUpdate()
    {
    // Set 1
    if (SMA1[0] <= SMA1[1])
    {
    BarBrush = new SolidColorBrush(Colors.Red);
    }

    }
    }
    }

    #2
    Hello,

    The general syntax from NT7 will still work in NT8 the same way, the only difference in this would be BarBrush as you have demonstrated.

    To set the SMA as a variable, you could use the following sample which is very similar to NT7 but because there are now States you have to pick the correct state. In this case I used Historical as the state to create the variable. Additionally you are using 1 bars ago in the logic, in general you should always check if there are at least as many Bars on the chart as BarsAgo you use. In this case you would need to ensure there is at least 1 bar on the chart. That would be the if(CurrentBar < 1) return; check.

    Code:
    private SMA mySMA;
    		protected override void OnStateChange()
    		{
    			if (State == State.SetDefaults)
    			{
    				Description = @"Enter the description for your new custom Indicator here.";
    				Name = "Test";
    			}
    			else if (State == State.Historical)
    			{
    				mySMA = SMA(12);
    			}
    		}
    
    		protected override void OnBarUpdate()
    		{
    			if(CurrentBar < 1) return;
    			if(mySMA[0] >= mySMA[1])
    			{
    				BarBrush = Brushes.Gold;	
    			}
    		}

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    558 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    324 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    545 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    547 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X