I am trading a simple moving average cross over in the forex market, but I would like to limit trading to specific hours. In particular Monday to Friday 6am to 6pm, what would be the best way to apply this filter?
Below is the code I am currently using (but I suspect I am missing something, as it is not 100% accurate):
#region Variables
private int tradeok = 1;
#endregion
[...]
protected override void OnBarUpdate()
{
//Enter long
if (CrossAbove(SMA(Fast), SMA(Slow), 1) && tradeok == 1)
{
EnterLong();
}
//Enter short
if (CrossBelow(SMA(Fast), SMA(Slow), 1) && tradeok == 1)
{
EnterShort();
}
//Square position ahead of week-end
if (Time[0].DayOfWeek == DayOfWeek.Friday && ToTime(Time[0]) > ToTime(18, 0, 0))
{
ExitLong();
}
if (Time[0].DayOfWeek == DayOfWeek.Friday && ToTime(Time[0]) > ToTime(18, 0, 0))
{
ExitShort();
}
if (Time[0].DayOfWeek == DayOfWeek.Sunday)
{
tradeok = 0;
}
if (Time[0].DayOfWeek == DayOfWeek.Monday && ToTime(Time[0]) < ToTime(6, 0, 0))
{
tradeok = 0;
}
if (Time[0].DayOfWeek == DayOfWeek.Friday && ToTime(Time[0]) > ToTime(18, 0, 0))
{
tradeok = 0;
}
else
{
tradeok = 1;
}
}

Comment