0% found this document useful (0 votes)
13 views

step moving average mt5 coding

The document is a custom MetaTrader 5 (MQ5) indicator script named CustomMA, designed to calculate a step moving average based on user-defined parameters such as length, sensitivity factor, and step size. It includes initialization and calculation functions that manage indicator buffers for displaying moving average values, and allows for different color modes based on the closing price relative to the calculated average. The script contains placeholder functions for calculating step size and moving averages, which need to be implemented for full functionality.

Uploaded by

cloromax323
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

step moving average mt5 coding

The document is a custom MetaTrader 5 (MQ5) indicator script named CustomMA, designed to calculate a step moving average based on user-defined parameters such as length, sensitivity factor, and step size. It includes initialization and calculation functions that manage indicator buffers for displaying moving average values, and allows for different color modes based on the closing price relative to the calculated average. The script contains placeholder functions for calculating step size and moving averages, which need to be implemented for full functionality.

Uploaded by

cloromax323
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

//+------------------------------------------------------------------+

//| CustomMA.mq5 |
//| Copyright 2023, Your Name |
//| https://www.yourwebsite.com |
//+------------------------------------------------------------------+
#property copyright "Your Name"
#property link "https://www.yourwebsite.com"
#property version "1.00"
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Blue
#property indicator_color2 Green
#property indicator_color3 Red

//--- input parameters


input int Length = 14; // Length for calculation
input double Kv = 1.0; // Sensitivity factor
input double StepSize = 1.0; // Step size
input int ColorMode = 0; // Color mode (0, 1, 2)

//--- indicator buffers


double UpBuffer[];
double DnBuffer[];
double LineBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
SetIndexBuffer(0, LineBuffer);
SetIndexBuffer(1, UpBuffer);
SetIndexBuffer(2, DnBuffer);

ArraySetAsSeries(LineBuffer, true);
ArraySetAsSeries(UpBuffer, true);
ArraySetAsSeries(DnBuffer, true);

return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const double &price[],
const long &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const double &spread[])
{
int limit = rates_total - prev_calculated;

for(int shift = limit; shift >= 0; shift--)


{
int Step = StepSizeCalc(Length, Kv, StepSize, shift);
Comment("StepSize= ", Step);

double StepMA = StepMACalc(high, low, Step, shift) + (Percentage / 100.0) *


Step * Point;

if (ColorMode == 0)
LineBuffer[shift] = StepMA;

if (ColorMode == 1)
{
if (close[shift] > StepMA)
UpBuffer[shift] = StepMA;
else
DnBuffer[shift] = StepMA;
}
else if (ColorMode == 2)
{
if (close[shift] > StepMA)
{
UpBuffer[shift] = StepMA;
DnBuffer[shift] = EMPTY_VALUE; // Clear the down buffer
}
else
{
DnBuffer[shift] = StepMA;
UpBuffer[shift] = EMPTY_VALUE; // Clear the up buffer
}
}
}

return(rates_total);
}

//+------------------------------------------------------------------+
//| Function to calculate step size |
//+------------------------------------------------------------------+
int StepSizeCalc(int Length, double Kv, double StepSize, int shift)
{
// Your logic to calculate step size based on Length, Kv, and StepSize
// This is a placeholder; implement your calculation here
return StepSize; // Example return
}

//+------------------------------------------------------------------+
//| Function to calculate step moving average |
//+------------------------------------------------------------------+
double StepMACalc(const double &high[], const double &low[], int Step, int shift)
{
// Your logic to calculate the moving average based on high and low prices
// This is a placeholder; implement your calculation here
return (high[shift] + low[shift]) / 2; // Example return
}

You might also like