Came across something today that's curious. I have some logic that closes positions at session end (this supplants the ExitOnClose setting, which I leave as false). The code looks like this:
if (BacktestExitOnClose && Positions[BarsInProgress].MarketPosition != MarketPosition.Flat)
//sessionEnd.DayOfWeek == DayOfWeek.Friday)
{
int start = ToTime(sessionEnd.AddSeconds(-1*BacktestExitOnCloseSeconds));
int end = ToTime(sessionEnd);
if (ToTime(Time[0]) > start &&
ToTime(Time[0]) < end &&
!tradingClosed &&
!closingOrderSubmitted)
{
// Close the position
Positions[BarsInProgress].Close();
tradingClosed = true; // tradingClosed is here to ensure that no trades occur after specified close.
closingOrderSubmitted = true; // This flag ensures that only one closing order will be submitted.
// Need to add this here to prevent a new entry into a position.
barNumberOfOrder = CurrentBar;
if (DebugOutput)
DebugMessage(BarsInProgress, CurrentBar, Instrument, "PositionCloser(): Position Closed on bar #" + barNumberOfOrder);
if (TradingOutput)
TradingMessage(BarsInProgress, CurrentBar, Instrument, "Session Close Submitted.");
}
// Need code to turn things back on
if (ToTime(Time[0]) < start &&
ToTime(Time[0]) > end)
{
tradingClosed = false; // tradingClosed is here to ensure that no trades occur after specified close.
closingOrderSubmitted = false; // This flag ensures that only one closing order will be submitted.
}
break;
}
However, when I use NT's backtesting "ExitOnClose," this early close is correctly resolved.
I can't find anything related to holidays in NT's help file. The session in this case was for the default "Nymex Metals / Energy ETH" session, using UTC-05:00.
Any thoughts on what I might be missing here?
FYI I do initialize sessionStart and sessionEnd using:
// Get the session begin/end Bars.Session.GetNextBeginEnd(BarsArray[BarsInProgress], 0, out sessionBegin, out sessionEnd);

Comment