Put an Indicator inside and EA with Indicator-Only functions in MQL4

Put an Indicator inside and EA with Indicator-Only functions in MQL4

Manage alerts

Loading saved threads...

Pantelis Pap. · External communityPost link
External question — Stack Overflow Stack Exchange Author: Pantelis Pap. Original post: https://stackoverflow.com/questions/66496183 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. So I got a couple of ideas and I'd like to backtest them. And to do so, the indicator must run internally within the EA rather than take its value from the iCustom method. I've got the indicator free from a very nice guy from the internet (obviously...) and I've like to test around with it. Unfortunately the indicator mentioned is using the OnCalculate function which is only working within indicators and not EAs. So basically copy-pasting code inside EA is really not a option unfortunately. Didn't provide code as the problem is a simple as I describe it to keep it short. If you believe this would help, I am more than happy to provide that as well. EDIT. Added indicator code: int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { //--- int startIndex = MathMax(0, rates_total - prev_calculated - 1); // Only calculate up until the previous bar. We don't want to calculate the current bar. if (startIndex > 0) { for (int i = startIndex; i >= 1; i--) { double ohlcAvg = (open[i] + high[i] + low[i] + close[i])/4, vol = (double) tick_volume[i]; // Reset values when session changed if (TimeDay(time[i]) != TimeDay(__sessionStartTime)) { __sessionStartTime = time[i]; __ohlcvTotal = 0; __volumeTotal = 0; } __ohlcvTotal += ohlcAvg * vol; __volumeTotal += vol; ExtBuffer1[i] = NormalizeDouble(__ohlcvTotal/__volumeTotal, _Digits); } } return(rates_total - 1); }
Quote
Report
PaulB · External communityPost link
External answer — Stack Overflow Stack Exchange Author: PaulB Original post: https://stackoverflow.com/a/66654884 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. In most cases you can create a custom function within an EA that replicates an indicator by using buffers. I have done this numerous times for clients as it makes the EA a more robust solution when not relying on external indicators. Example code: //+------------------------------------------------------------------+ double customIndicator(int IDX, int timeFrame) //+------------------------------------------------------------------+ { double Buffer[1]; int buffersize=iBars(Symbol(), timeFrame); ArrayResize(Buffer, buffersize); ArraySetAsSeries(Buffer, true); int pos=buffersize-1; while(pos>=0) { // Indicator calculations Buffer[pos]=... pos--; } return(buffer[IDX]); }
Quote
Report

Post Reply

Quoted from Forex.com.bd-Editorial External question — Stack Overflow Stack Exchange Author: Pantelis Pap. Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/questions/66496183 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. So I got a couple of ideas and I'd like to backtest them. And to do so, the indicator must run internally within the EA rather than take its value from the iCustom method. I've got the indicator free from a very nice guy from the internet (obviously...) and I've like to test around with it. Unfortunately the indicator mentioned is using the OnCalculate function which is only working within indicators and not EAs. So basically copy-pasting code inside EA is really not a option unfortunately. Didn't provide code as the problem is a simple as I describe it to keep it short. If you believe this would help, I am more than happy to provide that as well. EDIT. Added indicator code: int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { //--- int startIndex = MathMax(0, rates_total - prev_calculated - 1); // Only calculate up until the previous bar. We don't want to calculate the current bar. if (startIndex > 0) { for (int i = startIndex; i >= 1; i--) { double ohlcAvg = (open[i] + high[i] + low[i] + close[i])/4, vol = (double) tick_volume[i]; // Reset values when session changed if (TimeDay(time[i]) != TimeDay(__sessionStartTime)) { __sessionStartTime = time[i]; __ohlcvTotal = 0; __volumeTotal = 0; } __ohlcvTotal += ohlcAvg * vol; __volumeTotal += vol; ExtBuffer1[i] = NormalizeDouble(__ohlcvTotal/__volumeTotal, _Digits); } } return(rates_total - 1); }

Cancel quote

Checking account access…