Where is OnTick()?
Maybe you should start learning MQ5, start here:
https://www.mql5.com/en/docs/runtime/running
https://www.metatrader5.com/en/terminal/help/startworking
https://www.metatrader5.com/en/terminal/help/startworking/start_advanced
https://www.metatrader5.com/en/terminal/help/mobile_trading
Quickstart for newbies: https://www.mql5.com/en/articles/496
Orders, Positions in MetaTrader 5: https://www.mql5.com/en/articles/211
How to start with MQL5: Usefull Hints and lots of code: https://www.mql5.com/en/forum/296230
https://www.mql5.com/en/forum/178706
- www.mql5.com
Your topic has been moved to the section: Expert Advisors and Automated Trading — In the future, please consider which section is most appropriate for your query.
Where is OnTick()?
Maybe you should start learning MQ5, start here:
https://www.mql5.com/en/docs/runtime/running
https://www.metatrader5.com/en/terminal/help/startworking
https://www.metatrader5.com/en/terminal/help/startworking/start_advanced
https://www.metatrader5.com/en/terminal/help/mobile_trading
Quickstart for newbies: https://www.mql5.com/en/articles/496
Orders, Positions in MetaTrader 5: https://www.mql5.com/en/articles/211
How to start with MQL5: Usefull Hints and lots of code: https://www.mql5.com/en/forum/296230
https://www.mql5.com/en/forum/178706
Hi Carl
Well 'learning MQ5 is what I am trying to do. Hence I am following a tutorial and asking questions here ;)
Here is my OnTick:
//+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //Counts the number of ticks received TicksReceivedCount++; //Checks for new candle bool IsNewCandle = false; if(TimeLastTickProcessed != iTime(Symbol(),Period(),0)) { IsNewCandle = true; TimeLastTickProcessed=iTime(Symbol(),Period(),0); } //If there is a new candle, process any trades if(IsNewCandle == true) { //Counts the number of ticks processed TicksProcessedCount++; //Check if position is still open. If not open, return 0. if (!PositionSelectByTicket(TicketNumber)) TicketNumber = 0; //Initiate String for indicatorMetrics Variable. This will reset variable each time OnTick function runs. IndicatorMetrics =""; StringConcatenate(IndicatorMetrics,Symbol()," | Last Processed: ",TimeLastTickProcessed," | Open Ticket: ", TicketNumber); //Money Management - ATR double CurrentAtr = GetATRValue(); //Gets ATR value double using custom function - convert double to string as per symbol digits StringConcatenate(IndicatorMetrics, IndicatorMetrics, " | ATR: ", CurrentAtr); //Strategy Trigger - MACD string OpenSignalMacd = GetMacdOpenSignal(); //Variable will return Long or Short Bias only on a trigger/cross event StringConcatenate(IndicatorMetrics, IndicatorMetrics, " | MACD Bias: ", OpenSignalMacd); //Concatenate indicator values to output comment for user //Strategy Filter - EMA string OpenSignalEma = GetEmaOpenSignal(); //Variable will return long or short bias if close is above or below EMA. StringConcatenate(IndicatorMetrics, IndicatorMetrics, " | EMA Bias: ", OpenSignalEma); //Concatenate indicator values to output comment for user //Enter trades and return position ticket number if(OpenSignalMacd == "Long" && OpenSignalEma == "Long") TicketNumber = ProcessTradeOpen(ORDER_TYPE_BUY,CurrentAtr); else if(OpenSignalMacd == "Short" && OpenSignalEma == "Short") TicketNumber = ProcessTradeOpen(ORDER_TYPE_SELL,CurrentAtr); //Adjust Open Positions - Trailing Stop Loss if(TslCheck == true) AdjustTsl(TicketNumber, CurrentAtr, AtrLossMulti); } //Comment for user Comment("\n\rExpert: ", InpMagicNumber, "\n\r", "MT5 Server Time: ", TimeCurrent(), "\n\r", "Ticks Received: ", TicksReceivedCount,"\n\r", "Ticks Processed: ", TicksProcessedCount,"\n\r" "Symbols Traded: \n\r", IndicatorMetrics); }
I am curious to know how much topics there are about invalid stops on this forum. I would bet for at least 1000 !
Hi Alain
There are many, many, many, posts, hence I have modified my code several times over the last couple of days to take into account the advice given by people such as William Roader and Fernando Carreiro.
As I have understood it so far, most of the posts seem to focus around problems with the NormalizeDouble function, so as you can see from my annotated code (in the first post) I have replaced that function for calculating the stops. There was another recommendation to check for any limit to stop distance, I implemented that too.
That's why I have asked for help, after days searching the forum and implementing the suggestions I find, I'm still getting the errors and I am still non-the wiser why there is a problem.
As well as fixing the code I'd like to understand.
Start to add printing in the log when there is an error, all the relevant values (the sl/tp but also open price, current bid/ask and the stoplevel).
Post the ALL relevant code ONLY please, so all related to the trade request and price/sl/tp. We don't care about the signals part or the lot size calculation. And in one post,not split across several messages.
And also post the log output.
- 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've just started learning to code EA and try and get my head around MT5, ticks, points etc, so I apologise in advance if I have done something very daft or missed something obvious!
So I've been following a tutorial online that gradually developed a more complex EA. It seemed to make sense and work with a fixed stop, but once the stop was changed to a value based on ATR it generated a lot of failed trades for 'Invalid Stops'.
That goes on until until 7:48 and it suddenly starts creating trades.
When I initially googled this I found suggestions for replacing NormalizeDouble function, which I have done. It did not seem to make any difference. I found suggestions to check the Min allowed stop, but that seems to be 0.
So I added lots of debugging info and watched breakpoints as I stepped through the code, all to no avail. I cannot isolate what causes a failure. ATR values themselves were ruled out as values that cause a fail at earlier time frames work fine later on.
I have noticed an inconstancy in the output of the Strategy Tester, which maybe the problem. The Journal shows "GBPUSD: symbol synchronized," but my chart is EURUSD? I don't know why GBPUSD was being synchronised but changing the chart to GBPUSD did not help. I've attached a screen grab of my MT5 settings for testing at the bottom.
Many thanks to anyone who takes the time to read this.
Pete
Here are my 'hopefully' relevant custom functions: