Discover new MetaTrader 5 opportunities with MQL5 community and services
- 2023.07.20
- www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
- Transferring Indicators from MQL4 to MQL5
- Creating an Indicator with Multiple Indicator Buffers for Newbies
- Creating an Indicator with Graphical Control Options
because you copy only 2 bars in the copybuffer which means that the indicator would try to copy data from two bars and ignore any new bars that are available for calculation. this causes the plot to go missing
I fixed the script:
//+------------------------------------------------------------------+ //| t.mq5 | //| Copyright 2022, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2022, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" #property indicator_separate_window #property indicator_buffers 3 #property indicator_plots 1 #property indicator_type1 DRAW_LINE #property indicator_color1 clrGreen #property indicator_style1 STYLE_SOLID #property indicator_label1 "Line" #property indicator_width1 3 input int fastPeriod = 5; input int slowPeriod = 25; double indvalue[]; double fastma[]; double slowma[]; int handle_fast; int handle_slow; int max_bars; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { // Indicator buffers mapping SetIndexBuffer(0, indvalue, INDICATOR_DATA); handle_fast = iMA(_Symbol, PERIOD_CURRENT, fastPeriod, 0, MODE_EMA, PRICE_CLOSE); handle_slow = iMA(_Symbol, PERIOD_CURRENT, slowPeriod, 0, MODE_EMA, PRICE_CLOSE); if (handle_fast == INVALID_HANDLE || handle_slow == INVALID_HANDLE) { Print("Get MA Handle failed!"); return INIT_FAILED; } max_bars = Bars(Symbol(), Period()); 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[]) { // Calculate the number of bars to copy int to_copy = rates_total - prev_calculated; if (to_copy <= 0) to_copy = max_bars; // Copy data from indicator buffers CopyBuffer(handle_fast, 0, 0, to_copy, fastma); CopyBuffer(handle_slow, 0, 0, to_copy, slowma); // Calculate the indicator values for (int i = prev_calculated; i < rates_total; i++) { indvalue[i] = fastma[i] - slowma[i]; } // Return value of prev_calculated for the next call return rates_total; } //+------------------------------------------------------------------+ void OnDeinit(const int reason) { IndicatorRelease(handle_fast); IndicatorRelease(handle_slow); }
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