Learning and writing together in MQL5 - page 10

 

I don't know if this is the right question.

Is it possible to combine debug and tester? I.e. if I start with F5, the tester doesn't start of course. And if I start the exprrt in the tester, the breakpoints are ignored. Or am I doing something wrong?

 
Lunatic:

I don't know if this is the right question.

Is it possible to combine debugging and a tester?

Not at the moment.
 
alexvd:
Not at the moment.

So only in real time on the demo? What about debugging on interesting situations?

I was so excited about the debugger at first, but I'm sliding back to debugging printouts. No other debugging techniques are visible. - Frustrating.

 

Studying the Expert Advisor supplied as an example, MovingAverage. Everything about it is beautiful. Question on the tester.

This EA analyses the candlestick only on the first tick. I noticed that some situations are slipped without processing. It turns out that a rare candlestick is born from the first tick. Is it a connectivity effect, my glitch or that of the tester?

Apparently, in order to process a candle only at birth, i.e. once, the last processed candle should be stored in a static variable?

P/S/ Just starting to learn the language, hence such tedious questions.

Документация по MQL5: Основы языка / Переменные / Статические переменные
Документация по MQL5: Основы языка / Переменные / Статические переменные
  • www.mql5.com
Основы языка / Переменные / Статические переменные - Документация по MQL5
 

Hello, I am writing an EA to see if I can make it in time for the championship. I wrote some simple code, it uses 2 custom indicators, but the value of these indicators is 0, so the trade is not done.

//+------------------------------------------------------------------+
//|                                                  trend_masim.mq5 |
//|                        Copyright 2010, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2010, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"

input int period =48;
input int period1 =30;
input int sl=200;
input double lot=0.1;
input long magicBUY1=102;
input int magicSELL1=202;
input string sim="eur";

double ind1[];
double ind2[];
int hind1;
int hind2;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

   hind1=iCustom("EURUSD",PERIOD_H1,"trend_v3_5",period);
   hind2=iCustom("EURUSD",PERIOD_H1,"trend_v3",4);



   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
IndicatorRelease(hind1);
IndicatorRelease(hind2);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int bar;
double MAX,MIN,H,A0,A1,A2;
void OnTick()
  {
  
 int   i, j,jj, k, g, q, ticket,l;
    double stop;
    MqlTradeRequest mrequest;  
 MqlTradeResult mresult;
   MqlTick latest_price;   
 


ArraySetAsSeries(ind1,true);
ArraySetAsSeries(ind2,true);
MAX=CopyBuffer(hind1,2,0,3,ind1);
MIN=CopyBuffer(hind2,0,0,3,ind2);

SymbolInfoTick(_Symbol,latest_price);
//BUY


   if (PositionSelect(_Symbol)==1 && PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY ){jj=1;} //определяет если позиция BUY
   if (jj==0 ){
if ( ind2[1]>ind1[1]){ // условие на покупку
Print("индикатор=",ind2[1]," ",ind1[1]);
         mrequest.action = TRADE_ACTION_DEAL;                                  // немедленное исполнение
         mrequest.price = NormalizeDouble(latest_price.ask,_Digits);           // последняя цена ask
         mrequest.sl = NormalizeDouble(latest_price.ask - sl*_Point,_Digits); // Stop Loss
         mrequest.tp = NormalizeDouble(0,_Digits); // Take Profit
         mrequest.symbol = _Symbol;                                            // символ
         mrequest.volume = lot;                                                // количество лотов для торговли
         mrequest.magic = magicBUY1;                                            // Magic Number
         mrequest.type = ORDER_TYPE_BUY;                                       // ордер на покупку
         mrequest.type_filling = ORDER_FILLING_AON;                            // тип исполнения ордера - все или ничего
         mrequest.deviation=10;                                               // проскальзывание от текущей цены
         //--- отсылаем ордер
         OrderSend(mrequest,mresult);

GlobalVariableSet( "k11"+sim,Bars2);
GlobalVariableSet( "bar11"+sim,Bars("EURUSD",NULL));
}}
   
  
  }
//+------------------------------------------------------------------+
 

Lunatic:

This EA analyses a candle only on the first tick. I noticed that some situations slip through without processing. It turns out that a rare candlestick is born from the first tick. Is it a connectivity effect, my glitch or that of the tester?

Apparently, to process a candlestick only at birth, i.e. once, we should store the last processed candlestick in a static variable?

It is better to track the appearance of a new bar by changing the time of the bar, previously storing it in a static variable.

Examples can be found in the article IsNewBar and in the article Limitations and checks in EAs.

 
Automated-Trading:

The appearance of a new bar is better tracked by changing the time of the bar, having previously saved it in a static variable.

Examples can be found in IsNewBar, and the article Limits and checks in EAs.

Thank you, that's clear.

But here's what I don't understand - how to debug EAs. It is clear that it is not the system that is being tested, but the bugs in the program. For example, it is not clear why the program does not enter a certain branch - why? Do I need to put different debugging stamps, then look for them in the journal, then put other stamps, wait for a certain time until the program is working and then look again in the journal ?

What is the best way to search for program errors?

 
m_a_sim:

Hello, I am writing an EA to see if I can make it in time for the championship. I wrote some simple code and used 2 custom indicators, but the values of these indicators are 0 and no trades are executed.

To debug your code I have replaced custom indicators with MA:

   hind1=iMA("EURUSD",PERIOD_H1,20,0,MODE_SMA,PRICE_CLOSE);
   hind2=iMA("EURUSD",PERIOD_H1,40,0,MODE_SMA,PRICE_CLOSE);

And corrected copying of values into array:

MAX=CopyBuffer(hind1,0,0,3,ind1);
MIN=CopyBuffer(hind2,0,0,3,ind2);

Now there are indicator values:

//+------------------------------------------------------------------+
//|                                                  trend_masim.mq5 |
//|                        Copyright 2010, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2010, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

input int period =48;
input int period1 =30;
input int sl=200;
input double lot=0.1;
input long magicBUY1=102;
input int magicSELL1=202;
input string sim="eur";

double ind1[];
double ind2[];
int hind1;
int hind2;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

   hind1=iMA("EURUSD",PERIOD_H1,20,0,MODE_SMA,PRICE_CLOSE);
   hind2=iMA("EURUSD",PERIOD_H1,40,0,MODE_SMA,PRICE_CLOSE);

   return(0);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
IndicatorRelease(hind1);
IndicatorRelease(hind2);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int bar;
double MAX,MIN,H,A0,A1,A2;
void OnTick()
  {
  
int   i, j,jj, k, g, q, ticket,l;
    double stop;
    MqlTradeRequest mrequest;  
MqlTradeResult mresult;
   MqlTick latest_price;  



ArraySetAsSeries(ind1,true);
ArraySetAsSeries(ind2,true);
MAX=CopyBuffer(hind1,0,0,3,ind1);
MIN=CopyBuffer(hind2,0,0,3,ind2);

SymbolInfoTick(_Symbol,latest_price);
//BUY


   if (PositionSelect(_Symbol)==1 && PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY ){jj=1;} //определяет если позиция BUY
   if (jj==0 ){
if ( ind2[1]>ind1[1]){ // условие на покупку
Print("индикатор=",ind2[1]," ",ind1[1]);
         mrequest.action = TRADE_ACTION_DEAL;                                  // немедленное исполнение
         mrequest.price = NormalizeDouble(latest_price.ask,_Digits);           // последняя цена ask
         mrequest.sl = NormalizeDouble(latest_price.ask - sl*_Point,_Digits); // Stop Loss
         mrequest.tp = NormalizeDouble(0,_Digits); // Take Profit
         mrequest.symbol = _Symbol;                                            // символ
         mrequest.volume = lot;                                                // количество лотов для торговли
         mrequest.magic = magicBUY1;                                            // Magic Number
         mrequest.type = ORDER_TYPE_BUY;                                       // ордер на покупку
         mrequest.type_filling = ORDER_FILLING_AON;                            // тип исполнения ордера - все или ничего
         mrequest.deviation=10;                                               // проскальзывание от текущей цены
         //--- отсылаем ордер
         OrderSend(mrequest,mresult);

}}
  
  
  }
//+------------------------------------------------------------------+
 
AM2:

To debug your code I replaced the custom indicators with MA:

And corrected the copying of values into the array:

Now there are indicator values:

So it's all about the indicators? The tester is giving me an error

2010.09.01 19:44:36 Core 1 2010.06.01 00:00:01 Array out of range in 'trend_v3.mq5' (71,9)
2010.09.01 19:44:36 Core 1 2010.06.01 00:00:01 Array out of range in 'trend_v3_5.mq5' (83,9)

 

To m_a_sim

I think it's more correct to check the handicapability of handles before copying information from the indices. It would also be better to check availability of these data ....

MAX=CopyBuffer(hind1,2,0,3,ind1);
MIN=CopyBuffer(hind2,0,0,3,ind2);
Reason: