//±-----------------------------------------------------------------+
//| 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);
}
//±-----------------------------------------------------------------+ как этот индикатор перенести на платформу tradingview