Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1901

 
JRandomTrader #:

or even

And no one noticed the joint!

static int head=0;
for(i=head,count=0;count<ArraySize(A);count++)
  {
   -if(++i>=ArraySize(A))i=0;
   DoSomething(A[i]);
   +if(++i>=ArraySize(A))i=0;
  }
 
JRandomTrader #:

And no one noticed the joint!

I still don't see it.

 
Vitaly Muzichenko #:

Still don't see.

i should be incremented at the end of the loop, after use.

But you can also check for array overruns at the end of the loop and at the beginning of the next one.

 
Alexey Viktorov #:

Who told you such nonsense?

Read carefully how it is done.


I didn't know this function was so universal. But even if it is, what difference does it make? ArrayCopy will shift data faster than a normal loop?

 
Vitaly Muzichenko #:

The code is working now, but there are doubts about the speed because of the cycle.

What will it be?

I want to test some kitchens for possibility of arbitrage, indicator will draw difference of values, works on timer once in 100ms. Code execution delays are critical for this situation.

Then I don't know. Try ArrayCopy. The function seems to be able to shift data. But it will hardly do it faster than a usual loop. Well, who knows... You have to try it.

 
Mihail Matkovskij #:

Then I don't know. Try ArrayCopy. The function seems to be able to shift data. But it is unlikely to do it faster than a normal loop. Well, who knows... You have to try it.

I don't know where to start

 
Vitaly Muzichenko #:

I don't know where to start.

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
  int H=100;
  double b, a;
  if(prev_calculated==0) {
    ArrayInitialize(Buffer1,EMPTY_VALUE);
    ArrayInitialize(Buffer2,EMPTY_VALUE);
  }

  b=SymbolInfoDouble(Symbol(),SYMBOL_BID);
  a=SymbolInfoDouble(Symbol(),SYMBOL_ASK);

  if(rates_total-prev_calculated==1) {
    Buffer1[H+1]=EMPTY_VALUE;
    Buffer2[H+1]=EMPTY_VALUE;
  }
  else if (rates_total == prev_calculated) { // сдвигаем данные только когда количество баров не поменялось (в случае появления нового бара они сдвигаются системой терминала)
    ArrayCopy(Buffer1, Buffer1, 1, 0, H);
    ArrayCopy(Buffer2, Buffer2, 1, 0, H);
  }
  // записываем новые данные
  Buffer1[0]=b;
  Buffer2[0]=a;
  return(rates_total);
}

It's supposed to work. Check it out.

P.S. And please write how much speed variant with ArrayCopy function changes. I would like to know it myself.

 
Vitaly Muzichenko #:

Don't know where to start

Here is a variant on mql5. But only for Ask price. For Bid price you have to make it yourself.

It has everything, and work with indicator buffer, and work with ticks, and example how it is not necessary to reverse indexing of indicator buffer.

At 1000 bars it displays without any delay at all.

Files:
 
Alexey Viktorov #:

Here is a variant on mql5. But only for Ask price. For Bid price finalize it on your own.

It has everything, including work with indicator buffer, and work with ticks, and example how indexing of indicator buffer does not need to be reversed.

At 1000 bars it displays without any delay at all.

Got it! Thank you!

 

Hello, I have recently started working with MT4 and came across the following issue: there is no proper function to track a closed order. My algorithm is simple: when a new candle appears, I place two pending orders: a Sell Stop and a Bid Stop and I need to follow the closed order (Sell Stop or Bid Stop) when one of them closes and place one order instead of the closed one. Orders are placed - no problem with that, but I can not understand how to track the closed ones, I put the code, but the message displays only SellSTOP, and does not react to BAYSTOP:


if (OrderSelect(OrdersHistoryTotal()-1, SELECT_BY_POS, MODE_HISTORY))
   {
   Print("OrderType", OrderType());//Тут всегда только только "1"
   }
   if(OrderType()==0)
    {
    Print("Закрылся  BUYSTOP:", OrderTicket());
    }
   if(OrderType()==1)
   {Print("Закрылся   SELLSTOP:",  OrderTicket());} //Тут сообщает корректно - номер ордера

And I also came across the fact that OnTrade orOnTradeTransaction does not work, tried this:

void OnTradeTransaction()
 {
 Print("status - "); 
 }  
Reason: