Altern Buys and sells

 

Hi,

I'd like to altern buys and sells op for my ea, but it keep on buys or sell..

I check the history tab with a loop with this code :

int AlternBuySell(int MagicNumber)
{
int altern=0;
int histb;
int histbs=OrdersHistoryTotal();
for(histb=0;histb<histbs;histb++)
{
OrderSelect(histb,SELECT_BY_POS,MODE_HISTORY);

if( OrderSymbol()!=Symbol() || OrderMagicNumber() != MagicNumber || OrdersHistoryTotal() == 0)
continue;
if(OrderType()==OP_SELL)
altern--;
if(OrderType()==OP_BUY)
altern++;
}//for
return(altern);
}

And take orders with :

if (SellSignal) {
if (AlternBuySell(MagicNumber) > 0) {
//Alert("SELL"); ....

and :

if (BuySignal) {
if (AlternBuySell(MagicNumber) <= 0) {
//Alert("BUY"); ....

Could somebody help me ??

Thanks a lot

 

What this code does is to try to maintain an even distribution of buys/sells, not to slavishly alternate. Although, if it reaches an equilibrium, then it should slavishly alternate on the assumption that you close down each order before checking what direction to open.

It may be that there are more buys than sells already in your history and this is causing sells to be invoked. Or perhaps your orders aren't getting closed before you check your history.


Since you don't have control of the contents of the trade history, I'd suggest there is a much easier and more reliable way to do what you say you want to achieve.

Just have your EA maintain a flag variable which indicates what its previous trade direction was, then check the flag before trading, trade the opposite way and amend the flag.

In order to set the flag in the first place, you could either:

- Read the most recent item (at position 0) in the trade history

or

- Maintain the flag in a file which you read at initialization and write when you modify the flag


Oh, and by the way, when constructing loops to read through trades or trade history, always start at the top and work downwards - the oppostite to what you have done.


CB

 
cloudbreaker wrote >>

What this code does is to try to maintain an even distribution of buys/sells, not to slavishly alternate. Although, if it reaches an equilibrium, then it should slavishly alternate on the assumption that you close down each order before checking what direction to open.

It may be that there are more buys than sells already in your history and this is causing sells to be invoked. Or perhaps your orders aren't getting closed before you check your history.

Since you don't have control of the contents of the trade history, I'd suggest there is a much easier and more reliable way to do what you say you want to achieve.

Just have your EA maintain a flag variable which indicates what its previous trade direction was, then check the flag before trading, trade the opposite way and amend the flag.

In order to set the flag in the first place, you could either:

- Read the most recent item (at position 0) in the trade history

or

- Maintain the flag in a file which you read at initialization and write when you modify the flag

Oh, and by the way, when constructing loops to read through trades or trade history, always start at the top and work downwards - the oppostite to what you have done.

CB

Thanks a lot, i succeed !!!

Thanks for the advice CB, very helpfull

Reason: