You made a huge mistake - you create several handles on each tick! Remember: according to the MQL5 style, the indicator handle should be created ONCE and it should be done in OnInit.
Just to clarify shall I move all of these to the OnInit Function?
MqlDateTime structLdnOpenTime, structLdnCloseTime, structNyOpenTime, structNyCloseTime; TimeCurrent(structLdnOpenTime); TimeCurrent(structLdnCloseTime); TimeCurrent(structNyOpenTime); TimeCurrent(structNyCloseTime); structLdnOpenTime.hour = ldnOpenTimeHour; structLdnOpenTime.min = ldnOpenTimeMin; structLdnOpenTime.sec = ldnOpenTimeSec; structLdnCloseTime.hour = ldnCloseTimeHour; structLdnCloseTime.min = ldnCloseTimeMin; structLdnCloseTime.sec = ldnCloseTimeSec; structNyOpenTime.hour = nyOpenTimeHour; structNyOpenTime.min = nyOpenTimeMin; structNyOpenTime.sec = nyOpenTimeSec; structNyCloseTime.hour = nyCloseTimeHour; structNyCloseTime.min = nyCloseTimeMin; structNyCloseTime.sec = nyCloseTimeSec; datetime ldnOpen = StructToTime(structLdnOpenTime); datetime ldnCLose = StructToTime(structLdnCloseTime); datetime nyOpen = StructToTime(structNyOpenTime); datetime nyClose = StructToTime(structNyCloseTime); //Print("London Open: ",ldnOpen, "London Close: ",ldnCLose," NY Open: ",nyOpen," New York Close: ",nyClose); int bars = iBars(_Symbol,PERIOD_M5); double high3 = iHigh(_Symbol,PERIOD_M5,3); double low3 = iLow(_Symbol,PERIOD_M5,3); double close1 = iClose(_Symbol,PERIOD_M5,1);
No. Read my post above - I'm talking about indicator handles.
Ok, I think this is what u meant. But now that I try to run this in the strategy tester with visual mode, the MT5 keeps on crashing.
#include <Trade/Trade.mqh> #include <LotSizeCalc.mqh> //enum IndexList{BUY=1,SELL=0,}; //input IndexList PROGRAM = SELL; //LDN Close is 13:00 (Server Time) and 11:00 WAT //NY Close is 18:00 (Server Time) and 16:00 WAT int ldnOpenTimeHour = 10; int ldnOpenTimeMin = 0; int ldnOpenTimeSec = 0; int ldnCloseTimeHour = 13; int ldnCloseTimeMin = 0; int ldnCloseTimeSec = 0; int nyOpenTimeHour = 16; int nyOpenTimeMin = 0; int nyOpenTimeSec = 0; int nyCloseTimeHour = 19; int nyCloseTimeMin = 0; int nyCloseTimeSec = 0; CTrade trade; int handle, h1slowEMA, h1fastEMA, m5slowEMA, m5fastEMA; int barsTotal, bars; int tradeCount; double myLotSize; double riskPerTrade = 0.1; int OnInit() { handle = iFractals(_Symbol,PERIOD_M5); barsTotal = iBars(_Symbol,PERIOD_M5); //H1 EMA Definition h1slowEMA = iMA(_Symbol,PERIOD_H1,18,0,MODE_EMA,PRICE_CLOSE); h1fastEMA = iMA(_Symbol,PERIOD_H1,8,0,MODE_EMA,PRICE_CLOSE); //M5 EMA Definition m5slowEMA = iMA(_Symbol,PERIOD_M5,18,0,MODE_EMA,PRICE_CLOSE); m5fastEMA = iMA(_Symbol,PERIOD_M5,8,0,MODE_EMA,PRICE_CLOSE); return(INIT_SUCCEEDED); } void OnTick() { MqlDateTime structLdnOpenTime, structLdnCloseTime, structNyOpenTime, structNyCloseTime; TimeCurrent(structLdnOpenTime); TimeCurrent(structLdnCloseTime); TimeCurrent(structNyOpenTime); TimeCurrent(structNyCloseTime); structLdnOpenTime.hour = ldnOpenTimeHour; structLdnOpenTime.min = ldnOpenTimeMin; structLdnOpenTime.sec = ldnOpenTimeSec; structLdnCloseTime.hour = ldnCloseTimeHour; structLdnCloseTime.min = ldnCloseTimeMin; structLdnCloseTime.sec = ldnCloseTimeSec; structNyOpenTime.hour = nyOpenTimeHour; structNyOpenTime.min = nyOpenTimeMin; structNyOpenTime.sec = nyOpenTimeSec; structNyCloseTime.hour = nyCloseTimeHour; structNyCloseTime.min = nyCloseTimeMin; structNyCloseTime.sec = nyCloseTimeSec; datetime ldnOpen = StructToTime(structLdnOpenTime); datetime ldnCLose = StructToTime(structLdnCloseTime); datetime nyOpen = StructToTime(structNyOpenTime); datetime nyClose = StructToTime(structNyCloseTime); //Print("London Open: ",ldnOpen, "London Close: ",ldnCLose," NY Open: ",nyOpen," New York Close: ",nyClose); bars = iBars(_Symbol,PERIOD_M5); double h1SlowMovingAverageArray[]; double h1FastMovingAverageArray[]; //sort the price array from the current candle backwards ArraySetAsSeries(h1SlowMovingAverageArray,true); ArraySetAsSeries(h1FastMovingAverageArray,true); double m5SlowMovingAverageArray[]; double m5FastMovingAverageArray[]; //sort the price array from the current candle backwards ArraySetAsSeries(m5SlowMovingAverageArray,true); ArraySetAsSeries(m5FastMovingAverageArray,true); double high3 = iHigh(_Symbol,PERIOD_M5,3); double low3 = iLow(_Symbol,PERIOD_M5,3); double close1 = iClose(_Symbol,PERIOD_M5,1); double equity = AccountInfoDouble(ACCOUNT_BALANCE); if(barsTotal != bars){ barsTotal = bars; double fractalUpper[], fractalLower[]; CopyBuffer(handle,UPPER_LINE,3,1,fractalUpper); CopyBuffer(handle,LOWER_LINE,3,1,fractalLower); tradeCount = PositionsTotal(); //Defined EA, one line, from candle 0, for 3 candles, store in Array CopyBuffer(h1slowEMA,0,1,1,h1SlowMovingAverageArray); CopyBuffer(h1fastEMA,0,1,1,h1FastMovingAverageArray); //Calc MA for candle 0 double myH1SlowEMA = h1SlowMovingAverageArray[0]; double myH1FastEMA = h1FastMovingAverageArray[0]; //Defined EA, one line, from candle 0, for 3 candles, store in Array CopyBuffer(m5slowEMA,0,1,3,m5SlowMovingAverageArray); CopyBuffer(m5fastEMA,0,1,3,m5FastMovingAverageArray); //Calc MA for candle 0 double myM5SlowEMA = m5SlowMovingAverageArray[0]; double myM5FastEMA = m5FastMovingAverageArray[0]; //Need to find a way to change font attributes if((myH1SlowEMA > myH1FastEMA) && (myM5SlowEMA > myM5FastEMA)){Comment("H1: BEARISH\nM5: BEARISH\nTrade Count: ",tradeCount);} else if((myH1SlowEMA < myH1FastEMA) && (myM5SlowEMA < myM5FastEMA)){Comment("H1: BULLISH\nM5: BULLISH\nTrade Count: ",tradeCount);} else if((myH1SlowEMA < myH1FastEMA) && (myM5SlowEMA > myM5FastEMA)){Comment("H1: BULLISH\nM5: BEARISH\nTrade Count: ",tradeCount);} else if((myH1SlowEMA > myH1FastEMA) && (myM5SlowEMA < myM5FastEMA)){Comment("H1: BEARISH\nM5: BULLISH\nTrade Count: ",tradeCount);} else{Comment("H1 && M5 are at EQUILIBRIUM\nTrade Count: ",tradeCount);} //Multi Check Phase and Trade if(((tradeCount < 3) && ((ldnOpen <= TimeCurrent()) && (ldnCLose > TimeCurrent())) || ((nyOpen <= TimeCurrent()) && (nyClose > TimeCurrent())))){ if((m5SlowMovingAverageArray[0]>m5FastMovingAverageArray[0]) && (fractalUpper[0] == high3) && (myH1SlowEMA > myH1FastEMA)){ double smallSL = high3 + 0.00030; double allSL = NormalizeDouble((smallSL - close1),5); double sendSl = NormalizeDouble((allSL*100000),1); double tp = NormalizeDouble((close1 - (allSL*3.69000)),5); //Print("Small SL: ",smallSL," High3: ",high3," Close1: ",close1," TP: ",tp,"SendSL: ",sendSl,"AllSL: ",allSL); myLotSize = calculateLotSize(_Symbol,equity,sendSl,riskPerTrade); trade.Sell(myLotSize,_Symbol,0,smallSL,tp);} else if((m5SlowMovingAverageArray[0]<m5FastMovingAverageArray[0]) && (fractalLower[0] == low3) && (myH1SlowEMA < myH1FastEMA)){ double smallSL = low3 - 0.00030; double allSL = NormalizeDouble((close1 - smallSL),5); double sendSl = NormalizeDouble((allSL*100000),1); double tp = NormalizeDouble((close1 + (allSL*3.69000)),5); //Print("Small SL: ",smallSL," High3: ",high3," Close1: ",close1," TP: ",tp,"SendSL: ",sendSl); myLotSize = calculateLotSize(_Symbol,equity,sendSl,riskPerTrade); trade.Buy(myLotSize,_Symbol,0,smallSL,tp);} else{Print("Hunting for Setups...");} } } }
The code contains errors.
can't open "C:\Users\barab\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075\MQL5\Include\LotSizeCalc.mqh" include file 1.mq5 7 11
Ive restored the previous code before relocating the handles, it now works as it used to before.
Can anyone assist me on how to prevent multiple trades during the same candle.
Maybe their is a superior check that I can utilize?
See code below for reference:
#include <Trade/Trade.mqh> #include <LotSizeCalc.mqh> //enum IndexList{BUY=1,SELL=0,}; //input IndexList PROGRAM = SELL; //LDN Close is 13:00 (Server Time) and 11:00 WAT //NY Close is 18:00 (Server Time) and 16:00 WAT int ldnOpenTimeHour = 10; int ldnOpenTimeMin = 0; int ldnOpenTimeSec = 0; int ldnCloseTimeHour = 13; int ldnCloseTimeMin = 0; int ldnCloseTimeSec = 0; int nyOpenTimeHour = 16; int nyOpenTimeMin = 0; int nyOpenTimeSec = 0; int nyCloseTimeHour = 19; int nyCloseTimeMin = 0; int nyCloseTimeSec = 0; CTrade trade; int handle; int barsTotal; int tradeCount; double myLotSize; double riskPerTrade = 0.1; int OnInit() { handle = iFractals(_Symbol,PERIOD_M5); barsTotal = iBars(_Symbol,PERIOD_M5); return(INIT_SUCCEEDED); } void OnTick() { MqlDateTime structLdnOpenTime, structLdnCloseTime, structNyOpenTime, structNyCloseTime; TimeCurrent(structLdnOpenTime); TimeCurrent(structLdnCloseTime); TimeCurrent(structNyOpenTime); TimeCurrent(structNyCloseTime); structLdnOpenTime.hour = ldnOpenTimeHour; structLdnOpenTime.min = ldnOpenTimeMin; structLdnOpenTime.sec = ldnOpenTimeSec; structLdnCloseTime.hour = ldnCloseTimeHour; structLdnCloseTime.min = ldnCloseTimeMin; structLdnCloseTime.sec = ldnCloseTimeSec; structNyOpenTime.hour = nyOpenTimeHour; structNyOpenTime.min = nyOpenTimeMin; structNyOpenTime.sec = nyOpenTimeSec; structNyCloseTime.hour = nyCloseTimeHour; structNyCloseTime.min = nyCloseTimeMin; structNyCloseTime.sec = nyCloseTimeSec; datetime ldnOpen = StructToTime(structLdnOpenTime); datetime ldnCLose = StructToTime(structLdnCloseTime); datetime nyOpen = StructToTime(structNyOpenTime); datetime nyClose = StructToTime(structNyCloseTime); //Print("London Open: ",ldnOpen, "London Close: ",ldnCLose," NY Open: ",nyOpen," New York Close: ",nyClose); int bars = iBars(_Symbol,PERIOD_M5); double h1SlowMovingAverageArray[]; double h1FastMovingAverageArray[]; double high3 = iHigh(_Symbol,PERIOD_M5,3); double low3 = iLow(_Symbol,PERIOD_M5,3); double close1 = iClose(_Symbol,PERIOD_M5,1); double equity = AccountInfoDouble(ACCOUNT_BALANCE); if(barsTotal != bars){ barsTotal = bars; double fractalUpper[], fractalLower[]; CopyBuffer(handle,UPPER_LINE,3,1,fractalUpper); CopyBuffer(handle,LOWER_LINE,3,1,fractalLower); tradeCount = PositionsTotal(); //Definition of the EMA int h1SlowEMA = iMA(_Symbol,PERIOD_H1,18,0,MODE_EMA,PRICE_CLOSE); int h1FastEMA = iMA(_Symbol,PERIOD_H1,8,0,MODE_EMA,PRICE_CLOSE); //sort the price array from the current candle backwards ArraySetAsSeries(h1SlowMovingAverageArray,true); ArraySetAsSeries(h1FastMovingAverageArray,true); //Defined EA, one line, from candle 0, for 3 candles, store in Array CopyBuffer(h1SlowEMA,0,1,1,h1SlowMovingAverageArray); CopyBuffer(h1FastEMA,0,1,1,h1FastMovingAverageArray); //Calc MA for candle 0 double myH1SlowEMA = h1SlowMovingAverageArray[0]; double myH1FastEMA = h1FastMovingAverageArray[0]; double m5SlowMovingAverageArray[]; double m5FastMovingAverageArray[]; //Definition of the EMA int m5slowEMA = iMA(_Symbol,PERIOD_M5,18,0,MODE_EMA,PRICE_CLOSE); int m5fastEMA = iMA(_Symbol,PERIOD_M5,8,0,MODE_EMA,PRICE_CLOSE); //sort the price array from the current candle backwards ArraySetAsSeries(m5SlowMovingAverageArray,true); ArraySetAsSeries(m5FastMovingAverageArray,true); //Defined EA, one line, from candle 0, for 3 candles, store in Array CopyBuffer(m5slowEMA,0,1,3,m5SlowMovingAverageArray); CopyBuffer(m5fastEMA,0,1,3,m5FastMovingAverageArray); //Calc MA for candle 0 double myM5SlowEMA = m5SlowMovingAverageArray[0]; double myM5FastEMA = m5FastMovingAverageArray[0]; //Need to find a way to change font attributes if((myH1SlowEMA > myH1FastEMA) && (myM5SlowEMA > myM5FastEMA)){Comment("H1: BEARISH\nM5: BEARISH\nTrade Count: ",tradeCount);} else if((myH1SlowEMA < myH1FastEMA) && (myM5SlowEMA < myM5FastEMA)){Comment("H1: BULLISH\nM5: BULLISH\nTrade Count: ",tradeCount);} else if((myH1SlowEMA < myH1FastEMA) && (myM5SlowEMA > myM5FastEMA)){Comment("H1: BULLISH\nM5: BEARISH\nTrade Count: ",tradeCount);} else if((myH1SlowEMA > myH1FastEMA) && (myM5SlowEMA < myM5FastEMA)){Comment("H1: BEARISH\nM5: BULLISH\nTrade Count: ",tradeCount);} else{Comment("H1 && M5 are at EQUILIBRIUM\nTrade Count: ",tradeCount);} //Multi Check Phase and Trade if(((tradeCount < 3) && ((ldnOpen <= TimeCurrent()) && (ldnCLose > TimeCurrent())) || ((nyOpen <= TimeCurrent()) && (nyClose > TimeCurrent())))){ if((m5SlowMovingAverageArray[0]>m5FastMovingAverageArray[0]) && (fractalUpper[0] == high3) && (myH1SlowEMA > myH1FastEMA)){ double smallSL = high3 + 0.00030; double allSL = NormalizeDouble((smallSL - close1),5); double sendSl = NormalizeDouble((allSL*100000),1); double tp = NormalizeDouble((close1 - (allSL*3.69000)),5); //Print("Small SL: ",smallSL," High3: ",high3," Close1: ",close1," TP: ",tp,"SendSL: ",sendSl,"AllSL: ",allSL); myLotSize = calculateLotSize(_Symbol,equity,sendSl,riskPerTrade); trade.Sell(myLotSize,_Symbol,0,smallSL,tp);} else if((m5SlowMovingAverageArray[0]<m5FastMovingAverageArray[0]) && (fractalLower[0] == low3) && (myH1SlowEMA < myH1FastEMA)){ double smallSL = low3 - 0.00030; double allSL = NormalizeDouble((close1 - smallSL),5); double sendSl = NormalizeDouble((allSL*100000),1); double tp = NormalizeDouble((close1 + (allSL*3.69000)),5); //Print("Small SL: ",smallSL," High3: ",high3," Close1: ",close1," TP: ",tp,"SendSL: ",sendSl); myLotSize = calculateLotSize(_Symbol,equity,sendSl,riskPerTrade); trade.Buy(myLotSize,_Symbol,0,smallSL,tp);} else{Print("Hunting for Setups...");} } } }
Forum on trading, automated trading systems and testing trading strategies
My EA is firing off multiple trades instead of 1 per candle.
Vladimir Karputov, 2022.06.28 12:03
The code contains errors.
can't open "C:\Users\barab\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075\MQL5\Include\LotSizeCalc.mqh" include file 1.mq5 7 11
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
As the title suggests its supposed to only fire of a single trade per candle.
Im guessing my loops have an issue or something I disregarded. Can someone with fresh eyes kindly assist.