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?
#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
}
}

Comment