Menu
Brokers
MT4 Forex Brokers
MT5 Forex brokers
PayPal Brokers
Skrill Brokers
Oil Trading Brokers
Gold Trading Brokers
Web Browser Platform
Brokers with CFD Trading
ECN Brokers
Bitcoin FX Brokers
PAMM Forex Brokers
With Cent Accounts
With High Leverage
Cryptocurrency Brokers
Forums
All threads
New threads
New posts
Trending
Search forums
What's new
New threads
New posts
Latest activity
Log in
Register
Search
Search titles only
By:
Search titles only
By:
Menu
Install the app
Install
Reply to thread
Forums
Trading Platforms
MetaTrader Expert Advisors
[MQL4 Code Help] Converting If else condition to function for more indicator & Time-frame flexibility
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Message
[QUOTE="shanmugapradeep, post: 242631, member: 70475"] I am redesigning EA, so it can have more accuracy and flexibility. [B]Original code :[/B] [CODE=mql4] if( iRSI(gRandomSymbol,PERIOD_M1,RSI_Period,RSI_AppliedPrice,RSI_shift) > RSI_UpLevel && iRSI(gRandomSymbol,PERIOD_M5,RSI_Period,RSI_AppliedPrice,RSI_shift) > RSI_UpLevel && iRSI(gRandomSymbol,PERIOD_M15,RSI_Period,RSI_AppliedPrice,RSI_shift) > RSI_UpLevel && iRSI(gRandomSymbol,PERIOD_M30,RSI_Period,RSI_AppliedPrice,RSI_shift) > RSI_UpLevel && iRSI(gRandomSymbol,PERIOD_H1,RSI_Period,RSI_AppliedPrice,RSI_shift) > RSI_UpLevel && iRSI(gRandomSymbol,PERIOD_H4,RSI_Period,RSI_AppliedPrice,RSI_shift) > RSI_UpLevel && (iStochastic(gRandomSymbol,PERIOD_M1,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_MAIN,Stochastic_Shift) > Stochastic_UpLevel && iStochastic(gRandomSymbol,PERIOD_M1,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_SIGNAL,Stochastic_Shift) > Stochastic_UpLevel) && (iStochastic(gRandomSymbol,PERIOD_M5,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_MAIN,Stochastic_Shift) > Stochastic_UpLevel && iStochastic(gRandomSymbol,PERIOD_M5,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_SIGNAL,Stochastic_Shift) > Stochastic_UpLevel) && (iStochastic(gRandomSymbol,PERIOD_M15,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_MAIN,Stochastic_Shift) > Stochastic_UpLevel && iStochastic(gRandomSymbol,PERIOD_M15,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_SIGNAL,Stochastic_Shift) > Stochastic_UpLevel) && (iStochastic(gRandomSymbol,PERIOD_M30,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_MAIN,Stochastic_Shift) > Stochastic_UpLevel && iStochastic(gRandomSymbol,PERIOD_M30,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_SIGNAL,Stochastic_Shift) > Stochastic_UpLevel) && (iStochastic(gRandomSymbol,PERIOD_H1,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_MAIN,Stochastic_Shift) > Stochastic_UpLevel && iStochastic(gRandomSymbol,PERIOD_H1,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_SIGNAL,Stochastic_Shift) > Stochastic_UpLevel) && (iStochastic(gRandomSymbol,PERIOD_H4,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_MAIN,Stochastic_Shift) > Stochastic_UpLevel && iStochastic(gRandomSymbol,PERIOD_H4,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_SIGNAL,Stochastic_Shift) > Stochastic_UpLevel) && MarketInfo(gRandomSymbol, MODE_BID) > iBands(gRandomSymbol,PERIOD_M1,BollingerBands_Period,BollingerBands_deviation,BollingerBands_BandShift,BollingerBands_AppliedPrice,MODE_UPPER,BollingerBands_Shift) && MarketInfo(gRandomSymbol, MODE_BID) > iBands(gRandomSymbol,PERIOD_M5,BollingerBands_Period,BollingerBands_deviation,BollingerBands_BandShift,BollingerBands_AppliedPrice,MODE_UPPER,BollingerBands_Shift) && MarketInfo(gRandomSymbol, MODE_BID) > iBands(gRandomSymbol,PERIOD_M15,BollingerBands_Period,BollingerBands_deviation,BollingerBands_BandShift,BollingerBands_AppliedPrice,MODE_UPPER,BollingerBands_Shift) && MarketInfo(gRandomSymbol, MODE_BID) > iBands(gRandomSymbol,PERIOD_M30,BollingerBands_Period,BollingerBands_deviation,BollingerBands_BandShift,BollingerBands_AppliedPrice,MODE_UPPER,BollingerBands_Shift) && MarketInfo(gRandomSymbol, MODE_BID) > iBands(gRandomSymbol,PERIOD_H1,BollingerBands_Period,BollingerBands_deviation,BollingerBands_BandShift,BollingerBands_AppliedPrice,MODE_UPPER,BollingerBands_Shift) && MarketInfo(gRandomSymbol, MODE_BID) > iBands(gRandomSymbol,PERIOD_H4,BollingerBands_Period,BollingerBands_deviation,BollingerBands_BandShift,BollingerBands_AppliedPrice,MODE_UPPER,BollingerBands_Shift) ) gSignalStatus = "RSB_UP"; if( iRSI(gRandomSymbol,PERIOD_M1,RSI_Period,RSI_AppliedPrice,RSI_shift) < RSI_BelowLevel && iRSI(gRandomSymbol,PERIOD_M5,RSI_Period,RSI_AppliedPrice,RSI_shift) < RSI_BelowLevel && iRSI(gRandomSymbol,PERIOD_M15,RSI_Period,RSI_AppliedPrice,RSI_shift) < RSI_BelowLevel && iRSI(gRandomSymbol,PERIOD_M30,RSI_Period,RSI_AppliedPrice,RSI_shift) < RSI_BelowLevel && iRSI(gRandomSymbol,PERIOD_H1,RSI_Period,RSI_AppliedPrice,RSI_shift) < RSI_BelowLevel && iRSI(gRandomSymbol,PERIOD_H4,RSI_Period,RSI_AppliedPrice,RSI_shift) < RSI_BelowLevel && (iStochastic(gRandomSymbol,PERIOD_M1,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_MAIN,Stochastic_Shift) < Stochastic_BelowLevel && iStochastic(gRandomSymbol,PERIOD_M1,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_SIGNAL,Stochastic_Shift) < Stochastic_BelowLevel) && (iStochastic(gRandomSymbol,PERIOD_M5,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_MAIN,Stochastic_Shift) < Stochastic_BelowLevel && iStochastic(gRandomSymbol,PERIOD_M5,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_SIGNAL,Stochastic_Shift) < Stochastic_BelowLevel) && (iStochastic(gRandomSymbol,PERIOD_M15,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_MAIN,Stochastic_Shift) < Stochastic_BelowLevel && iStochastic(gRandomSymbol,PERIOD_M15,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_SIGNAL,Stochastic_Shift) < Stochastic_BelowLevel) && (iStochastic(gRandomSymbol,PERIOD_M30,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_MAIN,Stochastic_Shift) < Stochastic_BelowLevel && iStochastic(gRandomSymbol,PERIOD_M30,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_SIGNAL,Stochastic_Shift) < Stochastic_BelowLevel) && (iStochastic(gRandomSymbol,PERIOD_H1,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_MAIN,Stochastic_Shift) < Stochastic_BelowLevel && iStochastic(gRandomSymbol,PERIOD_H1,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_SIGNAL,Stochastic_Shift) < Stochastic_BelowLevel) && (iStochastic(gRandomSymbol,PERIOD_H4,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_MAIN,Stochastic_Shift) < Stochastic_BelowLevel && iStochastic(gRandomSymbol,PERIOD_H4,Stochastic_Kperiod,Stochastic_Dperiod,Stochastic_Slowing,Stochastic_Method,Stochastic_PriceField,MODE_SIGNAL,Stochastic_Shift) < Stochastic_BelowLevel) && MarketInfo(gRandomSymbol, MODE_ASK) < iBands(gRandomSymbol,PERIOD_M1,BollingerBands_Period,BollingerBands_deviation,BollingerBands_BandShift,BollingerBands_AppliedPrice,MODE_LOWER,BollingerBands_Shift) && MarketInfo(gRandomSymbol, MODE_ASK) < iBands(gRandomSymbol,PERIOD_M5,BollingerBands_Period,BollingerBands_deviation,BollingerBands_BandShift,BollingerBands_AppliedPrice,MODE_LOWER,BollingerBands_Shift) && MarketInfo(gRandomSymbol, MODE_ASK) < iBands(gRandomSymbol,PERIOD_M15,BollingerBands_Period,BollingerBands_deviation,BollingerBands_BandShift,BollingerBands_AppliedPrice,MODE_LOWER,BollingerBands_Shift) && MarketInfo(gRandomSymbol, MODE_ASK) < iBands(gRandomSymbol,PERIOD_M30,BollingerBands_Period,BollingerBands_deviation,BollingerBands_BandShift,BollingerBands_AppliedPrice,MODE_LOWER,BollingerBands_Shift) && MarketInfo(gRandomSymbol, MODE_ASK) < iBands(gRandomSymbol,PERIOD_H1,BollingerBands_Period,BollingerBands_deviation,BollingerBands_BandShift,BollingerBands_AppliedPrice,MODE_LOWER,BollingerBands_Shift) && MarketInfo(gRandomSymbol, MODE_ASK) < iBands(gRandomSymbol,PERIOD_H4,BollingerBands_Period,BollingerBands_deviation,BollingerBands_BandShift,BollingerBands_AppliedPrice,MODE_LOWER,BollingerBands_Shift) ) gSignalStatus = "RSB_BELOW";[/CODE] This check RSI, Stochastic and BollingerBands together for all time frame but have no flexibility if i want to check for one timeframe or combination of other possibility indicator. So, with the help of Ai, i have changed the code to [CODE=mql4]extern string __TradingIndicator__ = "***Trading Indicator***"; extern bool RSI = true; extern bool Stochastic = true; extern bool BollingerBands = true; extern string __TimeFrame__ = "***TimeFrame Settings***"; extern bool PeriodM1 = true; extern bool PeriodM5 = true; extern bool PeriodM15 = true; extern bool PeriodM30 = true; extern bool PeriodH1 = true; extern bool PeriodH4 = true; extern bool PeriodD1 = false; //+------------------------------------------------------------------+ //| Function to check RSI conditions | //+------------------------------------------------------------------+ void CheckRSIConditions() { gRSISignal = "NoSignal"; bool conditionsUp[7], conditionsDown[7]; ENUM_TIMEFRAMES periods[7]; int count = 0; if(PeriodM1) periods[count++] = PERIOD_M1; if(PeriodM5) periods[count++] = PERIOD_M5; if(PeriodM15) periods[count++] = PERIOD_M15; if(PeriodM30) periods[count++] = PERIOD_M30; if(PeriodH1) periods[count++] = PERIOD_H1; if(PeriodH4) periods[count++] = PERIOD_H4; if(PeriodD1) periods[count++] = PERIOD_D1; if(count == 0) return; // Exit if no timeframes are selected bool allTrueUp = true, allTrueDown = true; for(int i = 0; i < count; i++) { double rsiValue = iRSI(gRandomSymbol, periods[i], RSI_Period, RSI_AppliedPrice, RSI_shift); conditionsUp[i] = (rsiValue > RSI_UpLevel); conditionsDown[i] = (rsiValue < RSI_BelowLevel); allTrueUp &= conditionsUp[i]; // If any is false, allTrueUp becomes false allTrueDown &= conditionsDown[i]; // If any is false, allTrueDown becomes false } if(allTrueUp) gRSISignal = "UP"; if(allTrueDown) gRSISignal = "BELOW"; } //+------------------------------------------------------------------+ //| Function to check Stochastic conditions | //+------------------------------------------------------------------+ void CheckStochasticConditions() { gStochasticSignal = "NoSignal"; bool conditionsUp[7], conditionsDown[7]; ENUM_TIMEFRAMES periods[7]; int count = 0; if(PeriodM1) periods[count++] = PERIOD_M1; if(PeriodM5) periods[count++] = PERIOD_M5; if(PeriodM15) periods[count++] = PERIOD_M15; if(PeriodM30) periods[count++] = PERIOD_M30; if(PeriodH1) periods[count++] = PERIOD_H1; if(PeriodH4) periods[count++] = PERIOD_H4; if(PeriodD1) periods[count++] = PERIOD_D1; if(count == 0) return; // Exit if no timeframes are selected bool allTrueUp = true, allTrueDown = true; for(int i = 0; i < count; i++) { double mainValue = iStochastic(gRandomSymbol, periods[i], Stochastic_Kperiod, Stochastic_Dperiod, Stochastic_Slowing, Stochastic_Method, Stochastic_PriceField, MODE_MAIN, Stochastic_Shift); double signalValue = iStochastic(gRandomSymbol, periods[i], Stochastic_Kperiod, Stochastic_Dperiod, Stochastic_Slowing, Stochastic_Method, Stochastic_PriceField, MODE_SIGNAL, Stochastic_Shift); conditionsUp[i] = (mainValue > Stochastic_UpLevel && signalValue > Stochastic_UpLevel); conditionsDown[i] = (mainValue < Stochastic_BelowLevel && signalValue < Stochastic_BelowLevel); allTrueUp &= conditionsUp[i]; // If any condition is false, allTrueUp becomes false allTrueDown &= conditionsDown[i]; // If any condition is false, allTrueDown becomes false } if(allTrueUp) gStochasticSignal = "UP"; if(allTrueDown) gStochasticSignal = "BELOW"; } //+------------------------------------------------------------------+ //| Function to check Bollinger Band conditions | //+------------------------------------------------------------------+ void CheckBollingerBandsConditions() { gBollingerBandsSignal = "NoSignal"; bool conditionsUp[7], conditionsDown[7]; ENUM_TIMEFRAMES periods[7]; int count = 0; if(PeriodM1) periods[count++] = PERIOD_M1; if(PeriodM5) periods[count++] = PERIOD_M5; if(PeriodM15) periods[count++] = PERIOD_M15; if(PeriodM30) periods[count++] = PERIOD_M30; if(PeriodH1) periods[count++] = PERIOD_H1; if(PeriodH4) periods[count++] = PERIOD_H4; if(PeriodD1) periods[count++] = PERIOD_D1; if(count == 0) return; // Exit if no timeframes are selected bool allTrueUp = true, allTrueDown = true; double bidPrice = MarketInfo(gRandomSymbol, MODE_BID); double askPrice = MarketInfo(gRandomSymbol, MODE_ASK); for(int i = 0; i < count; i++) { double upperBand = iBands(gRandomSymbol, periods[i], BollingerBands_Period, BollingerBands_deviation, BollingerBands_BandShift, BollingerBands_AppliedPrice, MODE_UPPER, BollingerBands_Shift); double lowerBand = iBands(gRandomSymbol, periods[i], BollingerBands_Period, BollingerBands_deviation, BollingerBands_BandShift, BollingerBands_AppliedPrice, MODE_LOWER, BollingerBands_Shift); conditionsUp[i] = (bidPrice > upperBand); conditionsDown[i] = (askPrice < lowerBand); allTrueUp &= conditionsUp[i]; // If any condition is false, allTrueUp becomes false allTrueDown &= conditionsDown[i]; // If any condition is false, allTrueDown becomes false } if(allTrueUp) gBollingerBandsSignal = "UP"; if(allTrueDown) gBollingerBandsSignal = "BELOW"; } //+------------------------------------------------------------------+ //| Indicator Possibilities | //+------------------------------------------------------------------+ void MultiTF_IndicatorPossibilities() { gMultiTimeFrameSignalStatus = "NoSignal"; string signals[3]; // Max size for RSI, Stochastic, and BollingerBands int count = 0; if(RSI) signals[count++] = gRSISignal; if(Stochastic) signals[count++] = gStochasticSignal; if(BollingerBands) signals[count++] = gBollingerBandsSignal; // Early exit if no indicators are enabled if(count == 0) return; bool allUP = true, allBELOW = true; for(int i = 0; i < count; i++) { if(signals[i] != "UP") allUP = false; if(signals[i] != "BELOW") allBELOW = false; } if(allUP) gMultiTimeFrameSignalStatus = "UP"; if(allBELOW) gMultiTimeFrameSignalStatus = "BELOW"; } [/CODE] I have also tried my best to debug and make the code working. i guess something missing which i am unable to find out. I request you to please review and let me know what changes should i make to make my code working. [/QUOTE]
Insert quotes…
Verification
Post reply
Top
Bottom
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.
Accept
Learn more…