134 lines
No EOL
9 KiB
MQL4
134 lines
No EOL
9 KiB
MQL4
//+------------------------------------------------------------------+
|
|
//| EMA-Crossover_Signal.mq4 |
|
|
//| Copyright © 2005, Jason Robinson (jnrtrading) |
|
|
//| http://www.jnrtading.co.uk |
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
|
#property indicator_chart_window
|
|
#property indicator_buffers 4
|
|
#property indicator_color1 Lime
|
|
#property indicator_color2 Red
|
|
#property indicator_width1 2
|
|
#property indicator_width2 2
|
|
double CrossUp[];
|
|
double CrossDown[];
|
|
extern int FastMA = 15;
|
|
extern ENUM_MA_METHOD FasterMode = MODE_EMA; //0=sma, 1=ema, 2=smma, 3=lwma
|
|
extern ENUM_APPLIED_PRICE FasterPrice = PRICE_CLOSE ;
|
|
|
|
extern int SlowMA = 60;
|
|
extern ENUM_MA_METHOD SlowerMode = MODE_EMA; //0=sma, 1=ema, 2=smma, 3=lwma
|
|
extern ENUM_APPLIED_PRICE SlowerPrice = PRICE_CLOSE;
|
|
|
|
extern string sound = "";
|
|
extern string soundup = "expert.wav";
|
|
extern string sounddown = "stops.wav";
|
|
extern string alert = "";
|
|
|
|
double alertTag;
|
|
//+------------------------------------------------------------------+
|
|
//| Custom indicator initialization function |
|
|
//+------------------------------------------------------------------+
|
|
int init()
|
|
{
|
|
//---- indicators
|
|
SetIndexStyle(0, DRAW_ARROW);
|
|
SetIndexArrow(0, 167);
|
|
SetIndexBuffer(0, CrossUp);
|
|
|
|
SetIndexStyle(1, DRAW_ARROW);
|
|
SetIndexArrow(1, 167);
|
|
SetIndexBuffer(1, CrossDown);
|
|
|
|
//----
|
|
return(0);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Custom indicator deinitialization function |
|
|
//+------------------------------------------------------------------+
|
|
int deinit()
|
|
{
|
|
//----
|
|
|
|
//----
|
|
return(0);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Custom indicator iteration function |
|
|
//+------------------------------------------------------------------+
|
|
int start() {
|
|
|
|
int limit, i, counter;
|
|
double fasterMAnow, slowerMAnow, fasterMAprevious, slowerMAprevious, fasterMAafter, slowerMAafter;
|
|
double Range, AvgRange;
|
|
int counted_bars=IndicatorCounted();
|
|
|
|
int ActivePer=Period();
|
|
int SlowerMA=SlowMA/ActivePer;
|
|
int FasterMA=FastMA/ActivePer;
|
|
|
|
//---- 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/40;
|
|
|
|
fasterMAnow = iMA(NULL, 0, FasterMA, 0, FasterMode, FasterPrice, i);
|
|
fasterMAprevious = iMA(NULL, 0, FasterMA, 0, FasterMode, FasterPrice, i+1);
|
|
fasterMAafter = iMA(NULL, 0, FasterMA, 0, FasterMode, FasterPrice, i-1);
|
|
|
|
slowerMAnow = iMA(NULL, 0, SlowerMA, 1, SlowerMode, SlowerPrice, i);
|
|
slowerMAprevious = iMA(NULL, 0, SlowerMA, 1, SlowerMode, SlowerPrice, i+1);
|
|
slowerMAafter = iMA(NULL, 0, SlowerMA, 1, SlowerMode, SlowerPrice, i-1);
|
|
|
|
if ( (fasterMAnow > slowerMAnow) && (fasterMAprevious < slowerMAprevious) && (fasterMAafter > slowerMAafter))
|
|
{
|
|
CrossUp[i] = Low[i] - Range;
|
|
if ( alertTag!=Time[0])
|
|
{
|
|
//PlaySound("alert2.wav");// buy wav
|
|
//Alert(Symbol()," M",Period()," MA cross BUY");
|
|
if (soundup != "") PlaySound(soundup);
|
|
else if (sound != "") PlaySound(sound);
|
|
if (alert != "") Alert(Symbol()," M",Period()," MA cross BUY", alert);
|
|
}
|
|
alertTag = Time[0];
|
|
}
|
|
else if ((fasterMAnow < slowerMAnow) && (fasterMAprevious > slowerMAprevious) && (fasterMAafter < slowerMAafter))
|
|
{
|
|
CrossDown[i] = High[i] + Range;
|
|
if ( alertTag!=Time[0])
|
|
{
|
|
//PlaySound("alert.wav"); //sell wav
|
|
// Alert(Symbol()," M",Period()," MA cross SELL");
|
|
if (sounddown != "") PlaySound(sounddown);
|
|
else if (sound != "") PlaySound(sound);
|
|
if (alert != "") Alert(Symbol()," M",Period()," MA cross SELL", alert);
|
|
}
|
|
alertTag = Time[0];
|
|
}
|
|
else
|
|
{
|
|
|
|
CrossDown[i] = NULL;
|
|
CrossUp[i] = NULL;
|
|
|
|
|
|
}
|
|
}
|
|
return(0);
|
|
} |