I can't figure out why I can't compile this script.

 

This is my script.


#property strict

#property indicator_chart_window



// Make sure the EMA period is a positive integer

input int EMA_Period = 60;



// Make sure the risk and risk-reward ratio are positive integers

input int Risk = 2;

input int RiskRewardRatio = 2;



// Make sure the lot size is a positive double value

input double Lots = 0.1;



double EMA_High;

double EMA_Low;



// Initialize the EA

int OnInit()

{

  // Set the EMA line style and color

  SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2, Blue);

  SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 2, Blue);



  // Indicate that the EA has been successfully initialized

  return(INIT_SUCCEEDED);

}



// Calculate the EMA values and plot them on the chart

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[])

{

  // Only calculate and plot the EMAs if there is new price data

  if(prev_calculated != rates_total)

  {

    // Calculate the EMA using the high as the source

    EMA_High = iMA(NULL, 0, EMA_Period, 0, MODE_EMA, PRICE_HIGH, prev_calculated);



    // Calculate the EMA using the low as the source

    EMA_Low = iMA(NULL, 0, EMA_Period, 0, MODE_EMA, PRICE_LOW, prev_calculated);



    // Plot the EMAs on the chart

    PlotIndexSetDouble(0, EMA_High, (int) rates_total);

    PlotIndexSetDouble(1, EMA_Low, (int) rates_total);

  }

  // Get the current bid and ask prices

  double bid = MarketInfo(Symbol(), MODE_BID);

  double ask = MarketInfo(Symbol(), MODE_ASK);



  // Check for a super bullish candle with a close near its high

  if(close[rates_total-1] >= high[rates_total-1] * 0.98)

  {

    // Check if the candle is between or above the EMAs

    if(close[rates_total-1] > EMA_High && close[rates_total-1] < EMA_Low)

    {

      // Calculate the stop loss and take profit levels

      double stopLoss = (double) low[rates_total-1] - 1.0;

      double takeProfit = (double) stopLoss + (stopLoss * Risk / 100.0) * RiskRewardRatio;



      // Open a long position with a 2% risk and fixed lot size

      int ticket = OrderSend(NULL, OP_BUY, Lots, ask, 3, stopLoss, takeProfit, "Long position", 0, clrNONE);

      if(ticket < 0)

      {

        // An error occurred, print the error code to the log

        Print("Error opening long position: ", GetLastError());

      }

    }

  }

  // Check for a super bearish candle with a close near its low

  if(close[rates_total-1] <= low[rates_total-1] * 0.98)

  {

        // Check if the candle is between or below the EMAs

    if(close[rates_total-1] < EMA_Low && close[rates_total-1] > EMA_High)

    {

      // Calculate the stop loss and take profit levels

      double stopLoss = (double) high[rates_total-1] + 1.0;

      double takeProfit = (double) stopLoss - (stopLoss * Risk / 100.0) * RiskRewardRatio;



      // Open a short position with a 2% risk and fixed lot size

      int ticket = OrderSend(NULL, OP_SELL, Lots, bid, 3, stopLoss, takeProfit, "Short position", 0, clrNONE);

      if(ticket < 0)

      {

        // An error occurred, print the error code to the log

        Print("Error opening short position: ", GetLastError());

      }

    }

  }



  // Indicate that the calculations were successful

  return(rates_total);

}


When I compile it said 2 warnings "possible loss of data due to type conversion Double EMAs Gold Digger.mq4 55 27" and "possible loss of data due to type conversion Double EMAs Gold Digger.mq4 56 27", any suggestions to fix this?

 
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  2. We have no idea which line is 55, since you didn't use code button, everything is double spaced.

  3. You haven't specified how many buffers you're going to have, nor created them. Nothing to display.

  4. EMA_High = iMA(NULL, 0, EMA_Period, 0, MODE_EMA, PRICE_HIGH, prev_calculated);

    You returned rates_total, what bar are you reading?

  5. Arrays must be manually sized. They have no direction. You must move elements if you want a stack/as-series (set non-series, enlarge the array, set as-series).

  6. Buffers are automatically size, are as-series, and elements are moved for you, new elements are set to EMPTY_VALUE (or your designated. They can also draw on the chart automatically.

  7. In MT4, buffers and MT4 predefined arrays are all ordered AsSeries. There is a difference between the arrays passed to OnCalculate (e.g. low[]) and the MT4 predefined variables (e.g. Low[].) The passed arrays have no default direction, just like MT5.

    To determine the indexing direction of time[], open[], high[], low[], close[], tick_volume[], volume[] and spread[], call ArrayGetAsSeries(). In order not to depend on default values, you should unconditionally call the ArraySetAsSeries() function for those arrays, which are expected to work with.
              Event Handling Functions - Functions - Language Basics - MQL4 Reference

  8.      int ticket = OrderSend(NULL, OP_BUY, Lots, ask, 3, stopLoss, takeProfit, "Long position", 0, clrNONE);

    Indicators can not trade.

    Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum (2020)