Questions from Beginners MQL5 MT5 MetaTrader 5 - page 411

 
Leanid Aladzyeu:

Started studying arrays .

I have written a pro-order function with arrays , 3 arrays = 0 buy, 1= sell, 2= total buy and sell trades.

Then I wrote a condition to buy if ( the first condition && array [0] == 0 )then we open a buy

and on sale if ( the first condition && array [1]== 0) then we open a Sell position.

But the tester throws an error and the test stops, what is the problem? ( error " array exit " )

It means that the index you are addressing is outside the array layout. For example, if array[ 3 ]; you may address only items from 0 to 2. Check your code.

Another possibility is that you don't partition the array (with ArrayResize()), but try to access some of its elements.

 
code...
void Oher(int Mag,string Symb, int &mas[2])
{
ArrayInitialize( mas,0);

int tip;
int ticket3 =-1;
  for (int pos=OrdersTotal()-1; pos>=0; pos--)
     {
     if(OrderSelect(pos,SELECT_BY_POS,MODE_TRADES) && OrderMagicNumber()==Mag && OrderSymbol()==Symb && OrderTicket()!=ticket3)
          {
          ticket3=OrderTicket();
          tip=OrderType();
          
          switch(tip)
              {
              case 0:mas[0]++; mas [2]++;break;// 0 покупка
              case 1:mas[1]++; mas [2]++;break;// 1 продажа
              }
          }
          continue;
     }

}
Чудо код вставил ) 2 раз получилось за все время что я на сайте)
 
Leanid Aladzyeu:
code...
Try to figure it out yourself, the tester should indicate in which line and position in the line the error occurred.
 
Tapochun:
Try to figure it out yourself, the tester should indicate in which line and position in the line the error occurred.

o_oops didn't know the terminal also pokes where the error is, first time I hit a critical error.

   case 1:mas[1]++; mas [2]++;break;// 1 продажа

It spits on it.

 
Fixed it. it was a total of 2 arrays and i needed 3. in the initialization of the array and in the function the number made +1. and it worked)
 

Please advise how to check how the indicator works.

The idea is the following: when the EA forms a new bar it calls the indicator, but the indicator has no data at this moment and I get the data of the previous bar (it has not had time to calculate).

How to avoid this problem. Moreover, everything is OK in the Strategy Tester. I get either fine or not at all on the demo!

That is it, it is not relevant. Resolved.

 

Hello, I have a beginner's problem...

I can't get the system to compare the last bar close value with the highs and lows of a certain period, e.g.

last 20 bars. What am I doing wrong?

extern int     MAGIC             = 90324752;
extern int     START_HOUR        = 7;
extern int     FINISH_HOUR       = 18;
extern int     TRADE_LIMIT       = 3;
extern int     SMMA_PERIOD       = 100;
extern int     BB_PERIOD         = 50;
extern double  BB_DEV            = 1;
extern double  LOT               = 1;
extern double  StopLoss          = 400;
extern double  TakeProfit        = 2000;

void OnTick()
{
static int    TICKET;

double SMMA                 = iMA (Symbol(),0,SMMA_PERIOD,0,MODE_SMMA,PRICE_CLOSE,0);
double BB_UP                = iBands(Symbol(),0,BB_PERIOD,BB_DEV,0,PRICE_CLOSE,MODE_UPPER,0);       

double HIGHEST              = High[iHighest(Symbol(),0,MODE_HIGH,20,0)];

if(Hour() >= START_HOUR && Hour() <= FINISH_HOUR && TRADE_COUNT < TRADE_LIMIT && POS_OPENED == FALSE && Seconds() >= 55)
if(Minute() == 29 || Minute() == 59)
{     
  if(Close[0] > SMMA && Close[0] > BB_UP && Close[0] >= HIGHEST)

  {
   TICKET = OrderSend(Symbol(),OP_BUY,LOT,Ask,0,Ask-StopLoss*Point,Ask+TakeProfit*Point,NULL,MAGIC,0,Blue);
   if(TICKET < 0)
      {
         Alert("Order Send failed, error # ", GetLastError() );
      } 
  }
}
}


Checked with Alert and for some unknown reason tester gives the maximum value from the calculation from the very last tick, but it gives this value

It is the same number of times as the ticks, so the order does not open.


The second issue is how to make my EA wait until the next bar opens (30m period)? When I test using control points everything is fine, but if I use ticks,

then EA opens in the same bar right after it closed at stop loss...


Also, how can I close a deal, regardless of whether it was a tick or not? For example, on a period of 30m I want to close the order 5 seconds before the end of the bar (29.55, 59.55)

Based on all the data at that moment. At this stage the problem is that if a tick does not occur within 5 seconds, therefore no

(In theory this is bullshit, but in reality the next tick might appear in a few candlesticks for some reason.

may turn out to be in a few candles for some reason.


Thanks in advance!
 

Help with the comments. On each bar, comments are generated. On every tick I want to add more text to the existing comment.

I call the code:

void UserMarketComment( )

{

   string com=ChartGetString(0,CHART_COMMENT);  // Получаем текст существующего комментария

   string st="Целевой убыток="+DoubleToStr(-inpTargetLoss,2)+" "+AccountCurrency()+"  Текущ";

   if(AccountProfit()<0) st=st+"ий убыток=";

   else st=st+"ая прибыль=";

   st=st+DoubleToStr(AccountProfit(),2)+" "+AccountCurrency()+"  Целевая прибыль="+DoubleToStr(inpTargetProfit,2)+" "+AccountCurrency();

   Comment(com+"",TimeToStr(TimeCurrent(),TIME_SECONDS),"    Spread = ",MarketInfo(Symbol(),MODE_SPREAD),

           "\n ",st);

  }

Adds new comment and on every tick in a new line and text slides down.

How to make it delete last few lines before outputting?

 
ForTorg ZEVs:

Help with the comments. On each bar, comments are generated. On every tick I want to add more text to the existing comment.

I call the code:

void UserMarketComment( )

{

   string com=ChartGetString(0,CHART_COMMENT);  // Получаем текст существующего комментария

   string st="Целевой убыток="+DoubleToStr(-inpTargetLoss,2)+" "+AccountCurrency()+"  Текущ";

   if(AccountProfit()<0) st=st+"ий убыток=";

   else st=st+"ая прибыль=";

   st=st+DoubleToStr(AccountProfit(),2)+" "+AccountCurrency()+"  Целевая прибыль="+DoubleToStr(inpTargetProfit,2)+" "+AccountCurrency();

   Comment(com+"",TimeToStr(TimeCurrent(),TIME_SECONDS),"    Spread = ",MarketInfo(Symbol(),MODE_SPREAD),

           "\n ",st);

  }

Adds new comment and on every tick in a new line and text slides down.

How to make it delete last few lines before outputting?

It seems to me that you can only completely replace Comment()

And consequently, to change a part of the comment on every tick, you have to change the comment completely on every tick.

 
ForTorg ZEVs:

Help with the comments. On each bar, comments are generated. On every tick I want to add more text to the existing comment.

I call the code:

void UserMarketComment( )

{

   string com=ChartGetString(0,CHART_COMMENT);  // Получаем текст существующего комментария

   string st="Целевой убыток="+DoubleToStr(-inpTargetLoss,2)+" "+AccountCurrency()+"  Текущ";

   if(AccountProfit()<0) st=st+"ий убыток=";

   else st=st+"ая прибыль=";

   st=st+DoubleToStr(AccountProfit(),2)+" "+AccountCurrency()+"  Целевая прибыль="+DoubleToStr(inpTargetProfit,2)+" "+AccountCurrency();

   Comment(com+"",TimeToStr(TimeCurrent(),TIME_SECONDS),"    Spread = ",MarketInfo(Symbol(),MODE_SPREAD),

           "\n ",st);

  }

Adds new comment and on every tick in a new line and text slides down.

How to make it delete last few lines before outputting?

So you have a control character "\n" in your comment - it's a line feed character. If you remove it, there won't be multiple lines.