How can I prevent trades from executing during pre-market or after-hours for U.S. stocks?

Questions about MultiCharts and user contributed studies.
samwjwj
Posts: 15
Joined: May 16 2010
Has thanked: 2 times
Been thanked: 1 time

May 26 2024

The trading hours for the U.S. stock market are from Monday to Friday, 9:30 AM to 4:00 PM Eastern Time. However, in MultiCharts, when receiving data from Interactive Brokers, the trading hours are extended from 8:00 AM to 6:30 PM, including one and a half hours of pre-market and two and a half hours of after-hours trading.

If I have a live strategy with stop orders, like the code below:

Code: Select all

if Time >=0901 and Time <=1601 then begin buy 1 contract next bar at 2000 stop; end;
In this scenario, the stop orders might be triggered during the pre-market or after-hours sessions, which will result in entries being displayed on the chart. How can I ensure that the strategy does not execute during these periods? Can this only be achieved by modifying the exchange's trading start and end times in QuoteManager?
Thank you so much for your help.

User avatar
Mark Brown
Posts: 203
Joined: Nov 29 2016
Has thanked: 152 times
Been thanked: 26 times

May 28 2024

Code: Select all

// Define input parameters for trading hours Inputs: startTradingTime(830), // Start trading at 08:30 endTradingTime(1600); // End trading at 16:00 // Variables to track trading status and previous date Vars: IntrabarPersist bool stopTrading(true), IntrabarPersist int prevDate(0); // New Trading Day Initialization // Reset trading status at the start of a new day If Date <> prevDate Then Begin stopTrading = true; // Assume stop trading at the start prevDate = Date; // Update to the current date End; // Trading Hours Check // Determine if current time is within specified trading hours If Time >= startTradingTime and Time < endTradingTime Then stopTrading = false // Enable trading Else stopTrading = true; // Disable trading outside hours // --- Placeholders for Trading Logic --- // Execute trading logic only during allowed hours If not stopTrading Then Begin // Insert your trading logic here End;

Gaempi
Posts: 62
Joined: Jul 27 2010
Location: Switzerland
Has thanked: 3 times
Been thanked: 8 times

Jun 17 2024

Yes, this looks good. But the problem is: if you send an stop/limit order during the mainsession (0830 - 1600), the order is still placed and active at the broker in the after market session (1600 - 1800), if the order is not executed in the mainsession. How can we stop this active orders in the after market ... only manually? It should be a way to stop all orders after the mainsession automatically.

MoeMiami
Posts: 15
Joined: Apr 09 2024
Been thanked: 3 times

Jun 29 2024

The trading hours for the U.S. stock market are from Monday to Friday, 9:30 AM to 4:00 PM Eastern Time. However, in MultiCharts, when receiving data from Interactive Brokers, the trading hours are extended from 8:00 AM to 6:30 PM, including one and a half hours of pre-market and two and a half hours of after-hours trading.

If I have a live strategy with stop orders, like the code below:

Code: Select all

if Time >=0901 and Time <=1601 then begin buy 1 contract next bar at 2000 stop; end;
In this scenario, the stop orders might be triggered during the pre-market or after-hours sessions, which will result in entries being displayed on the chart. How can I ensure that the strategy does not execute during these periods? Can this only be achieved by modifying the exchange's trading start and end times in QuoteManager?
Thank you so much for your help.
"if Time >=0901 and Time <=1601 then begin"
Remember you are always placing the order on the next bar so this should be Time<1600 meaning last order will be 1559, the way you have it it will place orders till 1602, but the order will not stay active after that, it's only active for the next bar.

Luto
Posts: 17
Joined: May 18 2022
Has thanked: 4 times
Been thanked: 1 time

Jun 30 2024

Yes, this looks good. But the problem is: if you send an stop/limit order during the mainsession (0830 - 1600), the order is still placed and active at the broker in the after market session (1600 - 1800), if the order is not executed in the mainsession. How can we stop this active orders in the after market ... only manually? It should be a way to stop all orders after the mainsession automatically.
Evaluation is per bar. So if the condition is not met for the current bar the action does not occur. It will cancel the order, if the order condition is not true. Another way to think about it is, every bar, the order conditions must be true otherwise the order is not submitted. Or another way, it is not like an order at IB where you set an order and it stays live until filled or canceled.

If time_s= 100000 then "order code" does not leave an order open when time_s<>100000.

Hope that helps.