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

 
Vitaly Muzichenko:

You check on an existing bar double rsi=iRSI(_Symbol,0,RSIperiod,PRICE_CLOSE,0);

The price goes up, there is a crossover of level 70 and entry into the trade. On the fact of closing the bar, you already see that the RSI is lower, but before that it was higher, and then returned below the level.

Do the check on a closed bar, then this will not happen, and entries will be on a confirmed signal:

double rsi=iRSI(_Symbol,0,RSIperiod,PRICE_CLOSE,1);

double rsi1=iRSI(_Symbol,0,RSIperiod,PRICE_CLOSE,2);

I see. Thank you.
 

I wrote it like this: (probably wrong).

double tp,sl,OrderBuy=0,OrderSell=0;
double slSell,slBuy,tpSell,tpBuy,TotalBiu,TotalSell;
int tiket;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   OrderBuy=0; OrderSell=0;
   for(int i=OrdersTotal()-1; i>=0; i--) //Цикл по всем ордерам
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) //Выбрали ордер
        {
         if(OrderType()==OP_BUY)
           {
            OrderBuy++;                      //Кол. покупок
           }
         if(OrderType()==OP_SELL)
           {
            OrderSell++;                     //Кол. продаж
           }
        }
     }
   double rsi=iRSI(_Symbol,0,RSIperiod,PRICE_CLOSE,0);
   double rsi1=iRSI(_Symbol,0,RSIperiod,PRICE_CLOSE,1);
   double rsi2=iRSI(_Symbol,0,RSIperiod,PRICE_CLOSE,2);
//+------------------------------------------------------------------+

   double StopLossLevel;
   double TakeProfitLevel;
   if(StopLoss>0) StopLossLevel=Bid-StopLoss*Point; else StopLossLevel=0.0;
   if(TakeProfit>0) TakeProfitLevel=Ask+TakeProfit*Point; else TakeProfitLevel=0.0;

   tpBuy=NormalizeDouble(Ask+TakeProfit*_Point,_Digits);
   slBuy=NormalizeDouble(Bid-StopLoss*_Point,_Digits);

   tpSell=NormalizeDouble(Bid-TakeProfit*_Point,_Digits);
   slSell=NormalizeDouble(Ask+StopLoss*_Point,_Digits);
///---
   if(OrderBuy==0 && rsi>Urov_70 && rsi1<Urov_70 && rsi2<Urov_70)
     {
      tiket=OrderSend(_Symbol,OP_BUY,Lot,Ask,slippage,slBuy,tpBuy,NULL,MagicNumber,0,clrBlue);
      if(tiket<0)Print("Ошибка открытия ордера № - ",GetLastError());
     }
   if(OrderSell==0 && rsi<Urov_30 && rsi1>Urov_30 && rsi2>Urov_30)
     {
      tiket=OrderSend(_Symbol,OP_SELL,Lot,Bid,slippage,slSell,tpSell,NULL,MagicNumber,0,clrRed);
      if(tiket<0)Print("Ошибка открытия ордера № - ",GetLastError());
     }
  }
//+------------------------------------------------------------------+

Result: Orders open when there is no crossover...


 
Ibragim Dzhanaev:

I wrote it like this: (probably wrong).

        }
     }
   double rsi=iRSI(_Symbol,0,RSIperiod,PRICE_CLOSE,0);
   double rsi1=iRSI(_Symbol,0,RSIperiod,PRICE_CLOSE,1);
   double rsi2=iRSI(_Symbol,0,RSIperiod,PRICE_CLOSE,2);
//+------------------------------------------------------------------+

   double StopLossLevel;

The result is as follows : orders open when there is no crossover...

Why do you need current bardouble rsi=iRSI(_Symbol,0,RSIperiod,PRICE_CLOSE,0); ???

Your rewritten code fulfills exactly the same condition as before - on the zero bar the price crossed the level and a trade was opened, later the price returned back and RSI returned under the level, as a result, after closing the bar there is no signal.

Do not use the zero bar in the calculation.

 
Vitaly Muzichenko:

Why do you need current bardouble rsi=iRSI(_Symbol,0,RSIperiod,PRICE_CLOSE,0); ???

Your rewritten code fulfills exactly the same condition as before, on the zero bar the price crossed the level and a trade was opened, later the price returned back and RSI returned under the level, as a result, after closing the bar there is no signal.

Do not use the zero bar in the calculation.

It works. Thank you.
 

Can you tell me why it gives an error ?

double tp,sl,OrderBuy=0,OrderSell=0;
double slSell,slBuy,tpSell,tpBuy,TotalBiu,TotalSell;
int tiket;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   OrderBuy=0; OrderSell=0;
   for(int i=OrdersTotal()-1; i>=0; i--) //Цикл по всем ордерам
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) //Выбрали ордер
        {
         if(OrderType()==OP_BUY)
           {
            OrderBuy++;                      //Кол. покупок
           }
         if(OrderType()==OP_SELL)
           {
            OrderSell++;                     //Кол. продаж
           }
        }
     }
//double rsi=iRSI(_Symbol,0,RSIperiod,PRICE_CLOSE,0);
   double rsi1=iRSI(_Symbol,0,RSIperiod,PRICE_CLOSE,1);
   double rsi2=iRSI(_Symbol,0,RSIperiod,PRICE_CLOSE,2);
//+------------------------------------------------------------------+

   double StopLossLevel;
   double TakeProfitLevel;
   if(StopLoss>0) StopLossLevel=Bid-StopLoss*Point; else StopLossLevel=0.0;
   if(TakeProfit>0) TakeProfitLevel=Ask+TakeProfit*Point; else TakeProfitLevel=0.0;

   tpBuy=NormalizeDouble(Ask+TakeProfit*_Point,_Digits);
   slBuy=NormalizeDouble(Bid-StopLoss*_Point,_Digits);

   tpSell=NormalizeDouble(Bid-TakeProfit*_Point,_Digits);
   slSell=NormalizeDouble(Ask+StopLoss*_Point,_Digits);
///---
   if(CountBuy()+CountSell()==0 && rsi1>Urov_70 && rsi2<Urov_70)
     {
      tiket=OrderSend(_Symbol,OP_BUY,Lot,Ask,slippage,slBuy,tpBuy,NULL,MagicNumber,0,clrBlue);
      if(tiket<0)Print("Ошибка открытия ордера № - ",GetLastError());
     }
   if(CountBuy()+CountSell()==0 && rsi1<Urov_30 && rsi2>Urov_30)
     {
      tiket=OrderSend(_Symbol,OP_SELL,Lot,Bid,slippage,slSell,tpSell,NULL,MagicNumber,0,clrRed);
      if(tiket<0)Print("Ошибка открытия ордера № - ",GetLastError());
     }
  }
//+------------------------------------------------------------------+
int CountBuy()
  {
   int count=0;
   for(int trade=OrdersTotal()-1; trade>0; trade--);
     {
      if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES)==true)
        {
         if(OrderSymbol()==_Symbol && OrderMagicNumber()==MagicNumber && OrderType()==OP_BUY)
            count++;
        }
     }
   return(count);
  }
//+------------------------------------------------------------------+
int CountSell()
  {
   int count=0;
   for(int trade=OrdersTotal()-1; trade>0; trade--);
     {
      if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES)==true)
        {
         if(OrderSymbol()==_Symbol && OrderMagicNumber()==MagicNumber && OrderType()==OP_SELL)
            count++;
        }
     }
   return(count);
  }
//+------------------------------------------------------------------+
 
Ibragim Dzhanaev:

Can you tell me why it gives an error ?

double tp,sl,OrderBuy=0,OrderSell=0;
double slSell,slBuy,tpSell,tpBuy,TotalBiu,TotalSell;
int tiket;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   OrderBuy=0; OrderSell=0;
   for(int i=OrdersTotal()-1; i>=0; i--) //Цикл по всем ордерам
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) //Выбрали ордер
        {
         if(OrderType()==OP_BUY)
           {
            OrderBuy++;                      //Кол. покупок
           }
         if(OrderType()==OP_SELL)
           {
            OrderSell++;                     //Кол. продаж
           }
        }
     }
//double rsi=iRSI(_Symbol,0,RSIperiod,PRICE_CLOSE,0);
   double rsi1=iRSI(_Symbol,0,RSIperiod,PRICE_CLOSE,1);
   double rsi2=iRSI(_Symbol,0,RSIperiod,PRICE_CLOSE,2);
//+------------------------------------------------------------------+

   double StopLossLevel;
   double TakeProfitLevel;
   if(StopLoss>0) StopLossLevel=Bid-StopLoss*Point; else StopLossLevel=0.0;
   if(TakeProfit>0) TakeProfitLevel=Ask+TakeProfit*Point; else TakeProfitLevel=0.0;

   tpBuy=NormalizeDouble(Ask+TakeProfit*_Point,_Digits);
   slBuy=NormalizeDouble(Bid-StopLoss*_Point,_Digits);

   tpSell=NormalizeDouble(Bid-TakeProfit*_Point,_Digits);
   slSell=NormalizeDouble(Ask+StopLoss*_Point,_Digits);
///---
   if(CountBuy()+CountSell()==0 && rsi1>Urov_70 && rsi2<Urov_70)
     {
      tiket=OrderSend(_Symbol,OP_BUY,Lot,Ask,slippage,slBuy,tpBuy,NULL,MagicNumber,0,clrBlue);
      if(tiket<0)Print("Ошибка открытия ордера № - ",GetLastError());
     }
   if(CountBuy()+CountSell()==0 && rsi1<Urov_30 && rsi2>Urov_30)
     {
      tiket=OrderSend(_Symbol,OP_SELL,Lot,Bid,slippage,slSell,tpSell,NULL,MagicNumber,0,clrRed);
      if(tiket<0)Print("Ошибка открытия ордера № - ",GetLastError());
     }
  }
//+------------------------------------------------------------------+
int CountBuy()
  {
   int count=0;
   for(int trade=OrdersTotal()-1; trade>0; trade--);
     {
      if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES)==true)
        {
         if(OrderSymbol()==_Symbol && OrderMagicNumber()==MagicNumber && OrderType()==OP_BUY)
            count++;
        }
     }
   return(count);
  }
//+------------------------------------------------------------------+
int CountSell()
  {
   int count=0;
   for(int trade=OrdersTotal()-1; trade>0; trade--);
     {
      if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES)==true)
        {
         if(OrderSymbol()==_Symbol && OrderMagicNumber()==MagicNumber && OrderType()==OP_SELL)
            count++;
        }
     }
   return(count);
  }
//+------------------------------------------------------------------+
By the way.
for(int trade=OrdersTotal()-1; trade>0; trade--);
it has to be like this
for(int trade=OrdersTotal()-1; trade>=0; trade--);
otherwise you'll miss the last order
 
Ibragim Dzhanaev:

Can you tell me why an error is displayed?


   for(int trade=OrdersTotal()-1; trade>0; trade--);
     {
      if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES)==true)
        {
         if(OrderSymbol()==_Symbol && OrderMagicNumber()==MagicNumber && OrderType()==OP_SELL)
            count++;
        }
     }
   return(count);
  }
//+------------------------------------------------------------------+

extra " ; "

int CountBuy()
  {
   int count=0;
   for(int trade=OrdersTotal()-1; trade>0; trade--);  <<<
     {
      if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==_Symbol && OrderMagicNumber()==MagicNumber && OrderType()==OP_BUY)
            count++;
        }
     }
   return(count);
  }
//+------------------------------------------------------------------+
int CountSell()
  {
   int count=0;
   for(int trade=OrdersTotal()-1; trade>0; trade--);  <<<
     {
      if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==_Symbol && OrderMagicNumber()==MagicNumber && OrderType()==OP_SELL)
            count++;
        }
     }
   return(count);
  }

And that would be better, in this case:

for(int trade=0; trade<OrdersTotal(); trade++)
 
Sergey Gritsay:
By the way.
for(int trade=OrdersTotal()-1; trade>0; trade--);
it has to be so
for(int trade=OrdersTotal()-1; trade>=0; trade--);
or you'll miss the last order.
Thank you.
 
Vitaly Muzichenko:

superfluous " ; "

int CountBuy()
  {
   int count=0;
   for(int trade=OrdersTotal()-1; trade>0; trade--);  <<<
     {
      if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==_Symbol && OrderMagicNumber()==MagicNumber && OrderType()==OP_BUY)
            count++;
        }
     }
   return(count);
  }
//+------------------------------------------------------------------+
int CountSell()
  {
   int count=0;
   for(int trade=OrdersTotal()-1; trade>0; trade--);  <<<
     {
      if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==_Symbol && OrderMagicNumber()==MagicNumber && OrderType()==OP_SELL)
            count++;
        }
     }
   return(count);
  }

And that would be better, in this case:

for(int trade=0; trade<OrdersTotal(); trade++)
Thank you.
 

Hi all, how do I write the expression

the fifth lot is equal to the sum of lots 1 and 4 of the orders

to find the first or the last of all can be found via the ticket definition

But how to find any intermediate one considering constant changes in the current grid?

I mean every order should be stored somewhere or some other way

and is there a difference?

between

void OnTick()
  {
    if uslovie1==true   {...}
    if uslovie2==true   {...}
    if uslovie3==true   {...}
  }

И

void OnTick()
  {
     {
        if uslovie1==true
                   {...}
        else  if   {...}
        else  if   {...}
     }
  }
Reason: