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 wzgy0920, 04-20-2024, 06:09 PM
        2 responses
        27 views
        0 likes
        Last Post wzgy0920  
        Started by wzgy0920, 02-22-2024, 01:11 AM
        5 responses
        32 views
        0 likes
        Last Post wzgy0920  
        Started by wzgy0920, 04-23-2024, 09:53 PM
        2 responses
        49 views
        0 likes
        Last Post wzgy0920  
        Started by Kensonprib, 04-28-2021, 10:11 AM
        5 responses
        193 views
        0 likes
        Last Post Hasadafa  
        Started by GussJ, 03-04-2020, 03:11 PM
        11 responses
        3,235 views
        0 likes
        Last Post xiinteractive  
        Working...
        X