//+------------------------------------------------------------------+
//| Custom Moving Averages.mq4 |
//| Copyright 2005-2015, MetaQuotes Software Corp. |
//| MQL4: automated forex trading, strategy tester and custom indicators with MetaTrader |
//+------------------------------------------------------------------+
#property copyright "2005-2015, MetaQuotes Software Corp."
#property link "MQL4: automated forex trading, strategy tester and custom indicators with MetaTrader"
#property description "Moving Average"
#property strict
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//--- indicator parameters
extern string MA1="--------- К-во баров апроксимации ----------";
input int InpMAPeriod=5; // Period
extern string TMA1="--------- TMA ----------";
extern string TimeFrame = "60";
extern int HalfLength = 150;
extern int Price = PRICE_TYPICAL;
extern double ATRMultiplier = 4.5;
extern int ATRPeriod = 500;
extern bool Interpolate = true;
extern string Line="----- Какую линию ТМА сглаживать 0 средняя, 1- верняя, 2- нижняя ------";
extern int Nline = 0;
//input int InpMAShift=0; // Shift
input ENUM_MA_METHOD InpMAMethod=MODE_SMA; // Method
//--- indicator buffer
double ExtLineBuffer[];
double tmai, tmai2;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit(void)
{
string short_name;
int draw_begin=InpMAPeriod-1;
IndicatorShortName(short_name+string(InpMAPeriod)+")");
IndicatorDigits(Digits);
//--- check for input
if(InpMAPeriod<2)
return(INIT_FAILED);
//--- drawing settings
SetIndexStyle(0,DRAW_LINE);
// SetIndexShift(0,InpMAShift);
SetIndexDrawBegin(0,draw_begin);
//--- indicator buffers mapping
SetIndexBuffer(0,ExtLineBuffer);
//--- initialization done
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Moving Average |
//+------------------------------------------------------------------+
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 for bars count
if(rates_total<InpMAPeriod-1 || InpMAPeriod<2)
return(0);
//--- counting from 0 to rates_total
ArraySetAsSeries(ExtLineBuffer,false);
ArraySetAsSeries(close,false);
//--- first calculation or number of bars was changed
if(prev_calculated==0)
ArrayInitialize(ExtLineBuffer,0);
//+------------------------------------------------------------------+
//| simple moving average |
//+------------------------------------------------------------------+
int i,limit;
//--- first calculation or number of bars was changed
if(prev_calculated==0)
{
limit=InpMAPeriod;
//--- calculate first visible value
double firstValue=0;
for(i=0; i<limit; i++)
firstValue+=iCustom(NULL, 0, "TMAbistr_v2", TimeFrame, HalfLength, Price, ATRMultiplier, ATRPeriod, Interpolate, Nline, i);//
// firstValue+=price;// исходн код
firstValue/=InpMAPeriod;
ExtLineBuffer[limit-1]=firstValue;
}
else
limit=prev_calculated-1;
//--- main loop
// int j;
for(i=limit; i<rates_total && !IsStopped(); i++)
ExtLineBuffer=ExtLineBuffer[i-1]+(iCustom(NULL, 0, "TMAbistr_v2", TimeFrame, HalfLength, Price, ATRMultiplier, ATRPeriod, Interpolate, Nline, i)-
iCustom(NULL, 0, "TMAbistr_v2", TimeFrame, HalfLength, Price, ATRMultiplier, ATRPeriod, Interpolate, Nline, (i-InpMAPeriod)))/InpMAPeriod;
// ---------- проба вар 4 => пробую делить только последн член ------------
// ExtLineBuffer=ExtLineBuffer[i-1]+iCustom(NULL, 0, "TMAbistr_v2", TimeFrame, HalfLength, Price, ATRMultiplier, ATRPeriod, Interpolate, Nline, i)-
// (iCustom(NULL, 0, "TMAbistr_v2", TimeFrame, HalfLength, Price, ATRMultiplier, ATRPeriod, Interpolate, Nline, (i-InpMAPeriod)))/InpMAPeriod;
Print(" op100 i = ",i," limit = ",limit," rates_total = ",rates_total," i-InpMAPeriod = ",i-InpMAPeriod );
// ---------- проба вар 3 => array out of range in ------------
// j=i-InpMAPeriod;// array out of range in
// ExtLineBuffer=ExtLineBuffer[i-1]+(iCustom(NULL, 0, "TMAbistr_v2", TimeFrame, HalfLength, Price, ATRMultiplier, ATRPeriod, Interpolate, Nline, i)-
// iCustom(NULL, 0, "TMAbistr_v2", TimeFrame, HalfLength, Price, ATRMultiplier, ATRPeriod, Interpolate, Nline, j))/InpMAPeriod;
// ExtLineBuffer=ExtLineBuffer[i-1]+(price-price[i-InpMAPeriod])/InpMAPeriod;// исходный код
return(rates_total);
}// OnCalculate
//+------------------------------------------------------------------+
//| exponential moving average |
//+------------------------------------------------------------------+