Having a problem with my EA. It will open trades fine when just using a trailing stop, but when I try to use a trailing stop and and stoploss together it will not open trades. It works fine this way on the backtests, but will not open a real trade on the chart. [...]
This sounds like https://www.mql5.com/en/forum/120382
Thanks for the link jjc! Could you help me work that into my code?
I may be wrong about the cause of your problem. There are other possible causes. If I'm right, then you need to modify your code along the following lines (making the same kind of adjustment to the block of code which places the sell orders):
Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue); if(Ticket > 0) { if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) { if (OrderModify(OrderTicket(), OrderOpenPrice(), StopLossLevel, TakeProfitLevel, OrderExpiration())) { Print("BUY order opened : ", OrderOpenPrice()); if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy"); } else { // TODO Add whatever handling you think appropriate here Alert("Critical error: stoploss failed. Order is UNPROTECTED in market"); } } else { Print("Error opening BUY order : ", GetLastError()); } }
There are three extra things now going on in this code:
- The s/l and t/p parameters for OrderSend() are specified as zero.
- If the order is successfully placed, the t/p and s/l are then set using OrderModify()
- If the OrderModify() command fails, you have an unprotected trade in the market which has no stop loss. You need to decide how you want to handle this eventuality. For example, you could keep retrying a certain number of times, or you could close the opening order to protect yourself. It's up to you.
I may be wrong about the cause of your problem. There are other possible causes. If I'm right, then you need to modify your code along the following lines (making the same kind of adjustment to the block of code which places the sell orders):
There are three extra things now going on in this code:
- The s/l and t/p parameters for OrderSend() are specified as zero.
- If the order is successfully placed, the t/p and s/l are then set using OrderModify()
- If the OrderModify() command fails, you have an unprotected trade in the market which has no stop loss. You need to decide how you want to handle this eventuality. For example, you could keep retrying a certain number of times, or you could close the opening order to protect yourself. It's up to you.
Thanks jjc, I'm running the EA with the new coding now. I will let you know if it works when the next trade hits.
**EDIT**
That looks like it got it working! Thanks jjc!
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Having a problem with my EA. It will open trades fine when just using a trailing stop, but when I try to use a trailing stop and and stoploss together it will not open trades. It works fine this way on the backtests, but will not open a real trade on the chart. I'm trying to run a trailing stop of 5 pips and a stoploss of 200 pips. Any thoughts? I checked the Journal and Expert Advisor tab, no errors. Just not opening trades.
Here is the trailing stop code and the buy code, I think the prob may be in here, but not sure......//Trailing stop
if(UseTrailingStop && TrailingStop > 0) {
if((OrderOpenPrice() - Ask) > (Point * (TrailingStop+0.5))) {
if((OrderStopLoss() > (Ask + Point * (TrailingStop+0.5))) || (OrderStopLoss() == 0)) {
OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);
if (!EachTickMode) BarCount = Bars;
continue;
//Buy
if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
if(DecideToOpenTrade(OP_BUY) && TradeSlotsAvailable()) {
if (UseStopLoss) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0;
if (UseTakeProfit) TakeProfitLevel = Ask + TakeProfit * Point; else TakeProfitLevel = 0.0;
Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);
if(Ticket > 0) {
if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
Print("BUY order opened : ", OrderOpenPrice());
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy");
} else {
Print("Error opening BUY order : ", GetLastError());
}
}
if (EachTickMode) TickCheck = True;
if (!EachTickMode) BarCount = Bars;
return(0);