Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Take Profit & Stop Loss Order Persistence

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

    Take Profit & Stop Loss Order Persistence

    Hi new to NT8.. first forum post

    I am wondering the most efficient way to code a take profit and stop loss order at the same time as an EnterLong() order.

    For example I'd like the take profit and stop loss to be based on the average true range at the specific time of order entry so I would like to simultaneously enter the stops with the EnterLong(). However the conditions are within an OnBarUpdate() function so each time the script runs those stops are removed unless the conditions for meeting the EnterLong() are fulfilled again, correct?

    I have tried using the SetProfitTarget and StopLoss functions but I find the particularly buggy and would like to do this with direct order management. I remember with NT7 there was a LiveUntilCancelled field or something but I never fleshed out this same issue in that version of the script.

    Summary: How do you enter a market order and its take profit and stop loss at the same time while preventing canceling these orders after the next bar?

    Thanks

    #2
    Code sample

    For color my code looks something like this:

    protected override void OnBarUpdate()
    {
    if (//conditions)
    {
    EnterLong((2000), "longEntry");
    ExitLongLimit(2000, Close[0] + (12 * ATR1),"longExit","longEntry")
    ExitLongMIT(1000, Close[0] - (4 * ATR1),"atrExit","longEntry")
    }
    }

    Comment


      #3
      Hello jlkramer16,

      It is not possible to submit exit orders at the same time as your entry order under the managed approach, due to internal handling rules. At the time your entry order is submitted, there is no position to submit ExitLong() order against. This could be accomplished with the Unmanaged approach but it’s reserved for advanced programmers only.

      See the Managed Approach for Internal Order Handling Rules.


      What you could do is submit your EnterLong() and at that time set a variable equal to the ATR and another variable equal to 1.

      To implement, create a strategy which checks for market position of flat + your entry conditions. If true, it submits an entry order, sets Variable0=1, and sets Variable1=Average True Range. Your ATR Value is saved at the time your entry order is submitted.

      Next you create a condition to check if your market position is Long, Variable0=1, and that the stop/target are valid prices. If true, you then submit ExitLongLimit and ExitLongStop, with prices equal to Variable1 (your ATR) and set Variable0=0. Setting Variable0 back to zero is important so this condition does not become true and these orders are not resubmitted.

      This is not something you could due in the strategy wizard as you need to set LiveUntilCanceled equal to true for your ExitLongLimit and ExitLongStop.

      I have provided a sample code to demonstrate how to do this.

      Please let us know if you need further assistance.
      Attached Files
      Last edited by NinjaTrader_AlanP; 11-17-2016, 12:13 PM.
      Alan P.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      53 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      130 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      70 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      44 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      49 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X