//+------------------------------------------------------------------+
//| # Two MA.mq4 |
//| Copyright 2013, Kolosov D.E. |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, Kolosov D.E."
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_width1 2
#property indicator_color2 Blue
#property indicator_width2 2
extern string ������������_���_��_?;
extern bool ������������_? = true;
extern string ���������_������_��;
extern int ������_������_�� = 100;
extern int �����_������_�� = 0;
extern int �����_������_�� = 0;
extern string ���������_������_��;
extern int ������_������_�� = 200;
extern int �����_������_�� = 0;
extern int �����_������_�� = 0;
extern color ����_����� = Green;
extern int ������_������ = 10;
double ExtMapBuffer1[];
double ExtMapBuffer2[];
string ������_�� = "one";
string ������_�� = "two";
int ExtCountedBars=0;
int init()
{
int draw_begin, max;
string short_name;
SetIndexStyle(0, DRAW_LINE);
SetIndexShift(0, �����_������_��);
SetIndexStyle(1, DRAW_LINE);
SetIndexShift(1, �����_������_��);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
max = MathMax(������_������_��,������_������_��);
if (max < 2) max = MathMin(������_������_��,������_������_��);
draw_begin = max - 1;
short_name = "";
switch(�����_������_��)
{
case 1 : short_name = "EMA ("; draw_begin=0; break;
case 2 : short_name = "SMMA ("; break;
case 3 : short_name = "LWMA ("; break;
default :
�����_������_�� = 0;
short_name = "SMA (";
}
if (������������_?) {
switch(�����_������_��)
{
case 1 : short_name = short_name + ������_������_�� + ") " + "EMA ("; draw_begin=0; break;
case 2 : short_name = short_name + ������_������_�� + ") " + "SMMA ("; break;
case 3 : short_name = short_name + ������_������_�� + ") " + "LWMA ("; break;
default :
�����_������_�� = 0;
short_name = short_name + ������_������_�� + ") " + "SMA (";
}
short_name = short_name + ������_������_�� + ")";
} else short_name = short_name + ������_������_�� + ")";
IndicatorShortName(short_name);
SetIndexDrawBegin(0,draw_begin);
SetIndexDrawBegin(1,draw_begin);
SetIndexBuffer(0, ExtMapBuffer1);
SetIndexBuffer(1, ExtMapBuffer2);
return(0);
}
int deinit()
{
ObjectDelete(������_��);
if (������������_?) ObjectDelete(������_��);
return (0);
}
int start()
{
if (Bars <= MathMax(������_������_��,������_������_��)) return(0);
ExtCountedBars = IndicatorCounted();
if (ExtCountedBars < 0) return(-1);
if (ExtCountedBars > 0) ExtCountedBars--;
switch(�����_������_��)
{
case 0 : sma(������_������_��,1); break;
case 1 : ema(������_������_��,1); break;
case 2 : smma(������_������_��,1); break;
case 3 : lwma(������_������_��,1);
}
if (������������_?) {
switch(�����_������_��)
{
case 0 : sma(������_������_��,2); break;
case 1 : ema(������_������_��,2); break;
case 2 : smma(������_������_��,2); break;
case 3 : lwma(������_������_��,2);
}
}
upd_object();
return(0);
}
//+------------------------------------------------------------------+
//| Simple Moving Average |
//+------------------------------------------------------------------+
void sma(int ������_��, int �����_������)
{
double sum = 0;
int i, pos = Bars - ExtCountedBars - 1;
if (pos < ������_��) pos = ������_��;
for (i = 1; i < ������_��; i++, pos--)
sum += Close[pos];
while(pos >= 0)
{
sum += Close[pos];
if (�����_������ == 1) ExtMapBuffer1[pos] = sum/������_��;
if (�����_������ == 2) ExtMapBuffer2[pos] = sum/������_��;
sum -= Close[pos+������_��-1];
pos--;
}
if (ExtCountedBars < 1) {
for (i = 1; i < ������_��; i++) {
if (�����_������ == 1) ExtMapBuffer1[Bars-i] = 0;
if (�����_������ == 2) ExtMapBuffer2[Bars-i] = 0;
}
}
}
//+------------------------------------------------------------------+
//| Exponential Moving Average |
//+------------------------------------------------------------------+
void ema(int ������_��, int �����_������)
{
double pr = 2.0/(������_�� + 1);
int pos = Bars-2;
if (ExtCountedBars > 2) pos = Bars - ExtCountedBars - 1;
while(pos >= 0)
{
if (pos == Bars-2) {
if (�����_������ == 1) ExtMapBuffer1[pos+1] = Close[pos+1];
if (�����_������ == 2) ExtMapBuffer2[pos+1] = Close[pos+1];
}
if (�����_������ == 1) ExtMapBuffer1[pos] = Close[pos]*pr + ExtMapBuffer1[pos+1]*(1-pr);
if (�����_������ == 2) ExtMapBuffer2[pos] = Close[pos]*pr + ExtMapBuffer2[pos+1]*(1-pr);
pos--;
}
}
//+------------------------------------------------------------------+
//| Smoothed Moving Average |
//+------------------------------------------------------------------+
void smma(int ������_��, int �����_������)
{
double sum = 0;
int i, k, pos = Bars - ExtCountedBars + 1;
pos = Bars - ������_��;
if (pos > Bars-ExtCountedBars) pos = Bars-ExtCountedBars;
while (pos >= 0) {
if (pos == Bars - ������_��) {
for (i = 0, k = pos; i < ������_��; i++, k++) {
sum += Close[k];
if (�����_������ == 1) ExtMapBuffer1[k] = 0;
if (�����_������ == 2) ExtMapBuffer2[k] = 0;
}
} else {
if (�����_������ == 1) sum = ExtMapBuffer1[pos+1]*(������_��-1)+Close[pos];
if (�����_������ == 2) sum = ExtMapBuffer2[pos+1]*(������_��-1)+Close[pos];
}
if (�����_������ == 1) ExtMapBuffer1[pos] = sum/������_��;
if (�����_������ == 2) ExtMapBuffer2[pos] = sum/������_��;
pos--;
}
}
//+------------------------------------------------------------------+
//| Linear Weighted Moving Average |
//+------------------------------------------------------------------+
void lwma(int ������_��, int �����_������)
{
double sum = 0.0, lsum = 0.0;
double price;
int i, weight = 0, pos = Bars - ExtCountedBars - 1;
if (pos < ������_��) pos = ������_��;
for (i = 1; i <= ������_��; i++, pos--) {
price = Close[pos];
sum += price*i;
lsum += price;
weight+= i;
}
pos++;
i = pos + ������_��;
while (pos >= 0) {
if (�����_������ == 1) ExtMapBuffer1[pos] = sum/weight;
if (�����_������ == 2) ExtMapBuffer2[pos] = sum/weight;
if (pos == 0) break;
pos--;
i--;
price = Close[pos];
sum = sum - lsum + price*������_��;
lsum -= Close[i];
lsum += price;
}
if (ExtCountedBars < 1) {
for (i = 1; i < ������_��; i++) {
if (�����_������ == 1) ExtMapBuffer1[Bars-i] = 0;
if (�����_������ == 2) ExtMapBuffer2[Bars-i] = 0;
}
}
}
void upd_object()
{
string �����_������_�� = " MA" + ������_������_��;
string �����_������_�� = " MA" + ������_������_��;
int �����_1 = Time[0] + Period()*60*�����_������_��;
int �����_2 = Time[0] + Period()*60*�����_������_��;
if (ObjectFind(������_��) == -1) {
ObjectCreate( ������_��, OBJ_TEXT, 0, 0, 0);
ObjectSet( ������_��, OBJPROP_TIME1, �����_1);
ObjectSet( ������_��, OBJPROP_PRICE1, ExtMapBuffer1[0]);
ObjectSetText(������_��, �����_������_��, ������_������, "Arial", ����_�����);
} else {
ObjectSet( ������_��, OBJPROP_TIME1, �����_1);
ObjectSet( ������_��, OBJPROP_PRICE1, ExtMapBuffer1[0]);
ObjectSetText(������_��, �����_������_��, ������_������, "Arial", ����_�����);
}
if (������������_?) {
if (ObjectFind(������_��) == -1) {
ObjectCreate( ������_��, OBJ_TEXT, 0, 0, 0);
ObjectSet( ������_��, OBJPROP_TIME1, �����_2);
ObjectSet( ������_��, OBJPROP_PRICE1, ExtMapBuffer2[0]);
ObjectSetText(������_��, �����_������_��, ������_������, "Arial", ����_�����);
} else {
ObjectSet( ������_��, OBJPROP_TIME1, �����_2);
ObjectSet( ������_��, OBJPROP_PRICE1, ExtMapBuffer2[0]);
ObjectSetText(������_��, �����_������_��, ������_������, "Arial", ����_�����);
}}
}