Quick Answer
The message "Maximum number of orders (9000) was reached" appears when a Pine Script strategy places more orders than the backtest can keep. TradingView's support page for this message says the strategy placed or closed more orders than the allowed maximum, and that the limit is 9000. The Pine limitations page confirms the number: a script can place up to 9000 orders when backtesting, and when it goes beyond that, the older orders are trimmed to make room for new ones.
The support page gives two fixes. The first is to convert the strategy to Pine Script v6, where, according to the page, all orders above the limit are trimmed, and each new order is added to the List of Trades while the oldest one above the limit is removed. The second is to limit the dates in which the strategy can place orders, with a start and an end timestamp in the entry conditions. This post shows both, a small strategy that you can use as a base, and what the limit means for the results that you compare with MQL5 or cTrader.
From the author: I convert Pine strategies to MQL5 and cTrader C#. A strategy that reaches 9000 orders is almost always a lower-timeframe strategy on a long history. The number you see in TradingView then covers only part of the history, so comparing it with a full MT5 backtest gives different totals. That is the main thing to know before you compare.
Key Facts
| Topic | What the documentation says |
|---|---|
| Message | Maximum number of orders (9000) was reached |
| The limit | 9000 orders in a backtest (limitations page) |
| What happens above it | Older orders are trimmed to make room for new ones |
| Pine v6 | The support page says all orders above the limit are trimmed, the oldest one leaves the List of Trades |
| Deep Backtesting | The limitations page gives a higher cap, 1,000,000 orders |
| Find the first kept trade | strategy.closedtrades.first_index |
| Does the strategy stop? | The support page does not say |
The limits can change. Check the TradingView support page and the Pine limitations page before you rely on a number.
A Strategy That Can Reach the Limit
This strategy trades on every cross of two averages. On a one-minute chart with a long history it can place thousands of orders:
//@version=6
strategy("Many orders", overlay = true)
fast = ta.sma(close, 5)
slow = ta.sma(close, 10)
if ta.crossover(fast, slow)
strategy.entry("Long", strategy.long)
if ta.crossunder(fast, slow)
strategy.close("Long")
Each entry and each exit is an order. I have not run this script in TradingView, so the number of orders on your chart depends on the symbol, the timeframe and the history.
Fix 1: Use Pine Script v6
The support page lists the conversion to v6 as the first step. In v6 the oldest orders above the limit are trimmed and the newest ones stay in the List of Trades. If the script starts with //@version=5 or older, convert it and check the compile messages. See Pine Script v6 to MQL5 for what changes in v6 syntax.
Fix 2: Limit the Dates
The second fix is to place orders only in a date range. This also makes the backtest faster to read, because it covers a period that you chose:
//@version=6
strategy("Date range", overlay = true)
startTime = input.time(timestamp(2025, 1, 1), "Start")
endTime = input.time(timestamp(2025, 12, 31), "End")
inRange = time >= startTime and time <= endTime
fast = ta.sma(close, 5) // history functions at the top level
slow = ta.sma(close, 10)
longSignal = ta.crossover(fast, slow)
if inRange and longSignal
strategy.entry("Long", strategy.long)
if not inRange
strategy.close_all()
ta.sma() and ta.crossover() are called at the top level on purpose. Calls to history functions inside an if can trigger the CW10003 warning, see the CW10003 post.
Fix 3: Know Which Orders Are Still in the List
When orders are trimmed, the list starts later than the first trade of the history. The limitations page names strategy.closedtrades.first_index for this: it gives the index of the earliest closed trade that was not trimmed. A script that loops over the closed trades should start at that index, not at 0:
//@version=6
strategy("First kept trade", overlay = true)
if barstate.islast
int firstKept = strategy.closedtrades.first_index
label.new(bar_index, high, "First kept trade index: " + str.tostring(firstKept))
Fix 4: Fewer Orders by Design
If the date filter is not what you want, reduce the number of orders:
| Change | Effect |
|---|---|
| A higher timeframe | Fewer bars, fewer signals |
| A filter on the signal (trend, session, volatility) | Fewer entries |
| One position at a time | No repeated entries (check pyramiding in the declaration) |
| A minimum number of bars between trades | Fewer quick re-entries |
A strategy that places thousands of orders may also be trading mostly noise, so check the signal before you only change the limit. This is a design question, not a result: do not read a lower order count as a better strategy.
What This Means When You Compare With MQL5 or cTrader
| Pine Script | MQL5 Strategy Tester | cTrader backtest | |---|---|---| | Orders above 9000 are trimmed from the start of the history | The tester keeps the full history of the period you select | The same: the full period | | Total trades in the Strategy Tester summary | Counts every trade in the period | Counts every trade in the period |
If you compare totals (number of trades, net profit) between TradingView and MT5, first set both to the same date range and make sure TradingView did not trim the start. Otherwise the numbers differ for a reason that has nothing to do with the code. See why a Pine strategy gives different results in MQL5 for the other causes, and Pine Script strategy.entry to cTrader C# for the order side of the conversion.
Similar Pine Script Messages
| Message | What it means | Where to look | |---|---|---| | Maximum number of orders (9000) was reached | Too many orders in the backtest | This post | | Calculation takes too long to execute | A script time limit was passed | Calculation takes too long | | Memory limits exceeded | The script uses too much memory | Memory limits exceeded | | Historical offset beyond the buffer's limit | A buffer is too small | Historical offset error | | Function should be called on each calculation | A history function runs conditionally | CW10003 warning |
Checklist: What to Check When You See "Maximum Number of Orders"
- Check the Pine version and convert to v6 if the script is older.
- Add a start and end date to the entry conditions.
- Close open positions outside the date range.
- Check how many orders the strategy really places per day.
- Use
strategy.closedtrades.first_indexif your code reads the list of closed trades. - Set the same date range before you compare with MQL5 or cTrader.
- Review the signal if the order count is much higher than you expected.
- Test the converted strategy on a demo account.
Common Mistakes
- Comparing totals from a trimmed backtest with a full backtest.
- Reading the order count as a quality measure.
- Looping over closed trades from index 0.
- Putting history functions inside the date condition.
- Forgetting to close the position when the date range ends.
- Staying on an old Pine version.
FAQ
Related Guides
- Pine Script calculation takes too long to execute: the time limits.
- Pine Script memory limits exceeded: the memory limit.
- Pine Script function should be called on each calculation: history functions and conditions.
- Why a Pine strategy gives different results in MQL5: where differences come from.
- Pine Script strategy.entry to cTrader C#: entries in C#.
- Pine Script strategy.close to MQL5: closing positions.
- Pine Script v6 to MQL5: what changes in v6.
- Pine Script to MQL5 guide: converting a script step by step.
Convert Without Guessing
If you want a first draft of your Pine Script strategy in MQL5 or cTrader C#, try the CodeFlowOS converter. It produces a draft that you still have to review, and the compiler check is optional. Outputs are not guaranteed, and code that compiles can still have wrong logic, so always compare the result with the chart. If the converted code does not compile, paste it with the message into the Error Fixer: it suggests a minimal fix and shows what changed (1 credit, with up to 3 attempts included).
This article is educational. It does not describe trading performance and is not financial advice. Test every strategy on a chart or a demo account before you rely on it.