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

 

I am solving the task of closing all open charts using MQL4 tools. There are 3 open charts with different currency pairs and this is the script code:

   for (int i=1;i<=3;i++) {
      long current=ChartID();
      Print("итерация цикла: ",i,", идентификатор текущего графика ",current);
      bool result=ChartClose(current);
      if(result)Print("окно успешно закрыто");
      else Print("не удалось закрыть окно с идентификатором: ", current);
   }

When I run the script, only one of three charts is closed and here are the prints from the log:

AUDUSD,H1: итерация цикла: 1, идентификатор текущего графика 130627565403660021
AUDUSD,H1: окно успешно закрыто
AUDUSD,H1: итерация цикла: 2, идентификатор текущего графика 130627565403660021
AUDUSD,H1: окно успешно закрыто
AUDUSD,H1: итерация цикла: 3, идентификатор текущего графика 130627565403660021
AUDUSD,H1: окно успешно закрыто

It means that after closing of one chart the current chart should be the other one, but:

1. After the first chart is closed, theChartID functionreturns the identifier of the closed chart instead of the current one

2. ChartClose()function in 2 and 3 iterations returns true, which means it closes successfully the already closed window...

Who knows where the bug is and how to solve this problem?

 
CJIeCaPb:

I don't understand what the problem is?

A close message should appear. The order opens, gets to the stop, closes and that's it! Where is the error?

I think that OrderSelect() must be inside the while loop, and besides, the position number will be incorrect, because after it closes it will become different - in this case it's better to select by ticket
 
tuner:
I think OrderSelect() should be inside a while loop, and besides, the position number will be incorrect, because after closing it will become different - in this case it's better to select from the ticket
Hooray, it worked! Thanks!
 
tuner:

I am solving the task of closing all open charts using MQL4 tools. There are 3 open charts with different currency pairs and this is the script code:

When I run the script, only one of three charts is closed and here are the prints from the log:

It means that after closing of one chart the current chart should be the other one, but:

1. After the first chart is closed,ChartID functionreturns the identifier of the closed chart instead of the current one

2. ChartClose()function in 2 and 3 iterations returns true, which means it closes successfully the already closed window...

Who knows where the bug is and how to solve this problem?

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   long currChart,prevChart=ChartFirst();
   int i=0,limit=100;
   while(i<limit)
     {
      currChart=ChartNext(prevChart);
      if(currChart<0) break;  
      prevChart=currChart;
      ChartClose(currChart);
      i++;
     }
     ChartClose(ChartFirst());
  }
//+------------------------------------------------------------------+
 

pako, sometimes it works, sometimes it doesn't, I don't know why... I get an error:

2014.12.11 17:52:12.456 Access violation read to 0x09295000 in 'C:\.....................ex4'

Could it be because it's trying to close the chart the script is thrown on?

PS works correctly if the script is attached to the first chart from the left. But if I place it on any other chart, I get an error

 
tuner:

pako, sometimes it works, sometimes it doesn't, I don't know why... I get an error:

2014.12.11 17:52:12.456 Access violation read to 0x09295000 in 'C:\.....................ex4'

Could it be because it's trying to close the chart the script is thrown on?

PS works correctly if the script is attached to the first chart from the left. But if I place it on any other chart, I get an error

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   long currChart,prevChart=ChartFirst();
   int i=0,limit=100;
   while(i<limit)
     {
      currChart=ChartNext(prevChart);
      if(currChart<0) break; 
      ChartClose(currChart); 
      prevChart=currChart;
      i++;
     }
     ChartClose(ChartFirst());
  }
//+------------------------------------------------------------------+
 

Guys, help me out here

The problem is the following function to translate the order into non-loss, it starts working incorrectly if I set it on the Yen pairs and allow all open orders into non-loss

i leave them all ok, but on five digits it returns to zero even if i set it to five digits only but the best thing is that if i set it to five it works ok

What is the reason for such work? What should be taken into account in these functions, so that the correct execution was carried out and did not depend on the EA operation in what window I threw it three or five digits?

Thank you very much.

    }

 //==================================================================================================

 // функция перевода ордеров стоп лоса в без убыток по всем открытым ордерам

 //==================================================================================================

 if(_Simbol==false)//если false то по всем ордерам открытым 

 { 

   for(int i=0;i<OrdersTotal();i++)//цикл перебора ордеров

     {

       if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))//будем искать следующие

         {

           if(OrderType()==OP_BUY) //если тип бай

            {

             double  bid=MarketInfo(OrderSymbol(),MODE_BID);//последний известный бид инструмента

             double  point=MarketInfo(OrderSymbol(),MODE_POINT);//чему равен пункт инструмента

             

               if(OrderStopLoss()<OrderOpenPrice())//если стоп лос меньше цены открытия

                {

                  if(bid-Dist_Perivoda*point>OrderOpenPrice())//бид-профит*пункт больше цены открытия

                   {//модифицируем стоп лос

 bool ord_modifik=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+NormalizeDouble(Level_Zahiti*point,Digits),OrderTakeProfit(),0,clrNONE);

              if(!ord_modifik)//если не получилось

              Comment(GetLastError());//номер ошибки в угол

                   }

                   

                }

            }

            if(OrderType()==OP_SELL)//если тип селл

            {

              double ask=MarketInfo(OrderSymbol(),MODE_ASK);//последний известный аск инструмента

              double  point=MarketInfo(OrderSymbol(),MODE_POINT);//чему равен пункт инструмента

              

              if(OrderStopLoss()==0||OrderStopLoss()>OrderOpenPrice())//если стоп лос больше цены открытия или равен нолю

              {

                if(ask+Dist_Perivoda*point<OrderOpenPrice())//аск +профит*пункт меньше цены открытия

                  {//модифицируем стоп лос

  bool ord_modifik=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-NormalizeDouble(Level_Zahiti*point,Digits),OrderTakeProfit(),0,clrNONE);

                  if(!ord_modifik)//если не получилось

                  Comment(GetLastError());//номер ошибки в  угол

                  }

                }

             } 

          }

       }

    }

  

 
pako:

Thanks for the code, I understand the gist, although I still get an error sometimes.

Experimented with functions that show chart IDs. What is ChartFirst() from documentation is absolutely unclear, one can interpret it any way - may it mean the first chart opened in order from the left, or the first chart opened from all the open ones (in time), or the first ever opened in the terminal (the possibility that it is open now is almost zero, so it makes no sense to close it). I am more inclined to the latter version, because I have never seen this function return the ID of the chart, which is open at the moment.

 
tuner:

Thanks for the code, I understand the gist, although I still get an error sometimes.

Experimented with functions that show chart IDs. What is ChartFirst() from documentation is absolutely unclear, one can interpret it any way - may it mean the first chart opened in order from the left, or the first chart opened from all the open ones (in time), or the first ever opened in the terminal (the possibility that it is open now is almost zero, so it makes no sense to close it). I am more inclined to the latter version, because I have never seen this function returning the ID of chart which is open at the moment.

ChartFirst

Returns chart ID of the first chart of the client terminal.

I understand that this is the leftmost chart

 
pako:

ChartFirst

Returns the identifier of the first chart of the client terminal.

I understand that this is the leftmost chart

Unfortunately it is not, it is easy to check it:

1. open 2-3 currency pairs in the terminal

2. run the code to check it:

   long ch=ChartFirst();
   Print("Первый ID терминала: ",ch, ", символ: ",ChartSymbol(ch),", таймфрейм: ",ChartPeriod(ch));
   for(int i=1;i<=100;i++) {
      ch=ChartNext(ch);
      if(ch<0)break;
      Print(i," chart ID for ",ChartSymbol(ch),ChartPeriod(ch),": ",ch);
  }