Как выполнить алерт только один раз?

[Удален]  

Есть простейший индикатор, который показывает пересечение МА, но работает он немного не правильно.

Во первых, если пересечение произошло когда терминал был открыт и индикатор прицеплен к графику, то стрелка, указывающая пересечение не появляется. Она появится после перезапуска терминала.


Во вторых, алерт о пересечении срабатывает на каждом тике, а нужно, чтобы 1 раз и только по последнему пересечению.


Код привожу ниже. Подскажите пожалуйста, как побороть.


#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link "https://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Lime
#property indicator_color2 OrangeRed

//---- input parameters
extern int FastMA = 7;
extern int FastMetod = 1; //0=SMA, 1=EMA, 2=SMMA, 3=LWMA
extern int FastType = 5; //CLOSE=0, OPEN=1, HIGH=2, LOW=3, MEDIAN=4, TYPICAL=5, WEIGHTED=6
extern int SlowMA = 15;
extern int SlowMetod = 1; //0=SMA, 1=EMA, 2=SMMA, 3=LWMA
extern int SlowType = 5; //CLOSE=0, OPEN=1, HIGH=2, LOW=3, MEDIAN=4, TYPICAL=5, WEIGHTED=6
extern string ExtSoundFileName = "";


//---- buffers
double CrossUp[];
double CrossDown[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0, DRAW_ARROW, EMPTY);
SetIndexArrow(0, 233);
SetIndexBuffer(0, CrossUp);
SetIndexStyle(1, DRAW_ARROW, EMPTY);
SetIndexArrow(1, 234);
SetIndexBuffer(1, CrossDown);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int limit, i, counter;
double FastMA_now, SlowMA_now, FastMA_previous, SlowMA_previous, FastMA_after, SlowMA_after;
double Range, AvgRange;
string message;
int counted_bars = IndicatorCounted();
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;

limit = Bars-counted_bars;

for(i = 0; i <= limit; i++) {

counter = i;
Range = 0;
AvgRange = 0;
for (counter = i ;counter <= i+9; counter++)
{
AvgRange = AvgRange+MathAbs(High[counter]-Low[counter]);
}
Range = AvgRange/10;
//----
FastMA_now = iMA(NULL, 0, FastMA, 0, FastMetod, FastType, i);
FastMA_previous = iMA(NULL, 0, FastMA, 0, FastMetod, FastType, i+1);
FastMA_after = iMA(NULL, 0, FastMA, 0, FastMetod, FastType, i-1);

SlowMA_now = iMA(NULL, 0, SlowMA, 0, SlowMetod, SlowType, i);
SlowMA_previous = iMA(NULL, 0, SlowMA, 0, SlowMetod, SlowType, i+1);
SlowMA_after = iMA(NULL, 0, SlowMA, 0, SlowMetod, SlowType, i-1);

if ((FastMA_now > SlowMA_now) && (FastMA_previous < SlowMA_previous) && (FastMA_after > SlowMA_after)) {
CrossUp[i] = Low[i] - Range*0.5;
message = StringConcatenate(Symbol()," MA 7 пересекла MA 15 вверх в ",TimeToStr(TimeLocal(),TIME_SECONDS)," @",Bid,".");
Alert(message);
if ( ExtSoundFileName != "" )
PlaySound( ExtSoundFileName );
}

else if ((FastMA_now < SlowMA_now) && (FastMA_previous > SlowMA_previous) && (FastMA_after < SlowMA_after)) {
CrossDown[i] = High[i] + Range*0.5;
message = StringConcatenate(Symbol()," MA 7 пересекла MA 15 вниз в ",TimeToStr(TimeLocal(),TIME_SECONDS)," @",Bid,".");
Alert(message);
if ( ExtSoundFileName != "" )
PlaySound( ExtSoundFileName );
}
}
//----
return(0);
}
//+------------------------------------------------------------------+

 

Попробуй статической переменной переключать флаг.

Файлы:
123.mq4  4 kb
 
andro_id >>:

Есть простейший индикатор, который показывает пересечение МА, но работает он немного не правильно.



for(i = 0; i <= limit; i++) {

...................................................
//----
FastMA_now = iMA(NULL, 0, FastMA, 0, FastMetod, FastType, i);
FastMA_previous = iMA(NULL, 0, FastMA, 0, FastMetod, FastType, i+1);
FastMA_after = iMA(NULL, 0, FastMA, 0, FastMetod, FastType, i-1);

SlowMA_now = iMA(NULL, 0, SlowMA, 0, SlowMetod, SlowType, i);
SlowMA_previous = iMA(NULL, 0, SlowMA, 0, SlowMetod, SlowType, i+1);
SlowMA_after = iMA(NULL, 0, SlowMA, 0, SlowMetod, SlowType, i-1);

..............................
//+------------------------------------------------------------------+

Вот здесь заглядывает в будущее, дальше можно не напрягаться.....


Успехов.