//±-----------------------------------------------------------------+ // MAA.mq5 //…
- User
//±-----------------------------------------------------------------+ //| MAA.mq5 | //| Copyright 2024, MetaQuotes Ltd. | //| https://www.mql5.com | //±-----------------------------------------------------------------+ #property copyright "Copyright 2024, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" #property indicator_separate_window #property indicator_buffers 6 #property indicator_plots 5 // Plot settings for MA difference #property indicator_label1 "MADiff" #property indicator_type1 DRAW_LINE #property indicator_color1 clrRed #property indicator_style1 STYLE_SOLID #property indicator_width1 1 // Plot settings for Mean + StdDev #property indicator_label2 "Mean + StdDev" #property indicator_type2 DRAW_LINE #property indicator_color2 clrGreen #property indicator_style2 STYLE_DOT #property indicator_width2 1 // Plot settings for Mean - StdDev #property indicator_label3 "Mean - StdDev" #property indicator_type3 DRAW_LINE #property indicator_color3 clrGreen #property indicator_style3 STYLE_DOT #property indicator_width3 1 // Plot settings for Mean + 2StdDev #property indicator_label4 "Mean + 2StdDev" #property indicator_type4 DRAW_LINE #property indicator_color4 clrBlue #property indicator_style4 STYLE_DASH #property indicator_width4 1 // Plot settings for Mean - 2StdDev #property indicator_label5 "Mean - 2StdDev" #property indicator_type5 DRAW_LINE #property indicator_color5 clrBlue #property indicator_style5 STYLE_DASH #property indicator_width5 1 // Input parameters input int period; // Период скользящей средней input ENUM_TIMEFRAMES timeframe = PERIOD_CURRENT; // Таймфрейм скользящей средней // Indicator buffers double MAaBuffer[]; double MADiffBuffer[]; double MeanPlusStdDevBuffer[]; double MeanMinusStdDevBuffer[]; double MeanPlus2StdDevBuffer[]; double MeanMinus2StdDevBuffer[]; //±-----------------------------------------------------------------+ //| Custom indicator initialization function | //±-----------------------------------------------------------------+ int OnInit() { // Indicator buffers mapping SetIndexBuffer(0, MADiffBuffer, INDICATOR_DATA); SetIndexBuffer(5, MAaBuffer, INDICATOR_CALCULATIONS); SetIndexBuffer(1, MeanPlusStdDevBuffer, INDICATOR_DATA); SetIndexBuffer(2, MeanMinusStdDevBuffer, INDICATOR_DATA); SetIndexBuffer(3, MeanPlus2StdDevBuffer, INDICATOR_DATA); SetIndexBuffer(4, MeanMinus2StdDevBuffer, INDICATOR_DATA);
// Initialization done 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[]) { // Check if there are enough bars for calculation if(rates_total < period) return(0);
// Starting point for calculation int start = prev_calculated > 0 ? prev_calculated - 1 : period - 1;
// Calculate moving average for(int i = start; i < rates_total; i++) { double sum = 0.0; for(int j = 0; j < period; j++) { sum += close[i - j]; } MAaBuffer[i] = sum / period; // Calculate (MA - price) / price MADiffBuffer[i] = ((MAaBuffer[i] - close[i]) / close[i]) * 100; }
// Calculate mean and standard deviation for MADiffBuffer double mean_sum = 0.0; double stddev_sum = 0.0; int count = 0;
// Calculations over the entire history starting from period - 1 for(int i = period - 1; i < rates_total; i++) { mean_sum += MADiffBuffer[i]; count++; } double mean = mean_sum / count;
// Calculate standard deviation for(int i = period - 1; i < rates_total; i++) { stddev_sum += MathPow(MADiffBuffer[i] - mean, 2); } double stddev = MathSqrt(stddev_sum / count);
// Populate the new buffers with mean and deviations for(int i = period - 1; i < rates_total; i++) { MeanPlusStdDevBuffer[i] = mean + stddev; MeanMinusStdDevBuffer[i] = mean - stddev; MeanPlus2StdDevBuffer[i] = mean + 2 * stddev; MeanMinus2StdDevBuffer[i] = mean - 2 * stddev; }
Comment(StringFormat("Mean: %.5f, StdDev: %.5f", mean, stddev));
// Return value of prev_calculated for next call return(rates_total); } //±-----------------------------------------------------------------+ нужно что бы период менялся при переключении таймфрейма изначально задавать разные периоды для разных таймреймов
- Assistant
To accomplish dynamic adjustment of the period for the moving average based on the selected timeframe, you need to handle the timeframe switching event and adjust the period accordingly. Here is how you can achieve this in your MQL5 indicator code.
- Define default periods for each timeframe.
- Modify the
OnInit()function to set the initial period based on the current timeframe. - Change the period dynamically within
OnCalculate()or by handling a relevant event.
Here's the updated code with these adjustments:
//±-----------------------------------------------------------------+ //| MAA.mq5 | //| Copyright 2024, MetaQuotes Ltd. | //| https://www.mql5.com | //±-----------------------------------------------------------------+ #property copyright "Copyright 2024, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" #property indicator_separate_window #property indicator_buffers 6 #property indicator_plots 5 // Plot settings for MA difference #property indicator_label1 "MADiff" #property indicator_type1 DRAW_LINE #property indicator_color1 clrRed #property indicator_style1 STYLE_SOLID #property indicator_width1 1 // Plot settings for Mean + StdDev #property indicator_label2 "Mean + StdDev" #property indicator_type2 DRAW_LINE #property indicator_color2 clrGreen #property indicator_style2 STYLE_DOT #property indicator_width2 1 // Plot settings for Mean - StdDev #property indicator_label3 "Mean - StdDev" #property indicator_type3 DRAW_LINE #property indicator_color3 clrGreen #property indicator_style3 STYLE_DOT #property indicator_width3 1 // Plot settings for Mean + 2StdDev #property indicator_label4 "Mean + 2StdDev" #property indicator_type4 DRAW_LINE #property indicator_color4 clrBlue #property indicator_style4 STYLE_DASH #property indicator_width4 1 // Plot settings for Mean - 2StdDev #property indicator_label5 "Mean - 2StdDev" #property indicator_type5 DRAW_LINE #property indicator_color5 clrBlue #property indicator_style5 STYLE_DASH #property indicator_width5 1 // Input parameters input ENUM_TIMEFRAMES timeframe = PERIOD_CURRENT; // Таймфрейм скользящей средней // Indicator buffers double MAaBuffer[]; double MADiffBuffer[]; double MeanPlusStdDevBuffer[]; double MeanMinusStdDevBuffer[]; double MeanPlus2StdDevBuffer[]; double MeanMinus2StdDevBuffer[]; // Define periods for different timeframes int PeriodForTimeframe(ENUM_TIMEFRAMES tf) { switch(tf) { case PERIOD_M1 : return 12; case PERIOD_M5 : return 10; case PERIOD_M15 : return 8; case PERIOD_M30 : return 12; case PERIOD_H1 : return 14; case PERIOD_H4 : return 16; case PERIOD_D1 : return 20; case PERIOD_W1 : return 30; case PERIOD_MN1 : return 50; default : return 14; // Default period } } // Current period based on timeframe int period; //±-----------------------------------------------------------------+ //| Custom indicator initialization function | //±-----------------------------------------------------------------+ int OnInit() { // Determine the period based on the initial timeframe period = PeriodForTimeframe(timeframe); // Indicator buffers mapping SetIndexBuffer(0, MADiffBuffer, INDICATOR_DATA); SetIndexBuffer(5, MAaBuffer, INDICATOR_CALCULATIONS); SetIndexBuffer(1, MeanPlusStdDevBuffer, INDICATOR_DATA); SetIndexBuffer(2, MeanMinusStdDevBuffer, INDICATOR_DATA); SetIndexBuffer(3, MeanPlus2StdDevBuffer, INDICATOR_DATA); SetIndexBuffer(4, MeanMinus2StdDevBuffer, INDICATOR_DATA); // Initialization done 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[]) { // Adjust the period dynamically if needed period = PeriodForTimeframe(timeframe); // Check if there are enough bars for calculation if(rates_total < period) return(0); // Starting point for calculation int start = prev_calculated > 0 ? prev_calculated - 1 : period - 1; // Calculate moving average for(int i = start; i < rates_total; i++) { double sum = 0.0; for(int j = 0; j < period; j++) { sum += close[i - j]; } MAaBuffer[i] = sum / period; // Calculate (MA - price) / price MADiffBuffer[i] = ((MAaBuffer[i] - close[i]) / close[i]) * 100; } // Calculate mean and standard deviation for MADiffBuffer double mean_sum = 0.0; double stddev_sum = 0.0; int count = 0; // Calculations over the entire history starting from period - 1 for(int i = period - 1; i < rates_total; i++) { mean_sum += MADiffBuffer[i]; count++; } double mean = mean_sum / count; // Calculate standard deviation for(int i = period - 1; i < rates_total; i++) { stddev_sum += MathPow(MADiffBuffer[i] - mean, 2); } double stddev = MathSqrt(stddev_sum / count); // Populate the new buffers with mean and deviations for(int i = period - 1; i < rates_total; i++) { MeanPlusStdDevBuffer[i] = mean + stddev; MeanMinusStdDevBuffer[i] = mean - stddev; MeanPlus2StdDevBuffer[i] = mean + 2 * stddev; MeanMinus2StdDevBuffer[i] = mean - 2 * stddev; } Comment(StringFormat("Mean: %.5f, StdDev: %.5f", mean, stddev)); // Return value of prev_calculated for next call return(rates_total); }Key Changes:
- Function
PeriodForTimeframe(): Determines the period based on the selected timeframe. - Dynamic Period Adjustment: The
periodvariable is dynamically adjusted withinOnInit()andOnCalculate()functions based on the selected timeframe.
This should allow your indicator to adapt automatically to different timeframes, using the pre-defined periods for each timeframe. Ensure that all required safeguards and validations are in place to ensure the code's robustness and prevent potential errors due to unexpected timeframe changes or data inconsistencies.