Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1163

 
Roman:

If it has been entered, then there is a difference.
Reading an initialised variable is always faster than reading a function.

There is no difference. I can't find the link now - but the answer was given a long time ago.

 
Vladimir Karputov:

There is no difference. I can't find the link now - but the answer was given a long time ago.

For Juan and the compiler warnings, there is no difference ))

 
Vladimir Karputov:

There is no difference. When compiling, everything comes together in a single call. That's why it's better to do everything through functions.

Let's start a separate thread about it too and argue about it like about pips and points... That'll give people something to do...

You guys love to impose your opinions on everyone...

 

There is an MA with some shift. Let MA_shift = 5;

Why is it possible to get the right value of MA from the zero bar of the chart only if double negative shift is passed to CopyBuffer?
CopyBuffer(hMA, 0, -2*5, rates_total, MA_Buf);

MA_Buf[0] now has the correct value.

 
RickD:

There is an MA with some shift. Let MA_shift = 5;

Why is it possible to take the correct value of MA from the zero bar of the chart, only if you pass a double negative shift into CopyBuffer?
CopyBuffer(hMA, 0, -2*5, rates_total, MA_Buf);

MA_Buf[0] now has the correct value.

Draw a picture first and specify what is "zero bar" for your indicator with the shift parameter.

 
Roman:

Reading a variable, faster than a function.

Not really, I checked which is faster than _Symbol or Symbol()https://www.mql5.com/ru/forum/160683/page933#comment_12780905.

the access time is the same, code optimization in MQL is very cool, so use whatever is convenient.

according to my tests the access time may be different depending on where the variable is described and with what modifiers, but there is a 2-5% speed, which in principle can be written off to the implementation in a particular build, it depends on the build number

 
Can you tell me the easiest and fastest way to get the result of the last transaction for the current symbol? Because from the documentation everything is so complicated. To get the result first you need to find the ticket of the required deal, to find the ticket you need to find the deal in the history by its index, the index should be searched in the history, not to mention that you also need to filter by the symbol.
 
WinZip:
Could you please advise how to easily and quickly get the result of the last trade with the current symbol? Because the documentation is very complex. To get the result, you need to find the ticket of the required trade first, to find the ticket you need to find the trade in the history by its index, the index should be sought in the history, not to mention that we should filter filter by the symbol.

To avoid going to the trading history every time - just catch the deal inOnTradeTransaction with the type

TRADE_TRANSACTION_DEAL_ADD - adding transaction to the history


//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//--- get transaction type as enumeration value
   ENUM_TRADE_TRANSACTION_TYPE type=trans.type;
//--- if transaction is result of addition of the transaction in history
   if(type==TRADE_TRANSACTION_DEAL_ADD)
     {
      long     deal_ticket       =0;
      long     deal_order        =0;
      long     deal_time         =0;
      long     deal_time_msc     =0;
      long     deal_type         =-1;
      long     deal_entry        =-1;
      long     deal_magic        =0;
      long     deal_reason       =-1;
      long     deal_position_id  =0;
      double   deal_volume       =0.0;
      double   deal_price        =0.0;
      double   deal_commission   =0.0;
      double   deal_swap         =0.0;
      double   deal_profit       =0.0;
      string   deal_symbol       ="";
      string   deal_comment      ="";
      string   deal_external_id  ="";
      if(HistoryDealSelect(trans.deal))
        {
         deal_ticket       =HistoryDealGetInteger(trans.deal,DEAL_TICKET);
         deal_order        =HistoryDealGetInteger(trans.deal,DEAL_ORDER);
         deal_time         =HistoryDealGetInteger(trans.deal,DEAL_TIME);
         deal_time_msc     =HistoryDealGetInteger(trans.deal,DEAL_TIME_MSC);
         deal_type         =HistoryDealGetInteger(trans.deal,DEAL_TYPE);
         deal_entry        =HistoryDealGetInteger(trans.deal,DEAL_ENTRY);
         deal_magic        =HistoryDealGetInteger(trans.deal,DEAL_MAGIC);
         deal_reason       =HistoryDealGetInteger(trans.deal,DEAL_REASON);
         deal_position_id  =HistoryDealGetInteger(trans.deal,DEAL_POSITION_ID);

         deal_volume       =HistoryDealGetDouble(trans.deal,DEAL_VOLUME);
         deal_price        =HistoryDealGetDouble(trans.deal,DEAL_PRICE);
         deal_commission   =HistoryDealGetDouble(trans.deal,DEAL_COMMISSION);
         deal_swap         =HistoryDealGetDouble(trans.deal,DEAL_SWAP);
         deal_profit       =HistoryDealGetDouble(trans.deal,DEAL_PROFIT);

         deal_symbol       =HistoryDealGetString(trans.deal,DEAL_SYMBOL);
         deal_comment      =HistoryDealGetString(trans.deal,DEAL_COMMENT);
         deal_external_id  =HistoryDealGetString(trans.deal,DEAL_EXTERNAL_ID);
        }
      else
         return;
      ENUM_DEAL_ENTRY enum_deal_entry=(ENUM_DEAL_ENTRY)deal_entry;
      if(deal_symbol==m_symbol.Name() && deal_magic==InpMagic)
        {
         if(deal_type==DEAL_TYPE_BUY || deal_type==DEAL_TYPE_SELL)
           {
            
...
           }
        }
     }
  }

m_symbol.Name() - current symbol, InpMagic - unique identifier of the Expert Advisor

 
Hello I registered on the site and facebook mail and can't get into meta trader 5-4 help me replenish my account
 
Igor Makanu:

not a fact, I checked which is faster than _Symbol or Symbol()https://www.mql5.com/ru/forum/160683/page933#comment_12780905

I.e. access time is one and the same, code optimization in MQL is very cool, so whatever is convenient, use it.

according to my tests the access time may be different depending on where the variable is described and with what modifiers, but there 2-5% speed, which in principle can be written off to the implementation in a particular build, it depends on the build number

I agree that the optimizer works well, but relying always on the compiler's optimizer is wrong.
You need to think ahead about how the code is executed at the memory, initialization and value return level.
That is, calculate the number of executed actions per operation and if there are fewer actions, the code is faster. And take it as a practice to write an optimal code at once.
In short codes perhaps there is no difference, but when there is a project with hundreds of files, that's when delays will appear,
and there will only be a profiler and rewrite the code. Everyone chooses his own rake )).

Also google for keywords

__inline 
__forceinline
Oddly enough, they are in mql as undocumented.
Reason: