Errors, bugs, questions - page 1141

 

Hello all !

Such a problem.

I successfully get the value of Bands and MA indicators.

Bands_handle=iBands(NULL,0,BP_var_s,0,BDev_var_s,PRICE_CLOSE);
CopyBuffer(Bands_handle,0,0,1,Base);
CopyBuffer(Bands_handle,1,0,1,Upper);
CopyBuffer(Bands_handle,2,0,1,Lower);
B_med_s=Base[0];
B_up_s=Upper[0];
B_low_s=Lower[0];

MA_handle=iMA(NULL,0,MA_var_b,0,MODE_SMA,PRICE_TYPICAL);
CopyBuffer(MA_handle,0,0,1,MAv);
M_av_s=MAv[0];

Then print: B_up_s = 1.264249475876921 M_av_s = 1.2595

Then I try to perform a matrix operation B_up_s-M_av_s and get the result 0.00000000.

What is the miracle?

 
Alvin1976:

Hello all !

Such a problem.

I successfully get the value of Bands and MA indicators.

Bands_handle=iBands(NULL,0,BP_var_s,0,BDev_var_s,PRICE_CLOSE);
   CopyBuffer(Bands_handle,0,0,1,Base);
   CopyBuffer(Bands_handle,1,0,1,Upper);
   CopyBuffer(Bands_handle,2,0,1,Lower);
   B_med_s=Base[0];
   B_up_s=Upper[0];
   B_low_s=Lower[0];
   
   MA_handle=iMA(NULL,0,MA_var_b,0,MODE_SMA,PRICE_TYPICAL);
   CopyBuffer(MA_handle,0,0,1,MAv);
   M_av_s=MAv[0];  

Then print: B_up_s = 1.264249475876921 M_av_s = 1.2595

Then I try to perform a B_up_s-M_av_s mate operation and get 0.00000000 as a result.

What's so strange?

1) How to insert the code.

2) In what type variable do you store the result of the operation? Or better yet, please give me the part of the code where the output is produced.

 
MigVRN:

1) How do you insert the code.

2) What type of variable do you store the result of the operation in? Better yet, give the part of the code where the output is produced.

All variables are of type double.

I do not understand why Bands has many digits while MA has only 4 decimal places. There should not be a zero result at all. This is why the algorithm does not work.

Everything was so simple in MQL4. Now you need higher optimization speed and multicurrency checking. I have to transfer everything to MQL5 (screw it)).

 double q=B_up_s-M_av_s;
 Print( B_up_s-M_av_s=", DoubleToString(q));
 
Alvin1976:

All variables are of type double.

It is not clear why Bands has many digits and MA only 4 decimal places. And zero result should not be there at all. This is the reason why the algorithm does not work.

Everything was so simple in MQL4. Now you need higher optimization speed and multicurrency checking. Now I have to transfer everything to MQL5 (screw it)).

It should work. Below is a script based on your code and its result:

void OnStart()
  {
  
  double Base[1];
  double Upper[1];
  double Lower[1];  
  
  int BP_var_s = 20;
  int BDev_var_s = 30;
  int MA_var_b = 10;
  
  double M_av_s = 0;
  double MAv[1];
  
  double B_med_s, B_up_s, B_low_s;
  
  
   //---Далее Ваш код
   int Bands_handle=iBands(NULL,0,BP_var_s,0,BDev_var_s,PRICE_CLOSE);
   CopyBuffer(Bands_handle,0,0,1,Base);
   CopyBuffer(Bands_handle,1,0,1,Upper);
   CopyBuffer(Bands_handle,2,0,1,Lower);
   
   B_med_s=Base[0];
   B_up_s=Upper[0];
   B_low_s=Lower[0];
   
   int MA_handle=iMA(NULL,0,MA_var_b,0,MODE_SMA,PRICE_TYPICAL);
   
   CopyBuffer(MA_handle,0,0,1,MAv);
   M_av_s=MAv[0]; 
   
    double q = B_up_s - M_av_s;
    
    Print("B_up_s-M_av_s=", DoubleToString(q));
   
  }

 
Alvin1976:

All variables are of type double.

It is not clear why Bands has many digits and MA only 4 decimal places. The result of zero should not be there at all. This is why the algorithm does not work.

Everything was so simple in MQL4. Now you need higher optimization speed and multicurrency checking. I have to transfer everything to MQL5 (screw it)).

Before the "subtract" operation , print the values of variables to see what's in the buffers:
Print("B_up_s= ",B_up_s,"; M_av_s=",M_av_s);
double q=B_up_s-M_av_s;
 
MigVRN:

It should work. Below is the script according to your code and its result:

Yes, the script works.

Even the EA with this example works.

My EA, on the other hand, is giving out zero stubbornly. Let's look for an error ......

 

Hi all!

Question, how to make a variable value to be updated on each tick in a minute, and then this sum is written to a separate value, when the next minute closes, the value of the sum is replaced by the new one?

int t = TimeSeconds();

while (t !=0)

double b - the variable, which is updated during every minute on every tick.

double b1 - value of variable b on the previous tick.

double s - amount on all ticks within one minute.

double s1 - amount on all ticks within the previous minute.

b1=b;

s=b1+b;

if (t=0) s1=s;

How to make data summarize on every tick and how to write it all correctly in general?

 
Alvin1976:

Yes, the script works.

Even the EA with this example works.

But my Expert Advisor gives zero. Let's look for error ......

Here is what has been detected.

If you just run the EA, it works fine but in the Strategy Tester it produces zeros at the very beginning. But in the next line zeros magically become a normal indicator data. I don't understand what's going on.....

Below is the code.

This is what the tester reports.

CJ 0 19:40:47.022 Core 1 EURUSD,M15 (Alpari-Ltd-Demo): OHLC bar states generating. OnTick execution on the bar begin only
IJ 0 19:40:47.022 Core 1 EURUSD,M15: testing of experts\tmp.ex5 from 2004.01.07 00:00 to 2004.02.02 00:00 started
LK 0 19:40:47.022 Core 1 2004.01.07 00:00:00 B_up_s=0.0 M_av_s=0.0
IM 0 19:40:47.022 Core 1 2004.01.07 00:00:00 sbl_top=true B_up_s=0.0 M_av_s=0.0 B_up_s-M_av_s=0.00000000
OQ 0 19:40:47.022 Core 1 2004.01.07 00:15:00 B_up_s=1.279208140152992 M_av_s=1.2718
OI 0 19:40:47.022 Core 1 2004.01.07 00:15:00 sel=true B_up_s=1.279208140152992 M_av_s=1.2718 B_up_s-M_av_s=0.00740814
CJ 0 19:40:47.022 Core 1 2004.01.07 00:30:00 B_up_s=1.279239651523307 M_av_s=1.2721
LN 0 19:40:47.022 Core 1 2004.01.07 00:45:00 B_up_s=1.279382808470771 M_av_s=1.2714
DS 0 19:40:47.022 Core 1 2004.01.07 01:00:00 B_up_s=1.279407737043184 M_av_s=1.2719
OD 0 19:40:47.022 Core 1 2004.01.07 01:15:00 B_up_s=1.279398917431485 M_av_s=1.2727
NI 0 19:40:47.022 Core 1 2004.01.07 01:30:00 B_up_s=1.279380702214008 M_av_s=1.2731
IM 0 19:40:47.022 Core 1 2004.01.07 01:45:00 B_up_s=1.279440208228416 M_av_s=1.2723
LE 0 19:40:47.022 Core 1 2004.01.07 02:00:00 B_up_s=1.279473515350084 M_av_s=1.272
DK 0 19:40:47.022 Core 1 2004.01.07 02:15:00 B_up_s=1.279515146590278 M_av_s=1.2715
FL 0 19:40:47.022 Core 1 2004.01.07 02:30:00 B_up_s=1.279601656250741 M_av_s=1.2709
IQ 0 19:40:47.022 Core 1 2004.01.07 02:45:00 B_up_s=1.279656914787522 M_av_s=1.2709
CE 0 19:40:47.022 Core 1 2004.01.07 03:00:00 B_up_s=1.279579626541548 M_av_s=1.2707

//#property link      "http://www.mql5.com"
//#property version   "1.00"

//+------------------------------------------------------------------+

 double
   BDev_var_b=2,       
   BDev_var_s=2,       
   B_up_s=0, 
   B_low_s=0,
   B_med_s=0,
   M_av_s=0, 
   Base[1],
   Upper[1],
   Lower[1],
   MAv[1],   
   Dist_c=0.0015,
   Dist_d=0.002;
   
  bool 
    selord=false,  
    baiord=false,
    New_Bar=false, 
    sbl_top=false,    
    sbl_bot=false, 
    bai=false,  
    sel=false;
   
   int
    Bands_handle,    
    MA_handle,   
    MA_var_s=1,   
    BP_var_s=50;  

    
//=============================================================================================================
int OnInit()
  {

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {


   Bands_handle=iBands(NULL,0,BP_var_s,0,BDev_var_s,PRICE_CLOSE);
   CopyBuffer(Bands_handle,0,0,1,Base);
   CopyBuffer(Bands_handle,1,0,1,Upper);
   CopyBuffer(Bands_handle,2,0,1,Lower);
   B_med_s=Base[0];
   B_up_s=Upper[0];
   B_low_s=Lower[0];
   
   MA_handle=iMA(NULL,0,MA_var_s,0,MODE_SMA,PRICE_TYPICAL);
   CopyBuffer(MA_handle,0,0,1,MAv);
   M_av_s=MAv[0];
   
   
  Print(" B_up_s=",B_up_s, "   M_av_s=", M_av_s);  

   
   // установка флагов
  if (sbl_top==false && (B_up_s-M_av_s)<Dist_c) 
     {
       sbl_top=true;
       Print("sbl_top=true   ","  B_up_s=",B_up_s, "  M_av_s=", M_av_s, "  B_up_s-M_av_s=", DoubleToString(B_up_s-M_av_s));
     }
  if (sbl_top==true && (B_up_s-M_av_s)>Dist_d)
       {
       sbl_top=false;
       Print("sel=true  ","  B_up_s=",B_up_s, "  M_av_s=", M_av_s, "  B_up_s-M_av_s=", DoubleToString(B_up_s-M_av_s));
       }
    


    return;                                   
   }


 
Alvin1976:

Here's what has been discovered.

1) Do not create indicators on every tick!

2) Check whether the indicator was created or not, before accessing its values.

3) Check if the requested values are copied from the indicator.

4) Always delete the calculation part of the indicator after the completion of the expert, otherwise it may at some point run out of RAM.

The problem was because you tried to copy the values from the indicator which hasn't been created yet. Make it a rule - check the result of the functions you call.

Files:
 

Please advise where the error is.

Ekpert is running on demo, 3 pairs. Here is the Log:

PUSD,M15)       AUDUSD Request executed: delete buy Ticket: #729600  Price = 0.94794 Lots = 0.1 StopLoss = 0 TakeProfit = 0 Comment = BuyTP(4)
LH      0       08:45:05.881    Serendipity5.0 (GBPUSD,M15)     AUDUSD Request executed: delete buy Ticket: #729598  Price = 0.94514 Lots = 0.42 StopLoss = 0 TakeProfit = 0 Comment = BuyTP(4)
KM      0       08:45:06.053    Serendipity5.0 (GBPUSD,M15)     Error: AUDUSD Action: delete buy : Requested Volume: 0.42, Requested StopLoss:0, Requested TakeProfit:0, Requested Price:0.94514 Error:10013
OE      0       08:45:06.053    Serendipity5.0 (GBPUSD,M15)     Sleep() for 1 second, retry
LO      0       08:45:07.145    Serendipity5.0 (GBPUSD,M15)     DeletePendings(): AUDUSD Error deleting pending order: Error Code: 4756
CQ      0       08:45:07.145    Serendipity5.0 (GBPUSD,M15)     SendOrder(); OrderCheck() Error = 10013 Sleep() for 1 second, retry
GP      0       08:45:08.237    Serendipity5.0 (GBPUSD,M15)     DeletePendings(): AUDUSD Error deleting pending order: Error Code: 4756
PL      0       08:45:08.237    Serendipity5.0 (GBPUSD,M15)     SendOrder(); OrderCheck() Error = 10013 Sleep() for 1 second, retry

Here is the code.

bool SendTradeRequest(string symb,ENUM_ORDER_TYPE type,ENUM_TRADE_REQUEST_ACTIONS tradeAction,double price, 
   double lot,double stopLoss,double takeProfit,long const magic, string comment,ulong ticket){
//--- prepare a request
   MqlTradeRequest request={0};
   //ZeroMemory(request);
   
   request.action=tradeAction;        
   request.magic=magic;              
   request.symbol=symb;                 
   request.volume=lot;                         
   request.sl=stopLoss;                             
   request.tp=takeProfit;                                 
   request.type=type;              
   request.price=price;
   request.comment=comment;
   if(tradeAction==TRADE_ACTION_MODIFY || tradeAction==TRADE_ACTION_REMOVE)request.order=ticket;
      
//--- send a trade request
   MqlTradeResult result={0};
   MqlTradeCheckResult checkresult={0};
   //ZeroMemory(result);
   //ZeroMemory(checkresult);
   bool isSuccessful;
   isSuccessful=OrderCheck(request,checkresult);
   if(checkresult.retcode!=0 && checkresult.retcode!=10009 && checkresult.retcode!=10008){
      Print("SendOrder(); OrderCheck() Error = ",checkresult.retcode, " Sleep() for 1 second, retry"); 
      Sleep(1000); return(0);                                      
      }       
      
   isSuccessful=OrderSend(request,result);        
   if( (result.retcode == 10009 || result.retcode == 10008 || result.retcode == 0) ){
      //--- write the server reply to log  
      //Print(__FUNCTION__,":",result.comment);
      Print(symb," "+result.comment+": "+ConvertTradeActionToString(request.action)+" ",(string)ConvertOrderTypeToString(request.type)," Ticket: #",(string)result.order," Price = ",(string)request.price," Lots = ",(string)request.volume,
         " stopLoss = ",(string)request.sl," takeProfit = ",(string)request.tp," Comment = ",(string)request.comment);
      
      if(tradeAction==TRADE_ACTION_PENDING)   
         arrow_cntr=CreateSignalArrow("Open "+(string)ConvertOrderTypeToString(request.type)+" "+request.comment+" Ticket "+(string)result.order+
            " Lots "+DoubleToString(request.volume,2)+" Time "+TimeToString(TimeCurrent(),TIME_MINUTES),arrow_cntr,request.price,216,clrDarkViolet,symb);
      
      if(tradeAction==TRADE_ACTION_MODIFY)   
         arrow_cntr=CreateSignalArrow("OrderModify "+request.comment+" Ticket "+(string)request.order+" Lots "+DoubleToString(request.volume,2)+
            " Time "+TimeToString(TimeCurrent(),TIME_MINUTES),arrow_cntr,request.price,251,clrSilver,symb); 
      
      if(tradeAction==TRADE_ACTION_REMOVE)
         arrow_cntr=CreateSignalArrow("OrderDelete "+request.comment+" Ticket "+(string)request.order+" Lots "+DoubleToString(request.volume,2)+
            " Time "+TimeToString(TimeCurrent(),TIME_MINUTES),arrow_cntr,Close(1,symb),158,Red,symb);      
      
      CheckErrorMessage("SendTradeRequest():");
      return(1);  
      }  

Expert tries to execute OrderDelete(), gives error, SendTradeRequest() is in while loop, ekspert accordingly loops. Maybe the price has changed when I submitted the order? Do I need RefreshRates() and how to deal with it correctly? I searched in forum but found no example...

Thank you all in advance