Skip to content

Seven valid v6 forms that TradingView runs and Codegen refuses or mishandles #372

Closed

Description

@letiep97

Summary

Each script below saves and runs on TradingView and gives the same trades as its control, which differs from it in one place. PineForge runs every control and gives TradingView's trades; it refuses or mishandles the script itself.

# Form Codegen 1.11.0
1 Untyped parameter read with [], called inside another function with a value derived from that function's untyped parameter ANALYZER Cannot infer the per-callsite primitive type
2 var const <type> X = … (and varip const) PARSER Expected EQUALS
3 request.security_lower_tf() with a tuple expression refused inline; from a function, C++ that does not compile
4 input.string() whose defval or options is a var variable holding a literal input marked unsupported: every value set for it stops the run
5 A tab and spaces mixed in one line's indentation PARSER Expected INDENT or LEXER refusal
6 In a request.security payload, a helper that calls na()/nz() on its parameter, next to time(<tf>), timeframe.change(<tf>) or a tuple element from a user function CODEGEN Unknown variable 'x'
7 A function that is never called and passes its parameter to another function as a ta.* length CODEGEN Unsupported TA constructor length

Versions

pineforge-codegen 1.11.0 with pineforge-engine v1.11.0 (a4472324), and Codegen 1.10.0 with v1.10.0 (59f98ba8): the same results on both. Apple clang 17.0.0, macOS 15.8 arm64. BTCUSDT 1h bars from 2024-01-01 to 2024-03-01 (1,441 bars), initial capital 10000, no commission or slippage.

TradingView: BINANCE:BTCUSDT, 1h, Deep Backtesting over the same dates, measured on 2026-11-10.

Trade counts include the last trade of the range, which TradingView leaves open and PineForge closes on the last bar; net profits are over the other, closed trades on both sides. Scripts 5–7 declare default_qty_value = 0.01 with no default_qty_type; TradingView and PineForge both size each order at 0.00002 BTC, hence net profits below 1 USD.

1. Untyped parameter read with [], argument derived from the caller's untyped parameter

//@version=6
strategy("untyped history parameter, local argument", overlay = true, initial_capital = 10000)
change(src) =>
    src - src[1]
wrapper(x) =>
    s = x
    change(s)
v = wrapper(close)
m = ta.sma(v, 20)
if ta.crossover(v, m)
    strategy.entry("Long", strategy.long, qty = 0.01)
if ta.crossunder(v, m)
    strategy.close("Long")
ANALYZER 3:1 Cannot infer the per-callsite primitive type of history parameter 'src' in callable 'change' variant cs0; declare its type explicitly.

Control: change(float src) =>. It runs with v equal to close - close[1] on every bar: 351 trades, net profit 23.14.

In Pine v6 an untyped parameter takes the type of its argument in each call (type keywords); here every argument is "series float".

wrapper body (with the untyped change(src)) Codegen With change(float src) TradingView
s = x / change(s) refused 351 trades 351 trades
change(ta.sma(x, 3)) refused 191 trades 191 trades
change(smooth(x)), smooth(src) => ta.ema(src, 3) refused 244 trades 244 trades

These translate with the untyped change(src): the same call at top level (s = close / v = change(s)), wrapper(float x), float s = x, and close > open ? change(close) : change(open).

In 1.11.0, _callsite_primitive_expr_type (analyzer/call_handlers.py) returns UNKNOWN for an identifier that is not a parameter of the calling function and for any call other than the int()/float()/bool() casts, so the final check of the per-call-site loop in analyzer/base.py raises the error.

Workaround: a type keyword on either parameter or on the local.

2. var const and varip const

Pine v6 declares a variable as [export ][var |varip ][[qualifier ]<type> ]<identifier> = <expression> (single-variable declarations). The form appears in open-source v6 scripts published on TradingView.

//@version=6
strategy("var const declaration", overlay = true, initial_capital = 10000)
var const color LINE_COLOR = color.blue
fast = ta.ema(close, 10)
slow = ta.ema(close, 30)
plot(fast, color = LINE_COLOR)
if ta.crossover(fast, slow)
    strategy.entry("Long", strategy.long, qty = 0.01)
if ta.crossunder(fast, slow)
    strategy.close("Long")
PARSER 3:17 Expected EQUALS, got IDENT('LINE_COLOR').

Control: const color LINE_COLOR = color.blue. 26 trades, net profit 29.93, on PineForge and TradingView.

Other declarations as line 3 (Codegen only; these were not run on TradingView):

Line 3 Codegen
var const string C = "x" PARSER 3:11 Expected EQUALS, got TYPE_STRING('string').
var const float C = 1.5 PARSER 3:11 Expected EQUALS, got TYPE_FLOAT('float').
var const int C = 3 PARSER 3:11 Expected EQUALS, got TYPE_INT('int').
varip const int C = 3 PARSER 3:13 Expected EQUALS, got TYPE_INT('int').
var simple int C = 3 PARSER 3:12 Expected EQUALS, got TYPE_INT('int').
const color C = color.blue, var color C = color.blue translate

A var const float used in the calculation, also run on TradingView:

//@version=6
strategy("var const float in a calculation", overlay = true, initial_capital = 10000)
var const float BAND = 0.002
fast = ta.ema(close, 10)
slow = ta.ema(close, 30)
if fast > slow * (1 + BAND)
    strategy.entry("Long", strategy.long, qty = 0.01)
if fast < slow
    strategy.close("Long")

PARSER 3:11 Expected EQUALS, got TYPE_FLOAT('float'). Control without var: 21 trades, net profit −32.54, on PineForge and TradingView; TradingView gives the same for the script.

In 1.11.0, _parse_var_keyword_decl (parser.py) reads an optional type right after var/varip and then the name; the branch that consumes a leading qualifier runs only when the statement starts with it.

Workaround: drop var/varip in front of const.

3. request.security_lower_tf() with a tuple expression

TradingView returns a tuple of arrays for a tuple expression (tuples of intrabar data).

//@version=6
strategy("security_lower_tf with a tuple", overlay = true, initial_capital = 10000)
[closes, opens] = request.security_lower_tf(syminfo.tickerid, "15", [close, open])
float body = array.size(closes) > 0 ? array.last(closes) - array.first(opens) : close - open
fast = ta.ema(body, 5)
slow = ta.ema(body, 20)
if ta.crossover(fast, slow)
    strategy.entry("Long", strategy.long, qty = 0.01)
if ta.crossunder(fast, slow)
    strategy.close("Long")
ANALYZER 3:44 request.security_lower_tf does not support tuple expressions yet. Issue separate request.security_lower_tf calls for each series.

The engine's docs/coverage.md states this limit, so for the inline form this is a request to support it. Control: one call per series (closes = request.security_lower_tf(syminfo.tickerid, "15", close), likewise opens). 222 trades, net profit 49.77, on PineForge and TradingView; TradingView gives the same for the script.

The function form, replacing line 3 of the script above, is not refused:

closeOpen() =>
    [close, open]
[closes, opens] = request.security_lower_tf(syminfo.tickerid, "15", closeOpen())

It translates with no diagnostic. The generated C++ declares std::vector<double> _req_sec_lower_tf_0, fills it with push_back(std::make_tuple(bar.close, bar.open)), declares double closes = 0.0; and calls closes.size(). clang: member reference base type 'double' is not a structure or union. Until tuples are supported, this form should get the inline form's refusal.

Workaround: one call per series.

4. input.string() whose defval or options is a var variable

//@version=6
strategy("input.string default from a var variable", overlay = true, initial_capital = 10000)
var DEFAULT_MODE = "Long only"
mode = input.string(DEFAULT_MODE, "Mode", options = ["Long only", "Long and short"])
fast = ta.ema(close, 10)
slow = ta.ema(close, 30)
if ta.crossover(fast, slow)
    strategy.entry("Long", strategy.long, qty = 0.01)
if ta.crossunder(fast, slow)
    if mode == "Long and short"
        strategy.entry("Short", strategy.short, qty = 0.01)
    else
        strategy.close("Long")

A variable initialized with a literal is "const" with or without var: the const qualifier page uses var PLOT1_TITLE = "High" as a plot() title. Control: DEFAULT_MODE = "Long only" (no var).

Script Control
inputs from transpile_full "default": "", "options": [], "supported": false "default": "Long only", both options, "supported": true
run, no input set 26 trades, net profit 29.93 the same
run, Mode = "Long only" PineForge rejected setting 'Mode': input cannot be honoured by this compiled strategy 26 trades
run, Mode = "Long and short" the same rejection 52 trades (26 long, 26 short), net profit −40.68

On TradingView the script with "Mode" set to "Long and short" gives the control's 52 trades and net profit −40.68.

The same happens when only the options come from var variables (var LONG_ONLY = "Long only", var LONG_SHORT = "Long and short", options = [LONG_ONLY, LONG_SHORT]): "options": [], "supported": false, both values refused.

In 1.11.0, _string_setting_arg (codegen/checked_settings.py) keeps a string default or option only when its lowered C++ is a string literal; the var variable lowers to the script member.

Workaround: declare the variable without var, or write the literal in the call.

5. A tab and spaces in one line's indentation

TradingView reads a tab as one level of four spaces. Below, → stands for a tab character and · for a space; replace them with real tabs and spaces before saving.

//@version=6
strategy("Repro", overlay = true, initial_capital = 10000, default_qty_value = 0.01)
fast = ta.ema(close, 9)
slow = ta.ema(close, 21)
if ta.crossover(fast, slow)
→if close > open
→····strategy.entry("Long", strategy.long)
if ta.crossunder(fast, slow)
    strategy.entry("Short", strategy.short)

PARSER 7:6 Expected INDENT, got IDENT('strategy').: the tab followed by four spaces counts as one level.

With lines 6 and 7 indented ····if close > open and ····→strategy.entry(...): LEXER 7:6 Indentation cannot contain spaces followed by a tab.

Controls: lines 6 and 7 indented → and →→, or ···· and ········. 59 trades, net profit 0.15, on PineForge and TradingView; TradingView gives the same for both scripts.

The lexer (lexer.py, _tokenize_line) takes the level from the tab count alone when a line contains a tab, and refuses a space before a tab. Counting a tab as four columns wherever it appears (for example len(raw.expandtabs(4)) // 4) gives both forms their TradingView level. The comma-continuation branch below it also measures a tabbed line by count("\t") * 4.

Workaround: four spaces per level, or tabs only.

6. na()/nz() helper in a request.security payload

//@version=6
strategy("Repro", overlay = true, initial_capital = 10000, default_qty_value = 0.01)
valid(x) => na(x) ? 0 : 1
weekStart = request.security(syminfo.tickerid, "D", valid(close) * time("W"))
fast = ta.ema(close, 9)
slow = ta.ema(close, 21)
if ta.crossover(fast, slow) and time >= weekStart
    strategy.entry("Long", strategy.long)
if ta.crossunder(fast, slow)
    strategy.entry("Short", strategy.short)

CODEGEN 3:16 Unknown variable 'x' — not a PineForge builtin or a declared variable.

Control: the helper written inline, (na(close) ? 0 : 1) * time("W"). 71 trades, net profit 0.08, on PineForge and TradingView; TradingView gives the same for the script.

It happens only when the payload also contains time(<tf>), timeframe.change(<tf>) or an element of a global tuple returned by a user function. Each part alone translates, and so does a helper that does not call na()/nz() on its parameter (x > 0 ? 1 : 0, math.abs(x)). The same error with valid(x) => nz(x) and with valid(float x) in the script above, and in the two scripts below.

6b: timeframe.change() in the payload
//@version=6
strategy("Repro", overlay = true, initial_capital = 10000, default_qty_value = 0.01)
valid(x) => na(x) ? 0 : 1
dailyScore = request.security(syminfo.tickerid, "D", valid(close) + (timeframe.change("W") ? 1 : 0))
fast = ta.ema(close, 9)
slow = ta.ema(close, 21)
if ta.crossover(fast, slow) and dailyScore == 1
    strategy.entry("Long", strategy.long)
if ta.crossunder(fast, slow)
    strategy.entry("Short", strategy.short)

CODEGEN 3:16 Unknown variable 'x'. TradingView: 63 trades. (No PineForge control was run for this form.)

6c: a tuple element from a user function, read through the payload function
//@version=6
strategy("Repro", overlay = true, initial_capital = 10000, default_qty_value = 0.01)
channel() =>
    upper = ta.highest(high, 20)
    lower = ta.lowest(low, 20)
    [upper, lower]
[upper, lower] = channel()
valid(x) => na(x) ? 0 : 1
score() => valid(close) + valid(upper)
dailyScore = request.security(syminfo.tickerid, "D", score())
fast = ta.ema(close, 9)
slow = ta.ema(close, 21)
if ta.crossover(fast, slow) and dailyScore == 2
    strategy.entry("Long", strategy.long)
if ta.crossunder(fast, slow)
    strategy.entry("Short", strategy.short)

CODEGEN 8:16 Unknown variable 'x'. Control: [upper, lower] = channel() moved inside score():

score() =>
    [upper, lower] = channel()
    valid(close) + valid(upper)

51 trades, net profit 0.11, on PineForge and TradingView; TradingView gives the same for the script.

With valid(x) => x > 0 ? 1 : 0 instead (no na()), the error becomes request.security payload reads 'upper', an element of a tuple declaration it cannot re-evaluate (PF-E3044). If a tuple element from a user function stays unsupported in a payload, that message is clearer than Unknown variable on the helper's parameter.

6d: related, the history of a tuple element in the payload
//@version=6
strategy("Repro", overlay = true, initial_capital = 10000, default_qty_value = 0.01)
channel() =>
    upper = ta.highest(high, 20)
    lower = ta.lowest(low, 20)
    [upper, lower]
[upper, lower] = channel()
dailyAbove = request.security(syminfo.tickerid, "D", close > upper[1])
fast = ta.ema(close, 9)
slow = ta.ema(close, 21)
if ta.crossover(fast, slow) and dailyAbove
    strategy.entry("Long", strategy.long)
if ta.crossunder(fast, slow)
    strategy.entry("Short", strategy.short)

request.security payload reads the history of 'upper', an element of a tuple declaration (PF-E3047). Control: upper = ta.highest(high, 20) instead of the tuple line. 3 trades, net profit −0.10, on PineForge and TradingView; TradingView gives the same for the script. Tuples from built-ins (ta.macd, ta.supertrend) read the same way translate.

Workaround: write the na()/nz() test inline; call the tuple-returning function inside the payload function.

7. An uncalled function passing its parameter on as a ta.* length

//@version=6
strategy("Repro", overlay = true, initial_capital = 10000, default_qty_value = 0.01)
smooth(src, len) => ta.rma(src, len)
smoothTwice(src, len) => smooth(smooth(src, len), len)
fast = smooth(close, 9)
slow = ta.ema(close, 21)
if ta.crossover(fast, slow)
    strategy.entry("Long", strategy.long)
if ta.crossunder(fast, slow)
    strategy.entry("Short", strategy.short)

CODEGEN 3:27 Unsupported TA constructor length 'len' for ta::RMA: it is neither a compile-time constant nor derived from an input, so PineForge cannot size the indicator buffer.

smoothTwice is never called. Control: the script without that line. 50 trades, net profit −0.02, on PineForge and TradingView; TradingView gives the same for the script. Calling smoothTwice once with a constant (plot(smoothTwice(close, 9))) also translates. An unused function that uses its parameter directly as a length (unused(src, len) => ta.rma(src, len) * 2) does not trigger it. Close to the uncalled request.security wrapper of #294.

Workaround: delete the unused function.

Expected

Each script translates and gives its control's trades, as on TradingView. For case 3, until tuples are supported, the function form is refused like the inline form.

Activity

  1. luisleo526 commented on Nov 10, 2026

    @luisleo526
    Contributor

    Thank you for the seven forms, each with its control and TradingView's result.

    We are reproducing each form on the current codegen. For each one we are confirming whether it is refused, or builds and gives different trades. We will post what we find here, with which forms are fixed in which release.

  2. luisleo526 commented on Nov 10, 2026

    @luisleo526
    Contributor

    Known issue (form 4): confirmed as a silent wrong value. Thank you for listing it. Forms 1-3 and 5-7 are refusals or build failures; form 4 is different. Details:

    What happens. With

    var DEFAULT_MODE = "Long only"
    mode = input.string(DEFAULT_MODE, "Mode", options = ["Long only", "Long and short"])
    

    the generated C++ reads the input with an EMPTY default (get_input_string("Mode", std::string(""))), so mode is the empty string, not "Long only", whenever no value is set for the input. The run does not stop and prints no error. A strategy whose logic depends on the default option (for example if mode == "Long only") therefore gives different trades from TradingView. The script in the issue only tests the other option (mode == "Long and short"): the empty string and "Long only" take the same branch there, so its trades equal its control's, as the issue measured.

    Measured. Codegen 1.11.0 with engine 1.11.0, and again on the current codegen main, 8,000 ETHUSDT 15-minute bars, no input value set. A script that tests the default option: 264 trades. The same script with the variable declared without var (the control): 132 trades. The script of the issue and its control: 132 trades each at the default and 264 each with Mode set to "Long and short". The input manifest lists the input as "supported": false with default "" and no options.

    Versions. The rule that turns such a default into the empty string is in the source of every release from 0.8.1 up to and including 1.11.0 (checked in each tag); the translation of the script was checked on 0.10.4, 1.0.0, 1.5.0, 1.9.0, 1.10.0 and 1.11.0, and on each the generated C++ reads the empty default where the control reads "Long only". The "supported": false mark in the manifest exists from 1.3.0.

    Other input types. A bool input and a float input whose default is a var variable give the same trades as their controls (checked on 1.11.0 and on the current main). An int input that sizes an indicator is refused in the script and in its control alike, which is a different limit.

    Workaround, verified by output. Declare the variable without var (DEFAULT_MODE = "Long only"), or write the literal in the call (input.string("Long only", "Mode", options = ["Long only", "Long and short"])). Both give the control's trades (132 in the check above), and the manifest then shows default "Long only", both options and "supported": true.

    Fix. A change is being prepared that carries the literal of a variable that is declared once and never assigned, and refuses the script, with an error that names the variable, when it cannot prove that. It will not fall back to an empty default. It will be named in the release notes.

  3. luisleo526 commented on Nov 10, 2026

    @luisleo526
    Contributor

    Form 4 is fixed on main (#374): a string input default or option held by a global variable is now published as its string, when the variable is declared once before the input with a string literal and never reassigned. A default that cannot be proven this way is now refused with PF-E5096 instead of silently becoming an empty string. It ships in the next codegen release. The other six forms stay open in this issue.

  4. letiep97 commented on Nov 13, 2026

    @letiep97
    Author

    Checked on 1.12.0: forms 1 and 4 are fixed, thank you.

    Checked on the 1.12.0 release (engine v1.12.0 6e27d0f2, pineforge-codegen 1.12.0), built from source on macOS 15.8 arm64, with the scripts from this issue (BTCUSDT 1h, 2024-01-01 to 2024-03-01, capital 10000, no commission).

    • Form 1 (an untyped parameter read with [] that receives a value derived from the caller's untyped parameter): the three variants (local argument, built-in call argument, user call argument) now translate and give their controls' trades, 351, 191 and 244.
    • Form 4 (input default or options from a var string): both variants give their controls' trades.
    • Forms 2, 3, 5, 6 and 7 are as before: var const and varip const, request.security_lower_tf with a tuple, mixed tab and space indentation, a helper calling na()/nz() inside request.security, and an uncalled function passing a parameter to a ta.* length are still refused.
  5. luisleo526 commented on Nov 13, 2026

    @luisleo526
    Contributor

    Status 2026-11-13: forms 1 and 4 are fixed in 1.12.0, as you confirmed. The other forms are not fixed yet; we post the plan and date for each of them here by 2026-11-14.

    🤖 Generated with Claude Code

  6. luisleo526 commented on Nov 14, 2026

    @luisleo526
    Contributor

    Status 2026-11-14: work on the fix is under way, but its date is not established yet. We will post the date here by 2026-11-14 18:00 (UTC+8).

  7. luisleo526 commented on Nov 14, 2026

    @luisleo526
    Contributor

    Status 2026-11-14 17:00 (UTC+8): a fix for form 2 is written but not yet fully verified. For form 5 (mixed indentation), we are checking how TradingView treats each indentation combination before we change the translator. The date is not established yet. We will post the next update here by 2026-11-15 12:00 (UTC+8).

  8. luisleo526 commented on Nov 15, 2026

    @luisleo526
    Contributor

    Status 2026-11-15 11:10 (UTC+8): not done yet, and we do not have a date.

    The translator changes for your forms are written, but we still have to check each form against TradingView: for several of them we need TradingView's own compile result (accept or refuse, with the message) and the plotted value, and we are adding that check to our TradingView comparison tooling now. Form 3 also needs a run on genuine 15-minute intrabars. We will post here when those checks have run, with a date, and the merge commit when the fix lands.

  9. luisleo526 commented on Nov 15, 2026

    @luisleo526
    Contributor

    Fixed on main in f848df5 (#411). Persistent typed qualifiers such as var const are accepted, mixed indentation levels complete, tuple-valued lower-timeframe requests lower to independently typed arrays, and helper bindings and tuple histories are kept on the requested evaluator. The regressions in tests/test_v6_declaration_request_compilation.py reproduce 14 parser, code generation and C++ compilation failures on the previous main and pass now, with the existing controls unchanged. If one of your scripts still fails to compile on the current main, please post it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions