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

 
spoiltboy:

Good. Can you tell me where the error is?

extern int pointsl=100, pointtp=100, MagicB=1111, MagicS=2222, bars=10;  extern double lotB=0.1, lotS=0.1;
double slB, tpB, slS, tpS;  double x=0, z=0; int ticketUP, ticketD;

void OnTick()
  {
double maxpr1=-9999; double minpr1=9999;

for(int shift1=0; shift1<bars; shift1++)
{double i=iHigh(Symbol(), PERIOD_CURRENT, shift1);
if (i>maxpr1){maxpr1=i;}}

for(int shiftA1=0; shiftA1<bars; shiftA1++)
{double y=iLow(Symbol(), PERIOD_CURRENT, shiftA1);
if (y<minpr1) {minpr1=y;}}



slS=NormalizeDouble(maxpr1+pointsl*Point,5);
tpS=NormalizeDouble(maxpr1-pointtp*Point,5);
ticketD=OrderSend(Symbol(), OP_SELLLIMIT, lotS, maxpr1, 3, slS, tpS, "", MagicS, 0, Blue);
if (ticketD==-1) Print("ERROR OP_SELL"); else Print("OP_SELL OK");
  } 

Everything works, places an order at the price of maxpr1.

Then I want to do the same, but at minpr1 price:

extern int pointsl=100, pointtp=100, MagicB=1111, MagicS=2222, bars=10;  extern double lotB=0.1, lotS=0.1;
double slB, tpB, slS, tpS;  double x=0, z=0; int ticketUP, ticketD;

void OnTick()
  {
double maxpr1=-9999; double minpr1=9999;

for(int shift1=0; shift1<bars; shift1++)
{double i=iHigh(Symbol(), PERIOD_CURRENT, shift1);
if (i>maxpr1){maxpr1=i;}}

for(int shiftA1=0; shiftA1<bars; shiftA1++)
{double y=iLow(Symbol(), PERIOD_CURRENT, shiftA1);
if (y<minpr1) {minpr1=y;}}



slS=NormalizeDouble(minpr1+pointsl*Point,5);
tpS=NormalizeDouble(minpr1-pointtp*Point,5);
ticketD=OrderSend(Symbol(), OP_SELLLIMIT, lotS, minpr1, 3, slS, tpS, "", MagicS, 0, Blue);
if (ticketD==-1) Print("ERROR OP_SELL"); else Print("OP_SELL OK");
  }

Error 130 (wrong stops). What am I doing wrong?


When you place a pending order, the opening price cannot be too close to the market. The minimum distance of the pending price from the current market price in points can also be obtained using the MarketInfo() function with the MODE_STOPLEVEL parameter. If the open price of the pending order is incorrect, an error 130 (ERR_INVALID_STOPS) will be generated.

 
Alekseu Fedotov:
No, that's not it, the gap is there. I also tried to change the buy pending, I tested it on the same chart and it made the same mistakes.
 
spoiltboy:
No, that's not it, the gap is there. Moreover, I tried to change my Buy pending, tested it on the same chart, same errors.

A gap is a gap, but you probably didn't read all of the following

....... Ifthe open price of the pending order is incorrect, error 130 (ERR_INVALID_STOPS).......... will be generated.

i.e. you are trying to set OP_SELLLIMIT below market price.

 
Alekseu Fedotov:

A gap is a gap, but you probably didn't read all of the following

....... Ifthe open price of the pending order is incorrect, error 130 (ERR_INVALID_STOPS).......... will be generated.

You are trying to set OP_SELLLIMIT below market price.

Thank you.
 
//------------закрываем ордер по обратному сигналу удаляем или модифицируем отложки------

  for(int i2=total-1; i2>=0; i2--)
     if(OrderSelect(i2, SELECT_BY_POS))
         if(OrderSymbol()==Symbol()      )
         if (OrderMagicNumber()==Magic)
      {
      if (OrderType()==OP_BUY)
      {
     if (sig2==1)  {bool cl = OrderClose(OrderTicket(),OrderLots(),Bid,Slip,0);if (cl==false) {Print("OrderClose завершилась с ошибкой #",GetLastError());}}
    
      }
      if (OrderType()==OP_SELL)
      {
     if (sig2==2)  {bool cl = OrderClose(OrderTicket(),OrderLots(),Ask,Slip,0);if (cl==false) {Print("OrderClose завершилась с ошибкой #",GetLastError());}}
    
      }
    
      if (OrderType()==OP_BUYSTOP)
      {
    
     if (sig2==2&&Delete_Order==true)  {bool del = OrderDelete(OrderTicket());if (del==false) {Print("OrderDelete завершилась с ошибкой #",GetLastError());}}
    
     //if (sig==1&&OrderOpenPrice()!=buystop_open&&Ask<buystop_open-stops) {bool mod = OrderModify(OrderTicket(),buystop_open,buystop_sl,0,0);Print("Мод. цены бай стоп=" ,buystop_open,", СЛ=",buystop_sl);if (mod==false) {Print("OrderModify завершилась с ошибкой #",GetLastError());}}
    
      }
    
      if (OrderType()==OP_SELLSTOP)
      {
  
     if (sig2==1&&Delete_Order==true)  {bool del = OrderDelete(OrderTicket());if (del==false) {Print("OrderDelete завершилась с ошибкой #",GetLastError());}}
  
     //if (sig==1&&OrderOpenPrice()!=sellstop_open&&Bid>sellstop_open+stops) {bool mod = OrderModify(OrderTicket(),sellstop_open,sellstop_sl,0,0);Print("Мод. цены бай стоп=" ,sellstop_open,", СЛ=",sellstop_sl);if (mod==false) {Print("OrderModify завершилась с ошибкой #",GetLastError());}}
      
      }
      
      }

    
  
  }
//+------------------------------------------------------------------+
if (FMA1<GrossMA1 && FMA2>GrossMA2 ) {sig=2;} // sell stop

if (FRMA1>GrossMA1 && FRMA2<GrossMA2 ) {sig=1;} // buy stop

There is no signal to close an open trade by itself.

 
Movlat Baghiyev:
if (FMA1<GrossMA1 && FMA2>GrossMA2 ) {sig=2;} // sell stop

if (FRMA1>GrossMA1 && FRMA2<GrossMA2 ) {sig=1;} // buy stop

There is no signal to exit a trade by itself.

On the zero bar the signal "flickers" which is not visible post factum. Run visualisation on all ticks in the tester, questions will disappear.
 
Vitalie Postolache:
At bar zero the signal "flickers", which is not visible post-facto. Run the visualisation on all ticks in the tester, the questions will disappear.
That's not the problem. When an order triggers, a trade opens and immediately closes when a new candle appears and there is no reverse signal. That's why I gave you a piece of code for closing trades.
 
Movlat Baghiyev:
This is not the problem. When an order is triggered a trade is opened and it is immediately closed when a new candle appears and there is no reverse signal. That is why I gave you a piece of code for closing trades.
You need to null the variable"sig" after setting because the signal will be constant, if you null it, then the variable will take value again after the next crossing and null it again after completing all actions. Or you can put a flag that if there was an up crossing, the next one should be down and if there is no down crossing, then all signals are ignored until there is a reverse crossing
 

Hello.

Can you tell me what is wrong?

The icon should be set if the indicator line has crossed level 20, on period M1, and the indicator line is above level 50, on period M5.

For some reason the mark is set even if the line on M5 is below the set level of 50.

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[])
  {
//---
Comment("");
int limit = rates_total;
int count=prev_calculated;

for(int i=limit-count; i>=1;i--)
{
//Getting Stochastic buffer values using the iCustom function
  double Stoch1 = iStochastic(NULL,1,KPeriod,DPeriod,Slowing,MODE_SMA,0,MODE_MAIN,i);
  double Stoch2 = iStochastic(NULL,1,KPeriod,DPeriod,Slowing,MODE_SMA,0,MODE_MAIN,i+1);
  
   double Stoch50_1 = iStochastic(NULL,5,KPeriod1,DPeriod1,Slowing1,MODE_SMA,0,MODE_MAIN,i);
  double Stoch50_2 = iStochastic(NULL,5,KPeriod1,DPeriod1,Slowing1,MODE_SMA,0,MODE_MAIN,i+1);
  
  if(Stoch1>20 && Stoch2<20&&Stoch50_1>50)
  {
   UP[i]=Low[i]-distance*MyPoint;
  }
  //if(Stoch1<80 && Stoch2>80&&Stoch50_1<50)
  //{
  // DOWN[i]=High[i]+distance*MyPoint;
  //}
}
//--- return value of prev_calculated for next call
   return(rates_total);
  }
 
mila.com:

Hello.

Can you tell me what is wrong?

The icon should be set if the indicator line has crossed level 20, on M1 period, and the indicator line is above level 50, on M5 period.

For some reason the sign is set even if the line on M5 is below the set level of 50.

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[])
  {
//---
Comment("");
int limit = rates_total;
int count=prev_calculated;

for(int i=limit-count; i>=1;i--)
{
//Getting Stochastic buffer values using the iCustom function
  double Stoch1 = iStochastic(NULL,1,KPeriod,DPeriod,Slowing,MODE_SMA,0,MODE_MAIN,i);
  double Stoch2 = iStochastic(NULL,1,KPeriod,DPeriod,Slowing,MODE_SMA,0,MODE_MAIN,i+1);
  
   double Stoch50_1 = iStochastic(NULL,5,KPeriod1,DPeriod1,Slowing1,MODE_SMA,0,MODE_MAIN,i);
  double Stoch50_2 = iStochastic(NULL,5,KPeriod1,DPeriod1,Slowing1,MODE_SMA,0,MODE_MAIN,i+1);
  
  if(Stoch1>20 && Stoch2<20&&Stoch50_1>50)
  {
   UP[i]=Low[i]-distance*MyPoint;
  }
  //if(Stoch1<80 && Stoch2>80&&Stoch50_1<50)
  //{
  // DOWN[i]=High[i]+distance*MyPoint;
  //}
}
//--- return value of prev_calculated for next call
   return(rates_total);
  }

Your cycle is strange. Strange.

//+------------------------------------------------------------------+
   if(rates_total<xxx) return(0);         // xxx здесь - количество баров, при которых невозможно рассчитать индикатор
   int limit=rates_total-prev_calculated;
   if(limit>1) {                          // limit больше 1 в том случае, когда в истории произошли изменения
      limit=rates_total-1;                // не обязательно -1, если в цикле есть i+1, значит limit=rates_total-2, и т.д., и т.п.
      // тут проводим действия когда нужно пересчитать всю историю
      }
//---
   for(int i=limit; i>=0; i--) {
      // основной цикл индикатора
      }
//+------------------------------------------------------------------+

Why limit check for more than 1. For example, history is loaded and the difference will be greater than one. If everything is normal, then the difference rates_total-prev_calculated will be either 0 or 1
0 - a new tick has come and a new bar has not begun to be formed.
1 - a new tick has come and a new bar has started to form

Show us your whole indicator - let's see what's wrong.