Hi..
I have been working with an indicator and was able to create an EA that opens an order based on the indicator which says "Buy" or "Sell".
I am not a programmer and was quite happy to see the EA place the order.
I can not however figure out how to close the order..
The EA is set for 25pip target & a 25pip stop loss. The target and stop loss works fine... I want the order to run until it either reaches profit, gets stopped out OR the indicator shows the opposite sign...
For example if I have a buy in place and am 6 pips in profit, I want to close the existing buy with the 6 pips if the indicator produces a "Sell" sign..
Any help would be greatly appreciatedHello,
here is a piece of code you may use which will close all open orders:
//----------------------- CLOSE ORDER FUNCTION
//----------------------- SOURCE: FIREDAVE
void subCloseOrder()
{
int
cnt,
total = 0,
ticket = 0,
err = 0,
c = 0;
total = OrdersTotal();
for(cnt=total-1;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol() &&
OrderMagicNumber()==MagicNumber)
{
switch(OrderType())
{
case OP_BUY :
for(c=0;c<10;c++)
{
ticket=OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
err=GetLastError();
if(err==0)
{
if(ticket>0) break;
}
else
{
if(err==0 || err==4 || err==136 || err==137 || err==138 || err==146) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
if(ticket>0) break;
}
}
}
break;
case OP_SELL :
for(c=0;c<10;c++)
{
ticket=OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
err=GetLastError();
if(err==0)
{
if(ticket>0) break;
}
else
{
if(err==0 || err==4 || err==136 || err==137 || err==138 || err==146) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
if(ticket>0) break;
}
}
}
break;
case OP_BUYLIMIT :
case OP_BUYSTOP :
case OP_SELLLIMIT:
case OP_SELLSTOP :
OrderDelete(OrderTicket());
}
}
}
}
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi..
I have been working with an indicator and was able to create an EA that opens an order based on the indicator which says "Buy" or "Sell".
I am not a programmer and was quite happy to see the EA place the order.
I can not however figure out how to close the order..
The EA is set for 25pip target & a 25pip stop loss. The target and stop loss works fine... I want the order to run until it either reaches profit, gets stopped out OR the indicator shows the opposite sign...
For example if I have a buy in place and am 6 pips in profit, I want to close the existing buy with the 6 pips if the indicator produces a "Sell" sign..
Any help would be greatly appreciated