This is going to be a long post with lots of lines of code...
I have existing code that works as intended that takes multiple positions and closes at hidden SL and TP levels.
I want to add to the existing code functions that allows the EA to close and reverse if signals are generated before the SL or TP is hit.
Here is the existing code that works well:
The above works great but I wanted to add a "close if opposite signal and reverse function"...
Although the EA enters with multiple magic numbers and exits hidden SLs and TPs with multiple magic numbers, I want it to exit all open positions if a contrary opposite signal is generated before the SL or TP is hit, and I want it to reverse and open a new series of opposite positions.
This is what I've tried so far but it doesn't work as expected...any other ideas? I'd like to use the existing framework...What am I doing wrong here?
The added code doesn't work as I thought. Any other workarounds?
I have existing code that works as intended that takes multiple positions and closes at hidden SL and TP levels.
I want to add to the existing code functions that allows the EA to close and reverse if signals are generated before the SL or TP is hit.
Here is the existing code that works well:
Inserted Code
//-------------------------------------------------- // TRADING PARAMETERS //-------------------------------------------------- RefreshRates(); { if (CUSTOM TRADING SIGNAL FOR BUY) { if((Hour()==0) && (Minute()==0)) Sleep(60000); { fBuy1(); fBuy2(); fBuy3(); fBuy4(); fBuy5(); fBuy6(); } } if (CUSTOM TRADING SIGNAL FOR SELL) { if((Hour()==0) && (Minute()==0)) Sleep(60000); { fSell1(); fSell2(); fSell3(); fSell4(); fSell5(); fSell6(); } } } } //-------------------------------------------------- // BUY //-------------------------------------------------- void fBuy1() { int result = OrderSend(ST1,OP_BUY,Lots,Ask,Slippage,0,0,"Open LONG Magic1",Magic1,0,Blue); OrderSelect(result, SELECT_BY_TICKET, MODE_TRADES); Print("BUY ORDER OPENED!", "#", result," at ", DoubleToStr(OrderOpenPrice(), Digits), ", SL: ", DoubleToStr(OrderStopLoss(), Digits), ", TP: ", DoubleToStr(OrderTakeProfit(), Digits), ", OpenTrades: ", OrdersTotal(), ", Close[1]: ", Close[1]); if (SignalMail) SendMail("EA OPEN TRADE ALERT!", "BUY ORDER OPENED!" + ST1 + ", #" + result + " at " + DoubleToStr(OrderOpenPrice(), Digits) + ", SL: " + DoubleToStr(OrderStopLoss(), Digits) + ", TP: " + DoubleToStr(OrderTakeProfit(), Digits)); if (result == -1) { int e = GetLastError(); Print(e); } } void fBuy2() { int result = OrderSend(ST1,OP_BUY,Lots,Ask,Slippage,0,0,"Open LONG Magic2",Magic2,0,Blue); OrderSelect(result, SELECT_BY_TICKET, MODE_TRADES); Print("BUY ORDER OPENED!", "#", result," at ", DoubleToStr(OrderOpenPrice(), Digits), ", SL: ", DoubleToStr(OrderStopLoss(), Digits), ", TP: ", DoubleToStr(OrderTakeProfit(), Digits), ", OpenTrades: ", OrdersTotal(), ", Close[1]: ", Close[1]); if (SignalMail) SendMail("EA OPEN TRADE ALERT!", "BUY ORDER OPENED!" + ST1 + ", #" + result + " at " + DoubleToStr(OrderOpenPrice(), Digits) + ", SL: " + DoubleToStr(OrderStopLoss(), Digits) + ", TP: " + DoubleToStr(OrderTakeProfit(), Digits)); if (result == -1) { int e = GetLastError(); Print(e); } } ETC...repeat code continues through VoidfBuy6 //-------------------------------------------------- // SELL //-------------------------------------------------- void fSell1() { int result = OrderSend(ST1,OP_SELL,Lots,Bid,Slippage,0,0,"Open SHORT Magic1",Magic1,0,Red); OrderSelect(result, SELECT_BY_TICKET, MODE_TRADES); Print("SELL ORDER OPENED!", "#", result," at ", DoubleToStr(OrderOpenPrice(), Digits), ", SL: ", DoubleToStr(OrderStopLoss(), Digits), ", TP: ", DoubleToStr(OrderTakeProfit(), Digits), ", OpenTrades: ", OrdersTotal(), ", Close[1]: ", Close[1]); if (SignalMail) SendMail("EA OPEN TRADE ALERT!", "SELL ORDER OPENED!" + ST1 + ", #" + result + " at " + DoubleToStr(OrderOpenPrice(), Digits) + ", SL: " + DoubleToStr(OrderStopLoss(), Digits) + ", TP: " + DoubleToStr(OrderTakeProfit(), Digits)); if (result == -1) { int e = GetLastError(); Print(e); } } void fSell2() { int result = OrderSend(ST1,OP_SELL,Lots,Bid,Slippage,0,0,"Open SHORT Magic2",Magic2,0,Red); OrderSelect(result, SELECT_BY_TICKET, MODE_TRADES); Print("SELL ORDER OPENED!", "#", result," at ", DoubleToStr(OrderOpenPrice(), Digits), ", SL: ", DoubleToStr(OrderStopLoss(), Digits), ", TP: ", DoubleToStr(OrderTakeProfit(), Digits), ", OpenTrades: ", OrdersTotal(), ", Close[1]: ", Close[1]); if (SignalMail) SendMail("EA OPEN TRADE ALERT!", "SELL ORDER OPENED!" + ST1 + ", #" + result + " at " + DoubleToStr(OrderOpenPrice(), Digits) + ", SL: " + DoubleToStr(OrderStopLoss(), Digits) + ", TP: " + DoubleToStr(OrderTakeProfit(), Digits)); if (result == -1) { int e = GetLastError(); Print(e); } } void fSell3() { int result = OrderSend(ST1,OP_SELL,Lots,Bid,Slippage,0,0,"Open SHORT Magic3",Magic3,0,Red); OrderSelect(result, SELECT_BY_TICKET, MODE_TRADES); Print("SELL ORDER OPENED!", "#", result," at ", DoubleToStr(OrderOpenPrice(), Digits), ", SL: ", DoubleToStr(OrderStopLoss(), Digits), ", TP: ", DoubleToStr(OrderTakeProfit(), Digits), ", OpenTrades: ", OrdersTotal(), ", Close[1]: ", Close[1]); if (SignalMail) SendMail("EA OPEN TRADE ALERT!", "SELL ORDER OPENED!" + ST1 + ", #" + result + " at " + DoubleToStr(OrderOpenPrice(), Digits) + ", SL: " + DoubleToStr(OrderStopLoss(), Digits) + ", TP: " + DoubleToStr(OrderTakeProfit(), Digits)); if (result == -1) { int e = GetLastError(); Print(e); } } ETC...repeat code continues through VoidfSell6 //-------------------------------------------------- // HIDDEN SL TP FUNCTION //-------------------------------------------------- void hidden_stop_loss() { int totalorders = OrdersTotal(); for(int i=totalorders-1;i>=0;i--) { OrderSelect(i, SELECT_BY_POS); bool result = false; { if (OrderMagicNumber() == Magic1) { if (OrderType() == OP_BUY && iClose(Symbol(),1,0) < NormalizeDouble(OrderOpenPrice()-((OrderOpenPrice()/100)*StopLossLong1),3) ) result = OrderClose( OrderTicket(), OrderLots(), Bid, 50, Red ); if (OrderType() == OP_SELL && iClose(Symbol(),1,0) > NormalizeDouble(OrderOpenPrice()+((OrderOpenPrice()/100)*StopLossShort1),3) ) result = OrderClose( OrderTicket(), OrderLots(), Ask, 50, Red ); } if (OrderMagicNumber() == Magic2) { if (OrderType() == OP_BUY && iClose(Symbol(),1,0) < NormalizeDouble(OrderOpenPrice()-((OrderOpenPrice()/100)*StopLossLong2),3) ) result = OrderClose( OrderTicket(), OrderLots(), Bid, 50, Red ); if (OrderType() == OP_SELL && iClose(Symbol(),1,0) > NormalizeDouble(OrderOpenPrice()+((OrderOpenPrice()/100)*StopLossShort2),3) ) result = OrderClose( OrderTicket(), OrderLots(), Ask, 50, Red ); } if (OrderMagicNumber() == Magic3) { if (OrderType() == OP_BUY && iClose(Symbol(),1,0) < NormalizeDouble(OrderOpenPrice()-((OrderOpenPrice()/100)*StopLossLong3),3) ) result = OrderClose( OrderTicket(), OrderLots(), Bid, 50, Red ); if (OrderType() == OP_SELL && iClose(Symbol(),1,0) > NormalizeDouble(OrderOpenPrice()+((OrderOpenPrice()/100)*StopLossShort3),3) ) result = OrderClose( OrderTicket(), OrderLots(), Ask, 50, Red ); } ETC...repeat code continues through Magic6 } } return; } void hidden_take_profit() { int totalorders = OrdersTotal(); for(int i=totalorders-1;i>=0;i--) { OrderSelect(i, SELECT_BY_POS); bool result = false; { if (OrderMagicNumber() == Magic1) { if (OrderType() == OP_BUY && iClose(Symbol(),1,0) > NormalizeDouble(OrderOpenPrice()+((OrderOpenPrice()/100)*TakeProfitLong1),3) ) result = OrderClose( OrderTicket(), OrderLots(), Bid, 50, Red ); if (OrderType() == OP_SELL && iClose(Symbol(),1,0) < NormalizeDouble(OrderOpenPrice()-((OrderOpenPrice()/100)*TakeProfitShort1),3) ) result = OrderClose( OrderTicket(), OrderLots(), Ask, 50, Red ); } if (OrderMagicNumber() == Magic2) { if (OrderType() == OP_BUY && iClose(Symbol(),1,0) > NormalizeDouble(OrderOpenPrice()+((OrderOpenPrice()/100)*TakeProfitLong2),3) ) result = OrderClose( OrderTicket(), OrderLots(), Bid, 50, Red ); if (OrderType() == OP_SELL && iClose(Symbol(),1,0) < NormalizeDouble(OrderOpenPrice()-((OrderOpenPrice()/100)*TakeProfitShort2),3) ) result = OrderClose( OrderTicket(), OrderLots(), Ask, 50, Red ); } if (OrderMagicNumber() == Magic3) { if (OrderType() == OP_BUY && iClose(Symbol(),1,0) > NormalizeDouble(OrderOpenPrice()+((OrderOpenPrice()/100)*TakeProfitLong3),3) ) result = OrderClose( OrderTicket(), OrderLots(), Bid, 50, Red ); if (OrderType() == OP_SELL && iClose(Symbol(),1,0) < NormalizeDouble(OrderOpenPrice()-((OrderOpenPrice()/100)*TakeProfitShort3),3) ) result = OrderClose( OrderTicket(), OrderLots(), Ask, 50, Red ); } ETC...repeat code continues through Magic6 } } return; }
The above works great but I wanted to add a "close if opposite signal and reverse function"...
Although the EA enters with multiple magic numbers and exits hidden SLs and TPs with multiple magic numbers, I want it to exit all open positions if a contrary opposite signal is generated before the SL or TP is hit, and I want it to reverse and open a new series of opposite positions.
This is what I've tried so far but it doesn't work as expected...any other ideas? I'd like to use the existing framework...What am I doing wrong here?
Inserted Code
{ if (CUSTOM TRADING SIGNAL FOR BUY) { if((Hour()==0) && (Minute()==0)) Sleep(60000); { CloseSell(); <<----- This was added thinking it would work logically...additional logic below Sleep(10000); <<----- added to give a buffer to close existing positions before executing new ones don't think it affects the exit fBuy1(); fBuy2(); fBuy3(); fBuy4(); fBuy5(); fBuy6(); } etc... //-------------------------------------------------- // CLOSE EXISTING FUNCTION BEFORE fOpenPostions //-------------------------------------------------- void CloseSell() { int totalorders = OrdersTotal(); for(int i=totalorders-1;i>=0;i--) { OrderSelect(i, SELECT_BY_POS); bool result = false; { if (OrderType() == OP_SELL) { result = OrderClose( OrderTicket(), OrderLots(), Ask, 50, Red ); } } } } void CloseBuy() { int totalorders = OrdersTotal(); for(int i=totalorders-1;i>=0;i--) { OrderSelect(i, SELECT_BY_POS); bool result = false; { if (OrderType() == OP_BUY) { result = OrderClose( OrderTicket(), OrderLots(), Bid, 50, Red ); } } } // return; }
The added code doesn't work as I thought. Any other workarounds?