Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

ATR based stops and targets

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

    ATR based stops and targets

    Hey folks
    I want to add a stop loss and profit target to my startegy which depends on the atr value.
    I would like to have my stop loss .9 * ATR(14) off the entry and my profit target 1.2 * ATR(14) off the entry.
    How do I add this to the ninja scipt strategy?
    Thanks in advance.

    #2
    Hello jzag456,

    Thank you for writing in.

    You'll be able to call SetStopLoss() and SetProfitTarget() to place a stop loss and profit target for your strategy.




    Now, to set your profit target and stop loss, what I would suggest is using OnExecution(). This will allow both your stop loss and profit target to be placed when your entry order fills.



    To clarify, in a long position for example, are you wanting to place the profit target at the fill price + 1.2 * ATR(14) and the stop loss at the fill price - .9 * ATR(14)?

    If this is the case, here's some example logic on how this can be accomplished:

    Code:
    protected override void OnBarUpdate()
    {
    	EnterLong("Entry");
    }
    
    protected override void OnExecution(IExecution e)
    {
    	if (e.Order != null && e.Order.Name == "Entry" && e.Order.OrderState == OrderState.Filled)
    	{
                    // grab the average fill price of our order and add the current value of 1.2 * ATR(14) to it
    		SetProfitTarget(CalculationMode.Price, e.Order.AvgFillPrice + (1.2 * ATR(14)[0]));
    
                    // grab the average fill price of our order and subtract the current value of .9 * ATR(14) from it
    		SetStopLoss(CalculationMode.Price, e.Order.AvgFillPrice - (.9 * ATR(14)[0]));
    	}
    }
    Please, let us know if we may be of further assistance.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Thanks so far.
      I copied the lines into my strategy script, but there are still some errors.
      I attached the script.
      Attached Files

      Comment


        #4
        I found the problem, its working now.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        68 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        41 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        24 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        27 views
        0 likes
        Last Post TheRealMorford  
        Started by Mindset, 02-28-2026, 06:16 AM
        0 responses
        53 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Working...
        X