Trailing and one trad per day once again

 

Hello, I know many times were posts about these topics, but I my EA did something strange. Assumption:

1. EA switch on at hour example X and should work to hour Y

2. After conditions it makes trade with trailing stop

3. After close trade by trailing stop EA switch off to the next calendar day (not 24 h)

After backtest I saw in results:

a. trade opened after hour X, appeared trailing stop-modified levels, trade was closed by SL. In this moment EA should stoped work, but not. It did next trades according I wrote at start. Every thing between hour X and Y

b. trade opened after hour X, EA nothing did, at hour Y swithed on trailing stop, and it worked to closed position by trailing stop

It looks like sometime works function to make one trade, but not correctly trailing-case b. In the case a it looks like correctly works trailing, but not function to one trade.

Function I wrote according suggestion of RaptorUK, but I wonder I probebly did error in the code, because one trade is as bool function, if it is true return(0), if not return false, and if false, should EA calls function OrderSend autmatically (code below)

I tried used suggestion of WHRoeder, but i didn't work correctly too. I changed OrdersTotal() to OrdersHistoryTotal(), but reslut was the same. Similar using TimeCurrent(),86400 seconds etc.

Structure of the code is:

function start() - info about pips to trailing stop, loop counting orders (Symbol(), MagicNumber), code for trailingstop, code for open and close trades at the correct hour,calling functions buy,sell, and tradplacedtoday

Outside start ()

- int buy (int Ticket)

- int sell (int Ticket)

- bool TradePlacedToday()

Have anyone any idea what can be wrong done or wrong in the my logic?

Thank you for all suggestions.

ver.1
 bool TradePlacedToday()
    {
     for (int k=OrdersTotal()-1;k>=0;k--)
      {
         if (OrderSelect(k,SELECT_BY_POS,MODE_HISTORY))
         {                     
            datetime opp=OrderOpenTime();          
          if (opp>0)return(0);
         }
           return(false);
          //  else {Ticket=OrderTicket();}                
      }
    }   

ver.2
 bool TradePlacedToday()
    {
     datetime now = TimeCurrent();
     datetime bod = now-now%86400; 
     for (int k=OrdersTotal()-1;k>=0;k--)
      {
         if (OrderSelect(k,SELECT_BY_POS,MODE_HISTORY))
         {                     
            datetime opp=OrderOpenTime();
            datetime cur=opp-opp%86400;           
          if (opp<cur)return(0);
         }
           return(false);
          //  else {Ticket=OrderTicket();}               
      }
    }   
 
ver.1
 bool TradePlacedToday()
    {
     for (int k=OrdersTotal()-1;k>=0;k--)
      {
         if (OrderSelect(k,SELECT_BY_POS,MODE_HISTORY))
         {                     
            datetime opp=OrderOpenTime();          
          if (opp>0)return(0);   //---------opp is ALWAYS going to be >0   Both returns are false
         }
           return(false); 
          //  else {Ticket=OrderTicket();}                
      }
    }   
 

Both functions will return false before the first loop is executed.

The return(false) is not subject to any condition