Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

ATR Based Stops Not Correctly Implemented

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

    ATR Based Stops Not Correctly Implemented

    Hello,

    I am trying to implement an ATR based stop and it's not working properly. I tried implementing the suggested method that PatrickH describes here but when I try implementing it the stops are triggered exactly at the entries as opposed to where they should be. Essentially I'm being stopped out as soon as I enter. Can someone please tell me what is wrong in my code that is causing this to happen?

    Code:
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion
    
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
        /// <summary>
        /// Enter the description of your strategy here
        /// </summary>
        [Description("Enter the description of your strategy here")]
        public class test5 : Strategy
        {
            #region Variables
            // Wizard generated variables
            // User defined variables (add any user defined variables below)
            #endregion
    
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
                CalculateOnBarClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                // Condition set 1
                if (Bollinger(2, 20).Upper[0] > Bollinger(2, 20).Upper[1]
                    && CrossAbove(MACD(12, 26, 9), MACD(12, 26, 9).Avg, 1)
                    && MACD(12, 26, 9).Avg[0] > MACD(12, 26, 9).Avg[1])
                {
                    EnterLong(DefaultQuantity, "LE");
                    Variable0 = ATR(20)[0];
        	        SetStopLoss("LE", CalculationMode.Ticks, Variable0, false);
    	            SetProfitTarget("LE", CalculationMode.Ticks, Variable0);
                }
    
                // Condition set 2
                if (Bollinger(2, 20).Lower[0] < Bollinger(2, 20).Lower[1]
                    && CrossBelow(MACD(12, 26, 9), MACD(12, 26, 9).Avg, 1)
                    && MACD(12, 26, 9).Avg[0] < MACD(12, 26, 9).Avg[1])
                {
                    EnterShort(DefaultQuantity, "SE");
                    Variable1 = ATR(20)[0];
    	            SetStopLoss("SE", CalculationMode.Ticks, Variable1, false);
        	        SetProfitTarget("SE", CalculationMode.Ticks, Variable1);
                }
            }
    
            #region Properties
            #endregion
        }
    }
    Attached Files

    #2
    Hello JVP3122,

    Thanks for your post.

    The actual value of ATR in this case is less than 1. When you specify CalculationMode.Ticks the expected value would be an integer number. So you would want to convert the ATR value to Ticks. For example Variable0/TickSize.

    TickSize is a system variable that will change based on instrument.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    65 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
    23 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    26 views
    0 likes
    Last Post TheRealMorford  
    Started by Mindset, 02-28-2026, 06:16 AM
    0 responses
    52 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Working...
    X