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

 
Алексей КоКоКо:
Please advise mql5, I want to collect statistics of the average size of eg a 12 hour candle, today for example is Friday, and I want to take the data from Thursday, Wednesday, Tuesday and Monday for the calculations.
I take time[] I convert TimeToString, then SplitString and TD. Is there a faster and less cumbersome way to refer to the same time candle a day or more back? Of course, I could run with the number of candlesticks of the current timeframe per day, but I'm afraid that if there are some holes in the quotes, they will be shifted.

I think we cannot do without the loop. But the loop can be built in different ways. Pay attention to CopyRates()

int  CopyRates(
   string           symbol_name,       // имя символа
   ENUM_TIMEFRAMES  timeframe,         // период
   datetime         start_time,        // с какой даты
   int              count,             // сколько копируем
   MqlRates         rates_array[]      // массив, куда будут скопированы данные
   );

We set PERIOD_H1, start_time - date 12:00 and count 1. At the next iteration, we add to this date or subtract from it (depending on which way the loop is organized) PeriodSeconds(PERIOD_D1)

 
MAKSIM KASHFYLGAIANOV:
Please tell me how to enumerate indicator parameters in iCustom.
You can set an empty string instead of string variables, but enumerations... we need to see how they are written in the code.
 

I have a stochastic indicator with allergy, it gives a signal on crossing of moving lines. Please tell me how to make it give a signal by crossing the slips only when it is above or below the overbought or oversold zone.

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 LightSeaGreen
#property indicator_color2 Red
#property indicator_color3 Blue
#property indicator_color4 Red
#property indicator_level1 80
#property indicator_level2 20
#property indicator_maximum 100
#property indicator_minimum 0

//---- input parameters
/*************************************************************************
PERIOD_M1   1
PERIOD_M5   5
PERIOD_M15  15
PERIOD_M30  30 
PERIOD_H1   60
PERIOD_H4   240
PERIOD_D1   1440
PERIOD_W1   10080
PERIOD_MN1  43200
You must use the numeric value of the timeframe that you want to use
when you set the TimeFrame' value with the indicator inputs.
---------------------------------------
MODE_SMA    0 Simple moving average, 
MODE_EMA    1 Exponential moving average, 
MODE_SMMA   2 Smoothed moving average, 
MODE_LWMA   3 Linear weighted moving average. 
You must use the numeric value of the MA Method that you want to use
when you set the 'ma_method' value with the indicator inputs.

**************************************************************************/
extern int TimeFrame=240;
extern int KPeriod=5;
extern int DPeriod=3;
extern int Slowing=3;
extern int MAMethod=0;
extern int PriceField=0;// PriceField:  0=Hi/Low   1=Close/Close

extern string note_TimeFrames = "M1;5,15,30,60H1;240H4;1440D1;10080W1;43200MN";
extern string __MA_Method = "SMA0 EMA1 SMMA2 LWMA3";
extern string __PriceField = "0=Hi/Low   1=Close/Close";
//extern string __Price = "0O,1C 2H3L,4Md 5Tp 6WghC: Md(HL/2)4,Tp(HLC/3)5,Wgh(HLCC/4)6";


double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];

datetime last_t=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicator line
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexStyle(1,DRAW_LINE,STYLE_DOT);
   SetIndexLabel(0,  "MTF_Stochastic("+KPeriod+","+DPeriod+","+Slowing+")TF"+TimeFrame+"");
   SetIndexLabel(1,"MTF_Stochastic("+KPeriod+","+DPeriod+","+Slowing+")TF"+TimeFrame+"");
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,233);
   SetIndexBuffer(3,ExtMapBuffer4);
   SetIndexStyle(3,DRAW_ARROW);
   SetIndexArrow(3,234);

//---- name for DataWindow and indicator subwindow label   
   switch(TimeFrame)
   {
      case 1 : string TimeFrameStr="Period_M1"; break;
      case 5 : TimeFrameStr="Period_M5"; break;
      case 15 : TimeFrameStr="Period_M15"; break;
      case 30 : TimeFrameStr="Period_M30"; break;
      case 60 : TimeFrameStr="Period_H1"; break;
      case 240 : TimeFrameStr="Period_H4"; break;
      case 1440 : TimeFrameStr="Period_D1"; break;
      case 10080 : TimeFrameStr="Period_W1"; break;
      case 43200 : TimeFrameStr="Period_MN1"; break;
      default : TimeFrameStr="Current Timeframe";
   } 
   IndicatorShortName("MTF_Stochastic("+KPeriod+","+DPeriod+","+Slowing+") "+TimeFrameStr);  
   start();
   return(0);
  }
//----
   
 
//+------------------------------------------------------------------+
//| MTF Stochastic                                                   |
//+------------------------------------------------------------------+
 int deinit()
  {
   for (int i=Bars;i>=0;i--){
      ObjectDelete("st"+Symbol()+Period()+DoubleToStr(KPeriod+DPeriod+Slowing,0)+DoubleToStr(i,0));
   }
   return(0);
  }

bool up_a=false;
bool dn_a=false;
int start()
  {
   datetime TimeArray[];
   ArrayResize(TimeArray,Bars);
   int    i,limit,y=0,counted_bars=IndicatorCounted();
    
// Plot defined timeframe on to current timeframe   
   ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame); 
   
 //  limit=Bars-counted_bars+TimeFrame/Period(); //igorad
limit=Bars-1;
limit=MathMax(limit,TimeFrame/Period());
//limit=MathMin(limit,BarsToCount);
   for(i=0,y=0;i<limit;i++)
   {
   if (Time[i]<TimeArray[y]) y++; 
   
 /***********************************************************   
   Add your main indicator loop below.  You can reference an existing
      indicator with its iName  or iCustom.
   Rule 1:  Add extern inputs above for all neccesary values   
   Rule 2:  Use 'TimeFrame' for the indicator timeframe
   Rule 3:  Use 'y' for the indicator's shift value
 **********************************************************/  
   ExtMapBuffer3[i]=EMPTY_VALUE;  
   ExtMapBuffer4[i]=EMPTY_VALUE;
   ExtMapBuffer1[i]=EMPTY_VALUE;  
   ExtMapBuffer2[i]=EMPTY_VALUE;

   ExtMapBuffer1[i]=iStochastic(NULL,TimeFrame,KPeriod,DPeriod,Slowing,MAMethod,PriceField,0,y);
   ExtMapBuffer2[i]=iStochastic(NULL,TimeFrame,KPeriod,DPeriod,Slowing,MAMethod,PriceField,1,y);
   ObjectDelete("st"+Symbol()+Period()+DoubleToStr(KPeriod+DPeriod+Slowing,0)+DoubleToStr(i,0));   

   if (NormalizeDouble(ExtMapBuffer1[i],Digits)>NormalizeDouble(ExtMapBuffer2[i],Digits) && NormalizeDouble(ExtMapBuffer1[i+1],Digits)<=NormalizeDouble(ExtMapBuffer2[i+1],Digits)  && NormalizeDouble(ExtMapBuffer1[i+1],Digits)!=NormalizeDouble(ExtMapBuffer1[i],Digits) ){
      ExtMapBuffer3[i]=ExtMapBuffer1[i];
      ObjectDelete("st"+Symbol()+Period()+DoubleToStr(KPeriod+DPeriod+Slowing,0)+DoubleToStr(i,0));
      ObjectCreate("st"+Symbol()+Period()+DoubleToStr(KPeriod+DPeriod+Slowing,0)+DoubleToStr(i,0),22,0,Time[i],Low[i]-5*Point);
      ObjectSet("st"+Symbol()+Period()+DoubleToStr(KPeriod+DPeriod+Slowing,0)+DoubleToStr(i,0),6,Blue);
      ObjectSet("st"+Symbol()+Period()+DoubleToStr(KPeriod+DPeriod+Slowing,0)+DoubleToStr(i,0),14,233);      
   }
   if (NormalizeDouble(ExtMapBuffer1[i],Digits)<NormalizeDouble(ExtMapBuffer2[i],Digits) && NormalizeDouble(ExtMapBuffer1[i+1],Digits)>=NormalizeDouble(ExtMapBuffer2[i+1],Digits)  && NormalizeDouble(ExtMapBuffer1[i+1],Digits)!=NormalizeDouble(ExtMapBuffer1[i],Digits)){
      ExtMapBuffer4[i]=ExtMapBuffer1[i];
      ObjectDelete("st"+Symbol()+Period()+DoubleToStr(KPeriod+DPeriod+Slowing,0)+DoubleToStr(i,0));
      ObjectCreate("st"+Symbol()+Period()+DoubleToStr(KPeriod+DPeriod+Slowing,0)+DoubleToStr(i,0),22,0,Time[i],High[i]+5*Point);
      ObjectSet("st"+Symbol()+Period()+DoubleToStr(KPeriod+DPeriod+Slowing,0)+DoubleToStr(i,0),6,Red);
      ObjectSet("st"+Symbol()+Period()+DoubleToStr(KPeriod+DPeriod+Slowing,0)+DoubleToStr(i,0),14,234);      
   }
   }  
     
//
   //----  Refresh buffers ++++++++++++++ 
   if (TimeFrame < Period()) TimeFrame = Period();
   if (TimeFrame>Period()) {
     int PerINT=TimeFrame/Period()+1;
     datetime TimeArr[]; ArrayResize(TimeArr,PerINT);
     ArrayCopySeries(TimeArr,MODE_TIME,Symbol(),Period()); 
     for(i=0;i<PerINT+1;i++) {if (TimeArr[i]>=TimeArray[0]) {
 /********************************************************     
    Refresh buffers:         buffer[i] = buffer[0];
 ************************************************************/  

   ExtMapBuffer1[i]=ExtMapBuffer1[0];
   ExtMapBuffer2[i]=ExtMapBuffer2[0];

   } } }
//+++++++++++++++++++++++++++++++++++++++++++++++++++++   Raff 

   if (ExtMapBuffer3[0]!=EMPTY_VALUE && !up_a){
      up_a=true;
      dn_a=false;    
      Alert("MTF Stochastic Long cross Signal on "+Symbol());
   }
   if (ExtMapBuffer4[0]!=EMPTY_VALUE && !dn_a){
      dn_a=true;
      up_a=false;    
      Alert("MTF Stochastic Short cross Signal on "+Symbol());
   }

   return(0);
  }
 

Good day to all.
Can you please tell me how to code the following condition correctly using I don't remember which mathematical function.

if (Bid - Low[1]>=0.0030 && Bid - Low[1]<0.0035) {action;}
I know that there is a math function that can be used in the above condition without && sign . But I don't remember what this mathematical function is called and how to apply it.
Thanks for the help.

 
This question seems to be from the basics of MQL4. Suppose some variables X, Y, Z are declared in the global scope. Then, the program calls the F1() function to get the X value. Although this function returns only X, the Y and Z variables are calculated during its operation, i.e. they are assigned some values that will not be changed later. Further down the line, Y and Z are again used in some function F2(). To calculate these variables, should we address the F1() function again providing for return of not only X, but Y and Z as well? Or Y and Z are already overwritten in the global scope and will be automatically inserted into the F2() function in a changed form?
 
Oleksandr Nozemtsev:

If you change a global variable in a function, it will change. But this is a dangerous way of programming, because in the code, as it grows, there will be unobvious assignments in different functions of the program.

There's a main function in the program, that's where you do the global variable assignment. And in other functions, do it this way:

int X, Y, Z;

void OnTick()
   {
   X=Sum(Y,Z);
   }

int Sum(int y, int z)
   {
   return(y+z);
   }

Or like this:

int X, Y, Z;

void OnTick()
   {
   Replace(X,Y,Z);
   }

void Replace(int & x, int & y, int & z)
   {
   int a=x;
   x=y;
   y=z;
   z=a;
   }
 
ANDREY:

how to correctly code the following condition using I don't remember which mathematical function

I don't know a better way of setting the condition

 
ANDREY:

Good day to all.
Can you please tell me how to code the following condition correctly using I don't remember which mathematical function.

if (Bid - Low[1]>=0.0030 && Bid - Low[1]<0.0035) {action;}
I know that there is a math function that can be used in the above condition without && sign and the program will check for price consistency in the range of 4 points. But I don't remember what this mathematical function is called and how to apply it.
Thanks for the help.

if(Bid - Low[1] >= 0.0030))
  {
   if(Bid - Low[1] < 0.0035)
     {
      действие;
     }
  }
Without and
 
Александр:
Without and

Thank you very much for the tip.

 
Could you please tell me why the program (on mql4, on minutes, all ticks) reads the number explicitly and the same number calculated in the function differently?
Here is the code
double Pr,Lt1;
int Tick,H;
void OnTick()
{
Tick++;
if (Tick>15240&&Tick<15821)
{
Pr=iLow( NULL ,PERIOD_H4,0)+0.0030;
Print("--------- 0 ---------=     ",DoubleToString(Pr,5) );
if ((H!=Hour()&&Bid - iLow( NULL ,PERIOD_H1,1)>=0.0030&&Lt1!=Pr )||Bid==1.60854)
{
OrderSend(Symbol(),OP_SELL,2,Bid, 3,Ask+300*Point,Ask-100*Point,"300",0);
Lt1=Bid;
H=Hour();
}
}
}

The program does not read Pr as 1.60854 althoughPrint( ) shows the value of Pr as 1.60854 . As a result, the program opens the second order, but it shouldn't do that
.


And if we set the number 1.60854 instead of the Pr variable , the program reads it and doesn't open the second order.

double Pr,Lt1;
int Tick,H;
void OnTick()
{
Tick++;
if (Tick>15240&&Tick<15821)
{
Pr=iLow( NULL ,PERIOD_H4,0)+0.0030;
Print("--------- 0 ---------=     ",DoubleToString(Pr,5) );
if ((H!=Hour()&&Bid - iLow( NULL ,PERIOD_H1,1)>=0.0030&&Lt1!= 1.60854)||Bid==1.60854)
{
OrderSend(Symbol(),OP_SELL,2,Bid, 3,Ask+300*Point,Ask-100*Point,"300",0);
Lt1=Bid;
H=Hour();
}
}
}

QUESTION What changes we should make in the code so that the program reads Pr and does not open the second order.
Thank
you for your help.

Reason: