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 rhyminkevin, Today, 04:58 PM
    4 responses
    52 views
    0 likes
    Last Post dp8282
    by dp8282
     
    Started by iceman2018, Today, 05:07 PM
    0 responses
    5 views
    0 likes
    Last Post iceman2018  
    Started by lightsun47, Today, 03:51 PM
    0 responses
    7 views
    0 likes
    Last Post lightsun47  
    Started by 00nevest, Today, 02:27 PM
    1 response
    14 views
    0 likes
    Last Post 00nevest  
    Started by futtrader, 04-21-2024, 01:50 AM
    4 responses
    50 views
    0 likes
    Last Post futtrader  
    Working...
    X