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.
    Paul H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by algospoke, Yesterday, 06:40 PM
    2 responses
    23 views
    0 likes
    Last Post algospoke  
    Started by ghoul, Today, 06:02 PM
    3 responses
    15 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by jeronymite, 04-12-2024, 04:26 PM
    3 responses
    46 views
    0 likes
    Last Post jeronymite  
    Started by Barry Milan, Yesterday, 10:35 PM
    7 responses
    23 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by AttiM, 02-14-2024, 05:20 PM
    10 responses
    181 views
    0 likes
    Last Post jeronymite  
    Working...
    X