- Don't check using the forming candle — it can cross and uncross multiple times.
- Your alert is useless — remove them.
- Only check once per bar (if you remove № 1)
For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
New candle - MQL4 programming forum - or ; You are looking at a signal. Act on a change of signal.
MQL4 (in Strategy Tester) - double testing of entry conditions - Strategy Tester - Expert Advisors and Automated Trading - MQL5 programming forum #1
- Don't check using the forming candle — it can cross and uncross multiple times.
- Your alert is useless — remove them.
- Only check once per bar (if you remove № 1)
- or ; You are looking at a signal. Act on a change of signal.
MQL4 (in Strategy Tester) - double testing of entry conditions - Strategy Tester - Expert Advisors and Automated Trading - MQL5 programming forum #1
Thanks for the advice.
I added iTime but it still prints the alerts every few seconds.
I can't seem to understand what I'm missing..
#property copyright "Copyright 2019, Martin Ellul" #include <Trade\Trade.mqh> // Get code from other places //--- Input Variables (Accessible from MetaTrader 5) input int shortemaPeriods = 50; input int longemaPeriods = 200; input int K = 21; input int D = 3; input int Slow = 3; CTrade myTradingControlPanel; double shortemaData[], longemaData[], medemaData[]; // You can declare multiple variables of the same data type in the same line. int numberOfShortemaData, numberOfLongemaData, numberofMedemaData; int shortemaControlPanel, longemaControlPanel, medemaControlPanel; int P; double currentBid, currentAsk; double stopLossPipsFinal, takeProfitPipsFinal, stopLevelPips; double stopLossLevel, takeProfitLevel; double shortema1, shortema2, longema1, longema2, medema1, medema2; bool alert; datetime lastbar; int _stochasticHandle; double _stochstic[]; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { lastbar = iTime(NULL,PERIOD_CURRENT,1); //--- ArraySetAsSeries(shortemaData,true); // Setting up table/array for time series data ArraySetAsSeries(longemaData,true); // Setting up table/array for time series data ArraySetAsSeries(_stochstic,true); _stochasticHandle=iStochastic(_Symbol,_Period,K,D,Slow,MODE_EMA,STO_CLOSECLOSE); shortemaControlPanel = iMA(_Symbol, _Period, shortemaPeriods, 0, MODE_LWMA, PRICE_CLOSE); // Getting the Control Panel/Handle for short SMA longemaControlPanel = iMA(_Symbol, _Period, longemaPeriods, 0, MODE_LWMA, PRICE_CLOSE); // Getting the Control Panel/Handle for long SMA if(_Digits == 5 || _Digits == 3 || _Digits == 1) P = 10;else P = 1; // To account for 5 digit brokers // ArrayResize(array_tickets,0,100000); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- IndicatorRelease(shortemaControlPanel); IndicatorRelease(longemaControlPanel); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { // -------------------- Collect most current data -------------------- currentBid = SymbolInfoDouble(_Symbol,SYMBOL_BID); // Get latest Bid Price currentAsk = SymbolInfoDouble(_Symbol,SYMBOL_ASK); // Get latest Ask Price CopyBuffer(_stochasticHandle,0,0,3,_stochstic); numberOfShortemaData = CopyBuffer(shortemaControlPanel, 0, 0, 3, shortemaData); // Collect most current SMA(10) Data and store it in the datatable/array shortSmaData[] numberOfLongemaData = CopyBuffer(longemaControlPanel, 0, 0, 3, longemaData); // Collect most current SMA(40) Data and store it in the datatable/array longSmaData[] shortema1 = shortemaData[0]; longema1 = longemaData[0]; CheckforAlert(); } void CheckforAlert() { if(lastbar < iTime(NULL,PERIOD_CURRENT,0)) { // if(alert == false) // { if(shortema1 > longema1 && _stochstic[0] < 20) { //Alert("Long Opportunity on GBPUSD M1"); Print("Long Opportunity on GBPUSD M1"); // alert == true; } else if (shortema1 < longema1 && _stochstic[0] > 20) { //Alert("Short Opportunity on GBPUSD M1"); Print("Short Opportunity on GBPUSD M1"); // alert == true; } // } // alert == false; } }
bool prev_long = false, prev_short=false; void CheckforAlert(const bool has_long, const bool has_short) { if(lastbar < iTime(NULL,PERIOD_CURRENT,0)) { if(prev_long != has_long) { if (has_long) Print("Long Opportunity on GBPUSD M1"); prev_long = has_long; } if (prev_short != has_short) { if (has_short) Print("Short Opportunity on GBPUSD M1"); prev_short = has_short; } } }
Call of the function
should be like:
shortema1 = shortemaData[0]; longema1 = longemaData[0]; bool has_long = shortema1 > longema1 && _stochstic[0] < 20; bool has_short = shortema1 < longema1 && _stochstic[0] > 20; CheckforAlert(has_long, has_short);
I understand what you did, however, it still prints the message every few seconds.. Is the time parameter correct?
It's mean, that the signal for long or short appear and disappear every few seconds.
And also it's bad idea to take the last value from indicator, because it can give a lot of false positive values.
Try to change start_pos to 1 on the CopyBuffer functions.
It's mean, that the signal for long or short appear and disappear every few seconds.
And also it's bad idea to take the last value from indicator, because it can give a lot of false positive values.
Try to change start_pos to 1 on the CopyBuffer
functions.
Changing start_pos to 1 worked! Thank you very much!
Hey guys,
I have been using this EA as an alert, however, it has been sending alerts not exactly when the stochastic crossed the 20 and 80 zones.
Can anyone help me find the issue on this?
I had never used iTime() before, perhaps it is not properly coded?
Thanks in advance,
Martin
#property copyright "Copyright 2019, Martin" #include <Trade\Trade.mqh> // Get code from other places //--- Input Variables (Accessible from MetaTrader 5) input int shortemaPeriods = 50; input int longemaPeriods = 200; input int K = 21; input int D = 3; input int Slow = 3; CTrade myTradingControlPanel; double shortemaData[], longemaData[], medemaData[]; // You can declare multiple variables of the same data type in the same line. int numberOfShortemaData, numberOfLongemaData, numberofMedemaData; int shortemaControlPanel, longemaControlPanel, medemaControlPanel; int P; double currentBid, currentAsk; double stopLossPipsFinal, takeProfitPipsFinal, stopLevelPips; double stopLossLevel, takeProfitLevel; double shortema1, shortema2, longema1, longema2, medema1, medema2; bool alert; datetime lastbar; bool prev_long = false, prev_short=false; int _stochasticHandle; double _stochstic[]; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { lastbar = iTime(NULL,PERIOD_M1,1); //--- ArraySetAsSeries(shortemaData,true); // Setting up table/array for time series data ArraySetAsSeries(longemaData,true); // Setting up table/array for time series data ArraySetAsSeries(_stochstic,true); _stochasticHandle=iStochastic(_Symbol,PERIOD_M1,K,D,Slow,MODE_EMA,STO_CLOSECLOSE); shortemaControlPanel = iMA(_Symbol, PERIOD_M1, shortemaPeriods, 0, MODE_LWMA, PRICE_CLOSE); // Getting the Control Panel/Handle for short SMA longemaControlPanel = iMA(_Symbol, PERIOD_M1, longemaPeriods, 0, MODE_LWMA, PRICE_CLOSE); // Getting the Control Panel/Handle for long SMA if(_Digits == 5 || _Digits == 3 || _Digits == 1) P = 10;else P = 1; // To account for 5 digit brokers // ArrayResize(array_tickets,0,100000); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- IndicatorRelease(shortemaControlPanel); IndicatorRelease(longemaControlPanel); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { // -------------------- Collect most current data -------------------- currentBid = SymbolInfoDouble(_Symbol,SYMBOL_BID); // Get latest Bid Price currentAsk = SymbolInfoDouble(_Symbol,SYMBOL_ASK); // Get latest Ask Price CopyBuffer(_stochasticHandle,0,1,3,_stochstic); numberOfShortemaData = CopyBuffer(shortemaControlPanel, 0, 0, 3, shortemaData); // Collect most current SMA(10) Data and store it in the datatable/array shortSmaData[] numberOfLongemaData = CopyBuffer(longemaControlPanel, 0, 0, 3, longemaData); // Collect most current SMA(40) Data and store it in the datatable/array longSmaData[] shortema1 = shortemaData[0]; longema1 = longemaData[0]; bool has_long = shortema1 > longema1 && _stochstic[1] < 20; bool has_short = shortema1 < longema1 && _stochstic[1] > 80; CheckforAlert(has_long, has_short); } void CheckforAlert(const bool has_long, const bool has_short) { if(lastbar < iTime(NULL,PERIOD_M1,0)) { if(prev_long != has_long) { if (has_long) Print("Long Opportunity on M1"); Alert("Long Opportunity on M1"); prev_long = has_long; } if (prev_short != has_short) { if (has_short) Print("Short Opportunity on M1"); Alert("Short Opportunity on M1"); prev_short = has_short; } } }
Hey guys,
I have been using this EA as an alert, however, it has been sending alerts not exactly when the stochastic crossed the 20 and 80 zones.
Can anyone help me find the issue on this?
I had never used iTime() before, perhaps it is not properly coded?
Thanks in advance,
Martin
To execute the alert function once and on start new candle put this code before calling the function:
if(iVolume(Symbol(),Period(),0)<=1)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Thanks a lot, the code is below!
Martin