Help to add MaxOpenOrder code please

 

Hello, just wondering if someone could add for me code to this EA for MaxOpenOrders so I can limit the amount of max trades EA is allowed to open.

Thank you

//+------------------------------------------------------------------+

//| MA envelope exhausting system |

//+------------------------------------------------------------------+

//----------------------- USER INPUT

extern double MA_length = 10;

extern double slip = 0;

extern double Lots = 1;

extern double TakeProfit = 30;

extern double Stoploss = 2000;

extern double PipStep = 30;

//----------------------- SETUP VARS

double PriceTarget;

double AveragePrice;

double LastPrice;

int flag;

//----------------------- MAIN PROGRAM LOOP

int start()

{

int cnt=0, total;

double Stopper=0;

total=OrdersTotal();

OrderSelect(total, SELECT_BY_POS, MODE_TRADES);

LastPrice=OrderOpenPrice();

OrderSelect(total, SELECT_BY_POS, MODE_TRADES);

flag=0;

if(iMA(NULL,0,MA_length,0,MODE_SMA,PRICE_OPEN,0)*1.02=(LastPrice+(PipStep*Point)))||(total Only sell if >= 30 pips above previous position entry

{

OrderSend(Symbol(),OP_SELL,Lots,Bid,slip,Bid+(Stoploss*Point),Bid-(TakeProfit*Point),0,0,HotPink);

if(total>0)flag=1;

}

if(iMA(NULL,0,MA_length,0,MODE_SMA,PRICE_OPEN,0)*0.98>Ask && (Ask<=(LastPrice-(PipStep*Point)))||total Only buy if >= 30 pips below previous position entry

{

OrderSend(Symbol(),OP_BUY,Lots,Ask,slip,Ask-(Stoploss*Point),Ask+(TakeProfit*Point),0,0,Lime);

if(total>0)flag=1;

}

//----------------------- CALCULATE AVERAGE OPENING PRICE

total=OrdersTotal();

AveragePrice=0;

for(cnt=0;cnt<total;cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

AveragePrice=AveragePrice+OrderOpenPrice();

}

if (OrdersTotal() > 0)

AveragePrice=AveragePrice/total;

//----------------------- RECALCULATE STOPLOSS & PROFIT TARGET BASED ON AVERAGE OPENING PRICE

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())

{

flag=1;

if(OrderType()==OP_BUY) // Calculate profit/stop target for long

{

PriceTarget=AveragePrice+(TakeProfit*Point);

Stopper=AveragePrice-(Stoploss*Point);

}

else // Calculate profit/stop target for short

{

PriceTarget=AveragePrice-(TakeProfit*Point);

Stopper=AveragePrice+(Stoploss*Point);

}

}

//----------------------- IF NEEDED CHANGE ALL OPEN ORDERS TO NEWLY CALCULATED PROFIT TARGET

if(flag==1)// check if average has really changed

{

for(cnt=0;cnt<total;cnt++)

{

PriceTarget=total;

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

OrderModify(OrderTicket(),0,Stopper,PriceTarget,0,Yellow);// set all positions to averaged levels

}

}

}

 
matrixebiz:
Hello, just wondering if someone could add for me code to this EA for MaxOpenOrders so I can limit the amount of max trades EA is allowed to open.

Thank you

There seems to be several mistakes is this EA, here is a cleaner code (not tested)

//+------------------------------------------------------------------+

//| MA envelope exhausting system |

//+------------------------------------------------------------------+

//----------------------- USER INPUT

extern double MA_length = 10;

extern double slip = 0;

extern double Lots = 1;

extern double TakeProfit = 30;

extern double Stoploss = 2000;

extern double PipStep = 30

extern int MaxOpenOrders = 5;

//----------------------- MAIN PROGRAM LOOP

int start()

{

int total=0, cnt, dir=0, ticket=0;

double Stopper=0, LastPrice=0, AveragePrice=0, PriceTarget=0;

double SMA=iMA(NULL,0,MA_length,0,MODE_SMA,PRICE_OPEN,0);

for(cnt=OrdersTotal()-1;cnt>=0;cnt--)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderSymbol()!=Symbol()) continue;

dir=1-OrderType()*2;

total++;

if(OrderTicket()>ticket)

{

ticket=OrderTicket();

LastPrice=OrderOpenPrice();

}

}

if(total>=MaxOpenOrders) return(0);

ticket=0;

if(SMA*1.02=LastPrice+PipStep*Point)||total Only sell if >= 30 pips above previous position entry

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,slip,Bid+(Stoploss*Point),Bid-(TakeProfit*Point),0,0,HotPink);

if(SMA*0.98>Ask && ((dir==1 && Ask<=LastPrice-PipStep*Point)||total Only buy if >= 30 pips below previous position entry

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,slip,Ask-(Stoploss*Point),Ask+(TakeProfit*Point),0,0,Lime);

if(ticket==0) return(0);

if(total<1) return(0);

//----------------------- CALCULATE AVERAGE OPENING PRICE

total=0;

for(cnt=0;cnt<OrdersTotal();cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderSymbol()!=Symbol()) continue;

total++;

AveragePrice+=OrderOpenPrice();

}

AveragePrice/=total;

//----------------------- RECALCULATE STOPLOSS & PROFIT TARGET BASED ON AVERAGE OPENING PRICE

PriceTarget=AveragePrice+TakeProfit*Point*dir;

Stopper=AveragePrice-Stoploss*Point*dir;

//----------------------- IF NEEDED CHANGE ALL OPEN ORDERS TO NEWLY CALCULATED PROFIT TARGET

for(cnt=0;cnt<OrderTotal;cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderSymbol()!=Symbol()) continue;

OrderModify(OrderTicket(),0,Stopper,PriceTarget,0,Yellow);// set all positions to averaged levels

}

}
 
Michel:
There seems to be several mistakes is this EA, here is a cleaner code (not tested)
//+------------------------------------------------------------------+

//| MA envelope exhausting system |

//+------------------------------------------------------------------+

//----------------------- USER INPUT

extern double MA_length = 10;

extern double slip = 0;

extern double Lots = 1;

extern double TakeProfit = 30;

extern double Stoploss = 2000;

extern double PipStep = 30

extern int MaxOpenOrders = 5;

//----------------------- MAIN PROGRAM LOOP

int start()

{

int total=0, cnt, dir=0, ticket=0;

double Stopper=0, LastPrice=0, AveragePrice=0, PriceTarget=0;

double SMA=iMA(NULL,0,MA_length,0,MODE_SMA,PRICE_OPEN,0);

for(cnt=OrdersTotal()-1;cnt>=0;cnt--)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderSymbol()!=Symbol()) continue;

dir=1-OrderType()*2;

total++;

if(OrderTicket()>ticket)

{

ticket=OrderTicket();

LastPrice=OrderOpenPrice();

}

}

if(total>=MaxOpenOrders) return(0);

ticket=0;

if(SMA*1.02=LastPrice+PipStep*Point)||total Only sell if >= 30 pips above previous position entry

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,slip,Bid+(Stoploss*Point),Bid-(TakeProfit*Point),0,0,HotPink);

if(SMA*0.98>Ask && ((dir==1 && Ask<=LastPrice-PipStep*Point)||total Only buy if >= 30 pips below previous position entry

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,slip,Ask-(Stoploss*Point),Ask+(TakeProfit*Point),0,0,Lime);

if(ticket==0) return(0);

if(total<1) return(0);

//----------------------- CALCULATE AVERAGE OPENING PRICE

total=0;

for(cnt=0;cnt<OrdersTotal();cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderSymbol()!=Symbol()) continue;

total++;

AveragePrice+=OrderOpenPrice();

}

AveragePrice/=total;

//----------------------- RECALCULATE STOPLOSS & PROFIT TARGET BASED ON AVERAGE OPENING PRICE

PriceTarget=AveragePrice+TakeProfit*Point*dir;

Stopper=AveragePrice-Stoploss*Point*dir;

//----------------------- IF NEEDED CHANGE ALL OPEN ORDERS TO NEWLY CALCULATED PROFIT TARGET

for(cnt=0;cnt<OrderTotal;cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderSymbol()!=Symbol()) continue;

OrderModify(OrderTicket(),0,Stopper,PriceTarget,0,Yellow);// set all positions to averaged levels

}

}

Thank you when I compile I get an error. Had multiple errors before I added the ; on the pipstep line then now the only error I get is;

'OrderTotal' - variable not defined C:\Documents and Settings\!.mq4 (61, 18)

also can I add an 'extern double Percent = 0.3;' line

then change the trade code to look like;

if(SMA*(1+Percent/100))>Ask && ((dir==1 && Ask<=LastPrice-PipStep*Point)||total Only buy if >= 30 pips below previous position entry[/code]

instead of;

[code]

if(SMA*0.98>Ask && ((dir==1 && Ask<=LastPrice-PipStep*Point)||total Only buy if >= 30 pips below previous position entry

Thanks and WOW you pretty much changed the whole EA

also, is the StopLoss setting for all open orders added together or for each individual order? If EA is set to close all orders when total SL is hit, can you change it so that SL is for each separate order.

Thanks again

 

OrderTotal

Actually is

OrdersTotal

 
Shinigami:
OrderTotal

Actually is

OrdersTotal

That was it plus it was missing the ()

Fixed - OrdersTotal()

Thanks

 
matrixebiz:
That was it plus it was missing the ()

Fixed - OrdersTotal()

Thanks

Sorry for the two mistakes, I am in hollyday and cannot check the code from here.

I made two modifications :

1) the EA use now only the positions of the pair on which he is attached.

2) the EA keep the direction of the first position (of that pair). This is needed is some TP / SL / entry configuration where the EA may open buys and sells simultaneously; then the average price, new TP and SL have no meanings anymore. It is possible to write an EA working in both direction at the same time but it's much more complex. The simplest way to do that is to work with two EAs with different MagicNumbers and only long / only short. If you need it, tell me if you want any help.

The other modifs are only the right way to achieve the same goal. For example, one cannot assume that the last order is indexed by OrderTotal(): first it should be OrderTotal()-1, but this may also be wrong because it depends of the sorted collumn of the "trade" tab of the terminal or if you have opened positions on other pairs.

I tryed to keep most of your code, and I keep also most of it's lack, for example, don't play with manual pendings on the same pair: all will be wrong...

It will be possible to improve the security if you find that this basic EA is profitable.

"also, is the StopLoss setting for all open orders added together or for each individual order? If EA is set to close all orders when total SL is hit, can you change it so that SL is for each separate order."

Now the SL is based on the average price (as I understood your code). If you do not want to have a general SL, just don't modify them. Then the "modify" line should only modify the TP :

OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),PriceTarget,0,Yellow);

BTW, I am not sure that "0" as OpenPrice() is correct in the curent line.

 
Michel:
Sorry for the two mistakes, I am in hollyday and cannot check the code from here.

I made two modifications :

1) the EA use now only the positions of the pair on which he is attached.

2) the EA keep the direction of the first position (of that pair). This is needed is some TP / SL / entry configuration where the EA may open buys and sells simultaneously; then the average price, new TP and SL have no meanings anymore. It is possible to write an EA working in both direction at the same time but it's much more complex. The simplest way to do that is to work with two EAs with different MagicNumbers and only long / only short. If you need it, tell me if you want any help.

The other modifs are only the right way to achieve the same goal. For example, one cannot assume that the last order is indexed by OrderTotal(): first it should be OrderTotal()-1, but this may also be wrong because it depends of the sorted collumn of the "trade" tab of the terminal or if you have opened positions on other pairs.

I tryed to keep most of your code, and I keep also most of it's lack, for example, don't play with manual pendings on the same pair: all will be wrong...

It will be possible to improve the security if you find that this basic EA is profitable.

"also, is the StopLoss setting for all open orders added together or for each individual order? If EA is set to close all orders when total SL is hit, can you change it so that SL is for each separate order."

Now the SL is based on the average price (as I understood your code). If you do not want to have a general SL, just don't modify them. Then the "modify" line should only modify the TP :

OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),PriceTarget,0,Yellow);
BTW, I am not sure that "0" as OpenPrice() is correct in the curent line.

Ok, not sure what you said about the SL ? is it for each order?

so are you also saying that the EA cannot place a buy and sell with the same currency at the same time untill one of them is closed? can that be fixed? also, how do you add a little bullet (graphic) on the chart that shows when the order was placed?

Thanks

 
matrixebiz:
Ok, not sure what you said about the SL ? is it for each order?

so are you also saying that the EA cannot place a buy and sell with the same currency at the same time untill one of them is closed? can that be fixed? also, how do you add a little bullet (graphic) on the chart that shows when the order was placed?

Thanks

About the SL, both ideas may work:

if you want a general SL, ie the same level for all positions so they are all closing at the same time, use this line (This was the way your original code was intended to work):

OrderModify(OrderTicket(),OrderOpenPrice(),Stopper,PriceTarget,0,Yellow);

[/PHP]

But if you want that each order keeps it's SL from it's openning, use this line :[PHP]OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),PriceTarget,0,Yellow);

In this second case, each order may hit its own SL separetaly; but keep in mind that the EA should then be more complex to work properly: if a position is closed by SL, the average price becomes wrong, so you have to recompute it at each tick and check if the other positions need to be modified.

About to buy and sell at the same time, you must understand that the average price, the SL and TP as they were defined in your code have no meaning if the directions are mixed. Just look at this example:

situation 1: you have a buy at 1.200 and a sell at 1.220;

situation 2: a buy at 1.200 and a buy at 1.220;

Your original code makes no distinction between both situations, but they are really different...

That's why I suggest you to add a MagicNumber to the EA and to work on one chart with the EA to open only the buys and on an other chart with the same EA but with another MagicNumber to open only the sells.

 
Michel:
About the SL, both ideas may work:

if you want a general SL, ie the same level for all positions so they are all closing at the same time, use this line (This was the way your original code was intended to work):

OrderModify(OrderTicket(),OrderOpenPrice(),Stopper,PriceTarget,0,Yellow);

[/PHP]

But if you want that each order keeps it's SL from it's openning, use this line :[PHP]OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),PriceTarget,0,Yellow);

In this second case, each order may hit its own SL separetaly; but keep in mind that the EA should then be more complex to work properly: if a position is closed by SL, the average price becomes wrong, so you have to recompute it at each tick and check if the other positions need to be modified.

About to buy and sell at the same time, you must understand that the average price, the SL and TP as they were defined in your code have no meaning if the directions are mixed. Just look at this example:

situation 1: you have a buy at 1.200 and a sell at 1.220;

situation 2: a buy at 1.200 and a buy at 1.220;

Your original code makes no distinction between both situations, but they are really different...

That's why I suggest you to add a MagicNumber to the EA and to work on one chart with the EA to open only the buys and on an other chart with the same EA but with another MagicNumber to open only the sells.

Ok, I don't mind then to only have one order per currency open at one time. So in effect there was no need to add MaxOpenOrders correct? since the EA will only open one order at a time. or will it open two or more orders but only in one direction (sells or buys) until all uni-positions are closed?

If you have time can you fix it so that it will recompute when I change the SL code as you mentioned plus work with multiple direction trades without using a magic number.

Thanks alot

 
...or will it open two or more orders but only in one direction (sells or buys) until all uni-positions are closed?

Yes, that's true. You can easily check it !

 

if any ask me more than 888 Private messages

i'll do multiple position/order/open position

backtesting

with Visual Basic

Michel:
Yes, that's true. You can easily check it !