Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Displaced SMA in strategy

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

    Displaced SMA in strategy

    Understand displacement is covered in the manual, https://ninjatrader.com/support/help...splacement.htm

    but is there an example of how that int is called in a strategy? What I'm trying to do below isn't working. Thanks

    private SMA sma;

    Period = 14;
    Displacement = 2;


    Code:
    			else if (State == State.Configure)
    			{
    				sma = SMA(Period).Displacement;
    			}
    			
    			else if (State == State.DataLoaded)
    			{
    				sma = SMA(Period).Displacement;
    				AddChartIndicator(sma);

    #2
    Hello,

    Thank you for the post.

    Yes, the Displacement property would need to be set by the indicator its self but this would not be used when being hosted by a Strategy.

    For a strategy to displace an indicators data, it would need to replot that data using the BarsAgo concept for the displacement needed. Otherwise, the indicator would need to plot a displaced value to begin with to avoid using the UI option for displacement completely.

    For example, instead of adding the SMA from the strategy, you could replot its displaced value using the strategy like the following:

    Code:
    protected override void OnStateChange()
    {
    	if (State == State.SetDefaults)
    	{
    		AddPlot(Brushes.Red, "SMA");
    	}
    }
    
    protected override void OnBarUpdate()
    {
    	if(CurrentBar < 2) return;
    	Value[0] = SMA(14)[2]; //2 for displacement
    }
    This would affect the actual plot value here, so if your strategy references this plot you would need to remember the values are actually displaced instead of just visually displaced.



    I look forward to being of further assistance.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    78 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    40 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    63 views
    2 likes
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    63 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    54 views
    0 likes
    Last Post CarlTrading  
    Working...
    X