Added RSI & Stochastic indicators
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| RSI.mq4 |
|
||||
//| Copyright 2005-2014, MetaQuotes Software Corp. |
|
||||
//| https://www.mql5.com |
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "2005-2014, MetaQuotes Software Corp."
|
||||
#property link "https://www.mql5.com"
|
||||
#property description "Relative Strength Index"
|
||||
#property strict
|
||||
|
||||
#property indicator_separate_window
|
||||
#property indicator_buffers 3
|
||||
#property indicator_plots 1
|
||||
#property indicator_minimum 0
|
||||
#property indicator_maximum 100
|
||||
#property indicator_color1 DodgerBlue
|
||||
#property indicator_level1 30.0
|
||||
#property indicator_level2 70.0
|
||||
#property indicator_levelcolor clrSilver
|
||||
#property indicator_levelstyle STYLE_DOT
|
||||
//--- input parameters
|
||||
input int InpRSIPeriod=14; // RSI Period
|
||||
//--- buffers
|
||||
double ExtRSIBuffer[];
|
||||
double ExtPosBuffer[];
|
||||
double ExtNegBuffer[];
|
||||
|
||||
//
|
||||
// Initialize MedianRenko indicator for data processing
|
||||
// according to settings of the MedianRenko indicator already on chart
|
||||
//
|
||||
|
||||
#include <RangeBarIndicator.mqh>
|
||||
RangeBarIndicator rangeBarsIndicator;
|
||||
|
||||
|
||||
//
|
||||
//
|
||||
//
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| Custom indicator initialization function |
|
||||
//+------------------------------------------------------------------+
|
||||
int OnInit(void)
|
||||
{
|
||||
string short_name;
|
||||
//--- 2 additional buffers are used for counting
|
||||
SetIndexBuffer(0,ExtRSIBuffer);
|
||||
SetIndexBuffer(1,ExtPosBuffer);
|
||||
SetIndexBuffer(2,ExtNegBuffer);
|
||||
//--- indicator line
|
||||
PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_LINE);
|
||||
SetIndexBuffer(0,ExtRSIBuffer);
|
||||
//--- name for DataWindow and indicator subwindow label
|
||||
short_name="RSI("+string(InpRSIPeriod)+")";
|
||||
IndicatorSetString(INDICATOR_SHORTNAME,short_name);
|
||||
PlotIndexSetString(0,PLOT_LABEL,short_name);
|
||||
//--- check for input
|
||||
if(InpRSIPeriod<2)
|
||||
{
|
||||
Print("Incorrect value for input variable InpRSIPeriod = ",InpRSIPeriod);
|
||||
return(INIT_FAILED);
|
||||
}
|
||||
//---
|
||||
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpRSIPeriod);
|
||||
//--- initialization done
|
||||
return(INIT_SUCCEEDED);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| Relative Strength Index |
|
||||
//+------------------------------------------------------------------+
|
||||
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 &TickVolume[],
|
||||
const long &Volume[],
|
||||
const int &Spread[])
|
||||
{
|
||||
|
||||
//
|
||||
// Precoess data through MedianRenko indicator
|
||||
//
|
||||
|
||||
if(!rangeBarsIndicator.OnCalculate(rates_total,prev_calculated,Time))
|
||||
return(rates_total);
|
||||
|
||||
//
|
||||
// Make the following modifications in the code below:
|
||||
//
|
||||
// medianRenkoIndicator.GetPrevCalculated() should be used instead of prev_calculated
|
||||
// medianRenkoIndicator.Open[] should be used instead of open[]
|
||||
// medianRenkoIndicator.Low[] should be used instead of low[]
|
||||
// medianRenkoIndicator.High[] should be used instead of high[]
|
||||
// medianRenkoIndicator.Close[] should be used instead of close[]
|
||||
// if applied_price is used
|
||||
// medianRenkoIndicator.Price[] should be used instead of price[]
|
||||
//
|
||||
|
||||
int _prev_calculated = rangeBarsIndicator.GetPrevCalculated();
|
||||
|
||||
//
|
||||
//
|
||||
//
|
||||
|
||||
int i,pos;
|
||||
double diff;
|
||||
//---
|
||||
if(Bars(_Symbol,_Period)<=InpRSIPeriod || InpRSIPeriod<2)
|
||||
return(0);
|
||||
//--- counting from 0 to rates_total
|
||||
ArraySetAsSeries(ExtRSIBuffer,false);
|
||||
ArraySetAsSeries(ExtPosBuffer,false);
|
||||
ArraySetAsSeries(ExtNegBuffer,false);
|
||||
ArraySetAsSeries(rangeBarsIndicator.Close,false);
|
||||
//--- preliminary calculations
|
||||
pos=_prev_calculated-1;
|
||||
if(pos<=InpRSIPeriod)
|
||||
{
|
||||
//--- first RSIPeriod values of the indicator are not calculated
|
||||
ExtRSIBuffer[0]=0.0;
|
||||
ExtPosBuffer[0]=0.0;
|
||||
ExtNegBuffer[0]=0.0;
|
||||
double sump=0.0;
|
||||
double sumn=0.0;
|
||||
for(i=1; i<=InpRSIPeriod; i++)
|
||||
{
|
||||
ExtRSIBuffer[i]=0.0;
|
||||
ExtPosBuffer[i]=0.0;
|
||||
ExtNegBuffer[i]=0.0;
|
||||
diff=rangeBarsIndicator.Close[i]-rangeBarsIndicator.Close[i-1];
|
||||
if(diff>0)
|
||||
sump+=diff;
|
||||
else
|
||||
sumn-=diff;
|
||||
}
|
||||
//--- calculate first visible value
|
||||
ExtPosBuffer[InpRSIPeriod]=sump/InpRSIPeriod;
|
||||
ExtNegBuffer[InpRSIPeriod]=sumn/InpRSIPeriod;
|
||||
if(ExtNegBuffer[InpRSIPeriod]!=0.0)
|
||||
ExtRSIBuffer[InpRSIPeriod]=100.0-(100.0/(1.0+ExtPosBuffer[InpRSIPeriod]/ExtNegBuffer[InpRSIPeriod]));
|
||||
else
|
||||
{
|
||||
if(ExtPosBuffer[InpRSIPeriod]!=0.0)
|
||||
ExtRSIBuffer[InpRSIPeriod]=100.0;
|
||||
else
|
||||
ExtRSIBuffer[InpRSIPeriod]=50.0;
|
||||
}
|
||||
//--- prepare the position value for main calculation
|
||||
pos=InpRSIPeriod+1;
|
||||
}
|
||||
//--- the main loop of calculations
|
||||
for(i=pos; i<rates_total && !IsStopped(); i++)
|
||||
{
|
||||
diff=rangeBarsIndicator.Close[i]-rangeBarsIndicator.Close[i-1];
|
||||
ExtPosBuffer[i]=(ExtPosBuffer[i-1]*(InpRSIPeriod-1)+(diff>0.0?diff:0.0))/InpRSIPeriod;
|
||||
ExtNegBuffer[i]=(ExtNegBuffer[i-1]*(InpRSIPeriod-1)+(diff<0.0?-diff:0.0))/InpRSIPeriod;
|
||||
if(ExtNegBuffer[i]!=0.0)
|
||||
ExtRSIBuffer[i]=100.0-100.0/(1+ExtPosBuffer[i]/ExtNegBuffer[i]);
|
||||
else
|
||||
{
|
||||
if(ExtPosBuffer[i]!=0.0)
|
||||
ExtRSIBuffer[i]=100.0;
|
||||
else
|
||||
ExtRSIBuffer[i]=50.0;
|
||||
}
|
||||
}
|
||||
//---
|
||||
return(rates_total);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
@@ -0,0 +1,174 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| Stochastic.mq5 |
|
||||
//| Copyright 2009, MetaQuotes Software Corp. |
|
||||
//| http://www.mql5.com |
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "2009, MetaQuotes Software Corp."
|
||||
#property link "http://www.mql5.com"
|
||||
//--- indicator settings
|
||||
#property indicator_separate_window
|
||||
#property indicator_buffers 4
|
||||
#property indicator_plots 2
|
||||
#property indicator_type1 DRAW_LINE
|
||||
#property indicator_type2 DRAW_LINE
|
||||
#property indicator_color1 LightSeaGreen
|
||||
#property indicator_color2 Red
|
||||
#property indicator_style2 STYLE_DOT
|
||||
//--- input parameters
|
||||
input int InpKPeriod=5; // K period
|
||||
input int InpDPeriod=3; // D period
|
||||
input int InpSlowing=3; // Slowing
|
||||
|
||||
//--- indicator buffers
|
||||
double ExtMainBuffer[];
|
||||
double ExtSignalBuffer[];
|
||||
double ExtHighesBuffer[];
|
||||
double ExtLowesBuffer[];
|
||||
|
||||
//
|
||||
// Initialize MedianRenko indicator for data processing
|
||||
// according to settings of the MedianRenko indicator already on chart
|
||||
//
|
||||
|
||||
#include <RangeBarIndicator.mqh>
|
||||
RangeBarIndicator rangeBarsIndicator;
|
||||
|
||||
|
||||
//
|
||||
//
|
||||
//
|
||||
|
||||
//+------------------------------------------------------------------+
|
||||
//| Custom indicator initialization function |
|
||||
//+------------------------------------------------------------------+
|
||||
void OnInit()
|
||||
{
|
||||
//--- indicator buffers mapping
|
||||
SetIndexBuffer(0,ExtMainBuffer,INDICATOR_DATA);
|
||||
SetIndexBuffer(1,ExtSignalBuffer,INDICATOR_DATA);
|
||||
SetIndexBuffer(2,ExtHighesBuffer,INDICATOR_CALCULATIONS);
|
||||
SetIndexBuffer(3,ExtLowesBuffer,INDICATOR_CALCULATIONS);
|
||||
//--- set accuracy
|
||||
IndicatorSetInteger(INDICATOR_DIGITS,2);
|
||||
//--- set levels
|
||||
IndicatorSetInteger(INDICATOR_LEVELS,2);
|
||||
IndicatorSetDouble(INDICATOR_LEVELVALUE,0,20);
|
||||
IndicatorSetDouble(INDICATOR_LEVELVALUE,1,80);
|
||||
//--- set maximum and minimum for subwindow
|
||||
IndicatorSetDouble(INDICATOR_MINIMUM,0);
|
||||
IndicatorSetDouble(INDICATOR_MAXIMUM,100);
|
||||
//--- name for DataWindow and indicator subwindow label
|
||||
IndicatorSetString(INDICATOR_SHORTNAME,"Stoch("+(string)InpKPeriod+","+(string)InpDPeriod+","+(string)InpSlowing+")");
|
||||
PlotIndexSetString(0,PLOT_LABEL,"Main");
|
||||
PlotIndexSetString(1,PLOT_LABEL,"Signal");
|
||||
//--- sets first bar from what index will be drawn
|
||||
PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpKPeriod+InpSlowing-2);
|
||||
PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpKPeriod+InpDPeriod);
|
||||
//--- initialization done
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
//| Stochastic Oscillator |
|
||||
//+------------------------------------------------------------------+
|
||||
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 &TickVolume[],
|
||||
const long &Volume[],
|
||||
const int &Spread[])
|
||||
{
|
||||
|
||||
//
|
||||
// Precoess data through MedianRenko indicator
|
||||
//
|
||||
|
||||
if(!rangeBarsIndicator.OnCalculate(rates_total,prev_calculated,Time))
|
||||
return(rates_total);
|
||||
|
||||
//
|
||||
// Make the following modifications in the code below:
|
||||
//
|
||||
// medianRenkoIndicator.GetPrevCalculated() should be used instead of prev_calculated
|
||||
// medianRenkoIndicator.Open[] should be used instead of open[]
|
||||
// medianRenkoIndicator.Low[] should be used instead of low[]
|
||||
// medianRenkoIndicator.High[] should be used instead of high[]
|
||||
// medianRenkoIndicator.Close[] should be used instead of close[]
|
||||
//
|
||||
|
||||
int _prev_calculated = rangeBarsIndicator.GetPrevCalculated();
|
||||
|
||||
//
|
||||
//
|
||||
//
|
||||
|
||||
int i,k,start;
|
||||
//--- check for bars count
|
||||
if(rates_total<=InpKPeriod+InpDPeriod+InpSlowing)
|
||||
return(0);
|
||||
//---
|
||||
start=InpKPeriod-1;
|
||||
if(start+1<_prev_calculated) start=_prev_calculated-2;
|
||||
else
|
||||
{
|
||||
for(i=0;i<start;i++)
|
||||
{
|
||||
ExtLowesBuffer[i]=0.0;
|
||||
ExtHighesBuffer[i]=0.0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
//--- calculate HighesBuffer[] and ExtHighesBuffer[]
|
||||
for(i=start;i<rates_total && !IsStopped();i++)
|
||||
{
|
||||
double dmin=1000000.0;
|
||||
double dmax=-1000000.0;
|
||||
for(k=i-InpKPeriod+1;k<=i;k++)
|
||||
{
|
||||
if(dmin>rangeBarsIndicator.Low[k]) dmin=rangeBarsIndicator.Low[k];
|
||||
if(dmax<rangeBarsIndicator.High[k]) dmax=rangeBarsIndicator.High[k];
|
||||
}
|
||||
ExtLowesBuffer[i]=dmin;
|
||||
ExtHighesBuffer[i]=dmax;
|
||||
}
|
||||
|
||||
//--- %K
|
||||
start=InpKPeriod-1+InpSlowing-1;
|
||||
if(start+1<_prev_calculated) start=_prev_calculated-2;
|
||||
else
|
||||
{
|
||||
for(i=0;i<start;i++) ExtMainBuffer[i]=0.0;
|
||||
}
|
||||
//--- main cycle
|
||||
for(i=start;i<rates_total && !IsStopped();i++)
|
||||
{
|
||||
double sumlow=0.0;
|
||||
double sumhigh=0.0;
|
||||
for(k=(i-InpSlowing+1);k<=i;k++)
|
||||
{
|
||||
sumlow +=(rangeBarsIndicator.Close[k]-ExtLowesBuffer[k]);
|
||||
sumhigh+=(ExtHighesBuffer[k]-ExtLowesBuffer[k]);
|
||||
}
|
||||
if(sumhigh==0.0) ExtMainBuffer[i]=100.0;
|
||||
else ExtMainBuffer[i]=sumlow/sumhigh*100;
|
||||
}
|
||||
//--- signal
|
||||
start=InpDPeriod-1;
|
||||
if(start+1<_prev_calculated) start=_prev_calculated-2;
|
||||
else
|
||||
{
|
||||
for(i=0;i<start;i++) ExtSignalBuffer[i]=0.0;
|
||||
}
|
||||
for(i=start;i<rates_total && !IsStopped();i++)
|
||||
{
|
||||
double sum=0.0;
|
||||
for(k=0;k<InpDPeriod;k++) sum+=ExtMainBuffer[i-k];
|
||||
ExtSignalBuffer[i]=sum/InpDPeriod;
|
||||
}
|
||||
//--- OnCalculate done. Return new prev_calculated.
|
||||
return(rates_total);
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
|
||||
Reference in New Issue
Block a user