I need a counter that restart everytime i close a trade.

 
    static int counter;
    static datetime hour;
    if(hour!=iTime(NULL, PERIOD_H1,0)) {counter++; hour=iTime(NULL, PERIOD_H1,0)II 

I need to set the counter on 0 every time I close a trade... 

I tried to do this... but i think when the trade close, also the if statement... 

     if(OrderSelect(orderID,SELECT_BY_TICKET)== true)
     { (PriceAsk == OrderTakeProfit() || PriceBid == OrderStopLoss())
            {
               counter = 0;
            }
     };


Thanks for the help.

 
  1. David Salem: I need to set the counter on 0 every time I close a trade...
        static int counter=0;
        static int nTrades=0;
        int prevTrades=nTrades; nTrades=OrdersTotal();
        if(nTrades < prevTrades) counter=0; // A trade closed.
    
  2.     if(hour!=iTime(NULL, PERIOD_H1,0)) {counter++; hour=iTime(NULL, PERIOD_H1,0)II 

    How To Ask Questions The Smart Way. (2004)
              The XY Problem

    No need for a counter; just compute how many bars has passed.

    if(OrderSelect(orderID,SELECT_BY_TICKET)){
       int counter = iBarShift(_Symbol, _Period, OrderOpenTime();
  3. Remember:

    1. If you select by ticket, you must check if the order has closed.

    2. EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover?

      Use a OrderSelect / Position select loop to recover, or persistent storage (GV+flush or files) of ticket numbers required.

 
William Roeder #:
  1. How To Ask Questions The Smart Way. (2004)
              The XY Problem

    No need for a counter; just compute how many bars has passed.

  2. Remember:

    1. If you select by ticket, you must check if the order has closed.

    2. EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover?

      Use a OrderSelect / Position select loop to recover, or persistent storage (GV+flush or files) of ticket numbers required.

Hey William, thanks for ur reply.

Im pretty new coding and with my scarce resources im trying to make an EA. I take all the scenarios you are mentioned and will try to think in something else! 

Again thanks for ur time! 

Ícono de validado por la comunidad