Questions from Beginners MQL5 MT5 MetaTrader 5 - page 964

 
vladzeit:

Thank you for the example.

Could you please clarify... if I understood correctly.

With this=(datetime)OrderGetInteger. we are telling OrderGetInteger to take the explicitdatetimetype. ?

Does this rule apply to all functions that do not have an explicit type or do not match the type of the variable being assigned? Or is it just a special case?

I want to understand if this is a rule or just need to be remembered as a special case.

It is not a special case. If you write a value of a different type into a variable of one type, the compiler will warn you about it. And it is up to you to decide which variable to take data from.

In your case - when returning an explicit datetime-value from a function with the long type, of course, it is safe and you should explicitly specify the type of value you get from the function.

But if you take a double value into int-variable, then it is up to you to understand whether the data will not be lost by such an assignment. If you know for sure that the return value will fit in int, then specify int explicitly, if you don't know, then take it as a double variable.

 
Artyom Trishkin:

This is not a special case. If you write a value of another type into a variable of one type, the compiler will warn you about it. And it is up to you to decide which variable to take data from.

In your case - when you return an explicit datetime-value from a function with the long type, it is of course safe and you should explicitly specify the type of the value you get from the function.

But if you take a double value into int-variable, then you must understand yourself that no data will be lost during such assignment. If you know for sure that the returned value will fit into int, specify int explicitly, if you don't know, take it into a double variable.

Thank you for your detailed reply. Got it.

 

(Good afternoon to all of you)))


I've mastered MT4, but MT5 .... I do not understand((( There are not many examples, and those that are not explain what I need.

Please advise how to use MT5 with some samples from MT4 code or give me some links to the indicators with similar cases.


1.

if(Period()<=PERIOD_H4 && TimeHour(Time[h])==0 && TimeMinute(Time[h])==0)


2.

while(A<1)
  {
   ObjectDelete("MB");
   OP=iOpen(NULL,Timeframe,br);
   CL=iClose(NULL,Timeframe,br);
 if(OP>CL)
   bear=(OP-CL)/_Point;
 if(SizeCandles*FactorCandleы <= bear)
   A=2;
 if(br>iBars(NULL,Timeframe))
  {
   Lab("MB", " No Bear candle", 4, 510, 1, clrWhite);
   break;
  }
   br++;
  }


3. I don't understand how to do element comparison with iMa handle... I know about structures!!! Tried it, but MT5 is not as clear unlike MT4...

   
 for(int i=limit; i>0 && !IsStopped(); i--)
   {
   ma1=iMA(NULL,Period(),Ma1,0,MAMethod1,MAPrice1,i);
   ma2=iMA(NULL,Period(),Ma2,0,MAMethod2,MAPrice2,i);
   ma3=iMA(NULL,Period(),Ma3,0,MAMethod3,MAPrice3,i);
   ma4=iMA(NULL,Period(),Ma4,0,MAMethod4,MAPrice4,i);
   MA=iMA(NULL,Period(),MAPeriod,0,MAMethod,MAPrice,i);
//----
 if(filtercase==true && xPer==Period())
  {
//--- High signal
 if(Open[i]<Close[i] && MA>Open[i] && MA<Close[i] && ma1>ma2 && ma1<ma3 && MA<ma4)  
   ExtLineBuffer1[i]=High[i]+shiftmap*_Point;
 if(ShowPeriod==false && xPeriod==Period())
   ExtLineBuffer1[i]=0.0;
//--- Low signal
 if(Open[i]>Close[i] && MA<Open[i] && MA>Close[i] && ma1<ma2 && ma1>ma3 && MA>ma4)  
   ExtLineBuffer2[i]=Low[i]-shiftmap*_Point;
 if(ShowPeriod==false && xPeriod==Period())
   ExtLineBuffer2[i]=0.0;
  } 
 

The general differences of programming in MT5 are already understood! But I can't program yet (the dice don't add up)))))

It's important for me to LISTEN in my head to these parts and modules of MT5.... I understand the logic quickly in MT4, but MT5 only "cubes" ....

Thanks for the tips)))

 
Please change my robot's fix lot to % lot. The code is in my private message.
 
Sprut112:
Please change my robot's fix lot to % lot. Code in private.

Freelance.

 
kopeyka2:

(Good afternoon to all of you)))


I've mastered MT4, but MT5 .... I do not understand((( There are not many examples, and those that are not explain what I need.

Please advise how to use MT5 with some samples from MT4 code or give me some links to the indicators with similar cases.


1.


2.


3. I don't understand how to do element comparison with iMa handle... I know about structures!!! Tried it, but MT5 is not as clear unlike MT4...

The general differences of programming in MT5 are already understood! But I can't program yet (the dice don't add up)))))

It's important for me to LISTEN in my head to these parts and modules of MT5.... I understand the logic quickly in MT4, but MT5 only "dice"....

Thanks for the tips)))

First, read the documentation carefully. The section"Access to timeseries and indicators".

It should be clear how to get the time, "put" it in the structure, and then get hour and minute separately.

There is also CopyBuffer to get indicator values by its handle.

If you still have questions...


 
kopeyka2:

(Good afternoon to all of you)))


I've mastered MT4, but MT5 .... I do not understand((( There are not many examples, and those that are not explain what I need.

Please advise how to use MT5 with some samples from MT4 code or give me some links to the indicators with similar cases.


1.


2.


3. I don't understand how to do element comparison with iMa handle... I know about structures!!! Tried it, but MT5 is not as clear unlike MT4...

The general differences of programming in MT5 are already understood! But I can't program yet (the dice don't add up)))))

It's important for me to LISTEN in my head to these parts and modules of MT5.... In MT4 I understand the logic quickly, but MT5 only "cubes" ....

Thanks for the tips)))

If you need to compare several timeseries of one timeframe, work with an array likeMqlRates-> and useCopyRates to fill this array.

It is convenient to store the time in aMqlDateTime structure -> useTimeToStruct to convert the time fromdatetime toMqlDateTime structure.

Now your example 1 will look like this:

//+------------------------------------------------------------------+
//|                                                      Test_en.mq5 |
//+------------------------------------------------------------------+
#property script_show_inputs
//--- input parameters
int InpBarTime=3; // Bar where we compare the time
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   MqlRates rates[];
   ArraySetAsSeries(rates,true);

   int start_pos  =0;                           // start position 
   int count=(InpBarTime<10)?10:InpBarTime+1;   // data count to copy 

   int result=CopyRates(Symbol(),Period(),start_pos,count,rates);
   if(result!=count)
     {
      Print("Ordered: ",IntegerToString(count),", received ",IntegerToString(result));
     }

//--- example 1
// if(Period()<=PERIOD_H4 && TimeHour(Time[h])==0 && TimeMinute(Time[h])==0)
   MqlDateTime SDateTime;
   TimeToStruct(rates[InpBarTime].time,SDateTime); // Converts a value of datetime type into a structure variable MqlDateTime
   if(Period()<=PERIOD_H4 && SDateTime.hour==0 && SDateTime.min==0)
     {

     }
  }
//+------------------------------------------------------------------+
Files:
Test_en.mq5  3 kb
 
Please tell me if there is a rule.
In mql5 one and the same task can be solved in many ways.
If I refer to an instance of a class in one code, for example: CSymbolInfo. m_symbol.Ask()... and so on.
and in the same code I assign an Ask value without reference to the class. e.g.
double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);

Question: does this cause errors or is it normal within the limits of one code or even one function?

 
kopeyka2:

(Good afternoon to all of you)))


I've mastered MT4, but MT5 .... I do not understand((( There are not many examples, and those that are not explain what I need.

Please advise how to use MT5 with some samples from MT4 code or give me some links to the indicators with similar cases.


1.


2.


3. I don't understand how to do element comparison with iMa handle... I know about structures!!! Tried it, but MT5 is not as clear unlike MT4...

The general differences of programming in MT5 are already understood! But I can't program yet (the dice don't add up)))))

It's important for me to LISTEN in my head to these parts and modules of MT5.... In MT4 I understand the logic quickly, but MT5 only "cubes" ....

Thanks for the tips)))

Working with indicators - usingMA Force code as an example

Step 1: on a global program level declare variable handle_iMA, which will store the indicator handle

int    handle_iMA;                           // variable for storing the handle of the iMA indicator

Step 2: create an indicator in OnInit() and return thehandle_iMA variable

//--- create handle of the indicator iMA
   handle_iMA=iMA(m_symbol.Name(),Inp_MA_period,Inp_MA_ma_period,Inp_MA_ma_shift,
                  Inp_MA_ma_method,Inp_MA_applied_price);
//--- if the handle is not created 
   if(handle_iMA==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code 
      PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
                  m_symbol.Name(),
                  EnumToString(Inp_MA_period),
                  GetLastError());
      //--- the indicator is stopped early 
      return(INIT_FAILED);
     }

Step 3: make a request in OnTick() - copy a certain amount of data(count) from the indicator intoma[] array

   double ma[],force[];
   ArraySetAsSeries(ma,true);
   ArraySetAsSeries(force,true);
   int start_pos=0,count=InpTrendBars+3;
   if(!iGetArray(handle_iMA,0,start_pos,count,ma) || 
      !iGetArray(handle_iForce,0,start_pos,count,force))
     {
      PrevBars=0;
      return;
     }

Step 4: work with the indicator data array and compare its elements:

   bool trend_up=(ma[1]>ma[2]);
   bool trend_down=(ma[1]<ma[2]);
 
vladzeit:
Please tell me if there is a rule.
In mql5 one and the same task can be solved in many ways.
If I refer to an instance of a class in one code, for example: CSymbolInfo. m_symbol.Ask()... and so on.
and in the same code I assign an Ask value without reference to the class. e.g.
double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);

Question: does this cause errors or is it normal within the limits of one code or even one function?

It's better not to be confused and use one approach: either trade classCSymbolInfo or system functions like SymbolInfoXXXX.

Reason: