Login Page - Create Account

Support Board


Date/Time: Sat, 23 Nov 2024 08:52:48 +0000



Post From: Flatten trades at specific time - Market Replay

[2024-05-29 12:58:58]
emmanuel - Posts: 57
Here's an example, however don't use it blindly. Make sure you understand it well. In short, it means when there's an open position outside of the trading hours, automatically close it.

SCDateTime startTime;
SCDateTime endTime;
SCDateTime barTime = sc.BaseDateTimeIn[sc.Index].GetTime();

startTime.SetDate(barTime);
startTime.SetTimeHMS_MS(9, 30, 0, 0);
endTime.SetDate(barTime);
endTime.SetTimeHMS_MS(15, 58, 0, 0); // barTime is at the start of the bar, so you may need to adjust the minutes according to the chart's timeframe.

bool isTradingSession = barTime >= startTime && barTime < endTime;
s_SCPositionData positionData;
sc.GetTradePosition(positionData);

if(!sc.IsFullRecalculation
&& !isTradingSession
&& positionData.PositionQuantity != 0) {
sc.FlattenAndCancelAllOrders();
}