I wrote this sample code for learning purposes but I can't seem to solve the warning message of "possible loss of data due to type conversion".
I have tried to search online but I can't seem to find a solution on how to pass the value of the MQL5 built-in functions without getting this warning.
The iAMA
built-in function of MQL5 returns an integer and this integer is getting assigned to a variable of type integer (as shown in the sample code below). I am not sure why there is a data loss warning if both of them are integers.
Note: The code is compiling and running well but for educational purposes, I would like to understand how to solve this warning message.
Your help will be much appreciated!
Here is the code:
// --- Information Properties
#property description "Adaptive Moving Average Function Test"
//--- Core Properties
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots 1
// --- Styling & Labels Properties
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrBlack
#property indicator_width1 2
#property indicator_label1 "Adaptive Moving Average"
//--- Constant Variables
const int ama_period = 34;
const int ama_fast_ma = 2;
const int ama_slow_ma = 34;
//--- Global Variables
double ama_src = PRICE_MEDIAN;
//--- Buffers
double ama_buffer[];
//--- Handles
int ama_buffer_handle;
//+------------------------------------------------------------------+
//| Indicator Initialization |
//+------------------------------------------------------------------+
int OnInit()
{
//--- Buffers Assignment
SetIndexBuffer(0, ama_buffer, INDICATOR_DATA);
//--- Accuracy Setting
IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
//--- First Bar Setting
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, ama_period - 1);
//--- Line Shifts Calculations
PlotIndexSetInteger(0, PLOT_SHIFT, 0);
//--- Labels Calculations
PlotIndexSetString(0, PLOT_LABEL, "AMA");
//--- Handles Calculations
ama_buffer_handle = iAMA(NULL, 0, ama_period, ama_fast_ma, ama_slow_ma, 0, ama_src);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| OnCalculate function |
//+------------------------------------------------------------------+
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[])
{
if(rates_total < ama_period)
{
return(0);
}
//--- Troubleshooting: Data Calculation
int calculated = BarsCalculated(ama_buffer_handle);
if(calculated < rates_total)
{
Print("Not all data of ama_buffer_handle is calculated (", calculated, " bars). Error ", GetLastError());
return(0);
}
//--- Troubleshooting: Data Copying
int to_copy;
if(prev_calculated > rates_total || prev_calculated < 0)
{
to_copy = rates_total;
}
else
{
to_copy = rates_total - prev_calculated;
if(prev_calculated > 0)
{
to_copy++;
}
}
//--- Copy & Validate Buffers
if(CopyBuffer(ama_buffer_handle, 0, 0, to_copy, ama_buffer) <= 0)
{
Print("getting ama_buffer_handle is failed! Error ", GetLastError());
return(0);
}
if(IsStopped())
{
return(0);
}
return(rates_total);
}
After reviewing back the documentation, I found out that I need to pass an ENUM_APPLIED_PRICE
to the iAMA
function instead of a double
variable. That resolves the problem.
As in: ENUM_APPLIED_PRICE ama_src = PRICE_MEDIAN;