VALYNOXSTRATEGY RESEARCH TECHNOLOGY
AlphaPrimeAlphaPrime
ACADEMY / Platforms and export

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 · Published · updated · Editorial policy

Contents
  1. How do you write MultiCharts day trading code that exits before the close?
  2. How do you write time conditions in PowerLanguage? Handle the day and night sessions separately
  3. How do you write an exit a few minutes before the close? The time depends on the bar size
  4. Does SetExitOnClose work on the night session?
  5. How do you limit entries to once a day?
  6. How do you confirm from a backtest that the exit actually ran?
  7. Frequently asked questions
  8. 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:

  1. 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.
  2. 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.
  3. 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.

A day-session trading script uses three time segments of the TAIEX futures day session: entries allowed from 09:00 to 12:30, no new entries from 12:30 to 13:40, forced exit from 13:40 to 13:45 Same session, three zones TAIEX futures day session, 08:45–13:45 09:00–12:30 Entry allowed 12:30–13:40 No new entries 13:40–13:45 Forced exit All three must be coded. So: code each zone separately.
A day trading script is three time windows on one session line: entry allowed, no new entries, forced exit. All three are required.

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.

Time values are HHMM: the day session rises steadily from 845 to 1345, so one range is enough; the night session runs from 1500 to 2359, jumps back to 0 at midnight and rises to 500, so two ranges joined with OR are needed Night times jump at midnight Time as HHMM, day vs night session Day 845–1345 One range Night 1500–2359 0–500 Two ranges Midnight One comparison fails at night. So: split it and join with OR.
In the day session the time values only increase, so one comparison marks the window; in the night session the time falls from 2359 back to 0000 at midnight, so the same expression must be split in two.

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:

ItemDay session (08:45–13:45)Night session (15:00–05:00 next day)
Does the session cross midnight?NoYes
How to write the entry windowOne part: “greater than or equal to” and “less than”Two parts joined with “or”
Forced exit time valueFor example 1340, greater than the entry timeFor example 455, a smaller number than the entry time
What counts as “today”Date equals the calendar dayThe night session open starts the trading day; you cannot rely on Date alone
The closing barThe bar labeled 13:45The bar labeled 05:00, already dated the next day
Four differences between night session and day session code: the night session crosses midnight, the entry window needs two ranges tested with OR, exit time numbers are smaller than entry time numbers, and “today” cannot be judged by Date alone How the night session differs Day-session logic does not carry over Midnight Crosses midnight Entry window Two ranges, OR Exit time Smaller number Today Not Date alone
How the day and night sessions differ in time, date and trading day. Check every time condition in night session code again.

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:

  1. Confirm the chart's bar size. The delay is different for 5-minute, 15-minute and 30-minute bars.
  2. 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.
  3. 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.
  4. 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.

The 1340 exit condition is true when the bar closes and fills on the next bar: on 5-minute bars it fills before the 13:45 close and the position goes flat; on 15-minute bars it becomes true at 13:45 and fills on the first bar of the next day Longer bars, earlier exit 1340 exit condition, 13:45 close 13:30 13:40 13:45 Next day 5-min Fill Flat 15-min Fill Condition true at bar close On 15-min bars, 1340 fills next day. So: set 1330 or earlier.
The condition becomes true at a bar's close and fills at the next bar's open. The longer the bar, the earlier the exit condition must be set, or the next bar is already the next day.

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:

ItemSetExitOnCloseYour own time condition
Where the exit time comes fromThe close setting in the session templateThe number in your code
A day-session-only templateExits at 13:45Exits at the set time
A day plus night session templateWhether the two sessions are separate decides whether it exits once in eachExits at the set time
When the template is changedBehavior changes with it, and the code does not show itBehavior stays the same
What the trader controlsOnly on or offThe time, the buffer, and separate values for the day and night sessions
Whether it exits at the 13:45 day session close: SetExitOnClose exits under a day-only template or a template with separate day and night sessions, but not under a merged template; a custom time exit fires under all three templates Put the main exit in code 13:45 exit? Depends on session template SetExitOnClose Custom exit Day only Separate Merged SetExitOnClose depends on template. So: use it only as backup.
SetExitOnClose behavior follows the session template; a time condition you write follows your code. The two can coexist, but the main exit belongs in the code.

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:

  1. Declare a variable that records whether you have entered during this trading day.
  2. 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.
  3. Check whether you are inside the entry window. If not, skip.
  4. Check whether the flag is already set. If it is, skip without looking at the signal.
  5. 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.

Order of checks for one entry per day: first check for a new trading day and reset the flag, then skip if outside the entry window, then skip if the flag is already set, and only then enter and set the flag when the signal is true; on the night session a new trading day starts at 15:00 Reset first, then check Night: new trading day at 15:00 1 2 3 4 New trading day? In entry window? Flag set? Signal true? Reset flag No: skip Yes: skip Enter, set flag A stale flag blocks the new day. So: reset before every check.
The order of checks for one entry a day: first check for a new trading day and reset the flag, then the window, then the flag, and the signal last. Night session bars after midnight still belong to the same trading day.

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:

  1. Open the trade list and sort by exit time. The latest one is the exit closest to the close.
  2. 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.
  3. 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.
  4. 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).
Four backtest checks that show the time exit works: sort the trade list by exit time, confirm no exit is later than 13:45, confirm each trade's entry and exit are on the same day, and confirm time in market is below 30%, not near 90% Prove the time exit works In the trade list and the report Sort by exit Latest exit on top Meets the cutoff None after 13:45 Same date Entry = exit date Time in market Under 30%, not 90%
Four checks that show the time exit works. Any exit after the cutoff time, or any trade whose entry and exit dates differ, means a gap in the code.

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.

NumberResult
Net profit (closed trades)$116,360
Max drawdown$110,535
Trades35
Win rate54.29%
Profit factor1.487
Average trade$3,324.57
Time in market95.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.

Look at the “time in market” row. A rule with no time exit condition keeps its position in the market almost from start to finish; this is what the report looks like when day trading code is missing its forced exit.
Look at the “time in market” row. A rule with no time exit condition keeps its position in the market almost from start to finish; this is what the report looks like when day trading code is missing its forced exit. NQ · daily · 2019-01-02 to 2025-06-30 · CPU engine backtest

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