[ARCHIVE] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 3. - page 46

 
rustein:

where is the question exactly?
 
ilunga:
where is the question exactly?
https://www.mql5.com/ru/forum/115354/page78
 

in the code you gave, if there is only 1 order, the loop

for(;Cnt>=0;Cnt--)
    {
      RefreshRates();
      OrderSelect(Cnt,SELECT_BY_POS);
      if(OrderMagicNumber()==Magic)
      {
      TotalOpenOrders++;
      }
    }

The first time will be an attempt to select an order which does not exist

 
ilunga:

in the above code, if there is only 1 order, the loop will

The first time will be an attempt to select an order which does not exist

Thank you, I will correct it for myself.
 
artmedia70:
The terminal went into deep thought, after about 15 minutes I killed it
nothing...
I would have killed it without waiting a minute.
 
PapaYozh:

Immediately before using Ask and Bid, execute RefreshRates()


Tried it, nothing changes

Order sending function, all seems to be standard

 int OpenPosition(string AdvisorName,int Position,int Magic,double Lots,double StopLoss){ 
 int N; int Сount = 5; int Slippage = (CalculateSpread() * 2); 
 string PeriodString = GetPeriod(Period());
 if(Position == OP_BUY){double Price = NormalizeDouble(Ask,Digits); 
 string OrderName = " - Покупка по рынку";
 string ErrorName = "Ошибка открытия ордера на покупку";
 string CriticalErrorName = "Невозможно открыть ордер на покупку";
 string MailCriticalErrorName = "Ошибка открытия"; color Arrow = Gold;}
 if(Position == OP_SELL){Price = NormalizeDouble(Bid,Digits); 
 OrderName = " - Продажа по рынку"; ErrorName = "Ошибка открытия ордера на продажу";
 CriticalErrorName = "Невозможно открыть ордер на продажу";
 MailCriticalErrorName = "Ошибка открытия"; Arrow = YellowGreen;}
 
 while(IsTradeContextBusy() || !IsTradeAllowed()){int Interval = MathRand()+1000; 
 Print("Торговый поток занят. Повторная проверка через "+DoubleToStr((Interval/1000),0)+" сек"); 
 Sleep(Interval);} RefreshRates(); while(N < Сount){RefreshRates(); 
 int Ticket = OrderSend(Symbol(),Position,Lots,Price,Slippage,StopLoss,0,AdvisorName+
 OrderName,Magic,0,Arrow);
 if(Ticket > 0){if(OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES)){int Flag = 1; 
 PlaySound("recharge.wav"); break;}}
 else{Flag = 0; int PRM = GetLastError(); string ERR = Error(PRM,"OrderSend()"); 
 ErrorProcessing(AdvisorName+" "+PeriodString,ErrorName+" "+PRM+" ("+
 Error(PRM,"OrderSend()")+")","","OrderSend()"); N++; Sleep(5000);}
 if(N == Сount){ErrorProcessing(AdvisorName+" "+PeriodString,CriticalErrorName,
 MailCriticalErrorName+" "+PRM+" ("+Error(PRM,"OrderSend()")+")","OrderSend()");}}
 return(Flag);}
 
Dimoncheg:


Tried it, nothing changes.

Order sending function, everything seems to be standard


First you refresh prices with function RefreshRates() and store the required price in Price.

Then you perform Sleep(Interval) and at this time the quotes change.

After that you call OrderSend() with the quote from the year before.

What do you expect in response?

 
PapaYozh:


First you refresh prices with function RefreshRates() and store the required one in Price.

Then you perform Sleep(Interval), and at that time the quotes change.

And after that you call OrderSend() with the quote from the year before.

What do you expect in response?


My bad, I modernised a bit in the wrong place, now I've moved the price out of the function

OpenPosition("Sovetneg",OP_BUY,NormalizeDouble(Ask,Digits),Magic,SlowlyIncreasingLots(0.2),MultiStop(OP_BUY,StopLoss));
The function itself is now
 int OpenPosition(string AdvisorName,int Position,double Price,int Magic,double Lots,double StopLoss){ 
 int N; int Сount = 5; int Slippage = (CalculateSpread() * 2); 
 string PeriodString = GetPeriod(Period());
 if(Position == OP_BUY){
 string OrderName = " - Покупка по рынку";
 string ErrorName = "Ошибка открытия ордера на покупку";
 string CriticalErrorName = "Невозможно открыть ордер на покупку";
 string MailCriticalErrorName = "Ошибка открытия"; color Arrow = Gold;}
 if(Position == OP_SELL){
 OrderName = " - Продажа по рынку"; ErrorName = "Ошибка открытия ордера на продажу";
 CriticalErrorName = "Невозможно открыть ордер на продажу";
 MailCriticalErrorName = "Ошибка открытия"; Arrow = YellowGreen;}
 
 while(IsTradeContextBusy() || !IsTradeAllowed()){int Interval = MathRand()+1000; 
 Print("Торговый поток занят. Повторная проверка через "+DoubleToStr((Interval/1000),0)+" сек"); 
 Sleep(Interval);} while(N < Сount){RefreshRates(); 
 int Ticket = OrderSend(Symbol(),Position,Lots,Price,Slippage,StopLoss,0,AdvisorName+
 OrderName,Magic,0,Arrow);
 if(Ticket > 0){if(OrderSelect(Ticket,SELECT_BY_TICKET,MODE_TRADES)){int Flag = 1; 
 PlaySound("recharge.wav"); break;}}
 else{Flag = 0; int PRM = GetLastError(); string ERR = Error(PRM,"OrderSend()"); 
 ErrorProcessing(AdvisorName+" "+PeriodString,ErrorName+" "+PRM+" ("+
 Error(PRM,"OrderSend()")+")","","OrderSend()"); N++; Sleep(5000);}
 if(N == Сount){ErrorProcessing(AdvisorName+" "+PeriodString,CriticalErrorName,
 MailCriticalErrorName+" "+PRM+" ("+Error(PRM,"OrderSend()")+")","OrderSend()");}}
 return(Flag);}
Does it look right now?
 

Hello.

Could you tell me what type==4 means?

and type==5

in the following snippet:

double ma=iMA(Symbol(),PERIOD_D1,21,0,MODE_SMA,PRICE_CLOSE,0);

for(int k=1; k<=OrdersTotal(); k++)
bool select=OrderSelect(k-1,SELECT_BY_POS);

if(select)
{
int type=OrderType();
int ticket=OrderTicket();

if (Bid<ma && type==4)
OrderDelete(ticket);
if (Bid>ma && type==5)
OrderDelete(ticket);
}

I understand that conditions for deleting orders, by ma is clear, but together how ?

Thank you.

 
Dimoncheg:


Doesn't that seem right now?


No it isn't. It got even worse :)

Understand, RefreshRates() updates the values of the predefined Ask and Bid variables, you are using the value once stored in the Price variable.