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];
}