Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 199

 
Antonius:

Good evening.

Could you please tell me how to add closing all positions and deleting all orders at the end of the trading week?

Thank you!

Write such a script, then throw it on the chart when you want to close and delete everything. That's all...

You're welcome!

 
artmedia70:

Write such a script, then throw it on the chart when you want to close and delete everything. The point is...

Please!



Thank you:) Clarifying the request.

We need to add code to the EA that will close all orders and positions at the end of the trading week.

Could you please write this code?

 
Antonius:


Thank you:) Clarifying the request.

We need to add code to the EA that will close all orders and positions at the end of the trading week.

Could you please write this code?

I can do that. No problem. Have you ever done anything yourself? This thread is not about getting free stuff, it's about helping people who are trying to code for themselves.
 
artmedia70:
I can do that. No problem. Have you ever done anything yourself? This thread isn't about getting something ready for free, it's about helping those who are trying to code on their own.


Checked it out at https://book.mql4.com/ru/trading/orderclose.

Searched on google, on the forum. Didn't find any. Maybe I'm writing the query wrong.

Thought I'd ask in the newbie thread.

 

How do I prescribe?

As you run the script:

A window pops up with "general" and "input parameters" tabs.

I need to enter a date in the input parameters.

Is it possible that when I run the script, a date box will pop up, I enter the date, click ok and everything will work?

And can I make the script remember the last date I entered?

 
artmedia70:

The function determining the last closed position on the stop has the bool type, while you are trying to return the double type from it. Accordingly, it returns either 0 or 1.

I wrote you that you should add passing of one variable by reference into it:

Now check the last Buy, for example:

Like this...


Thank you very much.
 
artmedia70:

The function determining the last closed position on the stop has the bool type, while you are trying to return the double type from it. Accordingly, it returns either 0 or 1.

I told you that you should add passing of one variable by reference into it:

Now check the last Buy, for example:

Like this...


Did everything as you said. All compiled...... but function does not work when testing advisor..... all positions advisor opens 0.1 lot.

double  Magic, Lot;

int start()
  {
        

  
 double Price=iOpen (Symbol (),0,0);
 

          
 int last_order_bar = 0;                                                                                                                        
 int ot = OrdersTotal();                                                                                                                        
                                                                                                                        
 if (ot>0) //если есть ордера в рынке                                                                                                                   
 {                                                                                                                      
   if (OrderSelect (ot-1,SELECT_BY_POS))                                                                                                                        
      if (OrderType ()==OP_BUY || OrderType ()==OP_SELL )                                                                                                                       
         last_order_bar = iBarShift (Symbol (),0,OrderOpenTime ());                                                                                                             
 }                                                                                                                      
                                                                                                                        
 int last_hist_order_bar = 0;                                                                                                                   
 int oht = OrdersHistoryTotal();                                                                                                                        
                                                                                                                
 if (oht>0)                                                                                                                     
 {                                                                                                                      
   if (OrderSelect (oht-1,SELECT_BY_POS, MODE_HISTORY))                                                                                                                         
      if (OrderType ()==OP_BUY || OrderType ()==OP_SELL)                                                                                                                        
         last_hist_order_bar = iBarShift (Symbol (),0,OrderOpenTime ());
                                                                                                        
        }

                                                                                                        
if (ot==0)

 if (Bid ==Price)
//=============================================================== 
  
if(((Open[1]-Close[1])>100*Point)&&((Open[1]-Close[1])<120*Point))
if(((High[1]-Open[1])>40*Point)&&((High[1]-Open[1])<60*Point))
if(((Close[1]-Low[1])>40*Point)&&((Close[1]-Low[1])<60*Point))

//============================================================
                                                                           // задавать минимальное значение лота я не стал,мне кажется для тестера это не обязательно
if (isCloseLastPosByStop(Symbol(), OP_BUY, Magic, Lot))                        
{
Lot=Lot*2;
OrderSend(Symbol(),OP_SELL,Lot,Bid,1,Ask+1500*Point,Ask-300*Point,"jfh",123 );
}
else
{

Lot=0.1;        
OrderSend(Symbol(),OP_SELL,1,Bid,Lot,Ask+1500*Point,Ask-300*Point,"jfh",123 );
}
   return(0);
  }
//============================================================  
bool isCloseLastPosByStop(string sy, int op, int mn, double ll)       //у Вас перед названием формального  параметра  11 стоял знак &- я его убрал.
{
   double   pt;
   datetime t;                                                       //у вас тип переменной t был int - я сделал datetime
   int       dg, i, j=-1, k=OrdersHistoryTotal()-1;

   for (i=k; i>=0; i--) 
   {
     if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) 
     {
         if (OrderMagicNumber()!=mn)   continue;   // если магик не тот, переходим к следующему
         if (OrderSymbol()!=sy)        continue;   // если символ не тот, переходим к следующему
         if (OrderType()!=op)           continue;   // если тип не тот, переходим к следующему
         if (t<OrderCloseTime()) 
         {
            t=OrderCloseTime();
            j=i;
            }
         }
      }
      
   if (OrderSelect(j, SELECT_BY_POS, MODE_HISTORY)) 
   {
      dg=MarketInfo(OrderSymbol(), MODE_DIGITS);      // количество знаков в цене символа ордера
      pt=MarketInfo(OrderSymbol(), MODE_POINT);       // размер пункта инструмента в валюте котировки ордера
      if (MathAbs(OrderClosePrice()-OrderStopLoss())< 0.5*pt) // Если закрыт по стопу
      { 
         ll=OrderLots();                              // записываем количество лотов в ордере, закрытом по стопу
         return(true);                                // возвращаем истину
         }
      }
      
   return(False);                                     // возвращаем ложь (позиции нету, либо не по стопу)
}

Please tell me what my mistake is.

I do not understand this point - as you have correctly noticed the function returns the value of type bool, but in the function call, in which the function must put the value of type bool.

You suggested to specify other value types.

Thank you.

 
solnce600:

I did everything as you said. Everything compiles...... but the function does not work when testing the EA..... all positions the EA opens 0.1 lots.

Could you please tell me what my mistake is?

I don't understand this moment - as you correctly noticed the function returns value of bool type, but in the function call, in which the function should put value of bool type

Do you suggest to specify other types of values .

Thank you.


bool isCloseLastPosByStop(string sy, int op, int mn, double ll)       //у Вас перед названием формального  параметра  11 стоял знак &- я его убрал.

Why did you remove it. If you had left it, everything would have worked.

 
Vinin:

Why remove it. If they had, everything would have worked

I wonder ..... what that sign means.... I can't think of anything in the textbook about it.
 
Vinin:

Why remove it. If they had left it in, everything would have worked.

I put this mystery sign ...... but the result is the same ...function doesn't work

bool isCloseLastPosByStop(string sy, int op, int mn, double &  ll) 
{
   double   pt;
   datetime t;
   int       dg, i, j=-1, k=OrdersHistoryTotal()-1;

   for (i=k; i>=0; i--) 
   {
     if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) 
     {
         if (OrderMagicNumber()!=mn)   continue;   // если магик не тот, переходим к следующему
         if (OrderSymbol()!=sy)        continue;   // если символ не тот, переходим к следующему
         if (OrderType()!=op)           continue;   // если тип не тот, переходим к следующему
         if (t<OrderCloseTime()) 
         {
            t=OrderCloseTime();
            j=i;
            }
         }
      }
      
   if (OrderSelect(j, SELECT_BY_POS, MODE_HISTORY)) 
   {
      dg=MarketInfo(OrderSymbol(), MODE_DIGITS);      // количество знаков в цене символа ордера
      pt=MarketInfo(OrderSymbol(), MODE_POINT);       // размер пункта инструмента в валюте котировки ордера
      if (MathAbs(OrderClosePrice()-OrderStopLoss())< 0.5*pt) // Если закрыт по стопу
      { 
         ll=OrderLots();                              // записываем количество лотов в ордере, закрытом по стопу
         return(true);                                // возвращаем истину
         }
      }
      
   return(False);                                     // возвращаем ложь (позиции нету, либо не по стопу)
}
Reason: