I need sample expert code

 

hi everyone

Im a beginner and I need a sample code for reading candlestick parameters in mql.

I searched in internet and I find these commands. open[] , close[] , high[] , low[].

but those not work.

please advise me by a simple program code as an expert advisor to read for example 10 last candle parameters.

thanks.

 
ramin Beygi:

hi everyone

Im a beginner and I need a sample code for reading candlestick parameters in mql.

I searched in internet and I find these commands. open[] , close[] , high[] , low[].

but those not work.

please advise me by a simple program code as an expert advisor to read for example 10 last candle parameters.

thanks.

Example:

Forum on trading, automated trading systems and testing trading strategies

How to start with MQL5

Vladimir Karputov, 2020.03.20 09:52

Example CopyRates

The first form of calling CopyRates is used:

int  CopyRates(
   string           symbol_name,       // symbol name
   ENUM_TIMEFRAMES  timeframe,         // period
   int              start_pos,         // start position
   int              count,             // data count to copy
   MqlRates         rates_array[]      // target array to copy
   );


Please note that ArraySetAsSeries is applied to the ' rates ' array - in this case, rates [0] corresponds to the rightmost bar on the chart.

   MqlRates rates[];
   ArraySetAsSeries(rates,true);


The full code:

//+------------------------------------------------------------------+
//|                                            Example CopyRates.mq5 |
//|                              Copyright © 2020, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2020, Vladimir Karputov"
#property version   "1.000"
//--- input parameters
input uchar InpCount = 9; // Data count to copy
//---
datetime m_prev_bars                = 0;        // "0" -> D'1970.01.01 00:00';
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- we work only at the time of the birth of new bar
   datetime time_0=iTime(Symbol(),Period(),0);
   if(time_0==m_prev_bars)
      return;
   m_prev_bars=time_0;

   MqlRates rates[];
   ArraySetAsSeries(rates,true);

   int start_pos=0,count=(InpCount<1 || InpCount>9)?9:InpCount;
   if(CopyRates(Symbol(),Period(),start_pos,count,rates)!=count)
     {
      m_prev_bars=0;
      return;
     }

   string text="";
   for(int i=count-1; i>=0; i--)
     {
      text=text+
           TimeToString(rates[i].time,TIME_DATE|TIME_SECONDS)+
           " Open "+DoubleToString(rates[i].open,Digits())+
           " High "+DoubleToString(rates[i].high,Digits())+
           " Low "+DoubleToString(rates[i].low,Digits())+
           " Close "+DoubleToString(rates[i].close,Digits())+"\n";
     }
   Comment(text);
  }
//+------------------------------------------------------------------+


Result:

Example CopyRates


 
Vladimir Karputov:

Example:


THANKS.
 

In addition to what's been said:

The variables open, high, low, close are only available in indicator's OnCalculate.

//+------------------------------------------------------------------+
//| Custom indicator iteration 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[])
  {
    ...  <- use open, high, low, close here

The variables Open, High, Low, Close - note the upper case - could be used everywhere but are only available in MQL 4 .

In both MQL 4/5 we also have the functions iOpen, iHigh, iLow and iClose.

double currentHigh = iHigh(NULL,0,0);
double lastHigh    = iHigh(NULL,0,1);
double lastClose   = iClose(NULL,0,1);
 

ramin Beygi:

...

Forum on trading, automated trading systems and testing trading strategies

When you post code please use the CODE button (Alt-S)!

Use the CODE button

Reason: