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 rocketman7, Today, 01:00 AM
    1 response
    9 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by wzgy0920, 04-23-2024, 09:53 PM
    3 responses
    76 views
    0 likes
    Last Post NinjaTrader_BrandonH  
    Started by JonesJoker, 04-22-2024, 12:23 PM
    9 responses
    46 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by timko, Today, 06:45 AM
    1 response
    7 views
    0 likes
    Last Post gaz0001
    by gaz0001
     
    Started by Waxavi, 04-19-2024, 02:10 AM
    3 responses
    41 views
    0 likes
    Last Post gaz0001
    by gaz0001
     
    Working...
    X