Page 1 of 1
[GUIDE] Pine Script v6: Converting an old MQL4 indicator for TradingView – What to watch out for & how to fix it
Posted: Fri Aug 28, 2026 3:17 pm
by PTScalper
Hi traders/scalpers!
Many of us started coding on MT4, but the shift towards TradingView means eventually porting those old .mq4 files to Pine Script. With the recent release of Pine Script v6, the language has matured massively. However, the leap from MQL4 (which is C++ based) to Pine v6 can still break your brain if you don't understand the underlying paradigm shift. Here are the biggest traps you’ll face when converting an MQL4 indicator to Pine Script v6, along with practical examples of how to fix them.
1. The Execution Model (The "For Loop" Fallacy)
The MQL4 Way: In MQL4’s OnCalculate() or start(), you are given the entire history of the chart. You write a for loop to iterate through every bar from i = Bars - 1 down to 0.The Pine Script Way: Pine Script has a built-in, invisible loop. The script automatically executes once on every historical bar, from left to right, and then updates tick-by-tick on the live, forming candle.MQL4 Code:
Code: Select all
// Calculating a simple difference between Close and Open
for(int i = 0; i < limit; i++) {
Buffer[i] = Close[i] - Open[i];
}
Re: [GUIDE] Pine Script v6: Converting an old MQL4 indicator for TradingView – What to watch out for & how to fix it
Posted: Fri Aug 28, 2026 3:18 pm
by PTScalper
Pine Script v6 Fix:
Just drop the loop entirely. The close and open built-ins inherently reference the current bar being processed. To output the data, you just plot it.
Code: Select all
//@version=6
indicator("Close Open Diff")
// The loop is implicit. This calculates on every bar automatically:
diff = close - open
// Replaces MQL4 SetIndexBuffer
plot(diff, color=color.blue)
2. Strict Booleans (The C++ Implicit Cast Trap)
The MQL4 Way: Because MQL4 is C-based, a lot of old MT4 coders use integers as booleans. If a variable is 0, it’s false. If it’s 1 (or any non-zero), it’s true.The Pine Script Way (New in v6!): Pine v6 completely removed the implicit casting of int and float to bool. It also made it so booleans can no longer be na (null).
Code: Select all
int trendDir = 1; // 1 for up, -1 for down, 0 for flat
if(trendDir) {
// This runs because trendDir != 0
}
Pine Script v6 Fix:
You must now explicitly compare your integers.
Code: Select all
//@version=6
indicator("Strict Bool Example")
trendDir = 1
// if (trendDir) // <--- THIS WILL THROW A COMPILER ERROR IN V6
if (trendDir != 0) // <--- Correct v6 fix
label.new(bar_index, high, "Trending")
Bonus: Pine v6 also introduced short-circuit (lazy) evaluation for and/or. If the first condition is false, it stops checking the rest—meaning your conditional logic finally behaves exactly like MQL4!)
Re: [GUIDE] Pine Script v6: Converting an old MQL4 indicator for TradingView – What to watch out for & how to fix it
Posted: Fri Aug 28, 2026 3:19 pm
by PTScalper
3. Dynamic Multi-Symbol Data (Finally Fixed in v6!)
The MQL4 Way: If you wanted to build a dashboard or query multiple pairs, you just threw iMA("EURUSD", ...) inside a loop.The Pine Script Way: In older Pine versions, dynamically querying symbols was a nightmare that required hacky workarounds or hardcoding 40 different request.security() calls. Pine v6 enables Dynamic Requests by default. You can now loop through an array of strings and request external symbol data on the fly. Pine Script v6 Fix for Multi-Symbol Dashboards:
Code: Select all
//@version=6
indicator("v6 Dynamic Requests")
var symbols = array.from("EURUSD", "GBPUSD", "USDJPY")
if barstate.islast
for i = 0 to array.size(symbols) - 1
sym = array.get(symbols, i)
// In v6, request.security() can dynamically accept the 'sym' string inside a loop!
sym_close = request.security(sym, "15", close)
log.info(sym + " Close: " + str.tostring(sym_close))
Re: [GUIDE] Pine Script v6: Converting an old MQL4 indicator for TradingView – What to watch out for & how to fix it
Posted: Fri Aug 28, 2026 3:20 pm
by PTScalper
4. Arrays and Historical Offsets
The MQL4 Way: Close[1] means "1 bar ago". To access custom arrays, you use Array[index]. To get the last item in a dynamic array, you have to measure the size first.The Pine Script Way: Pine also uses close[1] to mean "previous bar's close". But if you are building actual data arrays (using array.new), Pine v6 introduced negative indexing. MQL4 Code (Accessing last element):
Code: Select all
double myLevels[];
int lastIndex = ArraySize(myLevels) - 1;
double lastLevel = myLevels[lastIndex];
Pine Script v6 Fix:
Instead of calculating the array size and subtracting 1, just use -1.
Code: Select all
//@version=6
indicator("v6 Negative Indexing")
var myLevels = array.new_float()
array.push(myLevels, close)
// Get the most recently added element without knowing the array size
lastLevel = array.get(myLevels, -1) // -1 gets the last element, -2 gets second to last!
Re: [GUIDE] Pine Script v6: Converting an old MQL4 indicator for TradingView – What to watch out for & how to fix it
Posted: Fri Aug 28, 2026 3:21 pm
by PTScalper
5. Static Variables and Preserving State
The MQL4 Way: If you want a variable to remember its value between ticks or bars, you declare it globally at the top of the file, or use the static keyword inside a function.
The Pine Script Way: Use the var or varip keywords.
var: Initializes the variable only on the first historical bar. It remembers its value from bar to bar.
varip: Remembers its value from tick to tick inside the real-time, currently forming candle.
MQL4 Code:
Code: Select all
static int signalCount = 0;
if(BuyCondition) {
signalCount++;
}
Pine Script v6 Fix:
Code: Select all
//@version=6
indicator("State Memory")
// Initializes once. Retains value across all bars.
var int signalCount = 0
buyCondition = ta.crossover(ta.sma(close, 10), ta.sma(close, 50))
if buyCondition
signalCount += 1 // Increment
plot(signalCount, title="Total Signals")
Summary
When migrating from MQL4 to Pine v6, the hardest part isn't the syntax—it's letting go of the procedural "looping over everything" mindset. Trust the implicit loop, respect the new strict v6 booleans, and take advantage of the massive upgrades to dynamic requests and arrays.
Has anyone else been porting old MT4 scripts lately? Drop your biggest headaches below!