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

 
Alexey Belyakov:

Put it in. I didn't. It opens in packs. Magic - it ignores it.



When you write code, translate its logic into Russian. At first, you have a cycle in which comparisons are made and if the selected order is not a market order, then to the next iteration; if the symbol is not EURUSD then to the next iteration and if the magik is not equal to your magik, to the next iteration. At the end of the cycle you count the number of iterations passed before the end of the cycle. The cycle finishes and the program starts to open orders according to the conditions. Considering that one of the conditions is always true, an order is opened. And this happens on every tick.

 
Valeriy Yastremskiy:

When writing the code, translate its logic into Russian. You first have a cycle in which comparisons are made; if the selected order is not a market order, then to the next iteration; if the instrument is not the Eurobucks, then to the next iteration; and if the magik is not equal to your magik, then to the next iteration. At the end of the cycle you count the number of iterations passed before the end of the cycle. The cycle finishes and the program starts to open orders according to the conditions. Considering that one of the conditions is always true, an order is opened. And this happens on every tick.

I see. But in this case, if(OrderMagicNumber() == MagicNumber)continue, the order that has been placed is accepted for an order placed by Magic(o). It means that the order placed manually on EUROBAX should be ignored and one more order should be opened by an Expert Advisor with Magic.

OrdersTotal() - this is the problem that puts an end to any manipulations with Magic.
 
Alexey Belyakov:

I understand. But in this case: if(OrderMagicNumber() == MagicNumber)continue; Orders that are placed are taken for orders placed by Magic(s). That is, a manually placed order on Eurobucks should be ignored, and another one should be opened by an EA with Magic.

OrdersTotal() - this is the infestation that puts a stop to any manipulation of the magician.

The code is executed from top to bottom. After the loop you have the placing of orders. if(OrderMagicNumber() == MagicNumber)continue; This will interrupt the execution of the loop body and a new loop iteration will start. The loop will terminate and order placing will start. This is how you have written it. The total number of orders has nothing to do with this. If you want to make a condition that if there are no orders placed with your magik and instrument, then the order placing Code should be different.

Cycle through the order numbers. If an order is found with our magik and on our instrument, then return - exit from start. Or a flag that your order is there and at the start ontik or start check on the flag.

And it is better to create an EA from a template to create an EA script, an indicator. The main code fields will be more correct.

datetime some_time=TimeCurrent();
//extern string Symbol3 = ""; //Инструмент (""текущий по умолчанию)
extern int P=1;          //Таймфрейм
extern int MagicNumber = 100500;


int start()
{

int send;
                                     
double SL=200;                                   
double TP=200;                       
double Lots=1;       

for(int i = OrdersTotal(); i >= 0; i--) 
{
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == "EURUSD" && OrderMagicNumber() == MagicNumber)return(0);
}
          

             
if ((Close[0]>High[1])&&(n==0))
{
send=OrderSend("EURUSD",OP_BUY,Lots,Ask,3,Bid-SL*Point,Bid+TP*Point,MagicNumber);
}

if ((Close[0]<Low[1])&&(n==0))  
{
send=OrderSend("EURUSD",OP_SELL,Lots,Bid,3,Ask+SL*Point,Ask-TP*Point,MagicNumber);
}

return(0);
}
 
Valeriy Yastremskiy:

The code is executed from top to bottom. in a loop according to the rules of the loop. After the loop you have placing orders. if(OrderMagicNumber() == MagicNumber)continue; This will interrupt the execution of the loop body and a new loop iteration will start. The loop will terminate and order placing will start. This is how you have written it. The total number of orders has nothing to do with this. If you want to make a condition that if there are no orders placed with your magik and instrument, then the order placing Code should be different.

Cycle through the order numbers. If an order is found with our magik and on our instrument, then return - exit from start. Or a flag that your order is there and at the start ontik or start check on the flag.

And it is better to create an EA from a template to create an EA script, an indicator. The main code fields will be more correct.

for(int i = OrdersTotal(); i >= 0; i--) 
{
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == "EURUSD" && OrderMagicNumber() == MagicNumber)return(0);
}

I guess this is not the solution, but in what direction to work?

I have tried a couple of dozen variants. Either opens one order, or without any interruption, tuyvukuchu.

Here, by the way, over OrderSekect - a variable of bool type should be set, otherwise it will swear.

 
Alexey Belyakov:

This is not a solution, but which direction should I work in?

I've already tried a couple of dozen variants. Either opens a single order, or opens without any interruption.

Here by the way, over OrderSekect - a variable of bool type should be set, otherwise it will swear.

Yes, it should, it returns to nowhere.

for(int i = OrdersTotal(); i >= 0; i--) 
{
     bool sel = OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() == "EURUSD" && OrderMagicNumber() == MagicNumber)return(0);
}

You'd better write algorithm, which you want to do.

 
Alexey Belyakov:

This is not a solution, but which way to go?

I've already tried a couple of dozen variants. Either opens a single order, or opens without any interruption.

Here by the way, over OrderSekect - a variable of bool type should be set, otherwise it will swear.

Instead of return(0) it should be continue

 

Good and knowledgeable people! Help to implement the Envelopes indicator in an MT5 EA. The EA itself has to process every tick (without skipping). I have tried some variants, I've looked in documentation, I seem to like the variant, but it is for handler OnCalculate. I haven't tried it with standard library yet ... Well, here is my initial implementation:

input int       Indicatorperiod         = 3;
input double    EnvelopesDeviation      = 0.07;
int    handle;                                          //--- переменная для хранения хэндла индикатора iEnvelopes
double upperEnv[3], lowerEnv[3];                        // динамические массивы для хранения численных значений Emvelopes
double local_envelopesupper, local_envelopeslower;      // в эти переменные пытаюсь получить значения верхней и нижней линии индикатора
...

int OnInit()
...
handle=iEnvelopes(_Symbol,_Period,Indicatorperiod,0,MODE_LWMA,PRICE_OPEN,EnvelopesDeviation);
...

void OnTick()
...
//--- Объявляем структуру, которая будут использоваться
   MqlRates mrate[3];           // Будет содержать цены, объемы и спред для каждого бара
   ArraySetAsSeries(mrate, true); 

      //--- Получить исторические данные последних 3-х баров
      if(CopyRates(_Symbol,_Period,0,3,mrate)!=3)
        {
         Alert("Ошибка копирования исторических данных - ошибка:",GetLastError(),"!!");
         return;
        }

      //--- Используя хэндлы индикаторов, копируем новые значения индикаторных буферов в массивы
      if(CopyBuffer(handle,0,0,3,upperEnv)<2 || CopyBuffer(handle,1,0,3,lowerEnv)<2)
        {
         Alert("Ошибка копирования буферов индикатора Envelopes - номер ошибки:",GetLastError(),"!!");
         return;
        }
...
local_envelopesupper = upperEnv[1];
local_envelopeslower = lowerEnv[1];
...

In the visual tester I get:


Can you tell me what's wrong and what's the best way to do it?
Документация по MQL5: Стандартная библиотека
Документация по MQL5: Стандартная библиотека
  • www.mql5.com
Некорректное отображение индикатора
 
Valeriy Yastremskiy:

Yes, it's supposed to swear, it returns to nowhere.

You'd better write down the algorithm you want to do, it's not clear what you need.

In the first message is the code. It's easy: we break through the previous high/low - open a deal. The orders opened by this EA should not cross the other orders opened manually or by another EA, i.e. the EA should work independently.

I searched all over the Internet. It is a rather trivial topic, but there are a lot of variations. It seems to be a simple thing, and nowhere to be found.

 
Alexey Belyakov:

In the first message, the code. It's simple: break the previous high/low - open a trade. The orders opened by this EA should not overlap with other orders opened manually, or with those opened by another EA, i.e. the EA should work independently.

I searched all over the Internet. It is a rather trivial topic, but there are a lot of variations. It seems to be a simple thing, and nowhere to be found.

Describe the work of the Expert Advisor in full step by step. The first step is to check if there are orders with our magik on the selected symbol. If we have them, then we finish the work and if we don't, then we set the orders. What happens after the orders have been placed? It goes like this
 
Maxim Kuznetsov:

instead of return(0) the meaning should be continue

There if equals, so we don't go any further and finish... I don't like it if it's not equal then go on. It's harder to understand.