How to count total number of trades per bar? - page 2

 
deVries:


Are All the trades closed when you open new trade of your EA ??


if you want to know the number of trades your EA traded the previous Bar[1] you have to find and count the trades started after Time[1] and before Time[0]......

you don't select trades opend in that timeperiod..... there is no count of trades

make it a normal thing for you to check trades use a counting down loop


Hi deVries,

No, all trades did not close when I open new trades because I use position count rather than TotalOrders count. I learned it the hard way. I use 1000 as max count just for the sake of counting it, I don't know how many lots it will count. May be I use lower. Yes I will add magic number.

Kindly show a counting down loop code. Big thanks.

 
RaptorUK:
The Strategy Tester looks at it's own trade history not the history of the account you are logged into.


Yes, I learned it the hard way.
 
Zaldy:


Hi deVries,

No, all trades did not close when I open new trades because I use position count rather than TotalOrders count. I learned it the hard way. I use 1000 as max count just for the sake of counting it, I don't know how many lots it will count. May be I use lower. Yes I will add magic number.

Kindly show a counting down loop code. Big thanks.

with lotsize you don't count your trades
Read Loops and Closing or Deleting Orders then do again an attempt

use inside your loop OrdersTotal() and other loop OrdersHistoryTotal()

 
Seemed the iBarShift function did the trick for me. I can now control how many trades the EA will do per bar or several bars ago. Here's the code. Hope to receive inputs. By the way this solution is not perfect because the EA needs to trade before realizing that it is trading losing trades. I still prefer a counter of price in Bar[1] crossing from a reference point say Open[1]. Anyone?
int start()
  {
//----
  double lot=0;
  
   for (int i=0; i<=1000; i++)
   {
    OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
    if((OrderType() == OP_BUY || OrderType() == OP_SELL) && (Symbol()== OrderSymbol()))
    int BarShift = iBarShift(NULL,0,OrderOpenTime(),false);// Set the number of bars to count total lots traded. 
              if(BarShift < BarsCountLot)
   {
   lot = lot + OrderLots();
   
   }
   }
   double TotalSymbolTrade = lot/lots;
 
Zaldy:
Seemed the iBarShift function did the trick for me. I can now control how many trades the EA will do per bar or several bars ago. Here's the code. Hope to receive inputs. By the way this solution is not perfect because the EA needs to trade before realizing that it is trading losing trades. I still prefer a counter of price in Bar[1] crossing from a reference point say Open[1]. Anyone?
No it is not
//+------------------------------------------------------------------+
//|  example                                                         |
//+------------------------------------------------------------------+
         int CountOpenTrades_at_Bar1()
           {
            int count=0;
            int trade;
            for(trade=OrdersTotal()-1;trade>=0;trade--)
              {
               if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES)==false)break;
               if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)continue;
               if(OrderType()==OP_SELL || OrderType()==OP_BUY)
                    {
                     if(OrderOpenTime()>=Time[1] && OrderOpenTime()<Time[0])count++;
                    }
              }//for
            return(count);
           }

Why is your orderloop not counting down ??

Why is there still 1000 trades to check ???

Why do you only check the closed trades ??

TotalSymbolTrade = lot/lots ?? wrong counting

 
deVries:
No it is not

Why is your orderloop not counting down ??

Why is there still 1000 trades to check ???

Why do you only check the closed trades ??

TotalSymbolTrade = lot/lots ?? wrong counting


Hi deVries, Big thanks. I updated the codes. It worked well in the backtest. However I noticed the current bar still can trade at will if the previous bar has fewer trades than set. How can I include the Bar[0] trades in the count. I thought the OrderOpenTime()>=Time[TimeBarBack] && OrderOpenTime()<Time[0] will count including the Bar[0] trades.
int start()
  {
//----
            
            int count=0;
            int trade;
            for(trade=OrdersHistoryTotal()-1;trade>=0;trade--)
              {
               if(OrderSelect(trade,SELECT_BY_POS,MODE_HISTORY)==false)break;
               if(OrderSymbol()!=Symbol()|| OrderMagicNumber()!=magic)continue;
               if(OrderType()==OP_SELL || OrderType()==OP_BUY)
                    {
                     if(OrderOpenTime()>=Time[TimeBarBack] && OrderOpenTime()<Time[0])count++;
                    }
                    int TotalSymbolTrade = count;  
              }
 
The backtesting became considerably slow. Is this code causing it // for(trade=OrdersHistoryTotal()-1;trade>=0;trade--)?? Counting from 1970 back and forth? If so is there a much efficient alternative coding? I run it in demo trade so far no problem, only the back testing was a snail pace.
 

Zaldy:

I would like to count the number of trades my EA traded the previous Bar[1] so that if the number of trades in Bar[1] exceeded say 4 trades the EA will not trade on Bar[0].



How can I include the Bar[0] trades in the count. I thought the OrderOpenTime()>=Time[TimeBarBack] && OrderOpenTime()<Time[0] will count including the Bar[0] trades.


why do you think that ??

if(OrderOpenTime()>=Time[1] && OrderOpenTime()<Time[0])count++;

What is this line say ??

look here.....

datetime iTime(string symbol, int timeframe, int shift)
Returns Time value for the bar of indicated symbol with timeframe and shift. If local history is empty (not loaded), function returns 0.
For the current chart, the information about bars open times is in the predefined array named Time[].

you can click Time[].

and click this link also OrderOpenTime


So What is Time[1] ?? ......

and what is Time[0] ??

So what can you say if a trade opens at bar 0 ???

 
Zaldy:
The backtesting became considerably slow. Is this code causing it // for(trade=OrdersHistoryTotal()-1;trade>=0;trade--)?? Counting from 1970 back and forth? If so is there a much efficient alternative coding? I run it in demo trade so far no problem, only the back testing was a snail pace.


You are doing every tick checking how many trades has opend at bar 1

if the first tick answers you 5 trades

then the ticks after the first tick you check this again will give you the same answer

So if you check every tick how many trades you have opend while you could do this also once then it will slow down your backtesting.....

 
Thank you for those who contributed here. Based on the subject here, my query is answered and I now able to code to count number of trades.