- Hoping someone can help me figure out why my "multiple bollinger fade" strategy is not entering or exiting properly...
Positions should be entered once as price closes above each bollinger band (i.e. max 3 contracts long or short at any time)
The positions (1, 2 or 3 contracts, either all long or all short) should then hold until one of three events takes place:
1. price closes above/below the mid line of 1st bollinger band (close 1, 2 or 3 contracts accordingly)
2. stop loss in $$ is hit (close 1, 2 or 3 contracts accordingly)
3. profit target in $$ is hit (close 1, 2 or 3 contracts accordingly)
It's probably something simple but I can't seem to figure it out.
Complete code below:
#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 MultiFadeAtEachBollingerZiggy2 : Strategy
{
#region Variables
private int boLength1a = 100; // Default length for first bolinger band
private int boLength2a = 100; // Default length for second bollinger band
private int boLength3a = 100; // Default length for third bollinger band
private double boDev1a = 1.500; // Default Std Dev for first bolinger band
private double boDev2a = 2.000; // Default Std Dev for second bolinger band
private double boDev3a = 1.000; // Default Std Dev for third bolinger band
private int trgtInDollars = 2500; // Default setting for profit target in $$
private int stpInDollars = 5000; // Default setting for stop loss in $$
#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()
{
Add(Bollinger(BoDev1a, BoLength1a));
Add(Bollinger(BoDev2a, BoLength2a));
Add(Bollinger(BoDev3a, BoLength3a));
Add(Bollinger(BoDev1a, BoLength1a));
Add(Bollinger(BoDev2a, BoLength2a));
Add(Bollinger(BoDev3a, BoLength3a));
Add(Bollinger(BoDev1a, BoLength1a));
Add(Bollinger(BoDev1a, BoLength1a));
SetProfitTarget(TrgtInDollars);
SetStopLoss(StpInDollars, false);
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Condition set 1
if (Close[0] > Bollinger(BoDev1a, BoLength1a).Upper[0]
&& Variable0 == 0)
{
EnterShort(DefaultQuantity, "Short1");
Variable0 = 1;
}
// Condition set 2
if (Close[0] > Bollinger(BoDev2a, BoLength2a).Upper[0]
&& Variable0 == 1)
{
EnterShort(DefaultQuantity, "Short2");
Variable0 = 2;
}
// Condition set 3
if (Close[0] > Bollinger(BoDev3a, BoLength3a).Upper[0]
&& Variable0 == 2)
{
EnterShort(DefaultQuantity, "Short3");
Variable0 = 3;
}
// Condition set 4
if (Close[0] < Bollinger(BoDev1a, BoLength1a).Lower[0]
&& Variable0 == 0)
{
EnterLong(DefaultQuantity, "Long1");
Variable0 = 1;
}
// Condition set 5
if (Close[0] < Bollinger(BoDev2a, BoLength2a).Lower[0]
&& Variable0 == 1)
{
EnterLong(DefaultQuantity, "Long2");
Variable0 = 2;
}
// Condition set 6
if (Close[0] < Bollinger(BoDev3a, BoLength3a).Lower[0]
&& Variable0 == 2)
{
EnterLong(DefaultQuantity, "Long3");
Variable0 = 3;
}
// Condition set 7
if (Close[0] < Bollinger(BoDev1a, BoLength1a).Middle[0])
// && Position.MarketPosition == MarketPosition.Long)
{
ExitLong("", "Long1");
ExitLong("", "Long2");
ExitLong("", "Long3");
Variable0 = 0;
}
// Condition set 8
if (Close[0] > Bollinger(BoDev1a, BoLength1a).Middle[0])
// && Position.MarketPosition == MarketPosition.Short)
{
ExitShort("", "Short1");
ExitShort("", "Short2");
ExitShort("", "short3");
Variable0 = 0;
}
}
#region Properties
[Description("")]
[GridCategory("Parameters")]
public int BoLength1a
{
get { return boLength1a; }
set { boLength1a = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int BoLength2a
{
get { return boLength2a; }
set { boLength2a = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int BoLength3a
{
get { return boLength3a; }
set { boLength3a = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public double BoDev1a
{
get { return boDev1a; }
set { boDev1a = Math.Max(0.00006, value); }
}
[Description("")]
[GridCategory("Parameters")]
public double BoDev2a
{
get { return boDev2a; }
set { boDev2a = Math.Max(0.00006, value); }
}
[Description("")]
[GridCategory("Parameters")]
public double BoDev3a
{
get { return boDev3a; }
set { boDev3a = Math.Max(0.00006, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int TrgtInDollars
{
get { return trgtInDollars; }
set { trgtInDollars = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int StpInDollars
{
get { return stpInDollars; }
set { stpInDollars = Math.Max(1, value); }
}
#endregion
}
}

Comment