Stop Loss according to time

 
Hi all,
In my strategy, I found that when a trade lasts more than 50 seconds and the result is negative, it will remain negative until a certain stop loss is reached. To anticipate this condition, I would like to place a stop loss over time.

I managed to finish the code up to this point, but for some reason it doesn't work. Can you help me?


#include <Trade\Trade.mqh>
CTrade trade;

input ulong  MagicNumber = 1234;
input double timetostop  = 10; //In seconds

//+------------------------------------------------------------------+
int OnInit()
  {

   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {


  }

//+------------------------------------------------------------------+
void OnTick()
  {
   if(PositionsTotal() < 1)
     {
      double Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
      trade.Buy(1,NULL,Ask,Ask-50*_Point,Ask+50*_Point,"Position Open");
     }
   if(PositionsTotal() == 1)
     {
      StopTimeout();
     }
  }

//+------------------------------------------------------------------+
void StopTimeout()
  {

   for(int i = PositionsTotal() - 1; i >= 0; i--)
     {
      string PositionSymbol = PositionGetSymbol(i);
      ulong  PositionMagic  = PositionGetInteger(POSITION_MAGIC);
      ulong  PositionTicket = PositionGetInteger(POSITION_TICKET);
      double PositionProfit = PositionGetDouble(POSITION_PROFIT);
      
      bool Negative = PositionProfit<0;

      if(PositionMagic == MagicNumber && PositionSymbol == _Symbol && Negative)
        {
         int timesetup      = (int)PositionGetInteger(POSITION_TIME);
         int timeCurrent    = (int)TimeCurrent();

         if(timetostop > 0 && (timeCurrent - timesetup) > timetostop)
           {
            bool ok = trade.PositionClose(PositionTicket);
            if(ok)
              {
               Print("Position Closed");
              }
           }

        }
     }
  }
//+------------------------------------------------------------------+
 
diegotfcastro:

I managed to finish the code up to this point, but for some reason it doesn't work. Can you help me?

Please don't just say " it doesn't work". That can mean almost anything.

Explain what the problem is.

Your code appears to be set up so that it closes a trade anytime (after 10 seconds) that the trade is in loss.

 
Keith Watford:

Please don't just say " it doesn't work". That can mean almost anything.

Explain what the problem is.

Your code appears to be set up so that it closes a trade anytime (after 10 seconds) that the trade is in loss.

You're right, I'm sorry.

What happens is that the position is not being closed.

But this has been a surprise for me because I validated each step of the code and everything seems to be ok. So it must be something very simple that I'm forgetting.

 

I note

   if(PositionsTotal() == 1)
     {
      StopTimeout();
     }

So if PositionsTotal() >1 the function will not be called.

 

When you are faced with such scenarios, it is very easy to debug by verbosing your code. That is, put a Print() statement inside your main blocks of logic (IF statements, loops, etc) to see what is going on, and if these blocks are being called and what the main variables store.

You will be surprised how quickly this can resolve such bugs for you. My 2 cents.