Quick Answer
The compile error "Compiled code contains too many tokens" (CE10117 in the TradingView documentation) means that your script is too large after compilation. The documentation gives the limit: "The maximum possible size of a script's IL translation is 100,256 tokens." The limit applies to the optimized intermediate form that the compiler builds, not to the number of lines or characters in your source, and the documentation says that there is no direct way to see this size when a script compiles.
The first thing to know is what does not help. Removing comments, removing whitespace, renaming variables, merging lines, replacing constants with literals and deleting unused code do not reduce the compiled size, because the compiler already removes them. What helps is to replace repeated statements with a loop, move repeated logic into a function, use built-ins instead of hand-written code, generate data at runtime instead of listing it, and split the script into libraries or separate indicators. This post shows each fix with a short script, the limits table from the documentation, and what to keep in mind when you convert a very large Pine script.
From the author: I convert Pine Script to MQL5 and cTrader C#. The largest scripts that I get are "all in one" dashboards with dozens of conditions. They are close to the limit, hard to read, and hard to convert. Shrinking them first, with the methods below, often makes the conversion simpler as well.
Key Facts
| Topic | What the documentation says | |---|---| | Message | Compiled code contains too many tokens (CE10117) | | Limit per script or library | 100,256 tokens of compiled (IL) code | | Imported libraries | The total size of the libraries a script imports can be up to 1 million tokens | | Chained indicators | Up to 24 indicator-on-indicator connections on a chart, and up to 11 indicators connected in a sequence | | What counts | The optimized compiled form, not the source text | | What does not help | Comments, whitespace, renaming, merging lines, literal for constant, unused code removal | | Can you check the size? | Not directly, when the script compiles successfully |
The numbers in this table are from the CE10117 page. The limitations page repeats the 100,256 and 1 million figures.
What Uses Tokens
The documentation lists the usual causes:
| Cause | Why it adds tokens |
|---|---|
| Large constants | Each int, float, bool and string literal is embedded. One array.from() with thousands of values can use the whole budget |
| Long constant strings | Strings are embedded, and each use at a call site adds to it |
| Unrolled statements | Many separate statements that the compiler cannot reduce |
| Calls with many parameters | Each argument counts, including defaults |
| Custom code that copies a built-in | A hand-written calculation usually costs more than the built-in |
| One huge script | Too much logic in one indicator or strategy |
One more detail from the page: enum members without an explicit title embed their name as a string, so they can add to the size.
Fix 1: Replace Unrolled Statements With a Loop
//@version=6
indicator("Loop example")
float total = 0.0
for i = 0 to 29
total += close[i]
plot(total / 30)
One loop body is stored once, while 30 separate + close[n] terms are stored 30 times. The documentation warns not to put calls such as ta.rsi() in a loop, because they cause the compiler warning CW10003 (see the CW10003 post).
Fix 2: Put Repeated Logic in a Function
The documentation says that function calls are not inlined, so the body of a function is stored once:
//@version=6
indicator("Function example")
calcROC(int length) => (close - close[length]) / close[length] * 100
float total = calcROC(5) + calcROC(10) + calcROC(20) + calcROC(40)
plot(total / 4)
Fix 3: Use Built-ins
//@version=6
indicator("Built-in example", overlay = true)
plot(ta.sma(close, 20), "SMA")
A built-in such as ta.sma is cheaper than a loop that you write yourself to do the same.
Fix 4: Generate Data, Do Not List It
A long literal list is expensive. Build the values when the script starts:
//@version=6
indicator("Generated data example")
var array<float> weights = array.new<float>()
if barstate.isfirst
for i = 1 to 200
weights.push(i * i)
plot(weights.size())
The documentation notes that this moves cost from compile time to run time, so fill such data on the first bar only. A related memory topic is in the Memory limits exceeded post.
Fix 5: Split the Script
When one script keeps growing, split it:
- Libraries. Move reusable functions into an exported library. The documentation says that libraries are compiled independently, and that a script can import libraries whose total size is up to 1 million tokens.
- Separate scripts. Put independent tasks into separate indicators.
- Chained indicators. Pass results between indicators with plots and source inputs. Remember the connection limits in the table above.
What the Source Size Does Not Tell You
The limitations page also mentions that the compilation request cannot exceed 5MB, and that this limit includes unused code. That is a different limit from the token limit: unused code does not change the tokens but it does count toward the request size. For the token limit, the limitations page says that removing unused code by hand has zero impact.
When You Convert a Very Large Script
MQL5 and C# have no such token limit for a program, so a converted file will compile. What changes is how readable the result is:
| Pine Script habit | In the converted code |
|---|---|
| 40 near-identical if blocks | The same 40 blocks in C# or MQL5, hard to review |
| A huge array.from() | A static array that you can load from a file or build in OnInit() |
| One script with indicator, dashboard and strategy | Better as an indicator class, an EA and a helper |
Before you convert, apply Fixes 1 to 4, so that the converter has clean loops and functions to work with. See Pine Script to MQL5 guide and, for state, Pine Script var and varip to MQL5.
Similar Pine Script Messages
| Message | What it means | Where to look | |---|---|---| | Compiled code contains too many tokens | The script is too large after compilation | This post | | Memory limits exceeded | The script uses too much memory while running | 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 | | Undeclared identifier | A name Pine does not know | Pine undeclared identifier |
Checklist: What to Check When You See "Too Many Tokens"
- Do not spend time on comments, whitespace or names. They do not change the size.
- Find repeated statements and replace them with a loop.
- Find repeated logic and move it into a function.
- Replace hand-written code with built-ins where one exists.
- Replace long literal lists with generated data on the first bar.
- Shorten constant strings that are used many times.
- Split the script into a library or separate indicators.
- Compile after each change to see which one helped.
Common Mistakes
- Deleting comments and expecting the error to go.
- Renaming variables to shorter names.
- Putting
ta.*calls in a loop to save lines. - Embedding a large data table in the source.
- Keeping everything in one script when parts are independent.
- Forgetting that unused code still counts toward the 5MB request size.
FAQ
Related Guides
- Pine Script memory limits exceeded: the runtime memory error.
- Pine Script historical offset beyond the buffer limit: a buffer that is too small.
- Pine Script function should be called on each calculation: why
ta.*calls do not belong in loops. - Pine Script var and varip to MQL5: keep state between bars.
- Pine Script cannot use plot in local scope: conditional plots.
- Pine Script v6 to MQL5: what changes when you convert a v6 script.
- Pine Script vs MQL5: the main differences between the two languages.
- Pine Script to MQL5 guide: converting a script step by step.
- Pine Script calculation takes too long to execute: the script time limits and the Profiler.
Convert Without Guessing
If you want a first draft of your Pine Script 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 script on a chart or a demo account before you rely on it.