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

 
Rustam Bikbulatov:
Hi, I need some advice. Why is my last text not showing? What's wrong here? Can you please tell me how to fix it!

Why do you need a loop if you only take the name of one object at all times?

for(int no1=0; no1<ObjectsTotal(OBJ_TEXT); no1++)
  {
   if(ObjectName(0)=="Profit1"+iBarShift(NULL,1440,0))
     {
      double name;
      name = ObjectDescription(0);;
      Comment(name);
     }
  }
 

Iurii Tokman:

no1<=ObjectsTotal(OBJ_TEXT);

You can't exactly do this - it will be out of the array's bounds. If there are 10 objects, the index of the last one will be 9. Because it counts from zero.

 
Artyom Trishkin:

Why a loop if you only take the name of one object at a time?

tell me exactly how to determine the penultimate text of this object.

 
Rustam Bikbulatov:

tell me exactly how to determine the penultimate text of this object.

It's hard to know what you want.

 
void Profit()
  {
   ObjectCreate("Profit"+iBarShift(NULL,1440,0),OBJ_TEXT,1,TimeCurrent(),0.6);
   ObjectSetText("Profit"+iBarShift(NULL,1440,0),DoubleToStr(AccountProfit(),2),11,"Arial",clrWhite);
   ObjectSetInteger(0,"Profit"+iBarShift(NULL,1440,0),OBJPROP_ANCHOR,ANCHOR_RIGHT_LOWER);
   return;
  }
void Profit1()
  {
   double  Prof = ObjectDescription("Profit1"+iBarShift(NULL,1440,0));
   double  P = -Prof;
   ObjectCreate("Profit1"+iBarShift(NULL,1440,0),OBJ_TEXT,1,TimeCurrent(),0.1);
   ObjectSetText("Profit1"+iBarShift(NULL,1440,0),DoubleToStr(MathMax(-AccountProfit(),-P),1),11,"Arial",clrWhite);
   ObjectSetInteger(0,"Profit1"+iBarShift(NULL,1440,0),OBJPROP_ANCHOR,ANCHOR_RIGHT_LOWER);
   return;
  }
void Profit2()
  {
   ObjectCreate("Profit2"+iBarShift(NULL,1440,0),OBJ_TEXT,1,TimeCurrent(),-0.4);
   ObjectSetText("Profit2"+iBarShift(NULL,1440,0),iBarShift(NULL,1440,0)-1000,11,"Arial",clrWhite);
   ObjectSetInteger(0,"Profit2"+iBarShift(NULL,1440,0),OBJPROP_ANCHOR,ANCHOR_RIGHT_LOWER);
   return;
  }
void Profit3()
  {
   for(int no1=0; no1<=ObjectsTotal(OBJ_TEXT); no1++)
     {
      if(ObjectFind(1,"Profit2"+iBarShift(NULL,1440,0))!=0)
        {

         string   obj_name;
         obj_name = ObjectName(3);
         string   name;
         name = ObjectDescription(obj_name);
         Comment(obj_name);
        }
     }

   ObjectCreate("Profit3"+iBarShift(NULL,1440,0),OBJ_TEXT,1,TimeCurrent(),-0.9);
   ObjectSetText("Profit3"+iBarShift(NULL,1440,0),DoubleToStr(name,1),11,"Arial",clrWhite);
   ObjectSetInteger(0,"Profit3"+iBarShift(NULL,1440,0),OBJPROP_ANCHOR,ANCHOR_RIGHT_LOWER);
   return;
  }

Provit - shows total profits during the test during the day

Provit1 - shows maximum drawdown for the day

Provit2 - normal numeric row starting from one

Provit3 - Target! Sum up Provit1 and divide by Provit2. I want to know the average daily drawdown for the total time of the test. Help to understand Provit3

 

Colleagues - what could this close error be and how to deal with it? Here is the code below, when the profit is exceeded - close all unidirectional positions. Thank you.

        
             
 // ЗАКРЫВАЕМ ВИРТУАЛЬНЫЕ ТР БАЙ позы            
     if(takeprofit < ProfitB)    
      {   
        Print(" ЗАКРЫТИЕ BUY POSITIONS. общий ProfitB = ", ProfitB," мин общий takeprofit buys positions = ", takeprofit);   
        for (i=OrdersTotal()-1; i>=0; i--)         
          if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
          if (OrderSymbol()==Symbol() &&  Magic_Прямой==OrderMagicNumber()) 
            {             
             if(OrderType()==OP_BUY)    
                             //if(OrderOpenPrice()+takeprofit*Point<=MarketInfo(Symbol(),MODE_BID) || 
                             //   OrderOpenPrice()-stoploss*Point>=MarketInfo(Symbol(),MODE_BID))   
                     
               if (OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symbol(),MODE_BID),NormalizeDouble(MarketInfo(Symbol(),MODE_SPREAD)*2,Digits()),clrGray))  
                   Print(" закрытие бай, тикет = ", OrderTicket());
               else  Print(" ошибка закрытия бай, Error = ", GetLastError());          
            }
       }     
      
       if(takeprofit < ProfitS)     
        { 
         Print(" ЗАКРЫТИЕ СЕЛЛов. общий ProfitS = ", ProfitS," мин общий takeprofit СЕЛЛов = ", takeprofit);   
 
         for (i=OrdersTotal()-1; i>=0; i--)      
          if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
          if (OrderSymbol()==Symbol() &&  Magic_Прямой==OrderMagicNumber()) 
            {                       
             if(OrderType()==OP_SELL)    
                             //  if(OrderOpenPrice()-takeprofit*Point>=MarketInfo(Symbol(),MODE_ASK) ||
                             //     OrderOpenPrice()+stoploss  *Point<=MarketInfo(Symbol(),MODE_ASK))                
             
               if (OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symbol(),MODE_ASK),NormalizeDouble(MarketInfo(Symbol(),MODE_SPREAD)*2,Digits()),clrGray)) 
                   Print(" закрытие SELL,  тикет = ", OrderTicket());
               else   Print(" ошибка закрытия sell, Error = ", GetLastError());     
            }       
         }    
           
 
Roman Shiredchenko:

Colleagues - what can this be a closing error and how to deal with it? If i want to use the code below, i need to close all of the one-directional positions if they are profitable. Thank you.

What's the error? wrong screen

in theory it should work, but I haven't seen such a sophisticated closing taking into account the spread and adding it as slippage - imho, if you decide to close, then slippage is not the most important thing to watch for, closing errors are more important, then requotes, then the connection is lost completely


Take OrderClose(....) away and replace it with Kim's ClosePosBySelect()https://www.mql5.com/ru/forum/131859/page2#comment_3359664

use it, then you will clearly see where it is breaking - either in your closing conditions or the server has a bug

 
Igor Makanu:

What's the error? wrong screen

in theory it should work, but I haven't seen such a sophisticated closing taking into account the spread and adding it as slippage - imho, if you decide to close, then slippage is not the most important thing to watch out for, closing errors are more important, then requotes, then the connection is lost completely


remove OrderClose(....) and pass the ticket to Kim's ClosePosBySelect() instead, then it will become clear where it does not work - either in your closing conditions or the server has a bug

He has a zero price close there. Accordingly, we need to control what was returned by MarketInfo-function, but there is no control. However, as usual on four, some people are used to write - something received, what - did not check, but sent an argument.

 
Artyom Trishkin:

He has a zero price close there. Accordingly, we need to control what was returned by MarketInfo-function, but there is no control. However, as usual on the four some people are used to write - something received, what - did not check, but sent the argument.

I have seen it, I doubt that he will be able to unset everything himself, that's why I suggested to use checked ClosePosBySelect() to close selected order viaOrderSelect( ) - he can do that, at least I have seen him assembling codes with constructor )))

SZY: I have another suspicion that it closes one order twice, but the Kim's function prints into the log, it seems there are no separate loops to close by the 2nd condition

 
Igor Makanu:

I've seen it, I doubt it will be able to unprime everything on its own, that's why I suggested using the proven closing of a highlighted order via OrderSelect()

Sure, Igor's functions will do everything for the user (and even some users manage to make a mistake with them). But in this case, he will never learn to do anything himself. Although, of course - their choice.

Reason: