Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 664

 
Roman Sharanov:

I don't understand how to write indicators, what is the problem?

I need a line to be drawn showing the difference in price between active_1 and active_2

it works

I also need an average ma_period ma_period to be plotted on this line, there is a problem with it - it is not displayed

You do not check the result of copying.

You copy the entire history on every tick.

The number of Bars(), not rates_total, should be copied from a non-native symbol (not the current chart symbol).

Every two minutes, you need to request, for example, current bar time for a non-native symbol to keep its history data up-to-date.

Use SimpleMAOnBuffer() to smooth the dataBuffer data

 
Artyom Trishkin:

You do not check the result of copying.

You copy the entire history on each tick.

A non-native symbol (not the current chart symbol) should have Bars() number copied, not rates_total.

Every two minutes, you need to request, for example, current bar time for non-native symbol to keep its history data up to date.

Use SimpleMAOnBuffer() to smooth the dataBuffer data

like this? stopped working at all

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
//---
   int bars1 = Bars(active_1, timeframe), bars2 = Bars(active_2, timeframe);
   
   if(CopyClose(active_1,timeframe,0,bars1,firstBuffer) < 0) return 0;
   if(CopyClose(active_2,timeframe,0,bars2,secondBuffer) < 0) return 0;
   if(bars1 != bars2) return 0;
   int first, bar;
   
   if(prev_calculated == 0) first = begin; else first = prev_calculated - 1;
   
   for(bar = first; bar<bars1; bar++){
      dataBuffer[bar] = firstBuffer[bar]-secondBuffer[bar];
      maBuffer[bar] = SimpleMA(bar, ma_period, dataBuffer);
   }
   SimpleMAOnBuffer(bars1, prev_calculated, begin, ma_period, dataBuffer, maBuffer);
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
void OnTimer(){
   iTime(active_1,timeframe,1);
   iTime(active_2,timeframe,1);
}
void OnDeinit(const int reason){
   EventKillTimer();
}
 

Greetings.

Could you please tell me how to record some values when an EA opens an order, so that they refer to that particular order, for example on a ticket.

 
Andrey Sokolov:

Greetings.

Could you please tell me how to record some values when an EA opens an order, so that they refer to that particular order, for example on a ticket.

In the comment

 

I want to observe the volatility. For this purpose I calculate the close-open difference in tmp1 array and then use it to plot SMA for a period of interest.


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 i;
double tmp1[];

      Print("rates_total = ",rates_total);
      for(i=1; i<rates_total; i++)
      {
      tmp1[i]=MathMax(open[i],close[i])-MathMin(open[i],close[i]);      
      ip1Buf[i]=iMA(NULL,0,1,0,0,tmp1[i],0);
      }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }


No errors occur at compilation, the terminal doesn't draw the indicator, the log shows CADJPY,H1: array out of range in '_t1.mq4' (65,11)

This is the line where the differenceMathMax-MathMin is calculated, what may be the reason?


 
psyman:

I want to observe the volatility. For this purpose I calculate the close-open difference in tmp1 array and then use it to plot SMA for a period of interest.



No errors occur at compilation, the terminal doesn't draw the indicator, the log shows CADJPY,H1: array out of range in '_t1.mq4' (65,11)

This is the line where the differenceMathMax-MathMin is calculated, what may be the reason?


I haven't understood the code, but if so:

for(i=1; i<rates_total-1; i++)
 

It doesn't work.


UPD

Simplified string to bare minimum tmp1[i]=close[i]; Error remains the same.

 
psyman:

I want to observe the volatility. For this purpose I calculate the close-open difference in tmp1 array and then use it to plot SMA for a period of interest.



No errors occur at compilation, the terminal doesn't draw the indicator, the log shows CADJPY,H1: array out of range in '_t1.mq4' (65,11)

This is the line where the differenceMathMax-MathMin is calculated.


Array tmp[] is declared as a dynamic array and is initially of zero length. Hence the error.

You should declare this array as an indicator buffer for calculations.

 
 

Forum on trading, automated trading systems & strategy testing

Any questions for beginners in MQL4, help and discussion on algorithms and codes

Alexey Viktorov, 2018.10.16 12:31

Array tmp[] is declared as a dynamic array and is initially of zero length. Hence the error.

This array should be declared as an indicator buffer for calculations.


Why can't we use a dynamic array?

In this case I have the closing price overwritten there without any calculations.

Where can I read how to use indicator buffers?

Reason: