Questions from Beginners MQL4 MT4 MetaTrader 4 - page 230

 
Aleksey Mavrin:

and what, is there always more profit after 10 seconds?)

Not always of course, but it often happens that the price just flies in a minute and the EA bluntly closes at a fixed price.
 
Nargiz Ravanova:

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

datetime sleep = TimeCurrent();
if(sleep > TimeCurrent())
     return;
//---
double op = CalculateProfit();

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

    {
     CloseAll();
     LastTime = 0;

     SendNotification("Trade is end");
     sleep = TimeCurrent()+60*60;
    }

That's how it is. Of course, it was just written on my knee.

 
Konstantin Nikitin:

Something like that. Of course it was just written on the spot.

Thanks, now the EA only closes one trade out of all the ones on the market, and I have a grid EA.

I have not got any slips and EA is opening a new trade again and ignoring slips. If i look at the EA, i don't think it is possible to open another one, but i have to return it as it was before. I do not know what to do with this EA and I will not know what to do with it.

 
Nargiz Ravanova:

Thank you, now the EA only closes one trade out of all the ones on the market, and I have a grid EA.

I have a lot of problems with this, but I'm not sure what to do with it, so I'm not sure what to do with it. I think it is not possible to implement it, I will just return it as it was before. I would like to thank them for their help.

You need to implement it as a whole
the reasons may be different

 
Nargiz Ravanova:

Thank you, now the EA only closes one trade out of all the ones on the market, and I have a grid EA.

I have a lot of problems with this, but I'm not sure what to do with it, and I'm not sure what to do with it. I think it is not possible to implement it, I will just return it as it was before. I have already implemented it and it has not been implemented yet, so I will just return it to the way it was before.

This should make more sense, right?

input double Profit   = 100.0;
input int    Second   = 10;
//+------------------------------------------------------------------+
void CloseAllSleep(void)
    {
     static datetime sleep_all = TimeCurrent();
     static datetime LastTime = 0;
     if(sleep_all > TimeCurrent())
          return;
//---
     if(LastTime == 0)
         {
          if(CalculateProfit() < Profit)
               return;
          LastTime = TimeCurrent();
         }
     if(TimeCurrent() - LastTime >= Second)
          return;
//---
     if(!CloseAll())
          return;
     LastTime = 0;

     SendNotification("Trade is end");
     sleep_all = TimeCurrent()+60*60;
    }

//--- Считаем профит открытых позиций
double CalculateProfit(void)
    {
     return 0.0;
    }
//--- Закрываем все позиции
bool CloseAll(void)
    {
     return true;
    }

Again, this is purely for clarity. And what and how you need to implement, think for yourself.

 
Nargiz Ravanova:
Of course, not always, but it often happens that the price just flies for a minute and the Expert Advisor bluntly closes at a fixed price.

Still, I think this approach is sub-optimal. If the price is flying, there should be a positive slippage. If you wait for the time - it's a game of roulette - then what is the strategy for, your profit may already be lower than the given 2 quid.

It is more reasonable to measure the speed of price change, and if it is high towards profit - wait, when it went down - to cover.

 
Konstantin Nikitin:

I think that makes more sense, don't you?

Again, this is purely for understanding. It's up to you to figure out what to implement and how to do it.

I don't understand it to be honest, forget it, I'll think of something.

 
Aleksey Mavrin:

Still, I think this approach is sub-optimal. If the price is flying, then the slippage should be positive. If you wait for the time - it's a game of roulette - then what is the strategy for, your profit may already be below the given 2 quid.

It is more reasonable to measure the speed of price change, and if it is high towards profit - wait, when it went down - to cover.

we can't fix a simple slip here and you're talking about speed))))
 
Nargiz Ravanova:
we can't fix a simple slip here and you're talking about speed))))

If you set high goals for yourself, the Almighty will help you achieve them! ;)

 
Nargiz Ravanova:
we can't fix a simple slip here and you're talking about speed))))

Well, to be more precise, it looks like you can't. And about SLEEP. Do you want to freeze the entire Expert Advisor for some time? Or just a certain function, after it has been worked out. In any case, even for interruption of Expert Advisor's work it is better to use the flag for long delays, and when checking it, if there is no permission, just exit to OnTick/OnTimer. For short delays, yes, you can useSLEEP.
By the way,SLEEP does not work in indicators.

The Sleep() function cannot be called from custom indicators, because indicators are executed in the interface thread and must not slow it down.

So, understand the use of flags, you will need it in future.

Reason: