//+------------------------------------------------------------------+
//| Overbought Oversold Level.mq4 |
//| Copyright 2020, Danil Makov. |
//| https://vk.com/danil_makov |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, Danil Makov."
#property link "https://vk.com/danil_makov"
#property version "1.00"
#property description "Overbought Oversold Level"
#property strict
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 10
#property indicator_buffers 2
#property indicator_plots 2
//--- plot Index
#property indicator_label1 "Index"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDeepSkyBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- plot Level
#property indicator_label2 "Level"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrRed
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//--- input parameters
input int IndShortPeriod = 12;
input int IndLongPeriod = 24;
input int IndLevel = 3;
//--- indicator buffers
double IndexBuffer[];
double LevelBuffer[];
//--- global variables
int short_highest,short_lowest; // bar index shorter channel
int long_highest,long_lowest; // bar index longer channel
double upper_diff,lower_diff; // channel difference
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
IndicatorDigits(Digits);
//--- check for bars count and period
if(Bars<=IndLongPeriod || IndLongPeriod<IndShortPeriod)
return(INIT_FAILED);
//--- set levels
SetLevelStyle(STYLE_DOT,1,clrDimGray);
IndicatorSetDouble(INDICATOR_LEVELVALUE,0,0.5+IndLevel);
IndicatorSetDouble(INDICATOR_LEVELVALUE,1,9.5-IndLevel);
//--- indicator buffers mapping
SetIndexBuffer(0,IndexBuffer);
SetIndexBuffer(1,LevelBuffer);
//--- 0 value will not be displayed
SetIndexEmptyValue(0,0.0);
SetIndexEmptyValue(1,0.0);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{//--- the last counted bar will be recounted
int counted_bar=IndicatorCounted();
int limit_bar=Bars-IndLongPeriod-1;
if(counted_bar>IndLongPeriod)limit_bar=Bars-counted_bar-1;
//---
for(int i=limit_bar; i>=0 && !IsStopped(); i--)
{//--- initial zero
upper_diff=0.0; lower_diff=0.0;
//--- shorter period channel
short_highest=iHighest(NULL,0,MODE_HIGH,IndShortPeriod,i);
short_lowest=iLowest(NULL,0,MODE_LOW,IndShortPeriod,i);
//---
for(int n=i; n<i+IndShortPeriod; n++)
{//--- longer period channel
long_highest=iHighest(NULL,0,MODE_HIGH,IndLongPeriod,n);
long_lowest=iLowest(NULL,0,MODE_LOW,IndLongPeriod,n);
//--- channel difference
upper_diff+=High[long_highest]-Low[short_lowest];
lower_diff+=High[short_highest]-Low[long_lowest];
}
//--- index calculation
if(upper_diff!=0.0)
IndexBuffer[i]=MathCeil(9.0-9.0/(1+lower_diff/upper_diff));
else
IndexBuffer[i]=9.0;
//--- overbought / oversold level
if(IndexBuffer[i]<=IndLevel || IndexBuffer[i]>=10.0-IndLevel)
LevelBuffer[i]=IndexBuffer[i];
else
LevelBuffer[i]=NULL;
}
//---
return(NULL);
}
//+------------------------------------------------------------------+
Comments