Download MetaTrader 5

EA to close opposite loosing Trade

To add comments, please log in or register
Create Forum topics and improve your rating!
forextrader007
7
forextrader007 2013.11.27 18:19 

Dear Friends

I have a few EAs running as a part of my strategy. The problem I faced is there are situations when EA will open opposite trades in a single symbol.

I have coded a EA which will close out the loosing trade and let the winning trade run

I am a newbie in the coding arena and have managed to code the EA but there seems to be some errors so it is not compiling

Can you please correct the code for me

Thanks for all your replies in advance

Kind Regards

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

void start()

{

double Lots;

int op;

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

{

for (int j= OrdersTotal()-2; j>=0; j--)

{

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderSelect(j, SELECT_BY_POS, MODE_TRADES))

{

if (OrderSymbol(i)== OrderSymbol(j))

{

if( OrderProfit(i) < OrderProfit(j) && OrderProfit(i) < -15 && OrderProfit(j) > 0 )

{


op=OrderType(i);

if (op==OP_BUY)

{

Lots=OrderLots(i);

OrderClose(OrderTicket(i),OrderLots(i),Bid,7,CLR_NONE);

OrderSend(Symbol(i),OP_SELL,Lots,Bid,7,0,0,"Reverse",0,0,CLR_NONE);

}

if (op==OP_SELL)

{

Lots=OrderLots(i);

OrderClose(OrderTicket(i),OrderLots(i),Ask,7,CLR_NONE);

OrderSend(Symbol(i),OP_BUY,Lots,Ask,7,0,0,"Reverse",0,0,CLR_NONE);

}

}

elseif(OrderProfit(i) > OrderProfit(j) && OrderProfit(j) < -15 && OrderProfit(i) >0 )

{

op=OrderType(j);

if (op==OP_BUY)

{

Lots=OrderLots(j);

OrderClose(OrderTicket(j),OrderLots(j),Bid,7,CLR_NONE);

OrderSend(Symbol(j),OP_SELL,Lots,Bid,7,0,0,"Reverse",0,0,CLR_NONE);

}

if (op==OP_SELL)

{

Lots=OrderLots(j);

OrderClose(OrderTicket(j),OrderLots(j),Ask,7,CLR_NONE);

OrderSend(Symbol(j),OP_BUY,Lots,Ask,7,0,0,"Reverse",0,0,CLR_NONE);

}

}

else

Print("No Hedges") ;

}


}

}

}

}

whroeder1
14514
whroeder1 2013.11.27 19:01  
forextrader007: when EA will open opposite trades in a single symbol.

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. OrderSend(Symbol(j),OP_SELL,Lots,Bid,7,0,0,"Reverse",0,0,CLR_NONE);
    What are Function return values ? How do I use them ? - MQL4 forum
  3. The OrderSend will ALWAYS fail unless your "Symbol(j)" equals the chart's Symbol() You can't use any Predefined variables - MQL4 Documentation when trading other symbols.
  4. if (OrderSymbol(i)== OrderSymbol(j)) 
    RTFM none of those functions take a argument. OrderSelect one and save what you need THEN select the other.
  5. If you are using a US broker, hedging is forbidden - you can't open opposite trades - you need to close one and then open.
  6. KISS - only trade one symbol the chart's. Then you put the EA on other charts for other pairs.
To add comments, please log in or register