MultiCharts Day Trading Code: Entry Times, Pre-Close Exit
A working MultiCharts day trading script has three time conditions: the window when entries are allowed, a cutoff after which no new positions open, and a forced exit a few minutes before the close. Set the exit at least one bar before the close, treat SetExitOnClose only as a backup, and after the backtest use the trade list and “time in market” to confirm the exit actually ran.
By Soren Huang · Published · updated · Editorial policy
Contents
- How do you write MultiCharts day trading code that exits before the close?
- How do you write time conditions in PowerLanguage? Handle the day and night sessions separately
- How do you write an exit a few minutes before the close? The time depends on the bar size
- Does SetExitOnClose work on the night session?
- How do you limit entries to once a day?
- How do you confirm from a backtest that the exit actually ran?
- Frequently asked questions
- References
How do you write MultiCharts day trading code that exits before the close?
The core of MultiCharts day trading code is three time conditions: allow entries only inside a set window, open no new positions after a cutoff time, and close everything with a Time condition a few minutes before the close. SetExitOnClose works only as a backup, because whether it fires depends on the session template, and the night session in particular. After the backtest, confirm from the trade list that no position was held overnight.
Day trading (same-day round trips) means opening a position the same day, closing it before that day's close, and holding nothing overnight. A time condition is a test on a bar's time value that decides when your code is allowed to act. Think of a trading session as a workday: you watch for a while in the morning before you start, you take no new work after a certain time in the afternoon, and you always wrap up what is in hand ten minutes before you leave. In code, that becomes three time windows:
- The entry window. For example, the TAIEX futures day session from 09:00 to 12:30. This avoids the violent first fifteen minutes after the open and leaves each trade enough time to develop.
- The cutoff for new positions. For example, after 12:30 the code only handles exits. An order entered at 13:30 would be force-closed ten minutes later, so you pay commission and slippage without giving the trade a chance to move.
- The forced exit time. For example, 13:40. This part looks at no indicator, only the time and whether you hold a position: if you do, close all of it with a market order.
The TAIEX futures day session runs 08:45 to 13:45 and the night session runs 15:00 to 05:00 the next day. The exchange may change these hours, so before you write code, check the Taiwan Futures Exchange (TAIFEX) contract specifications page, or compare against your own time zone with the futures market hours tool. TAIEX futures night session: hours, holidays and settlement sets out how the day and night sessions differ in backtest settings. If you are still deciding which platform should run your day trading code, a comparison of automated order placement across platforms explains how MultiCharts and other platforms differ in automated trading.
How do you write time conditions in PowerLanguage? Handle the day and night sessions separately
PowerLanguage gets a bar's time from the reserved word Time, an integer in HHMM format. A day session time condition can use plain greater-than and less-than comparisons, but the night session crosses midnight, a single comparison fails, and you must split it into two parts joined with “or.” A reserved word is a name the programming language predefines with a fixed meaning: Time is the time of the current bar, and Date is the date.
HHMM format means 13:40 is written as 1340 and 09:00 as 900. MultiCharts labels bars by their close time by default, so on a 5-minute chart the bar labeled 13:40 covers 13:35 to 13:40; if you have changed how your chart labels bars, check on the chart before you write conditions. For the day session, the entry window can be written as “Time greater than or equal to 900 and Time less than 1230,” because from 845 to 1345 the numbers only increase.
The night session is different. From 15:00 the numbers increase to 2359, then jump back to 0000 after midnight and increase to 0500. If you write “Time greater than or equal to 1500 and Time less than 450,” no moment can satisfy both parts, so the code never enters all night. The correct way is to split the condition into two parts: “Time greater than or equal to 1500” or “Time less than or equal to 450.” The exit time works the same way: the forced exit before the night session close falls after midnight, so you write a range such as “Time greater than or equal to 455 and Time less than 500,” which is true only in the early morning.
After midnight there is a second difference: Date may already have turned to the next calendar day, but those bars still belong to the same night session trading day. This affects the “enter only once a day” check and which day a daily bar belongs to, which the fifth section covers. The table lists the five places where day and night session code must be handled separately:
| Item | Day session (08:45–13:45) | Night session (15:00–05:00 next day) |
|---|---|---|
| Does the session cross midnight? | No | Yes |
| How to write the entry window | One part: “greater than or equal to” and “less than” | Two parts joined with “or” |
| Forced exit time value | For example 1340, greater than the entry time | For example 455, a smaller number than the entry time |
| What counts as “today” | Date equals the calendar day | The night session open starts the trading day; you cannot rely on Date alone |
| The closing bar | The bar labeled 13:45 | The bar labeled 05:00, already dated the next day |
How do you write an exit a few minutes before the close? The time depends on the bar size
Set the forced exit time at least one bar earlier than the close, plus allowance for fill delay, because the condition becomes true only when a bar closes and the order fills on the next bar. With 5-minute bars and the exit condition at 13:40, the fill comes at the open of the bar after 13:40. If you set it at 13:45, that is the last bar of the session, the next bar is already in the next session, and the position is held overnight.
This is the other side of the look-ahead bias that often shows up in backtests: the code only knows a condition is true when a bar closes, so the earliest it can fill is the next bar. Build that one-bar delay into your exit time with these steps:
- Confirm the chart's bar size. The delay is different for 5-minute, 15-minute and 30-minute bars.
- Subtract one bar from the close time to get the latest usable condition time. That is 13:40 for 5-minute bars, 13:30 for 15-minute bars and 13:15 for 30-minute bars.
- Then leave one more bar as a buffer. With 5-minute bars, set 13:35; the fill comes after 13:35 and before 13:40, so even if liquidity thins out before the close, there is still a bar to use.
- The condition looks only at time and position. “Time greater than or equal to the set value and position not zero” closes the position, with no indicator added.
Hypothetical example
On a day session 5-minute chart, the exit condition is Time greater than or equal to 1340. The bar labeled 13:40 closes at 13:40, the condition is true, and the market order fills at the open of the next bar (13:40 to 13:45), at about 13:40 and a little after, with one bar still left before the close. Move the same condition to a 15-minute chart and 13:40 falls inside the bar from 13:30 to 13:45; the condition is not true until that bar closes at 13:45, the next bar is already 08:45 the next day, and the position is held overnight. A 15-minute chart needs at least 1330.
AlphaPrime (Valynox)'s backtest fills every candidate strategy on the next bar and charges commission and slippage, so the exit time on its report already includes that one-bar delay, and you do not add it back when you compare against actual fill times in MultiCharts.
Does SetExitOnClose work on the night session?
SetExitOnClose exits only on the bar the session template marks as the “close.” Whether the day and night sessions each fire once depends entirely on how the template is set up; if you do not want to leave the day trading exit to the template, write your own time condition. SetExitOnClose is a PowerLanguage command that tells MultiCharts to close all positions at the end of the session's last bar.
A session template is the setting in MultiCharts that defines how many trading sessions each instrument has in a day and the start and end time of each. SetExitOnClose reads where the “close” is from this template. Three common templates give three results:
- A day-session-only template (08:45–13:45). The close is 13:45, and SetExitOnClose exits on the 13:45 bar. The night session data is not on the chart at all.
- A template with the day and night sessions as two separate sessions. There are two closes, 13:45 and 05:00, and it exits once at each. This is the behavior day trading needs.
- A template that merges the day and night sessions into one continuous session. There is only one close, so it does not exit at 13:45 in the day session, and the position carries from the day session into the night session.
In a backtest, SetExitOnClose fills at the close price of the last bar. In live trading, this market order has to be sent before the close and find a counterparty, and whether it fills at that price depends on liquidity at the time and on your broker. Neither source of uncertainty is in your code; both are in the template and the market. The table compares the two approaches:
| Item | SetExitOnClose | Your own time condition |
|---|---|---|
| Where the exit time comes from | The close setting in the session template | The number in your code |
| A day-session-only template | Exits at 13:45 | Exits at the set time |
| A day plus night session template | Whether the two sessions are separate decides whether it exits once in each | Exits at the set time |
| When the template is changed | Behavior changes with it, and the code does not show it | Behavior stays the same |
| What the trader controls | Only on or off | The time, the buffer, and separate values for the day and night sessions |
In practice, keep both: your own time condition closes positions two bars before the close and is the main exit; SetExitOnClose stays as a backup, so if the time condition fails to fire because of a missing bar in the data, the template's close can still catch it. If the number of exits differs between the two in a backtest, the template's close and the close you assumed are not on the same bar.
How do you limit entries to once a day?
Use EntriesToday or a date variable you maintain yourself to check whether you have already entered today, and for the night session use the trading day, not the calendar day, as “today.” EntriesToday is a PowerLanguage function that returns the number of entries already made on a given date; pass in Date to get how many entries were made today, and when it is greater than zero, stop entering.
For the day session EntriesToday is enough, because the calendar day is the trading day. The night session runs from 15:00 to 05:00 the next day, and after midnight Date may already be the next day, so EntriesToday thinks it is a new day and one night session can see two entries: one before midnight and one after. The fix is to maintain a flag yourself, with the night session open time as the point where the trading day switches:
- Declare a variable that records whether you have entered during this trading day.
- On each new bar, first check whether a new trading day has begun. In the day session, check whether Date has changed; in the night session, check whether Time has just crossed 1500. When a new trading day begins, reset the flag to zero.
- Check whether you are inside the entry window. If not, skip.
- Check whether the flag is already set. If it is, skip without looking at the signal.
- Enter only if the signal is true, and set the flag to entered.
Put the flag reset before the entry check, and do not reverse the order, or the first bar of a new trading day will be blocked by the old flag. Also confirm how MultiCharts shows the Date value on bars after midnight: on a night session chart, print Date and Time with a simple plot or print statement and look, which is more reliable than guessing.
How do you confirm from a backtest that the exit actually ran?
After the backtest, look at two places: in the trade list, every exit time should be before the cutoff time and every entry date should equal the exit date; in the strategy performance report, “time in market” should be well under half. If time in market is near 90%, the time exit did not take effect and positions are being held across sessions. Time in market is the percentage of the whole backtest period during which you hold a position.
A day session day trading strategy holds a position at most from 09:00 to 13:40 each day, which is more than 90% of the five-hour day session, but the fourteen-hour night session is entirely flat. If the chart includes night session data, the upper limit of time in market is about 30%. With day-session-only data the limit is somewhat higher, but if even one trade is held overnight, the number jumps noticeably. Four checking steps:
- Open the trade list and sort by exit time. The latest one is the exit closest to the close.
- Confirm every exit time is before the cutoff time. With a 5-minute bar and the exit set at 13:40, no exit time should be later than 13:45.
- Confirm every entry date equals its exit date. For a night session strategy, change this to confirming that they fall within the same trading day.
- Read “time in market” and “average bars held per trade” in the report. A 5-minute day session day trade should not hold more than about 57 bars per trade (09:00 to 13:40).
The opposite example best shows what this number means. The following backtest was run by AlphaPrime on NQ (E-mini Nasdaq-100 futures) daily bars, 2019-01-02 to 2025-06-30, long and short, one contract, with $20 commission and $10 slippage charged on every trade. It does not test day trading code itself but a stand-in rule: a standard deviation channel as the only entry and exit condition, with no stop loss, no profit target, no filter and no time exit. It is the one kept, ranked by return to drawdown, from the 1,024 candidates tried in the search; picking the one that is kept in hindsight flatters the result, so the numbers do not mean this rule earned that money, and they are not a result a reader can expect.
Source: AlphaPrime product engine (CPU), run 2026-10-31. NQ daily, 2019-01-02 to 2025-06-30, long and short, 1 contract, $20 commission and $10 slippage per trade.
| Number | Result |
|---|---|
| Net profit (closed trades) | $116,360 |
| Max drawdown | $110,535 |
| Trades | 35 |
| Win rate | 54.29% |
| Profit factor | 1.487 |
| Average trade | $3,324.57 |
| Time in market | 95.01% |
What to look at here is time in market of 95.01% and 35 trades: only 35 trades in six and a half years means the position stayed in the market almost the whole time, and every trade spanned many sessions. If a day trading script backtests to a time in market close to this, it is a sign the time exit did not take effect, whatever the net profit looks like.

If the strategy is generated elsewhere and then put into MultiCharts, the exit time is the place most likely to drift in the move. How to import a strategy into MultiCharts and match the backtest explains step by step which settings to check after importing, and the PowerLanguage strategy comparison checklist lists the items to line up one by one, such as session, fill method and commission.
Next step: open the backtest report of the MultiCharts day trading strategy you are running now, read “time in market” first, then sort the trade list by exit time and see what time the latest one is. If any trade is later than the exit time you set, go back to the third section, move the exit condition one bar earlier and run it again.
Frequently asked questions
How do you write a MultiCharts day trading exit a few minutes before the close?
Write an independent condition that looks only at time and position: when Time is greater than or equal to the set value and you hold a position, close it all with a market order, with no indicator added. The set value must be at least one bar before the close, plus one buffer bar, because the condition becomes true when a bar closes and fills on the next one. On a day session 5-minute chart, 13:35 or 13:40 are common; on 15-minute bars, move it earlier to 13:30 or before.
How do you handle day and night session time conditions separately?
Day session time rises from 845 to 1345, so one “greater than or equal to and less than” comparison marks the window. The night session runs from 1500 to 2359 and jumps back to 0000 through 0500, so the same expression can never be true, and it must be split into “Time greater than or equal to 1500” or “Time less than or equal to 450.” The night session exit time falls in the early morning, a smaller number than the entry time, and must be written separately too.
Is EntriesToday enough for one entry per day?
For the day session it is, because the calendar day is the trading day, and when EntriesToday with Date passed in is greater than zero, you stop entering. In the night session, after midnight Date may change to the next day, EntriesToday treats one night session as two days, and you might enter twice. For the night session, maintain your own flag and reset it at the 15:00 open as the start of the new trading day, rather than looking at Date.
How much time in market in a backtest report is normal for day trading?
A day session day trade holds from after the open to before the close at most each day; if the chart includes night session data, the upper limit of time in market is about 30%, and with day-session-only data it is somewhat higher. If even one trade is held overnight, the number jumps noticeably. Near 90% means the time exit did not take effect; sort the trade list by exit time first and find the trades held overnight.
Does SetExitOnClose behave the same in live trading and in a backtest?
In a backtest it fills at the close price of the session's last bar; in live trading this market order has to be sent before the close and find a counterparty, and the fill price depends on liquidity at the time. In both cases it looks only at the close of the session template, and when the template changes, the behavior changes with it. Treat it as a backup, and use your own time condition for the main exit.
References
- Taiwan Futures Exchange. Mini-TAIEX Futures. https://www.taifex.com.tw/enl/eng2/mTX
- CME Group. E-mini Nasdaq-100 Futures Contract Specs. https://www.cmegroup.com/markets/equities/nasdaq/e-mini-nasdaq-100.contractSpecs.html

