Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SetTrailStop in Strategy.OnMarketData

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

    SetTrailStop in Strategy.OnMarketData

    Does SetTrailStop work in the OnMarketData method of a Strategy? I'm trying to write a simple strategy with a trailing stop set to a fixed number of ticks but the trailing doesn't seem to work.
    Here's what I have:

    Code:
    protected override void Initialize()
    {
        SetTrailStop(CalculationMode.Ticks, 2);
        SetProfitTarget(....);
        ...
    }
    
    // OnBarUpdate commented out
    // protected override void OnBarUpdate() { }
    
    protected override void OnMarketData(MarketDataEventArgs e)
    {
        ....
        // Some condition...
        EnterLongLimit(bid);
        ...
    }

    #2
    SetTrailStop will use the last value submitted and automatically trail the market by the specified value. There is no advantage to placing it OnMarketData unless you were dynamically updating based on changes to level 1 data. It should still work here but is more efficient to place in other areas.

    There's also no need to comment out OnBarUpdate(). This event handler is essential to processing strategy logic.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Thanks for your reply. Your answer indirectly directed me to the solution.

      As soon as I uncommented the OnBarUpdate() method, the trailing stop started working.

      I'm not sure if this is documented elsewhere but it looks like the OnBarUpdate() method needs to be present (event if it is empty - say, if your strategy logic is programmed in the OnMarketData()), for the SetTrailStop() to work.

      Comment


        #4
        Originally posted by emini.ninja View Post
        Thanks for your reply. Your answer indirectly directed me to the solution.

        As soon as I uncommented the OnBarUpdate() method, the trailing stop started working.

        I'm not sure if this is documented elsewhere but it looks like the OnBarUpdate() method needs to be present (event if it is empty - say, if your strategy logic is programmed in the OnMarketData()), for the SetTrailStop() to work.
        Glad you got it working - correct this would be needed to process the tick by tick updates of the trail stop logic.

        Comment

        Latest Posts

        Collapse

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