Not sure what I am missing here. This strategy tries to submit a long order for one ETF on an SMA above crossover and a long order on another ETF on an SMA cross below. It also exits the other corresponding longs so you are only long one of the 2 funds at a given time.
When backtesting from June 2011, if never enters the long position on the second fund (noted in code comment below). The Print("CrossBelow Detected") is printed in the output window and the ExitLong("Primary Fund") appears to work. There is not a singal order places for the secondary ETF. Why does the EnterLong not work on bar index 1 (secondary ETF)?
public class ETFMACrossover : Strategy
{
#region Variables
private int fast = 3;
private int slow = 25;
#endregion
private DataSeries _ratioMA;
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
//Primary instrument will be ETF: HYLD period = day
Add("SHY", PeriodType.Day, 1); //Secondary fund
//This series will hold the ratio of close prices for the primary and secondary fund
//i.e. Close[Fund1]/Close[Fund2]
_ratioMA = new DataSeries(this, MaximumBarsLookBack.Infinite);
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick).
/// </summary>
protected override void OnBarUpdate()
{
if (BarsInProgress != 0) //process on primary bars only
return;
double ratio = Close[0] / Closes[1][0]; //Ratio of Primary Fund Close to secondary Fund Close
_ratioMA.Set(ratio); //populate new ratio MA Series
if (CrossAbove(SMA(_ratioMA, Fast), SMA(_ratioMA, Slow), 1))
{
Print("CrossAbove Detected");
ExitLong(1, 100, "Exit Secondary Fund", "Secondary Fund");
EnterLong("Primary Fund");
}
else if (CrossBelow(SMA(_ratioMA, Fast), SMA(_ratioMA, Slow), 1))
{
Print("CrossBelow Detected");
ExitLong("Primary Fund");
EnterLong(1, 100, "Secondary Fund"); //*** THIS ORDER NEVER GETS SUBMITTED
}
}
#region Properties
/// <summary>
/// </summary>
[Description("Period for fast MA")]
[GridCategory("Parameters")]
public int Fast
{
get { return fast; }
set { fast = Math.Max(1, value); }
}
/// <summary>
/// </summary>
[Description("Period for slow MA")]
[GridCategory("Parameters")]
public int Slow
{
get { return slow; }
set { slow = Math.Max(1, value); }
}
#endregion
}

Comment