Questions from Beginners MQL5 MT5 MetaTrader 5 - page 170

 
barabashkakvn:
Look in the indicator code, OnCalculate() function.

Doing the following:

int limit=rates_total-prev_calculated;
   double pr=close[0];
   //----
   if(rates_total-prev_calculated>=Period_)
      limit=limit-Period_;
      //----
   for(int i=limit-1;i>=0;i--)
     {
      CopyBuffer(ma1Handle,0,i,1,ma1Val);
      ExtMapBuffer1[i]=ma1Val[0]-pr;
     }
Nothing works. Everything is clear in MQL4. I do not know MQL5 very well. I guess some functions should be called to get the price?
 
forexman77:

I do as follows:

Nothing works. Everything is clear in MQL4. I don't know MQL5 very well. Perhaps, some functions have to be called to get the price?

Write in OnInit:

//--- установим индексацию для буфера как в таймсерии 
ArraySetAsSeries(ma1Val,true); 
 
forexman77:

I do as follows:

Nothing works. Everything is clear in MQL4. I don't know MQL5 very well. Maybe, some functions have to be called in order to get the price?

You can use CopyRates (similar to CopyBuffer) to access the price history.

The current Ask, Bid prices can be obtained using SymbolInfoTick or SymbolInfoDouble (as in Spread indicator).

 
barabashkakvn:

Write in OnInit:

Automated-Trading:
You can use CopyRates (similar to CopyBuffer) to access price history. The current values of Ask, Bid prices can be obtained using SymbolInfoTick.

Nothing works. Here's all the code:

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
#property  indicator_type1   DRAW_LINE
#property  indicator_color1  clrRed
#property  indicator_style1  STYLE_SOLID
#property  indicator_label1  "BUY TP"
input int Period_=34;
int ma1Handle;
double ma1Val[1];
double ExtMapBuffer1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0,ExtMapBuffer1,INDICATOR_DATA);
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,Period_);
   ArraySetAsSeries(ExtMapBuffer1,true);
   ma1Handle=iMA(_Symbol,_Period,Period_,0,MODE_EMA,PRICE_CLOSE);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
   int limit=rates_total-prev_calculated;
   //----
   if(rates_total-prev_calculated>=Period_)
      limit=limit-Period_;
      //----
   for(int i=limit-1;i>=0;i--)
     {
       MqlTick latest_price;       // Будет использоваться для текущих котировок
      if(!SymbolInfoTick(_Symbol,latest_price))
          {
          Alert("Ошибка получения последних котировок - ошибка:",GetLastError(),"!!");return(0);
          }
      double iclose=latest_price.bid;
      CopyBuffer(ma1Handle,0,i,1,ma1Val);
      ExtMapBuffer1[i]=iclose - ma1Val[0];
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
forexman77:

Nothing works. Here's all the code:

Why not? It's working for me:

The indicator is working, isn't it!

Files:
test.mq5  3 kb
 
Вbarabashkakvn:

I don't see why not. It works for me:

Visually, at first glance, it looks right. But when I recalculate the difference between the price and the moving average I get a completely different figure.

I checked on EURUSD/1.36421-1.36168=0.00253, and the indicator shows 0.00157.

 

Good afternoon.

Is there anywhere ready-made function codes in MQL5 language, like KimIV had in MQL4 http://forum.mql4.com/ru/38949 ?

Только "Полезные функции от KimIV". - MQL4 форум
  • www.mql5.com
Только "Полезные функции от KimIV". - MQL4 форум
 
forexman77:

Visually, at first glance, it looks right. But, having recalculated the difference between the price and the moving average, we get a completely different figure.

I checked on EURUSD/1.36421-1.36168=0.00253, and the indicator shows 0.00157 Screenshot attached.

Ah, well, you need to be more accurate in your requests. Now it is counting correctly.
Files:
test.mq5  3 kb
 
abeiks:

Good afternoon.

Is there anywhere ready-made function codes in MQL5 language, like KimIV had in MQL4 http://forum.mql4.com/ru/38949 ?

In mql5 not function codes are used, but object libraries. Functions are difficult to connect, in OOP it's easier: connect the required library, create an object and get the necessary methods in it - analogues of functions in algorithmic languages.
 
I signed up for a signal, can you tell me if I need the terminal to be running and the account to be open?
Reason: