Questions from Beginners MQL5 MT5 MetaTrader 5 - page 563

 
Василий:
Yes, I'm asking about MQL 5? Yes, the data of the current symbol should be accessed. The data is needed in the EA code. So I need, for example, to know the close price of the bar preceding the current one.
Look at example for CopyTime
 
Karputov Vladimir:
Check out the example for CopyTime
Got it, I'll try to figure it out. Thank you!
 
Василий:
Yes, I am asking about MQL5? Yes, the data of the current symbol should be accessed. The data we need in Expert Advisor code. So, for example, I need to know the close price of the bar preceding the current one

You should read the documentation a bit.

int  CopyClose( 
   string           symbol_name,       // имя символа 
   ENUM_TIMEFRAMES  timeframe,         // период 
   int              start_pos,         // откуда начнем  
   int              count,             // сколько копируем 
   double           close_array[]      // массив для копирования цен закрытия 
   );

Here's an example of getting the closing price and indicator buffers

ENUM_ORDER_TYPE indicator_01()
  {
   double ma[2];
   double close[2];
   static int h_ma=INVALID_HANDLE;
   if(Bars(_Symbol,PERIOD_CURRENT)<ma_period+1)return(WRONG_VALUE);
   if(h_ma==INVALID_HANDLE || h_ma==0)
     {
      h_ma=iMA(_Symbol,_Period,ma_period,ma_shift,ma_method,ma_price);
      return(WRONG_VALUE);
     }
   if(CopyBuffer(h_ma,0,1,2,ma)<2) return(WRONG_VALUE);
   if(CopyClose(_Symbol,PERIOD_CURRENT,1,2,close)<2) return(WRONG_VALUE);

   if(close[1]>ma[1]&&close[0]<ma[0])return(ORDER_TYPE_BUY);
   if(close[1]<ma[1]&&close[0]>ma[0])return(ORDER_TYPE_SELL);

   return(WRONG_VALUE);
  }
 
Sergey Gritsay:

...

Here's an example of getting the closing price as well as the indicator buffers

Bad example. The variable storing the indicator handle in MQL5 must be declared in the "header" - in the area of global variables, and the handle should be obtained in OnInit(). And only then you can refer to the indicator's handle for getting data from anywhere in the program.
 

Hello all !

I am getting data on trade history in the tester using OnTrade event.

  for(int i=0;i<HistoryDealsTotal();i++){
       ulong deal_ticket=HistoryDealGetTicket(i);
       Print("ticket=",deal_ticket);
       Print("price=",HistoryDealGetDouble(deal_ticket,DEAL_PRICE));
       Print("time=",TimeToString(HistoryDealGetInteger(deal_ticket,DEAL_TIME)));

And this is what I get in the logs.

0 22:58:16.487 Core 1 2016.04.01 00:00 Number of bars in the terminal history for the symbol-period GBPUSD at the moment = 7729
JR 0 22:58:16.487 Core 1 2016.04.01 03:00:00 Attempted trade 0 with lot 0.01
EL 0 22:58:16.487 Core 1 2016.04.01 03:00:00 market buy 0.01 GBPUSD sl: 1.43033 tp: 1.44034 (1.43646 / 1.43654 / 1.43646)
GO 0 22:58:16.487 Core 1 2016.04.01 03:00:00 deal #2 buy 0.01 GBPUSD at 1.43654 done (based on order #2)
GE 0 22:58:16.487 Core 1 2016.04.01 03:00:00 deal performed [#2 buy 0.01 GBPUSD at 1.43654]
NK 0 22:58:16.487 Core 1 2016.04.01 03:00:00 order performed buy 0.01 at 1.43654 [#2 buy 0.01 GBPUSD at 1.43654]
HI 0 22:58:16.487 Core 1 2016.04.01 03:00:00 ticket=1
HM 0 22:58:16.487 Core 1 2016.04.01 03:00:00 price=0.0
HH 0 22:58:16.487 Core 1 2016.04.01 03:00:00 time=2016.04.01 00:00
CR 0 22:58:16.487 Core 1 2016.04.01 03:00:00 ticket=2
ID 0 22:58:16.487 Core 1 2016.04.01 03:00:00 price=1.43654
OP 0 22:58:16.487 Core 1 2016.04.01 03:00:00 time=2016.04.01 03:00

Where does the first trade with zero price come from? I do not do it :-).

I do not do it :-) Explain plz.

Z.P. I think I got it. The first deal has a profit of 10K. I do not know what to do with it but it is still strange. Why?

And here is another question. The deal (buy at 1.43654) has not been opened yet. How did it appear in the history?

I need to get data ONLY on closed trades in the array like in MQL4. How to do it ???

P.S. Filter by DEAL_ENTRY_OUT ?

 
Alexandr Saprykin:
Have you tried reinstalling the terminal?
Yes, thank you. That was the only thing that helped.
 
Karputov Vladimir:
Bad example. The variable storing the indicator handle in MQL5 must be declared in the "header" - in the global variables area of the program, get the handle in OnInit(). And only then you can refer to the indicator's handle for getting data from anywhere in the program.

I do not quite agree with you about getting a handle, because the OnInit() function is executed once and there is no 100% guarantee that the indicator will be created from the first time. You might be right about declaring a variable, but I want to know if the following documentation says something worse than the variant with a static variable

Local variables declared with static keyword save their values as long as the function exists. With each subsequent call of a function, these local variables contain the values they had in the previous call.

 
<br / translate="no">

Filter by DEAL_ENTRY_OUT

 
Sergey Gritsay:

I do not quite agree with you about getting a handle, because the OnInit() function is executed once and there is no 100% guarantee that the indicator will be created from the first time. You might be right about the declaration of a variable, but I want to know how worse the option of a static variable is in the documentation

...

We will check for this in OnInit() when creating an indicator handle. If the indicator handle has not been created, onOnit() we will returnINIT_FAILED. That is, the indicator handle is created only once and only in OnInit(). This is not "4" - mess and wandering and declaration of handles in all corners of the program.
 
Karputov Vladimir:
For this purpose, OnInit() checks if the indicator handle is created. If the indicator handle has not been created, OnOnit() should returnINIT_FAILED. That is, the indicator handle is created only once and only in OnInit(). This is not "4" - it's not a mess and wandering and declaring handles in all corners of the program.
I would not be so categorical in my statement. It's not a mess in the closets, it's a mess in the heads (c)
Reason: