When Stochastics (K) crosses above (D) (just the time of the cross), then a boolean variable is set as true. Then if there is a cross of MACD avobe its Average in less bars than 5 we open a long trade.
The thing is that my code doesn't work fine and only take into account if both crosses happen at the same time and not with a difference of 5 bars.
The code is the following:
#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>
/// Versión 5 del giro de las 1600
/// </summary>
[Description("Versión 5 del giro de las 1600")]
public class GP16v5 : Strategy
{
#region Variables
// Wizard generated variables
private int barras = 5; // Default setting for Barras
private int stop = 6; // Default setting for Stop
private int profit = 4; // Default setting for Profit
private int trades = 2; // Default setting for Trades
private int tradesAnteriores = 0; // Default setting for TradesAnteriores
// 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()
{
SetStopLoss("", CalculationMode.Ticks, Stop, false);
SetProfitTarget("", CalculationMode.Ticks, Profit);
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
bool control=false;
bool control2=false;
int cont=100;
if (CurrentBar <20) return;
if(Stochastics(3,13,3).K[0]>Stochastics(3,13,3).D[0] && Stochastics(3,13,3).K[1]<=Stochastics(3,13,3).D[1])
{
control=true;
cont=0;
}
if(MACD(4,10,4)[0]>MACD(4,10,4).Avg[0] && MACD(4,10,4)[1]<=MACD(4,10,4).Avg[1])
{
control2=true;
}
if(control==true && control2==true && cont<5)
{
EnterLong();
control=false;
control2=false;
}
cont++;
}
#region Properties
[Description("")]
[GridCategory("Parameters")]
public int Barras
{
get { return barras; }
set { barras = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int Stop
{
get { return stop; }
set { stop = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int Profit
{
get { return profit; }
set { profit = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int Trades
{
get { return trades; }
set { trades = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int TradesAnteriores
{
get { return tradesAnteriores; }
set { tradesAnteriores = Math.Max(0, value); }
}
#endregion
}
}

Comment