- Bong Bungalan Jr.: I wish to solicit any thoughts or suggestions on how to handle this case efficiently in OnTick() function aside from series of "If" statements.
Why do you think you need a "series of if statements?"
-
Code your condition as a function and pass in the TF.
- FuncNewBar as to have three separate static variables (one per TF) or be three separate functions.
I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
Running EA once at the start of each bar - MQL4 programming forum 2011.05.06void OnTick(){ static datetime curTime=0; datetime preTime = curTime; curTime = iTime(NULL, SmallestTimeFrame,0); bool isNewBarSmall = preTime != curTime; if(!isNewBarSmall) return; // You said "efficient." bool isNewBarMiddle = curTime % PeriodSeconds(MiddleTimeFrame) == 0; bool isNewBarHigher = curTime % PeriodSeconds(HigherTimeframe) == 0;
@William Roeder - Thank you for your thoughts. As to the question of "why series of if statements" - this is my humble (noob) way of solving this problem and I am thinking there must be a better way.
To make this clearer, here is a snippet of my OnTick() function for you to critique. I wish to learn how to formulate algorithm to solve problems efficiently since I do not have any CS background and I am just started learning to code.
Also, _comment is my way of checking if signals have been detected by counting them (wondering too if there is a more efficient way).
void OnTick() { double ind_MiddleBB, ind_UpperBB, ind_LowerBB ; _comment = "Testing NewBarFunction: \nHigherTF: " + string (HTSignalCounter) + "\n"; _comment = _comment + "CSM Buy : " + string(_BuysignalInHigherTF) + "\n"; _comment = _comment + "CSM Sell : " + string(_SellsignalInHigherTF) + "\n"; _comment = _comment + "MIddle TF: " + string(MTSignalCounter) + "\n"; _comment = _comment + "Smallest TF: " + string(STSignalCounter) + "\n"; Comment (_comment); // if (FuncNewBar(HigherTF)) HTSignalCounter = HTSignalCounter +1; // if (FuncNewBar(MiddleTF)) MTSignalCounter = MTSignalCounter +1; // if (FuncNewBar(SmallestTF)) STSignalCounter = STSignalCounter +1; if(FuncNewBar(HigherTF)) { ind_MiddleBB = FuncInd_BBand(ind_BBandPeriod,HigherTF, MODE_MAIN, 0); ind_UpperBB = FuncInd_BBand(ind_BBandPeriod,HigherTF, MODE_UPPER, 0); ind_LowerBB = FuncInd_BBand(ind_BBandPeriod,HigherTF, MODE_LOWER, 0); if (SignalCSM_SELL(HigherTF,0)) { HTSignalCounter = HTSignalCounter +1; Print(" CSM Sell SIgnal: "); // Comment (_comment); _SellsignalInHigherTF = true; _BuysignalInHigherTF = false; } else if (SignalCSM_BUY(HigherTF,0)) { HTSignalCounter = HTSignalCounter +1; Print(" CSM Buy SIgnal: "); _BuysignalInHigherTF = true; _SellsignalInHigherTF = false; } } // Check Confirmation in Middle Time Frames if Signals occured in HigherTF if (FuncNewBar(MiddleTF)) { if (_BuysignalInHigherTF) { //Look for Extreme Buy on H1 (MiddleTF) if(SignalExtremeBuy(MiddleTF, 1)) { _BuysignalConfirmed = true; _SellsignalConfirmed = false; MTSignalCounter = MTSignalCounter + 1; } } else if (_SellsignalInHigherTF) { // Look for Extreme Sell on H1 if(SignalExtremeSell(MiddleTF, 1)) { _SellsignalConfirmed = true; _BuysignalConfirmed = false; MTSignalCounter = MTSignalCounter + 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
I wish to solicit any thoughts or suggestions on how to handle this case efficiently in OnTick() function aside from series of "If" statements.
THank you in advanced.