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

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.
        BertrandNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by martin70, 03-24-2023, 04:58 AM
        15 responses
        114 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by The_Sec, Today, 02:29 PM
        1 response
        5 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by jeronymite, 04-12-2024, 04:26 PM
        2 responses
        31 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by Mindset, 05-06-2023, 09:03 PM
        10 responses
        265 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by michi08, 10-05-2018, 09:31 AM
        5 responses
        743 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Working...
        X