Questions from Beginners MQL4 MT4 MetaTrader 4 - page 229

 
Aleksei Stepanenko:
Thank you, is there any other solution? The advisor then opens and immediately closes the trade.
 
Nargiz Ravanova:
Thank you, is there another solution? After that the EA opens and immediately closes the position.

you need to fix the time on condition

op>=Profit

and do not refresh it until the position is closed

then subtract from the current time the time you have memorised
, when the given seconds have passed, close the positions.

 
input int Second=10;
ulong LastTime=0;

void OnTick()
   {
   if(op>=Profit) LastTime=GetMicrosecondCount();
   if(LastTime>0 && GetMicrosecondCount()-LastTime>(ulong)Second*1000000) {CloseAll(); LastTime=0;}
   }
 
input int Second = 10;
datetime LastTime = 0;

void OnTick()
  {
   if(op >= Profit && LastTime == 0)
      LastTime = TimeCurrent();
   if(LastTime > 0 && TimeCurrent() - LastTime >= Second)
     {
      CloseAll();
      LastTime = 0;
     }
  }
 

I did so


double op = CalculateProfit();
int time_waiting=0;

if (op >= Profit)
time_waiting = TimeLocal() + 10;
if (TimeLocal() < time_waiting)
{
CloseAll();

}


but it gives me an error


possible loss of data due to type conversion

 

is not an error, but a warning: data may be lost when converting from one type to another:

datetime time_waiting;
 
Nargiz Ravanova:

That is, I don't want the EA to close as soon as I see 2 quid, but a bit more.

And what, always after 10 seconds the profit is bigger?)

 
Followed the MT4 example "STRINGS: ASCII CHARACTERS TABLE AND USE"

//+------------------------------------------------------------------+
//| StringLowerCase |
//+------------------------------------------------------------------+
string StringLowerCase(string str)
  {
   string s = str;
   int lenght = StringLen(str) - 1, symbol;
   while(lenght >= 0)
     {
      symbol = StringGetChar(s, lenght);
      if((symbol > 64 && symbol < 91) || (symbol > 191 && symbol < 224))
         s = StringSetChar(s, lenght, symbol + 32);// тут possible loss of data due to type conversion
      else
         if(symbol > -65 && symbol < -32)
            s = StringSetChar(s, lenght, symbol + 288);// тут possible loss of data due to type conversion
      lenght--;
     }
   return(s);
  }
//+------------------------------------------------------------------+
//| StringUpperCase |
//+------------------------------------------------------------------+
string StringUpperCase(string str)
  {
   string s = str;
   int lenght = StringLen(str) - 1, symbol;
   while(lenght >= 0)
     {
      symbol = StringGetChar(s, lenght);
      if((symbol > 96 && symbol < 123) || (symbol > 223 && symbol < 256))
         s = StringSetChar(s, lenght, symbol - 32);// тут possible loss of data due to type conversion
      else
         if(symbol > -33 && symbol < 0)
            s = StringSetChar(s, lenght, symbol + 224);// тут possible loss of data due to type conversion
      lenght--;
     }
   return(s);
  }

If you don't mind, please help me fix it...
 
s = StringSetChar(s, lenght, ushort(symbol + 32));
string  StringSetChar(
   string&   string_var,       // строка
   int       pos,              // позиция
   ushort    value             // код символа
   );

Accepting full responsibility for the fact that

ushort

The unsigned shorttype is the ushort type, which also has a size of 2 bytes. The minimum value is 0, the maximum value is 65,535.

int

The integer int type has a size of 4 bytes (32 bits). The minimum value is -2 147 483 648, the maximum value is 2 147 483 647.

 
Iurii Tokman:

I did as you said, but for some reason after closing the Expert Advisor closes a couple of trades, despite the fact that I have a one hour slip after the CloseAll() function.

input int Second = 10;
datetime LastTime = 0;

void OnTick()

double op = CalculateProfit();


if (op >= Profit && LastTime == 0)
LastTime = TimeCurrent ();
if(LastTime > 0 && TimeCurrent () - LastTime >= Second)

{
CloseAll();
LastTime = 0;

SendNotification("Trade is over");
Sleep(60*60000);// 60.000 = 1 min

}

Reason: