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

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;	
    			}
    		}
    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by burtoninlondon, Today, 12:38 AM
    0 responses
    10 views
    0 likes
    Last Post burtoninlondon  
    Started by AaronKoRn, Yesterday, 09:49 PM
    0 responses
    14 views
    0 likes
    Last Post AaronKoRn  
    Started by carnitron, Yesterday, 08:42 PM
    0 responses
    11 views
    0 likes
    Last Post carnitron  
    Started by strategist007, Yesterday, 07:51 PM
    0 responses
    14 views
    0 likes
    Last Post strategist007  
    Started by StockTrader88, 03-06-2021, 08:58 AM
    44 responses
    3,983 views
    3 likes
    Last Post jhudas88  
    Working...
    X