Counter orders per day

 

Good mornig,

I'm writing an expert advisor that set a counter for the order opened on the current symbol. This is the code.


 int CountSymbolPositions=0;
   for(int trade=OrdersTotal()-1;trade>=0;trade--)
       {
         if(!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
         continue;
         if(OrderSymbol()==Symbol())
         {
         if((OrderType()==OP_SELL||OrderType()==OP_BUY) && 
         (OrderMagicNumber()==1))
         CountSymbolPositions++;
         }
       }

Now I want to set another counter that count how many orders (for the current symbol) are closed on the current day. Every time that a new bar opens (on PERIOD_D1) the counter has to reset automatically.

This will be usefull if I want set number a maximum number of trades per day.  Can someone please help me?

 
Trader1529:

Good mornig,

I'm writing an expert advisor that set a counter for the order opened on the current symbol. This is the code.

Now I need to set another counter for the order closed  on the current day for the current symbol.

This counter need to reset every day, but I don't know how to write it. Can someone please help me?

For example:

static datetime new_D1 = Time[0];

if ( new_D1 != iTime(Symbol(), PERIOD_D1, 0) )
{
        new_D1 = iTime(Symbol(), PERIOD_D1, 0); 

        Print("## New day detected. Reseting conters");

        //Reset counters
        CountSymbolPositions = 0;
}
 
Fernando Morales:

For example:

Thank you for your answer, but I don't mean this...

I want to set another counter that count how many orders (for the current symbol) are closed on the current day. Every time that a new bar opens (on PERIOD_D1) the counter has to reset automatically.

This will be usefull if I want set number a maximum number of trades per day. 

 

Hello

if you want to count closed orders

you need to choose trades from OrderHistoryTotal() not OrderTotal()

and you need to select your trades with MODE_HISTORY not MODE_TRADES

first

 

about ' resetting your counter '

you got your answer on other topic you had opened same subject.

of course there are another ways too...

 
Ahmet Metin Yilmaz:

about ' resetting your counter '

you got your answer on other topic you had opened same subject.

of course there are another ways too...

Ok, thank you...

 

Do not double post!

I will delete your duplicate topic and move replies here.

 
Fernando Morales: For example:
static datetime new_D1 = Time[0];

if ( new_D1 != iTime(Symbol(), PERIOD_D1, 0) ){
        new_D1 = iTime(Symbol(), PERIOD_D1, 0); 
        Print("## New day detected. Reseting conters");
        CountSymbolPositions = 0;        //Reset counters
}
  1. Global and static variables work exactly the same way in MT4/MT5/C/C++.
    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use.)

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    4. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
                external static variable - MQL4 programming forum

  2. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4

  3. Simplify and avoid № 1 and № 2
    static datetime new_D1 = 0;
    datetime today = date();
    if ( new_D1 != today ){
            new_D1 = today;
            Print("## New day detected. Reseting conters");
            CountSymbolPositions = 0;        //Reset counters
    }
              Find bar of the same time one day ago - MQL4 programming forum