View Full Version : How to make a strategy stay in for more than 24 hours?
DanielB
12-29-2009, 11:37 AM
By default, the strategy analyzer has the session begin/end at 12:00 AM, giving it a theoretical maximum lifespan of 24 hours. To allow more than 24 hours, I have "ExitOnClose" to false, but then it will not exit when I close the strategy or on the weekends. I would like my strategies have the ability to stay in 24 hours a day but still exit on the weekends and if I stop the strategy. How can I program this?
Thank you,
- Daniel
MicroTrends
12-29-2009, 12:36 PM
it is best to leave the session times to 24 hgours 7 days - inside the strategy you can test the time and date of the bar - if it is within your date range eg. mon 8am to fri 16:00pm then process else go flat do not process...
you will need some parameters such as
sesssionStartHour = 8
sesssionStartMinute =0
sesssionEndHour = 16
sesssionEndMinute =0
weekends=0
means trade only within 08 to 16 weekdays
and a method to call on barupdate
privatebool InValidSession()
{
//Print("InValidSession");
bool result = true;
try
{
if ((Weekends == 0) && (Time[0].DayOfWeek == DayOfWeek.Saturday || Time[0].DayOfWeek == DayOfWeek.Sunday))
{
result = false;
}
int TimeNow = ToTime(Time[0].Hour, Time[0].Minute, Time[0].Second);
if (result && sessionMode == 1)
{
timeStart = ToTime(sessionStartHour, sessionStartMinute, 0);
timeEnd = ToTime(sessionEndHour, sessionEndMinute, 0);
result = (TimeNow >= timeStart) && (TimeNow <= timeEnd);
}
}
catch (Exception ex)
{
result = false;
Log(ex.ToString(), LogLevel.Error);
}
return result;
}
protectedoverridevoid OnBarUpdate()
{
canTrade=InValidSession()
if (!canTrade) return;
}
DanielB
12-30-2009, 10:06 PM
Thank you very much, that was most helpful. :-)