Possible loss of data due to type conversion in MQL5: the fix
Short answer
Warning 43 means a double is stored in an int without an explicit cast. In ported Pine code it is almost always a length, bar count or offset: give every quantity the type it had in Pine, and cast explicitly at the one point where a double has to become an int.
MetaEditor prints possible loss of data due to type conversion from 'double' to 'int' (warning 43 in the compiler's output) whenever a double value is stored in an int without an explicit cast. The program still compiles. The cause, in code ported from Pine Script, is almost always a length, a bar count or an offset that was a loose number in Pine and is now a double that MQL5 has to truncate.
Why it happens when converting Pine Script
Pine has two numeric types, int and float, but it rarely makes you think about them. input.int() gives an int, input.float() gives a float, and the arithmetic rules decide the rest: / always produces a float, even for two ints, which is why ta.sma(close, len / 2) is rejected by TradingView and people write int(len / 2) instead. math.round(), math.floor() and math.ceil() return int. Lengths passed to ta.* functions must be ints, so a Pine script that compiles has already resolved every int/float question at the TradingView end.
When the script is ported by hand, that information is lost. Three habits produce the warning:
- Declaring every input as
double"to be safe".input double Length = 20;then flows intoint half = Length / 2;, a loop bound, or an array index. - Assuming
MathRound(),MathFloor()andMathCeil()return an int as their Pine counterparts do. In MQL5 all three returndouble.int longer = MathRound(Length * Mult);warns. - Mixing a
doubleinto index arithmetic.MathMin(i, Length)with adouble Lengthreturns adouble, and storing it in anintwarns. Using it directly as an index,close[i - Length], is worse: that is the error'-' - integer expression expectedand the file does not compile at all.
The compiler is telling you the truth. 20.0 / 2 is fine, but Length * 1.5 with Length = 21 is 31.5, and the assignment to an int silently truncates to 31. Pine would have rounded to 32 with math.round. Whether the warning is harmless depends on which operation you meant.
Fix
Give every quantity the type it had in Pine, and cast explicitly at the one point where a double has to become an int. The indicator below is a port of a Pine script with len = input.int(20), mult = input.float(1.5), half = int(len / 2), longer = math.round(len * mult), an SMA over half bars and a highest-high over longer bars. First the version that produces the messages, as MetaEditor reports them:
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2
#property indicator_type1 DRAW_LINE
#property indicator_type2 DRAW_LINE
input double Length = 20; // Pine: len = input.int(20)
input double Mult = 1.5; // Pine: mult = input.float(1.5)
double MaBuf[], HiBuf[];
int OnInit()
{
SetIndexBuffer(0, MaBuf, INDICATOR_DATA);
SetIndexBuffer(1, HiBuf, INDICATOR_DATA);
return INIT_SUCCEEDED;
}
int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[],
const double &open[], const double &high[], const double &low[], const double &close[],
const long &tick_volume[], const long &volume[], const int &spread[])
{
int half = Length / 2; // Pine: half = len / 2
int longer = MathRound(Length * Mult); // Pine: longer = math.round(len * mult)
int start = prev_calculated > 0 ? prev_calculated - 1 : 0;
for(int i = start; i < rates_total; i++)
{
int lookback = MathMin(i, Length); // Pine: math.min(bar_index, len)
double sum = 0.0;
for(int k = 0; k < half; k++)
sum += close[i - k];
MaBuf[i] = sum / half;
HiBuf[i] = close[i - Length]; // Pine: close[len]
MaBuf[i] += lookback * 0.0;
}
return rates_total;
}Compiling it gives three warnings and one error:
possible loss of data due to type conversion from 'double' to 'int' (line 23: int half = Length / 2)
possible loss of data due to type conversion from 'double' to 'int' (line 24: int longer = MathRound(...))
possible loss of data due to type conversion from 'double' to 'int' (line 28: int lookback = MathMin(i, Length))
'-' - integer expression expected (line 33: close[i - Length])The corrected program. The input is an int, the one deliberate conversion carries a cast, and the warm-up bars are handled the way Pine would return na:
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrOrange
input int Length = 20; // Pine: len = input.int(20) -> int
input double Mult = 1.5; // Pine: mult = input.float(1.5) -> double
double MaBuf[], HiBuf[];
int OnInit()
{
SetIndexBuffer(0, MaBuf, INDICATOR_DATA);
SetIndexBuffer(1, HiBuf, INDICATOR_DATA);
PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetDouble(1, PLOT_EMPTY_VALUE, EMPTY_VALUE);
return INIT_SUCCEEDED;
}
int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[],
const double &open[], const double &high[], const double &low[], const double &close[],
const long &tick_volume[], const long &volume[], const int &spread[])
{
const int half = MathMax(Length / 2, 1); // int / int stays int
const int longer = (int)MathRound(Length * Mult); // the cast says "I meant it"
const int need = MathMax(half, longer);
int start = prev_calculated > 0 ? prev_calculated - 1 : 0;
for(int i = start; i < rates_total; i++)
{
if(i < need) // Pine would give na here
{
MaBuf[i] = EMPTY_VALUE;
HiBuf[i] = EMPTY_VALUE;
continue;
}
double sum = 0.0;
for(int k = 0; k < half; k++)
sum += close[i - k];
MaBuf[i] = sum / half;
double hi = high[i];
for(int k = 1; k < longer; k++)
hi = MathMax(hi, high[i - k]);
HiBuf[i] = hi;
}
return rates_total;
}This compiles in MetaEditor 5 (build 6140) with no warnings. The rules it follows:
input.intbecomesinput int;input.floatbecomesinput double. Never widen an int input todouble.int / intis integer division in MQL5 and staysint, soLength / 2needs no cast. It also truncates, exactly like Pine'sint(len / 2).math.round(x)is(int)MathRound(x). The same applies toMathFloorandMathCeil. Write the cast where the Pine script had the int-returning function, and nowhere else.- Array indices and loop bounds are built from
intvariables only. If a value really is a double, convert it once into a namedintand index with that.
Other causes of the same message
- Time arithmetic.
datetimeis a 64-bit integer;int seconds = TimeCurrent() - t0;warns withfrom 'datetime' to 'int'. Keep differences inlongor cast. longtoint. Volumes inOnCalculatearelongarrays and position tickets areulong.int v = tick_volume[i];warns; uselongor cast when you really want 32 bits.- Points to pips.
int pips = (ask - bid) / _Point;is a double divided by a double. Wrap it:(int)MathRound((ask - bid) / _Point). - Enum and colour arithmetic.
colorand enum values take part in arithmetic as integers; storing a computeddoubleback into them warns the same way. - Converted code. The Stratlate converter keeps Pine's types: an
intvariable or input staysint, a float becomesdouble, and everyta.*length is wrapped in an explicit cast such aspine_sma(close, __pineBar, (int)(half)), so those lines compile silently. One case still produces the warning: a Pinemath.round()result assigned to an int variable is emitted asMathRound(...), which returnsdoublein MQL5, and MetaEditor reports warning 43 on that line. The value is already whole, so nothing is lost; add the(int)cast if you want a clean build. If your script names a globallen, you will also seedeclaration of 'len' hides global variablefrom the runtime include; that is a different warning and is also harmless.
Pitfalls
- Silencing with
(int)everywhere. A cast onMathRound(x)rounds; a cast onxalone truncates.(int)(21 * 1.5)is 31, Pine'smath.round(21 * 1.5)is 32. The indicator is then one bar off and looks like a seeding difference. - Making the input a
doubleso the user can type 14.5. Pine never allowed a fractional length, so the original could not do that either. Keep the input anint; MetaTrader's input dialog then rejects fractions for you. - Trusting a loop bound that warns.
for(int k = 0; k < Length; k++)with adouble Lengthcompiles without a warning because the comparison promotesk. The loop runs the right number of times, but thesum / Lengthafter it is then a double divided by a double and no longer matches the integer length Pine used. Fix the declaration, not the loop. - A value that was a float in Pine squeezed into
int. A Pinemath.max(bar_index, len)is an int;ta.atr(len) * multis a float. If a float ends up in anintyou have changed the formula, and the warning is the only hint you will get. - Warnings hidden by the error list. MetaEditor shows errors and warnings in the same Errors tab. When a build has errors, fix them, then compile again and read the warnings that remain; a conversion that "compiles with 12 warnings" usually has two or three that change values.
FAQ
Is "possible loss of data due to type conversion" an error or a warning?
A warning. The .ex5 is produced and runs. The value is truncated toward zero at the point of assignment, so 31.5 becomes 31 and -2.7 becomes -2. The program is wrong only if you expected rounding or a fractional value to survive.
Why does MathRound return a double in MQL5 when math.round returns an int in Pine?
MQL5's math functions mirror the C runtime, where round, floor and ceil operate on and return doubles. Pine's math.round, math.floor and math.ceil return int so that the result can be passed straight to a length parameter. When porting, write (int)MathRound(x) wherever the Pine script relied on an int result.
Does the Stratlate converter produce this warning?
Rarely. Int stays int, float becomes double and ta.* lengths carry explicit casts. A math.round() stored in an int variable is the one pattern that still warns because it is emitted as MathRound() without a cast; the stored value is whole, so the warning can be silenced with a cast without changing results.
Let the converter do the mechanical part
Paste the original script and get a conversion that compiles, with every construct that could not be carried over reported instead of hidden. Review, compile and backtest the result before trading.
Open the converter