MQL4 Oscillator Indicator EA

The other type of indicator is an oscillator. Oscillators are drawn in a separate window and they oscillate between high and low price extremes. They are centered around a neutral axis (generally 0), or they are bound by an upper or lower extreme (such as 0 and 100).

Examples of oscillators include momentum, stochastics and RSI. Learn in this MQL4 Oscillator Indicator EA article how to program the Expert Advisor to trade using the most popular oscillators.

Oscillators indicate overbought and oversold levels. While they can be used as an indicator of trends, they are generally used to locate areas of pending reversal. These are used to produce counter-trend signals.

Below is a number of occillators:

 MACD(1)
Note//Buy: MACD rises above the signal line

//Sell: MACD falls below the signal line

Externint fastmacd=12; //Averaging period for calculation of a quick MA

extern int slowmacd=26; //Averaging period for calculation of a slow MA

extern int signalmacd=9; //Averaging period for calculation of a signal line

Indicator
Calling
macdmaincurr=iMACD(NULL,0,fastmacd,slowmacd,signalmacd,0,MODE_MAIN,0);
macdmainprev=iMACD(NULL,0,fastmacd,slowmacd,signalmacd,0,MODE_MAIN,1);
macdsigcurr=iMACD(NULL,0,fastmacd,slowmacd,signalmacd,0,MODE_SIGNAL,0);
macdsigprev=iMACD(NULL,0,fastmacd,slowmacd,signalmacd,0,MODE_SIGNAL,1);
BuyCondif (macdmainprev < <macdsignalprevious&&macdmaincurrent>macdsignalprev && macdmaincurr >= macdsignalcurr)
</macdsignalprevious&&macdmaincurrent>
SellCondif (macdmainprev > macdsignalprev && macdmaincurr <=macdsignalcurr)

 

 MACD(2)
Note//Buy: crossing 0 upwards

//Sell: crossing 0 downwards

Externint fastmacd=12; //Averaging period for calculation of a quick MA

extern int slowmacd=26; //Averaging period for calculation of a slow MA

extern int signalmacd=9; //Averaging period for calculation of a signal line

Indicator
Calling
macdmaincurr=iMACD(NULL,0,fastmacd,slowmacd,signalmacd,0,MODE_MAIN,0);
macdmainprev=iMACD(NULL,0,fastmacd,slowmacd,signalmacd,0,MODE_MAIN,1);
macdsigcurr=iMACD(NULL,0,fastmacd,slowmacd,signalmacd,0,MODE_SIGNAL,0);
macdsigprev=iMACD(NULL,0,fastmacd,slowmacd,signalmacd0,MODE_SIGNAL,1);
BuyCondif (macdmainprev < 0<macdsignalprevious&&macdmaincurrent> && macdmaincurr >= 0)
</macdsignalprevious&&macdmaincurrent>
SellCondif (macdmainprev > 0 && macdmaincurr <= 0)

 

 RSI
Note//Buy: crossing 30 upwards

//Sell: crossing 70 downwards

Externrsiu = 14; // averaging period
lowerband = 30;
upperband = 70;
Indicator
Calling
rsicurrent=iRSI(NULL,0,rsiu,PRICE_CLOSE,0);
rsiprevious=iRSI(NULL,0,rsiu,PRICE_CLOSE,1);
BuyCondif (rsiprevious<lowerband&&rsicurrent>=lowerband)
SellCondif (rsiprevious>upperband&&rsicurrent<=upperband)

 

 Stochastics Occilator(1)
Note//Buy: main line rises above 20 after it fell below this point

//Sell: main line falls lower than 80 after it rose above this point

Externstok = 5; // Period(amount of bars) for the calculation of %K line
stod = 3; //Averaging period for the calculation of %D line
stslow =3; // Value of slowdown
mamode = 1;
lowerband = 20;
upperband = 80;
Indicator
Calling
stocurrent=iStochastic(NULL,0,stok,stod,stslow,mamode,0,MODE_MAIN,0);
stoprevious=iStochastic(NULL,0,stok,stod,stslow,mamode,0,MODE_MAIN,1);
BuyCondif (stoprevious<lowerband&&stocurrent>=lowerband)
SellCondif (stoprevious>upperband&&stocurrent<=upperband)

 

 Stochastics Occilator(2)
Note//Buy: main line goes above the signal line
//Sell: signal line goes above the main line
Externstok = 5; // Period(amount of bars) for the calculation of %K line
stod = 3; //Averaging period for the calculation of %D line
stslow =3; // Value of slowdown
mamode = 1;
lowerband = 20;
upperband = 80;
Indicator
Calling
stomaincurr=iStochastic(NULL,0,stok,stod,stslow,mamode,0,MODE_MAIN,0);
stomainprev=iStochastic(NULL,0,stok,stod,stslow,mamode,0,MODE_MAIN,1);
stosignalcurr=iStochastic(NULL,0,stok,stod,stslow,mamode,0,MODE_SIGNAL,0);
stosignalprev=iStochastic(NULL,0,stok,stod,stslow,mamode,0,MODE_SIGNAL,1);
BuyCondif (stomainprev<stosignalprev&&stomaincurr>=stosignalcurr)
SellCondif (stomainprev>stosignalprev&&stomaincurr<=stosignalcurr)

 

 Williams % Range
Note//Buy: crossing -80 upwards

//Sell: crossing -20 downwards

Externwillperiod = 14; // averaging period
lowerband = -20;
upperband = -80;
Indicator
Calling
willcurrent=iWPR(NULL,0,willperiod,0);
willprevious=iWPR(NULL,0,willperiod,1);
BuyCondif (willprevious<upperband>willcurrent>= upperband)
SellCondif (willprevious>lowerband&&willcurrent<=lowerband)

 

 Force Index
Note/Flag 14 is 1, when FI recommends to buy (i.e. FI<0)

//Flag 14 is -1, when FI recommends to sell (i.e. FI>0)

Externforceu = 2; // averaging period
Indicator
Calling
forcecurrent=iForce(NULL,0,forceu,MODE_SMA,PRICE_CLOSE,0);
BuyCondif (forcecurrent<0)
SellCondif (forcecurrent>0)