If last order is a loss => Sleep() function

 

Hi,

I have a MT4 EA and I would like to add a function which stop new order open during xx minutes (with the Sleep() function) if the last close order is a loss.

I built this code below but it doesn't work correctly, what is wrong?

for(int i=0;i<OrdersTotal();i++)if(
OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)
&& OrderMagicNumber()==MagicNumber
&& OrderSymbol()==Symbol()){
double Profit = OrderProfit() + OrderSwap() + OrderCommission();
}  
// Sleep(3600000) if last Profit is < 0
if(Profit<0){return(0);Sleep(3600000);}
// Else Open new order
else{OpenNewOrder();}

Thank you for your help.

Documentation on MQL5: Common Functions / Sleep
Documentation on MQL5: Common Functions / Sleep
  • www.mql5.com
The Sleep() function can't be called for custom indicators, because indicators are executed in the interface thread and must not slow down it. The function has the built-in check of EA halt flag every 0.1 seconds.
 
8tango:

Hi,

I have a MT4 EA and I would like to add a function which stop new order open during xx minutes (with the Sleep() function) if the last close order is a loss.

I built this code below but it doesn't work correctly, what is wrong?

Thank you for your help.

Your sleep statement will never get activated because you have a return statement before it...

This is also a poor way to achieve your result, sending programs to sleep is rarely a good idea.

Set a timeout value instead and check it before opening a trade, that way you do not need to put the program to sleep.

 
8tango:

Hi,

I have a MT4 EA and I would like to add a function which stop new order open during xx minutes (with the Sleep() function) if the last close order is a loss.

I built this code below but it doesn't work correctly, what is wrong?

Thank you for your help.

Hellow ?? 

if you want to select the last closed order so that you can get to know its loss then you have to select the closed order among the OrdersHistory your for loop is wrong and do this ...??

bool stopnewOrder_x_minutes(int magic)
{
    for (int i=0; i<OrdersHistoryTotal()-1; i++){
     bool sel=(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY));
       double profit=OrderProfit();
        if(OrderMagicNumber() == magic && OrderSymbol() == Symbol()){
          profit =(double) NormalizeDouble(OrderProfit(), Digits());
           {if (profit<0) return (true);}
           }
       }
   return (false);
}
//---
 
Omega J Msigwa:

Hellow ?? 

if you want to select the last closed order so that you can get to know its loss then you have to select the closed order among the OrdersHistory your for loop is wrong and do this ...??

I test your code but it doesn't work correctly, it open a new trade after a loss.

My EA run on M1.

My code:

int start()
{
// Code here...
if(stopnewOrder(MagicNumber)==false){OpenNewOrder();}
if(stopnewOrder(MagicNumber)==true){Sleep(50000);} // Stop trading during 5 minutes
}

bool stopnewOrder(int magic)
{
    for (int i=0; i<OrdersHistoryTotal()-1; i++){
     bool sel=(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY));
       double profit=OrderProfit();
        if(OrderMagicNumber()==magic && OrderSymbol()==Symbol()){
          profit = NormalizeDouble(OrderProfit(), Digits());
           {
           if(profit<0) return(true);
           }
         }
       }
   return(false);
}
 
8tango:

I test your code but it doesn't work correctly, it open a new trade after a loss.

My EA run on M1.

My code:

Sir i just showed you a way to determine if the last position closed was a loss ..you are the one to decide the way you trade

 
Omega J Msigwa:

Hellow ?? 

if you want to select the last closed order so that you can get to know its loss then you have to select the closed order among the OrdersHistory your for loop is wrong and do this ...??

Omega J Msigwa:

Sir i just showed you a way to determine if the last position closed was a loss ..you are the one to decide the way you trade

Your code is wrong.

It doesn't find if the last trade closed with a loss, it finds if any trade (with MN and symbol) closed with a loss

 
Keith Watford:

Your code is wrong.

It doesn't find if the last trade closed with a loss, it finds if any trade (with MN and symbol) closed with a loss

I have this code below which work fine, it find the last order close (profit or loss) but when there is a loss the EA doesn't open anymore trade. I think there is a problem with refresh data (or RefreshRates), how to fix this problem?

int start()
{
  if(LastCloseOrder(MagicNumber)=="loss"){return(0);Sleep(5000);}
  else{OpenNewOrder();}
}

string LastCloseOrder(int magic)
{
   string last;
   for(int i=(OrdersHistoryTotal()-1);i>=0;i--)
   {
      if(OrderSelect(i, SELECT_BY_POS,MODE_HISTORY))
      {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic)
         {
            if(OrderProfit()>0) last="profit";
            if(OrderProfit()<0) last="loss";
            break; 
         }
      }
   }
   return(last);
}
 
8tango:

I have this code below which work fine, it find the last order close (profit or loss) but when there is a loss the EA doesn't open anymore trade. I think there is a problem with refresh data (or RefreshRates), how to fix this problem?

Again, your code does not necessarily find the last trade.

Orders History is not necessarily in chronological order. William has a link so maybe he will post it when he sees this.

8tango:

I have this code below which work fine, it find the last order close (profit or loss) but when there is a loss the EA doesn't open anymore trade. 

Of course it doesn't.

if(LastCloseOrder(MagicNumber)=="loss"){return(0);Sleep(5000);}

You return if the function returns "loss".

There is absolutely no point in the Sleep() after the return as it will never be executed.

Not really related but You really should be using up to date functions by now. OnTick() instead of start().

 
Keith Watford:

Again, your code does not necessarily find the last trade.

Orders History is not necessarily in chronological order. William has a link so maybe he will post it when he sees this.

Of course it doesn't.

You return if the function returns "loss".

There is absolutely no point in the Sleep() after the return as it will never be executed.

Not really related but You really should be using up to date functions by now. OnTick() instead of start().

Thanks Keith but I don't know how to proceed with OnTick().

I must replace "int start()" by "void OnTick()" or add "void OnTick()" (with int start() togethers) into my code?

I tried to replace "int start()" by "void OnTick()" but what is the value that the "void OnTick()" return (return(....);)?

 
You replace it and there is no return value.
 
Keith Watford:
You replace it and there is no return value.

I replaced "int start()" by "void OnTick()" into my code but now I don't know how to proceed using date function !

I would like a function which find the last order closed (profit or loss) and if it's a loss we wait 5 minutes before to open a new order.

This function find if the last close order is a profit or loss:

string LastCloseOrder(int magic)
{
   string last;
   for(int i=(OrdersHistoryTotal()-1);i>=0;i--)
   {
      if(OrderSelect(i, SELECT_BY_POS,MODE_HISTORY))
      {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic)
         {
            RefreshRates();
            if(OrderProfit()>0){last="profit";}
            if(OrderProfit()<0){last="loss";}
            break; 
         }
      }
   }
   return(last);
}

But now, how to open a new order only if the last close order is a profit?

Reason: