I've been trying to solve this problem for a few days now but cannot see the problem.
Searched the internet, forums, and youtube without any success.
ApplyTrailingStop function does not work properly. I cannot find out why so.
-
You can't see the problem, so that is your problem.
Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
Code debugging - Developing programs - MetaEditor Help
Error Handling and Logging in MQL5 - MQL5 Articles (2015)
Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010) -
Ticket = OrderSend(Symbol(), orderType, LotSize, OpenPrice, 2, StopLossPrice, TakeProfitPrice, InpTradeComment, InpMagicNumber);
Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
What are Function return values ? How do I use them ? - MQL4 programming forum (2012)
Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles (2014) Your code if (DayOfWeek()>=2 && DayOfWeek()<4) return(true); else { return(false); }
Simplified return DayOfWeek()>=2 && DayOfWeek()<4;
-
if (orderType==ORDER_TYPE_BUY) {
There is no such constant(s) in MT4. List of MQL4 Constants - MQL4 Reference
Use the correct Trade operation enumeration.
-
OpenPrice = Ask; StopLossPrice = OpenPrice-ATRSLValue; TakeProfitPrice = OpenPrice+ATRTPValue;
You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.
-
Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?
-
Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25 -
The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).
-
Don't hard code constants. Use the proper enumerations. ENUM_DAY_OF_WEEK
-
You can't see the problem, so that is your problem.
Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
Code debugging - Developing programs - MetaEditor Help
Error Handling and Logging in MQL5 - MQL5 Articles (2015)
Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010) -
Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
What are Function return values ? How do I use them ? - MQL4 programming forum (2012)
Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles (2014) Your code Simplified -
There is no such constant(s) in MT4. List of MQL4 Constants - MQL4 Reference
Use the correct Trade operation enumeration.
-
You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.
-
Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?
-
Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25 -
The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).
-
Don't hard code constants. Use the proper enumerations. ENUM_DAY_OF_WEEK
Thank you very much for the quick reply. Will start implementing all the things you said and find the solution.
Hey Roeder,
Because of your helpful tips I was able to find the issue with the logic of the code using print statements to see if the values are being printed correctly at the correct time.
The issue had to do with the ApplyTrailingStop function being called within the if-buy and if-sell condition causing it to be run once rather with every new bar forming as I intended.
I wouldn't have been able to see that issue if it wasn't for the print statements which caught my attention to the function being called once.
Simply putting the ApplyTrailingStop function within the NewBar section solved the issue.
Thanks a lot again.
Jeremy
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Trailing stop issue.
I've been trying to solve this problem for a few days now but cannot see the problem.
Searched the internet, forums, and youtube without any success.
ApplyTrailingStop function does not work properly. I cannot find out why so.
I think it may have to do with the condition "OrderOpenPrice<buyStopLoss" and vice versa for the sell condition.
The code runs without that specific condition, but it is not how I want the code to function.
I want the SL moved when it's greater than the opening price to cover for potential losses. Without that specific condition it modifies my SL straightaway.
Help would be greatly appreciated as this is my last resort of finding a solution to my problem.