How to test and tune an AI-written Pine Script strategy
Check an AI-written Pine Script strategy for code errors and test assumptions before tuning inputs and reviewing unseen data.
On this page
- How do you test an AI-written Pine Script strategy?
- What should you ask the AI to explain before testing?
- How do you check that the draft is a strategy?
- How can you check entries and exits on the chart?
- What data and timing problems need a review?
- Which test assumptions should you record?
- How do you decide which inputs are worth tuning?
- How can you compare input combinations by hand?
- What should happen after you choose a candidate?
- Sources
Key takeaways
- A script that compiles still needs a review of its trading rules and test assumptions.
- Inspect individual simulated trades before comparing different input combinations.
- Keep the script and test conditions fixed while comparing parameter values.
- Use data held out from tuning to check the chosen version without revising it against that same data.
How do you test an AI-written Pine Script strategy?
Start by checking the generated code against a written description of the strategy, then inspect its simulated trades under recorded test conditions before changing any inputs. Tune values only after you understand what they control, and reserve a separate period of data for checking the chosen version.
A script written by artificial intelligence, or AI, is a draft rather than evidence that the requested rules were translated correctly. Compilation, the process of checking code so it can run, catches some code errors but cannot establish that an entry, exit or sizing rule matches your intent.
Before opening a test, describe what starts and ends a position, which direction the script may use, when a condition is considered complete, and any limits on repeated entries. Compare that description with the code rather than relying on the explanation that came with the draft or the appearance of the chart.
If a condition is unclear, ask for an explanation of the relevant expression and check the answer against the code and the Pine documentation. Keep a copy of the original draft and note each material change, so an error correction is not confused with a later change to the trading idea.
The Pine Editor workflow provides context for working with the code, while the checks here focus on whether the draft behaves as described in your notes. A test can help you inspect that behavior, but it cannot turn an unclear rule into a clear one without further review.
What should you ask the AI to explain before testing?
A useful request describes behavior rather than asking for an attractive test report, because the code needs a clear rule to implement and a reviewer needs a clear rule to check. Name the intended entry and exit conditions, the chart data they use, and whether a condition must wait until the bar closes before it is evaluated.
A bar is a chart interval containing price information for that interval, and an open bar is one that has not yet finished forming. State how the script should treat that unfinished interval, since the condition observed while it forms may differ from the condition visible after it closes.
Ask the AI to identify assumptions about position direction, sizing, repeated entries and situations where entry and exit conditions overlap in the draft. If its answer adds a rule you did not specify, resolve that difference in your written description before testing the code on a chart.
A focused request could say: Explain each entry and exit condition in plain language, identify adjustable inputs, and list assumptions about bar timing and order fills without changing the trading rules. Use the response as a reading aid, then trace each explanation back to the relevant expression in the script before accepting it.
If the explanation and code disagree, ask about the specific expression instead of requesting a complete rewrite that may introduce unrelated changes to the rules. Avoid sharing private account information or code you do not have permission to disclose when a small, non-sensitive example would answer the question you are asking.
How do you check that the draft is a strategy?
TradingView’s script structure documentation, checked 2026-11-18, says a declaration statement identifies the script type and affects how the script can be used and executed.
Look for a strategy() declaration rather than assuming that a chart display is a testable strategy with the entries and exits you intended to review. An indicator can display a condition without describing the complete sequence of simulated actions needed for your review of the proposed rules.
Check the language version named in the draft and read any editor messages in that context before accepting a suggested correction to the code. An AI response can mix syntax or assumptions, so a repair deserves the same review as the original code and the same comparison with your notes.
When a compilation error appears, note the message and relevant line before requesting a correction, and ask the AI to explain why its proposed change addresses the error. Also ask whether the correction alters a trading rule, because a working script may still behave differently from the draft you originally requested.
After compilation, read the declaration and the entry and exit logic again against your written description of the intended behavior on the chart. A repair that removes an error can still change position direction, omit an exit or alter the timing described in your original notes about the strategy.
Keep a version that compiles and label later edits separately, so you can compare changed expressions when a revised draft behaves differently during a test. That comparison makes it easier to distinguish a code change from a difference in the chart, test period or input values used for the review.
How can you check entries and exits on the chart?
TradingView’s strategy documentation, checked 2026-11-18, says Pine Script strategies simulate trades across historical and realtime bars for backtesting and forward testing.
A backtest is a simulation of a strategy against historical data, and its report describes what the code did under the chosen test conditions. It does not establish what would have happened in actual trading, where prices and fills may differ from those used in the simulation.
Inspect individual simulated trades rather than starting with a summary, so that you can compare the reported actions with the rules in your written description. For each example, trace the condition that prompted the entry, the bar on which it was evaluated, the reported fill and the condition that ended the position.
Choose examples that include a normal entry, an exit and a situation where conditions overlap or appear close together on the chart you are reviewing. Compare the trade record with your written rules, not just with the explanation supplied by the AI when it generated the original draft.
If an entry appears later than expected, distinguish the time when the condition was evaluated from the time assumed for the simulated fill. The difference may reflect a test setting, a code behavior or a misunderstanding in the original request, so record what you find before changing the script.
If a trade is missing, check whether another condition blocked it or whether an existing position affected the action described in your notes. The Strategy Tester workflow provides context for inspecting a report, but a coherent summary is not a substitute for tracing individual simulated trades.
What data and timing problems need a review?
TradingView’s repainting documentation, checked 2026-11-18, defines repainting as script behavior that causes historical and realtime calculations or plots to behave differently.
Repainting is not a single code defect, so ask which behavior occurs and whether it matters for the particular rule you are trying to test. Review whether the script uses information from an unfinished bar, draws an event into the past, or requests data from another timeframe for a calculation.
Lookahead bias is the use of information in a historical test before it would have been available at that point in the time sequence. Ask the AI to identify expressions that could introduce it, then verify its answer against the relevant documentation and the actual code in the draft.
For data from another timeframe, write down when the requested value becomes available and when the strategy uses it to evaluate a specific condition. A line that seems sensible on a completed historical chart may need closer review when the underlying interval is still open and its value can change.
Chart type also deserves a note in the test record, especially when the chart uses synthetic prices rather than standard prices for the simulation. Synthetic prices are calculated chart values that may not represent traded prices, so check whether the prices used in the test fit your question.
A change intended to address timing may alter the strategy itself, even when the edit is presented as a technical correction by the AI. Save the revised script as a separate version, explain the change in your notes, and repeat the trade-level checks before comparing the test reports.
Which test assumptions should you record?
TradingView’s strategy properties documentation, checked 2026-11-18, lists commission, slippage, order size and fill settings among the properties that determine strategy behavior.
Commission is a fee associated with a transaction, while slippage is a difference between an expected price and a fill price. Both are assumptions to document in a simulation, rather than details to leave unexplained in the AI’s draft or omit from your test notes.
Record the symbol, chart timeframe, test period, chart type and script version alongside the strategy properties, so another review can use the same context. A timeframe is the interval represented by each chart bar, while the test period is the span of data being examined in the simulation.
Also note the sizing approach and the assumptions for orders and fills, including any choices that affect when the simulated action can occur on the chart. An order describes a requested action, while a fill is its simulated completion at a price under the test model used for the report.
Compare the property values with the plain-language specification you wrote before testing, and flag anything that the generated code added without a clear explanation. If the draft contains a value you cannot explain, find out what it controls before deciding whether it belongs in the test you plan to run.
Keep these conditions fixed when comparing input values, unless the purpose of the experiment is to examine a particular assumption. The guide to modeling slippage and commission provides more context for examining those costs as a separate part of the review.
How do you decide which inputs are worth tuning?
TradingView’s inputs documentation, checked 2026-11-18, says inputs receive values that users can change from a script’s settings.
A parameter is a value that controls part of a strategy, such as a lookback length or a threshold used in a trading condition. An input exposes a value for adjustment, but exposing it does not establish that the value represents a meaningful trading rule in your written description.
For each candidate input, write down what it changes and which part of the original description it represents before including it in a comparison. If you cannot explain the relationship, inspect the code or ask a focused question before adding that input to a search for alternative values.
Check whether changing the input affects the entry, exit, sizing or display, and note that purpose next to the input name in your record. A display choice may help with inspection, but it is different from a value that changes the simulated trading rules you are trying to compare.
Make sure the code uses the adjustable value where you expect, rather than assuming that a label proves the rule is connected to it. An AI draft may declare an input but leave a fixed value elsewhere, making a test appear to vary a rule that has not changed.
A range describes the values you plan to examine, and a step describes the spacing between those values in the planned comparison. The guide to choosing parameter ranges explains how to frame that choice without treating a particular value as a recommendation.
How can you compare input combinations by hand?
Start with the verified script and a written test plan that lists the inputs to vary and the candidate values for each selected input. Record the conditions that must remain fixed, including the data period and strategy properties, so the comparisons address the question you intended to test.
For each combination, change the selected inputs, run the test and record the values alongside the observations you intend to compare across the results. Keep a consistent record even when a test has few trades or reveals unexpected behavior, because those observations are part of the code review.
A small table can keep the comparison readable without turning a single summary into the whole decision or obscuring a change to the script:
| Record | What to write down |
|---|---|
| Script version | The saved code and the reason for any change |
| Inputs | The values used for this test |
| Conditions | The chart, period and relevant strategy properties |
| Review | Trade examples, report observations and unresolved questions |
Repeat the trade-level check if a combination produces behavior that differs sharply from nearby values, and trace the relevant conditions before drawing a conclusion. The difference may reflect a change in the rule’s behavior, a boundary in the code or a test condition that was not held fixed.
Manual comparison becomes harder to audit as combinations accumulate, particularly when notes do not show which inputs and conditions belong to each test in the record. TradingTune is a free Chrome and Brave extension that automates TradingView strategy optimization.
Do not edit the strategy logic midway through a parameter comparison and treat the resulting runs as interchangeable, because the rules being examined have changed. Save the changed script as another version and begin a separate comparison with its own record of inputs and conditions for that version.
TradingTune for Chrome
Stop tuning your strategy's inputs by hand.
Free tier, no API keys. A free account is required to run. Not affiliated with TradingView, Inc.
What should happen after you choose a candidate?
A candidate is a script version and set of inputs selected for further checking, not a proven outcome or a reason to overlook unresolved questions. Before reviewing it on reserved data, record the exact code, inputs and test assumptions that produced the selection during the earlier comparison.
Set aside a period that was not used to tune the inputs, and record its boundaries before you inspect the candidate on that data. This is a holdout period, meaning data reserved for a later check rather than used to choose among the earlier candidates in your comparison.
Run the frozen candidate on that period and inspect its simulated trades using the same questions you used during the initial review of its behavior. Look for rule behavior, timing and assumptions that need explanation, rather than relying on a summary without checking what the script actually did.
If the review leads you to revise the code or inputs, the period has informed that revision and is no longer an untouched check for the revised candidate. Record the change and treat a later independent period as a new check, if suitable data is available for a separate review.
The guide to in-sample and out-of-sample testing explains how a fitting period and a held-out period serve different roles in the review. The guide to avoiding overfitting covers the risk of tailoring choices too closely to historical data during a search.
Finish by recording what remains uncertain, including assumptions about fills and any behavior you could not trace back to a written rule. A clean compilation and a completed holdout check describe a review process, not a promise about future trading.
Sources
TradingTune for Chrome
Stop tuning your strategy's inputs by hand.
TradingTune runs systematic parameter searches on your own TradingView strategy, right inside the chart page. You keep the strategy, you read the results.
Free tier, no API keys. A free account is required to run. Not affiliated with TradingView, Inc.
TradingTune for Chrome
Stop tuning your strategy's inputs by hand.
TradingTune runs systematic parameter searches on your own TradingView strategy, right inside the chart page. You keep the strategy, you read the results.
Free tier, no API keys. A free account is required to run. Not affiliated with TradingView, Inc.
TradingTune for Chrome
Stop tuning your strategy's inputs by hand.
TradingTune runs systematic parameter searches on your own TradingView strategy, right inside the chart page. You keep the strategy, you read the results.
Free tier, no API keys. A free account is required to run. Not affiliated with TradingView, Inc.
TradingTune for Chrome
Stop tuning your strategy's inputs by hand.
TradingTune runs systematic parameter searches on your own TradingView strategy, right inside the chart page. You keep the strategy, you read the results.
Free tier, no API keys. A free account is required to run. Not affiliated with TradingView, Inc.
TradingTune for Chrome
Stop tuning your strategy's inputs by hand.
TradingTune runs systematic parameter searches on your own TradingView strategy, right inside the chart page. You keep the strategy, you read the results.
Free tier, no API keys. A free account is required to run. Not affiliated with TradingView, Inc.