OrderClose function, Help!

 

How can I put in these variables in a OrderClose function, closeallsell and closeallbuy?

int shift,BBtrend1;

int matrend1;

double buy1[25000],sell1[25000],bbuy1[25000],bsell1[25000];

double ma1,ma2;

for (shift=Nbars;shift>=0;shift--)

for (shift=Nbars-Length-1;shift>=0;shift--)

(

buy1[shift]=iBands(NULL,PERIOD_M30,Length,Deviation,0,PRICE_CLOSE,MODE_UPPER,shift);

sell1[shift]=iBands(NULL,PERIOD_M30,Length,Deviation,0,PRICE_CLOSE,MODE_LOWER,shift);

if (Close[shift]>buy1[shift+1]) BBtrend1=1;

if (Close[shift]<sell1[shift+1]) BBtrend1=-1;

if(BBtrend1>0 && sell1[shift]<sell1[shift+1]) sell1[shift]=sell1[shift+1];

if(BBtrend1buy1[shift+1]) buy1[shift]=buy1[shift+1];

bbuy1[shift]=buy1[shift]+0.5*(MoneyRisk-1)*(buy1[shift]-sell1[shift]);

bsell1[shift]=sell1[shift]-0.5*(MoneyRisk-1)*(buy1[shift]-sell1[shift]);

if(BBtrend1>0 && bsell1[shift]<bbuy1[shift+1]) bbuy1[shift]=bsell1[shift+1];

if(BBtrend1bbuy1[shift+1]) bbuy1[shift]=bsell1[shift+1];

ma1=iMA(NULL,PERIOD_H4,9,0,MODE_SMA,PRICE_CLOSE,0);

ma2=iMA(NULL,PERIOD_H4,20,0,MODE_SMA,PRICE_CLOSE,0);

if(ma1>ma2) matrend1= 1;

if(ma1<ma2) matrend1= -1;

)

The variables I want to closeallsell orders are:

((BBtrend1>0) && (matrend1<1))

The variables I want to closeallbuy orders are:

((BBtrend1>0) && (matrend1<1))

//Thomas

 
OneandOnly666:
How can I put in these variables in a OrderClose function, closeallsell and closeallbuy?

int shift,BBtrend1;

int matrend1;

double buy1[25000],sell1[25000],bbuy1[25000],bsell1[25000];

double ma1,ma2;

for (shift=Nbars;shift>=0;shift--)

for (shift=Nbars-Length-1;shift>=0;shift--)

(

buy1[shift]=iBands(NULL,PERIOD_M30,Length,Deviation,0,PRICE_CLOSE,MODE_UPPER,shift);

sell1[shift]=iBands(NULL,PERIOD_M30,Length,Deviation,0,PRICE_CLOSE,MODE_LOWER,shift);

if (Close[shift]>buy1[shift+1]) BBtrend1=1;

if (Close[shift]<sell1[shift+1]) BBtrend1=-1;

if(BBtrend1>0 && sell1[shift]<sell1[shift+1]) sell1[shift]=sell1[shift+1];

if(BBtrend1buy1[shift+1]) buy1[shift]=buy1[shift+1];

bbuy1[shift]=buy1[shift]+0.5*(MoneyRisk-1)*(buy1[shift]-sell1[shift]);

bsell1[shift]=sell1[shift]-0.5*(MoneyRisk-1)*(buy1[shift]-sell1[shift]);

if(BBtrend1>0 && bsell1[shift]<bbuy1[shift+1]) bbuy1[shift]=bsell1[shift+1];

if(BBtrend1bbuy1[shift+1]) bbuy1[shift]=bsell1[shift+1];

ma1=iMA(NULL,PERIOD_H4,9,0,MODE_SMA,PRICE_CLOSE,0);

ma2=iMA(NULL,PERIOD_H4,20,0,MODE_SMA,PRICE_CLOSE,0);

if(ma1>ma2) matrend1= 1;

if(ma1<ma2) matrend1= -1;

)

The variables I want to closeallsell orders are:

((BBtrend1>0) && (matrend1<1))

The variables I want to closeallbuy orders are:

((BBtrend1>0) && (matrend1<1))

//Thomas

Thomas

Simply add OrderClose() function after the final two conditions (after each of the conditions)

 
mladen:
Thomas Simply add OrderClose() function after the final two conditions (after each of the conditions)

Ok, so it should be something like this:

if ((BBtrend1>0) && (matrend1<1))

(

RefreshRates();

OrderClose(OrderTicket(),OrderLots(),

NormalizeDouble(MarketInfo(OrderSymbol(),

MODE_ASK),MarketInfo(OrderSymbol(),MODE_DIGITS)),Slippage,White);

if ((BBtrend1>0) && (matrend1<1))

RefreshRates();

OrderClose(OrderTicket(),OrderLots(),

NormalizeDouble(MarketInfo(OrderSymbol(),

MODE_BID),MarketInfo(OrderSymbol(),MODE_DIGITS)),Slippage,White);

)

Should it be put in the ending of the int start function, after the ordersend functions?

Or is it a standalone function?

//Thomas

 
OneandOnly666:
Ok, so it should be something like this:

if ((BBtrend1>0) && (matrend1<1))

(

RefreshRates();

OrderClose(OrderTicket(),OrderLots(),

NormalizeDouble(MarketInfo(OrderSymbol(),

MODE_ASK),MarketInfo(OrderSymbol(),MODE_DIGITS)),Slippage,White);

if ((BBtrend1>0) && (matrend1<1))

RefreshRates();

OrderClose(OrderTicket(),OrderLots(),

NormalizeDouble(MarketInfo(OrderSymbol(),

MODE_BID),MarketInfo(OrderSymbol(),MODE_DIGITS)),Slippage,White);

)

Should it be put in the ending of the int start function, after the ordersend functions?

Or is it a standalone function?

//Thomas

Thomas

You have to select the order you wish to close first (using OrderSelect() function) and only then you can close it the way you wrote in the code.

As of where should it be : sorry, but it entirely depends on the way how you want your EA to work and you should place that code accordingly. There is no general rule for that

 
mladen:
Thomas

You have to select the order you wish to close first (using OrderSelect() function) and only then you can close it the way you wrote in the code.

As of where should it be : sorry, but it entirely depends on the way how you want your EA to work and you should place that code accordingly. There is no general rule for that

So I should type

for (int i = 0; i < OrdersTotal(); i++) {

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

while(IsTradeContextBusy()) Sleep(100);

but do I have to type void closeallsell and void closeallbuy to

and then a bool action before the orderselect?

Was thinking of putting the code in the end of the int start function. Before the return(0)

Is this right?

 
OneandOnly666:
So I should type

for (int i = 0; i < OrdersTotal(); i++) {

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

while(IsTradeContextBusy()) Sleep(100);

but do I have to type void closeallsell and void closeallbuy to

and then a bool action before the orderselect?

Was thinking of putting the code in the end of the int start function. Before the return(0)

Is this right?

Thomas

Before you close an order you must select it

Otherwise you may end up in error or you may end up closing the order that you do not want to close. So first selsect an order, see if it is the one you wish to close and then, if the other conditions are right, close the order.

 

Ive tried different things.

sometimes it only makes sell orders and doesnt stop the orders as it should.

Now i have made some changes but now it gets orderclose error 4051 and invalid ticket for orderclose function.

here is the code

int total = OrdersTotal();

for(int i=total-1;i>=0;i--)

OrderSelect(i, SELECT_BY_POS);

int type = OrderType();

if (OrderMagicNumber()!=MagicNumber2 || OrderSymbol()!=Symbol())

{

RefreshRates();

if (OrderType()==OP_BUY)

{

if ((BBtrend1>0) && (matrend1<1))

{

OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits), Slippage);

}

}

}

{

OrderSelect(i, SELECT_BY_POS);

if (OrderMagicNumber()!=MagicNumber1 || OrderSymbol()!=Symbol())

{

RefreshRates();

if (OrderType()==OP_SELL)

{

if ((BBtrend10))

{

OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits), Slippage);

}

}

}

}

return (0);

}

}

what am I doing wrong?

 
OneandOnly666:
Ive tried different things.

sometimes it only makes sell orders and doesnt stop the orders as it should.

Now i have made some changes but now it gets orderclose error 4051 and invalid ticket for orderclose function.

here is the code

int total = OrdersTotal();

for(int i=total-1;i>=0;i--)

OrderSelect(i, SELECT_BY_POS);

int type = OrderType();

if (OrderMagicNumber()!=MagicNumber2 || OrderSymbol()!=Symbol())

{

RefreshRates();

if (OrderType()==OP_BUY)

{

if ((BBtrend1>0) && (matrend1<1))

{

OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits), Slippage);

}

}

}

{

OrderSelect(i, SELECT_BY_POS);

if (OrderMagicNumber()!=MagicNumber1 || OrderSymbol()!=Symbol())

{

RefreshRates();

if (OrderType()==OP_SELL)

{

if ((BBtrend10))

{

OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits), Slippage);

}

}

}

}

return (0);

}

}

what am I doing wrong?

Try like this :

for(int i=OrdersTotal()-1;i>=0;i--)

{

OrderSelect(i, SELECT_BY_POS);

int type = OrderType();

if (OrderMagicNumber()!=MagicNumber2 || OrderSymbol()!=Symbol())

{

if (type==OP_BUY)

if ((BBtrend1>0) && (matrend1<1))

{

RefreshRates();

OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits), Slippage);

}

if (type==OP_SELL)

if ((BBtrend10))

{

RefreshRates();

OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits), Slippage);

}

}

}

 
mladen:
Try like this :
for(int i=OrdersTotal()-1;i>=0;i--)

{

OrderSelect(i, SELECT_BY_POS);

int type = OrderType();

if (OrderMagicNumber()!=MagicNumber2 || OrderSymbol()!=Symbol())

{

if (type==OP_BUY)

if ((BBtrend1>0) && (matrend1<1))

{

RefreshRates();

OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits), Slippage);

}

if (type==OP_SELL)

if ((BBtrend10))

{

RefreshRates();

OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits), Slippage);

}

}

}

still the same problem, orderclose error. But it will depend on where i put the

for (int i=OrdersTotal()-1;i>=0;i--)

If I put it together with the rest of the code you gave me it go very slow in the backtest. Takes about 10 minutes to get a couple of hours.Dont know yet if it works there because of the slow backtest.

But if I put it in the beginning of the int start (before all ordersend functions) with the other int and for codes, it will give orderclose error again.

Why is there a difference.

Is the rest of my code wrong maybe?

if (indicator data... && (BBtrend1<1 )&& (matrend1<1))

{

if(OrderSend(Symbol(), OP_SELL, Lots,Bid, Slippage, Ask + SL * Point, Bid - TP * Point, indicator info..., 0, 0,MagicNumber2) > 0)

{

Print("...");

indicator nr = TRUE;

PlaySound("expert.wav");

}

}

 
OneandOnly666:
still the same problem, orderclose error. But it will depend on where i put the

for (int i=OrdersTotal()-1;i>=0;i--)

If I put it together with the rest of the code you gave me it go very slow in the backtest. Takes about 10 minutes to get a couple of days. But if I put it in the beginning of the int start (before all ordersend functions) with the other int and for codes, it will give orderclose error again.

Why is there a difference.

Is the rest of my code wrong maybe?

if (indicator data... && (BBtrend1<1 )&& (matrend1<1))

{

if(OrderSend(Symbol(), OP_SELL, Lots,Bid, Slippage, Ask + SL * Point, Bid - TP * Point, indicator info..., 0, 0,MagicNumber2) > 0)

{

Print("...");

indicator nr = TRUE;

PlaySound("expert.wav");

}

}

OneandOnly666

If you wish to check all opened orders and to close some depending on the conditions, you have to do it as it is in the code attached (OrderSelect() must be kept in a loop, not as you placed it outside the loop and then you tried to close an non-existing order).

As of the rest : I have no idea how the rest of your code looks like (I can not guess from the parts that you are posting) so I can not talk about it. Check the rest of your code (and the logic of it) since there is no faster way of closing a particular currently opened order

 
mladen:
OneandOnly666

If you wish to check all opened orders and to close some depending on the conditions, you have to do it as it is in the code attached (OrderSelect() must be kept in a loop, not as you placed it outside the loop and then you tried to close an non-existing order).

As of the rest : I have no idea how the rest of your code looks like (I can not guess from the parts that you are posting) so I can not talk about it. Check the rest of your code (and the logic of it) since there is no faster way of closing a particular currently opened order

Ok, thanks.

By the way, what was I doing wrong in the first orderclose code I sent you?