Download MetaTrader 5

Simple question (maybe for dummy)

To add comments, please log in or register
yoshi1988
30
yoshi1988  

Hello, 

I have a very simple question. 

I have posted entire code at very end.

 

/---- Copy latest MA indicator values into a buffer
   int copied=CopyBuffer(handle1,0,0,4,SmoothedBuffer1);
   if(copied>0)
      copied=CopyBuffer(handle2,0,0,4,SmoothedBuffer2);

   if(copied>0)

 I understand that CopyBuffer returns number of duplicated bars into Buffer.

Why does second CopyBuffer duplicate bars only when copied > 0? is it because at least CopyBuffer(handle1,0,0,4,SmoothedBuffer1) needs to be successful in prior?

Also, I have one more question. 

 

if(copied>0)
     {
      //---- If MAPeriod > MAPeriod+2 -> BUY
      if(SmoothedBuffer1[1]>SmoothedBuffer2[1] && SmoothedBuffer1[2]<SmoothedBuffer2[2])
        {
         trReq.price=tick.ask;                   // SymbolInfoDouble(NULL,SYMBOL_ASK);
         trReq.sl=tick.ask-_Point*sl;            // Stop Loss level of the order
         trReq.tp=tick.ask+_Point*tp;            // Take Profit level of the order
         trReq.type=ORDER_TYPE_BUY;              // Order type
         OrderSend(trReq,trRez);
        }
      //---- If MAPeriod < MAPeriod+2 -> SELL
      else if(SmoothedBuffer1[1]<SmoothedBuffer2[1] && SmoothedBuffer1[2]>SmoothedBuffer2[2])
        {
         trReq.price=tick.bid;
         trReq.sl=tick.bid+_Point*sl;            // Stop Loss level of the order
         trReq.tp=tick.bid-_Point*tp;            // Take Profit level of the order
         trReq.type=ORDER_TYPE_SELL;             // Order type
         OrderSend(trReq,trRez);
        }
     }

 I understand that code above is proceeded only when copied >0( where SmoothedBuffer1 and Smootedbuffer2f successfully receive duplicated bars),

However, do we have another case, other then ERROR, such that copied <= 0? Maybe very beginning of bars when bar counts is less than 4?

 

Thank you 

//+------------------------------------------------------------------+
//|                                                  K_eSimpleMA.mq5 |
//|                                                Copyright tsaktuo |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "tsaktuo"
#property link      "http://www.mql5.com"
#property version   "1.00"

//--- input parameters
input int Periods=17;  // Period for MA indicator
input int SL=31;       // Stop Loss
input int TP=69;       // Take Profit
input int MAGIC=999;   // MAGIC number

MqlTradeRequest trReq;
MqlTradeResult trRez;
int handle1;
int handle2;
double SmoothedBuffer1[];
double SmoothedBuffer2[];

int sl;
int tp;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   ZeroMemory(trReq);
   ZeroMemory(trRez);
//---- set default vaules for all new order requests
   trReq.action=TRADE_ACTION_DEAL;
   trReq.magic=MAGIC;
   trReq.symbol=Symbol();                 // Trade symbol
   trReq.volume=0.1;                      // Requested volume for a deal in lots
   trReq.deviation=1;                     // Maximal possible deviation from the requested price
   trReq.type_filling=ORDER_FILLING_FOK;  // Order execution type
   trReq.type_time=ORDER_TIME_GTC;        // Order execution time
   trReq.comment="MA Sample";

//---- Create handle for 2 MA indicators
   handle1=iMA(Symbol(),PERIOD_CURRENT,Periods,0,MODE_EMA,PRICE_CLOSE);
   handle2=iMA(Symbol(),PERIOD_CURRENT,Periods+2,0,MODE_EMA,PRICE_CLOSE);

//---- input parameters are ReadOnly
   tp=TP;
   sl=SL;

//---- Suppoprt for acount with 5 decimals
   if(_Digits==5)
     {
      sl*=10;
      tp*=10;
     }
   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

   MqlTick tick; //variable for tick info
   if(!SymbolInfoTick(Symbol(),tick))   //GetCurrentTick; update tick
     {
      Print("Failed to get Symbol info!");
      return;
     }

//---- Copy latest MA indicator values into a buffer
   int copied=CopyBuffer(handle1,0,0,4,SmoothedBuffer1);
   if(copied>0)
      copied=CopyBuffer(handle2,0,0,4,SmoothedBuffer2);

   if(copied>0)
     {
      //---- If MAPeriod > MAPeriod+2 -> BUY
      if(SmoothedBuffer1[1]>SmoothedBuffer2[1] && SmoothedBuffer1[2]<SmoothedBuffer2[2])
        {
         trReq.price=tick.ask;                   // SymbolInfoDouble(NULL,SYMBOL_ASK);
         trReq.sl=tick.ask-_Point*sl;            // Stop Loss level of the order
         trReq.tp=tick.ask+_Point*tp;            // Take Profit level of the order
         trReq.type=ORDER_TYPE_BUY;              // Order type
         OrderSend(trReq,trRez);
        }
      //---- If MAPeriod < MAPeriod+2 -> SELL
      else if(SmoothedBuffer1[1]<SmoothedBuffer2[1] && SmoothedBuffer1[2]>SmoothedBuffer2[2])
        {
         trReq.price=tick.bid;
         trReq.sl=tick.bid+_Point*sl;            // Stop Loss level of the order
         trReq.tp=tick.bid-_Point*tp;            // Take Profit level of the order
         trReq.type=ORDER_TYPE_SELL;             // Order type
         OrderSend(trReq,trRez);
        }
     }

  }
//+------------------------------------------------------------------+
Alain Verleyen
Moderator
33346
Alain Verleyen  

CopyBuffer() return -1 in case of error, or the number of copied values. This number can differ from the requested bar's count.

In fact the code you provided isn't very robust. The code who follows to send order, needs index 1 and 2 for both buffers. That means 3 values should be copied, as the code is executed if copied>0, and copied=2 for example, SmoothedBuffer1[2] and SmoothedBuffer2[2] will not be available and you will get a bug. Here is an example of how to improve things :

   ...
   //---- Copy latest MA indicator values into a buffer
   if(CopyBuffer(handle1,0,0,4,SmoothedBuffer1)>=3 && 
      CopyBuffer(handle2,0,0,4,SmoothedBuffer2)>=3)
     {
      //---- If MAPeriod > MAPeriod+2 -> BUY
      if(SmoothedBuffer1[1]>SmoothedBuffer2[1] && SmoothedBuffer1[2]<SmoothedBuffer2[2])
      ...

Also there is no need for 4 values if you only use 3. And no need to use dynamic arrays when number of values if known and constant. But it's an other matter.

yoshi1988
30
yoshi1988  
angevoyageur:

CopyBuffer() return -1 in case of error, or the number of copied values. This number can differ from the requested bar's count.

In fact the code you provided isn't very robust. The code who follows to send order, needs index 1 and 2 for both buffers. That means 3 values should be copied, as the code is executed if copied>0, and copied=2 for example, SmoothedBuffer1[2] and SmoothedBuffer2[2] will not be available and you will get a bug. Here is an example of how to improve things :

Also there is no need for 4 values if you only use 3. And no need to use dynamic arrays when number of values if known and constant. But it's an other matter.

Mr. Angevoyageur

Thank you very much.

Your detailed explanation is very helpful. 

To add comments, please log in or register