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

 
trader781:
I don't know how to explain it. But it's definitely a masterpiece.
I'm telling you, they like to be sarcastic here.) Trishkin took offense to that. He said he's gonna get help with the code.
 
Movlat Baghiyev:
I'm telling you they like to be sarcastic here ))) Trishkin also took offense. He said the code will help.

I'm as new as you but even I have a lot of questions

for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS)==true)
{
if(OrderSymbol()!=Symbol() ||OrderMagicNumber()!=Magic) continue;
if(OrderType()==OP_BUY||OrderType()==OP_SELL) p++;
if(OrderType()==OP_BUYSTOP)
if(signal_bue)OrderDelete(OrderTicket());
else b++;
if(OrderType()==OP_SELLSTOP)
if(signal_sell)OrderDelete(OrderTicket());
else s++;
}

}

1) where did you get b++

2) where did you gets++

3)if(signal_bue) is what else?

And the rest of the code. Sorry if I offended you.

 
That's probably the right way to go
//+------------------------------------------------------------------+
//|                                                      ОТЛОЖКИ.mq4 |
//+------------------------------------------------------------------+
extern double StopLoss     = 100; //Стоплосс ордера  
extern double TakeProfit   = 150; //Тейкпрофит ордера
extern double TrailingStop = 100; // трал
extern int    Delta        = 100; //Расстояние от цены для установки ордера
extern double LOT          = 0.1; //Объём позиции
extern int    Magic        =2;
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  if (TrailingStop!=0) Trailing();
  
   //ИНДИКАТОР RSI
   double RSI0=iRSI(NULL,0,5,PRICE_CLOSE,0);
   double RSI1=iRSI(NULL,0,5,PRICE_CLOSE,1);
  
   int b = 0, s = 0, p = 0, res = 0;

   double BuyPrice=Ask+Delta*Point;
   double SellPrice=Bid-Delta*Point;
  
   for (int i=0; i<OrdersTotal(); i++)
    {
     if (OrderSelect(i, SELECT_BY_POS)==true)
      {
        if (OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic) continue;
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) p++;
        if (OrderType()==OP_BUYSTOP)
        {
           if(RSI0<50&&RSI1>50)
                OrderDelete(OrderTicket());
            else
                b++;
        }
        if (OrderType()==OP_SELLSTOP)
        {
           if(RSI0>50&&RSI1<50)
                OrderDelete(OrderTicket());
              else
                s++;
        }
      }
    }

   double SL,TP; // < !!!!!!!!! эти переменные не используются, можно удалить

//---- buy stop
   if(RSI0>50&&RSI1<50 && p<1 && b<1) // < !!!!!!!!!
     {
      res=OrderSend(Symbol(),OP_BUYSTOP,LOT,BuyPrice,0,BuyPrice-StopLoss*Point,BuyPrice+TakeProfit*Point,"ОТЛОЖКИ",Magic,OP_SELLSTOP,Blue);
     }        
//---- sell stop  
   if(RSI0<50&&RSI1>50 && p<1 && s<1) // < !!!!!!!!!
     {
      res=OrderSend(Symbol(),OP_SELLSTOP,LOT,SellPrice,0,SellPrice+StopLoss*Point,SellPrice-TakeProfit*Point,"ОТЛОЖКИ",Magic,OP_BUYSTOP,Red);
     }
//----
   return(0);
}
 
Movlat Baghiyev:
I'm telling you, they like to be sarcastic here.) And Trishkin took offense at it. He said he'd help me with the code.

It's simpler that way:

void OrderDelete_(int Type)
  {
   for(int i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS)==true)
        {
         if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic) continue;
         if(OrderType()==Type)
            OrderDelete(OrderTicket());
        }
     }
  }

It's somewhere outside the void OnTick() function.

if (сигнал_bue) OrderDelete_(OP_SELLSTOP);

if (сигнал_sell)OrderDelete_(OP_BUYSTOP);

And this is in the void OnTick() function body.

 
Alekseu Fedotov:


And this is in the void OnTick() function body

Please make corrections in the code
 
trader781:

Okay.

Sort of finished what I could. The description is done. The purpose is to understand why it does not work as I want it to.

There is else inside the if() block. Was it really intended this way?

Further. The Counts() function accepts an uninitialized global variable count which, besides, is defined once again inside the function.

The Counts() function counts orders but compares them to some ticket that changes several times in the main function. But the ticket value is the same during the function operation. So, how many orders will the function calculate? Is it more than one?

This function in such a form does not need arguments. Just call it and let it return the result.

Next. Function FindLastOType() is again a comparison with the ticket. Can you guarantee that it is compared to a valid ticket? Isn't it simpler to interrupt the loop when the first order with the necessary magic number and symbol is found and then to return the order type?

Same with FindLastOrderOpenPrice() and FindLastLot().

The ModifyOrders() function horrified me so much that I would not look into it for a long time to avoid nightmares at night...

 
Movlat Baghiyev:
Please make corrections in the code

Try it ^_~

extern double StopLoss     = 100; //Стоплосс ордера  
extern double TakeProfit   = 150; //Тейкпрофит ордера
extern double TrailingStop = 100; // трал
extern int    Delta        = 100; //Расстояние от цены для установки ордера
extern double LOT          = 0.1; //Объём позиции
extern int    Magic        =2;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer


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


  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  
  int  res = 0;

//ИНДИКАТОР RSI
   double RSI0=iRSI(NULL,0,5,PRICE_CLOSE,0);
   double RSI1=iRSI(NULL,0,5,PRICE_CLOSE,1);

   double BuyPrice=Ask+Delta*Point;
   double SellPrice=Bid-Delta*Point;
  
//---- buy stop
   if(RSI0>50&&RSI1<50 && Number(OP_BUYSTOP)<0 && Number(OP_BUY)<1) // < !!!!!!!!!
     {
      res=OrderSend(Symbol(),OP_BUYSTOP,LOT,BuyPrice,0,BuyPrice-StopLoss*Point,BuyPrice+TakeProfit*Point,"ОТЛОЖКИ",Magic,OP_SELLSTOP,Blue);
     }        
//---- sell stop  
   if(RSI0<50&&RSI1>50 && Number(OP_SELLSTOP)<0 && Number(OP_SELL)<1) // < !!!!!!!!!
     {
      res=OrderSend(Symbol(),OP_SELLSTOP,LOT,SellPrice,0,SellPrice+StopLoss*Point,SellPrice-TakeProfit*Point,"ОТЛОЖКИ",Magic,OP_BUYSTOP,Red);
     }
//----  
  
  
if (RSI0>50&&RSI1<50) OrderDelete_(OP_SELLSTOP);

if (RSI0<50&&RSI1>50)OrderDelete_(OP_BUYSTOP);

  }

//+------------------------------------------------------------------+
void OrderDelete_(int Type)
  {
  bool  r;
   for(int i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS)==true)
        {
         if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic) continue;
         if(OrderType()==Type)
          r = OrderDelete(OrderTicket());
        }
     }
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int Number(int Type)
  {
  int kp=0;
   for(int i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS)==true)
        {
         if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic) continue;
         if(OrderType()==Type)
            kp++;
        }
     }
  return(kp);  
  }
//+------------------------------------------------------------------+
 
Alekseu Fedotov, thank you. God bless you.
 
Vitalie Postolache:

There's else inside the if() block. Was it really meant to be like that?

Further. The Counts() function accepts an uninitialized global variable count that is also declared once again in the function itself.

The Counts() function counts orders but compares them to some ticket that changes several times in the main function. But the ticket value is the same during the function operation. So, how many orders will the function calculate? Is it more than one?

In this form, this function does not need arguments. Just call it and let it return the result.

Next. Function FindLastOType() is again a comparison with the ticket. Can you guarantee that it is compared to a valid ticket? Isn't it simpler to interrupt the loop when the first order with the necessary magic number and symbol is found and then to return the order type?

Same with FindLastOrderOpenPrice() and FindLastLot().

Function ModifyOrders() terrified me so much that I would not look into it for a long time to avoid nightmares at night...

Correction

There's no other way to put it anywhere else, it'll be wrong parameters

It has to take into account the most recent

I'll try to change it.

I tried to use standard OrderModify(), but it gets banned if I move a bunch of orders simultaneously and constantly

Thanks for the critique.

 
spoiltboy:
Yes, that's a misprint on the bracket. The rest of the question is valid.

bool condition;

if(condition) {}

tantamount to

if(condition==true) {}

и

if(!condition) {}

equals

if(condition==false) {}

It's just shorter and more familiar.

Reason: