Jiyu:
Hi,
I created a simple custom indicator to display a ratio between 2 instruments. It's display a line, calcul is ok. But now i want to add an EMA7 on this custom indicator and when i try it display nothing. Could you help me? Thanks in advance :)
My code:
for(i = 0; i < limit; i++) { EMA7[i] = iMAOnArray(Ratio, 0, 7, 0, MODE_EMA, i ); }
Why are you calculating ema value all from start?
I don't think there is a problem except long loading time which is mostly due to iMAOnArray calculating everything from start on every single iteration.
Added that you are loading data from another chart that adds to load time.
Yashar Seyyedin #:
I don't think that's a loading problem. Perhaps something in my for loop (i'm a beginner). But i would like to try your idea: how to display my indicator only from now?
Why are you calculating ema value all from start?
I don't think there is a problem except long loading time which is mostly due to iMAOnArray calculating everything from start on every single iteration.
Added that you are loading data from another chart that adds to load time.
//+------------------------------------------------------------------+ //| SpreadV1.mq5 | //| Jiyu | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Jiyu" #property version "1.00" #property indicator_separate_window #property indicator_buffers 2 #property indicator_plots 2 #property indicator_label1 "Ratio" #property indicator_type1 DRAW_LINE #property indicator_color1 clrGray #property indicator_style1 STYLE_SOLID #property indicator_width1 1 #property indicator_label2 "EMA7" #property indicator_type2 DRAW_LINE #property indicator_color2 clrRed #property indicator_style2 STYLE_SOLID #property indicator_width2 1 input string Symbol01 = "EURUSD"; input string Symbol02 = "GBPUSD"; double Ratio[]; double EMA7[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { SetIndexBuffer(0, Ratio, INDICATOR_DATA); SetIndexBuffer(1, EMA7, INDICATOR_DATA); ArraySetAsSeries(Ratio,true); ArraySetAsSeries(EMA7,true); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ 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[]) { MqlRates rates1[]; ArraySetAsSeries(rates1,true); MqlRates rates2[]; ArraySetAsSeries(rates2,true); int limit = MathMax(rates_total-prev_calculated-2 ,1); for(int i = limit; i >=0 ; i--) { bool b1=CopyRates(Symbol01, 0, i, 1, rates1); bool b2=CopyRates(Symbol02, 0, i, 1, rates2); if(!b1 || !b2) { Print("Problem loading data!"); return prev_calculated; } Ratio[i] = rates1[0].close / rates2[0].close; EMA7[i]=EMA7[i+1]*(1-2.0/8.0)+Ratio[i]*2.0/8.0; } return(rates_total); }
// After calculating all Ratio values if(limit > 0) { // Calculate EMA for the entire Ratio array at once, outside the loop double previousEMA = Ratio[0]; // Initial EMA value can be the first Ratio value or another starting point for(i = 0; i < limit; i++) { double currentPrice = Ratio[i]; EMA7[i] = (currentPrice * (2.0 / (7 + 1))) + (previousEMA * (1 - (2.0 / (7 + 1)))); previousEMA = EMA7[i]; // Update previousEMA for the next iteration } }
I modified the script posted by Yashar above so that it won't give errors when switching to different timeframes
//+------------------------------------------------------------------+ //| SpreadV1.mq5 | //| Jiyu | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Jiyu" #property version "1.00" #property indicator_separate_window #property indicator_buffers 2 #property indicator_plots 2 #property indicator_label1 "Ratio" #property indicator_type1 DRAW_LINE #property indicator_color1 clrGray #property indicator_style1 STYLE_SOLID #property indicator_width1 1 #property indicator_label2 "EMA7" #property indicator_type2 DRAW_LINE #property indicator_color2 clrRed #property indicator_style2 STYLE_SOLID #property indicator_width2 1 input string Symbol01 = "EURUSD"; input string Symbol02 = "GBPUSD"; input int bars = 1000; double Ratio[]; double EMA7[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { SetIndexBuffer(0, Ratio, INDICATOR_DATA); SetIndexBuffer(1, EMA7, INDICATOR_DATA); ArraySetAsSeries(Ratio,true); ArraySetAsSeries(EMA7,true); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ 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[]) { MqlRates rates1[]; ArraySetAsSeries(rates1,true); MqlRates rates2[]; ArraySetAsSeries(rates2,true); // Copy rates data for both symbols bool b1 = CopyRates(Symbol01, 0, 0, rates_total, rates1); bool b2 = CopyRates(Symbol02, 0, 0, rates_total, rates2); if (!b1 || !b2) { Print("Problem loading data!"); return prev_calculated; } // Calculate Ratio and EMA7 up until the defined amount of bars for(int i = bars; i >= 0; i--){ Ratio[i] = rates1[i].close / rates2[i].close; if((i+1) > 0) EMA7[i] = EMA7[i+1] * (1 - 2.0/8.0) + Ratio[i] * 2.0/8.0; } return(rates_total); }
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi,
I created a simple custom indicator to display a ratio between 2 instruments. It's display a line, calcul is ok. But now i want to add an EMA7 on this custom indicator and when i try it display nothing. Could you help me? Thanks in advance :)
My code: