public class TestStrategy : Strategy
{
private TimeSpan eod5Min = new TimeSpan(9, 25, 0);
private bool alreadyProcessed = false;
protected override void Initialize()
{
// Add a 5 minute Bars object: BarsInProgress index = 1
Add(PeriodType.Minute, 5);
CalculateOnBarClose = false;
}
protected override void OnBarUpdate()
{
DateTime Min5Time = Times[1][0];
// Check which Bars object is calling the OnBarUpdate() method
if (BarsInProgress == 1)
{
// time test
LogMsg logMsg2; // I have my own logging class
logMsg2 = new LogMsg();
logMsg2.TimeStamp(Time[0]);
logMsg2.Append("Min5Time: ", Min5Time.TimeOfDay.ToString() );
if (Min5Time.TimeOfDay.CompareTo(eod5Min ) == 0)
{
logMsg2.Append("equals eod5Min : ", eod5Min .ToString());
}
else
{
logMsg2.Append("not equal to eod5Min : ", eod5Min .ToString());
}
Log(logMsg2.DisplayLogMessage, LogLevel.Information);
// time test
if (Min5Time.TimeOfDay.CompareTo(eod5Min) == 0)
{
if (alreadyProcessed)
return;
else
{
ProcessCurrentState();
alreadyProcessed = true;
}
}
else
{
alreadyProcessed = false;
return;
}
}
else if (BarsInProgress == 0)
{
}
}
}
Here are excerpts from the log:
2/11/2011 9:23:52 AM|1|4|2/11/2011 9:00:00 AM - Min5Time: : 09:00:00, not equal to test5Min: : 09:25:00
2/11/2011 9:23:52 AM|1|4|2/11/2011 9:05:00 AM - Min5Time: : 09:05:00, not equal to test5Min: : 09:25:00
2/11/2011 9:23:52 AM|1|4|2/11/2011 9:10:00 AM - Min5Time: : 09:10:00, not equal to test5Min: : 09:25:00
2/11/2011 9:23:52 AM|1|4|2/11/2011 9:15:00 AM - Min5Time: : 09:15:00, not equal to test5Min: : 09:25:00
2/11/2011 9:23:52 AM|1|4|2/11/2011 9:20:00 AM - Min5Time: : 09:20:00, not equal to test5Min: : 09:25:00
2/11/2011 9:23:52 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:23:56 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:24:00 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:24:02 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:24:03 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:24:05 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:24:07 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:24:08 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:24:09 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:24:10 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:24:12 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:24:12 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:24:15 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:24:16 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:24:17 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:24:19 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:24:26 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:24:28 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:24:29 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:24:30 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:24:33 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:24:41 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:24:45 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:24:47 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:24:52 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:24:53 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
2/11/2011 9:25:04 AM|1|4|2/11/2011 9:30:00 AM - Min5Time: : 09:30:00, not equal to test5Min: : 09:25:00
2/11/2011 9:25:20 AM|1|4|2/11/2011 9:30:00 AM - Min5Time: : 09:30:00, not equal to test5Min: : 09:25:00
2/11/2011 9:25:21 AM|1|4|2/11/2011 9:30:00 AM - Min5Time: : 09:30:00, not equal to test5Min: : 09:25:00
2/11/2011 9:25:24 AM|1|4|2/11/2011 9:30:00 AM - Min5Time: : 09:30:00, not equal to test5Min: : 09:25:00
It doesn’t look like this line of code:
DateTime Min5Time = Times[1][0];
stores seconds. It also doesn’t look like there is any consistency as to how often the OnBarUpdate() method is called. I would think it would be every tick/second but according to the log it does not.
I added code to check for whether the trade has been processed.
if (Min5Time.TimeOfDay.CompareTo(eod5Min) == 0)
{
if (alreadyProcessed)
return;
else
{
ProcessCurrentState();
alreadyProcessed = true;
}
}
else
{
alreadyProcessed = false;
return;
}
The questions I have are:
1. Is there a way to get seconds? Although considering that I may not hit the OnBarUpdate() method at exactly the second I want, that may not be the best way to trigger the trade.
2. Is there a better way of handling the trade than to set a flag that checks to see if it has already been processed?

Comment