How to count total number of trades per bar?

 
I am new in EA programming and so happy when I first develop my first EA by reading from this forum. However my EA though doing what I like it to do been very efficient in trading every opportunity as instructed. When the market is volatile it trades so many in a day offsetting profit resulting in a loss and when the market is calm it produces decent profits. 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].

Big appreciation to anyone who can help me how to code to count the number of trades in Bar[1]. Big thanks...
 
Zaldy:
I am new in EA programming and so happy when I first develop my first EA by reading from this forum. However my EA though doing what I like it to do been very efficient in trading every opportunity as instructed. When the market is volatile it trades so many in a day offsetting profit resulting in a loss and when the market is calm it produces decent profits. 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].

Big appreciation to anyone who can help me how to code to count the number of trades in Bar[1]. Big thanks...


you're new in programming ???

where is your programming ??

we can't see .... because you don't show what you tried

We help you if show your attempt what you tried ...

SSSo ....learn to code post your attempt or ask at Jobs and pay someone

 

Loop through OrderHistory and use iBarShift( OrderOpenTime() ) for each order.

If the bar shift ==1, then increment a counter.

Do the same for current open orders.

If your counter exceeds your limit, do not trade.

 

Thanks GulRai,

In my quest to count the number of historical trades I end up using the following to count the number of lots traded and base the decision to trade or not on that, this actually working according to my preference But it cannot run in the backtesting because it cannot access the history. Your proposal to Loop through OrderHistory will also lead to the same result of not running in the backtesting. I hope there is another way. Perhaps to count the number of the previous bar price crossover with Open[1] ? Is there a way to count it? I know Bid[1] is not possible to use but I hope you catch what I mean and propose better coding. Sorry for the slight change now from my subject.

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()))

{

lot = lot + OrderLots();

}

}

double TotalSymbolTrade = lot/lots;

 

SELECT_BY_POS, MODE_HISTORY does work in strategy tester.

 

SDC, Thanks.

I shorten the start time of back tester to where there are data in the history, then I run the EA -it worked. Now my problem is if I run it say back 5 years where my history don't have enough data the back tester will not give me the right results. I hope I can have a code that will not rely on the history but rather on a cross over/under a reference point, let say the reference point is Open[1] then every time the price in Bar[1] crosses over/under the Open[1] it will progressively count the number of crossings. Need help please.

 
Zaldy:

SDC, Thanks.

I shorten the start time of back tester to where there are data in the history, then I run the EA -it worked. Now my problem is if I run it say back 5 years where my history don't have enough data the back tester will not give me the right results. I hope I can have a code that will not rely on the history but rather on a cross over/under a reference point, let say the reference point is Open[1] then every time the price in Bar[1] crosses over/under the Open[1] it will progressively count the number of crossings. Need help please.

Please edit your post above and re-insert your code using the SRC button . . . please use the SRC button to post code: How to use the SRC button.

To edit your post . . .

 

I am a little confused! It seemed above EA lots counter doesn't count what is in my database but rather develops it's own data as the backtest progresses. When I set the back test to say 5 years back it first traded nicely then once it hit the limit it stops trading and never re-trade again which is not what I like. On demo trading I can just manually adjust the history to say 5 days of data and set the number of lots limit to allow trading, this I think my EA will work fine but I need to manually adjust the history to 5 days every day which defeats the purpose of automation. I need help on the following:

1. I need a code to instruct the EA to count say 5 days of history data without me manually adjusting the history data into 5 days data.

2. This is a little off the topic: As an alternative to counting lots history, I need a code to count the cross over of price from a reference point say Open[1], every time price on Bar[1] crosses Open[1] then it count progressively. Then I can limit trades based on that count.

Cheers

 
Thanks moderator. I now insert the code.
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()))
   {
   lot = lot + OrderLots();
   
   }
   }
   double TotalSymbolTrade = lot/lots;
 
Zaldy:
Thanks moderator. I now insert the code.


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

int start()
  {
//----
  double lot=0;
 
   for (int i=0; i<=1000; i++)//why do you choose to check 1000 trades ?
   {
    OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
    if((OrderType() == OP_BUY || OrderType() == OP_SELL) && (Symbol()== OrderSymbol()))// what about magicnumber ?? why don't you select on that also ??
   {
   lot = lot + OrderLots();
   
   }
   }
   double TotalSymbolTrade = lot/lots;

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

 
Zaldy:

SDC, Thanks.

I shorten the start time of back tester to where there are data in the history, then I run the EA -it worked. Now my problem is if I run it say back 5 years where my history don't have enough data the back tester will not give me the right results. I hope I can have a code that will not rely on the history but rather on a cross over/under a reference point, let say the reference point is Open[1] then every time the price in Bar[1] crosses over/under the Open[1] it will progressively count the number of crossings. Need help please.

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