//+------------------------------------------------------------------+
//|                                                       #dtosc.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_color1 Aqua
#property indicator_color2 DarkOrchid
#property indicator_width1 1
#property indicator_width2 1
#property indicator_level1 30
#property indicator_level2 70
//+------------------------------------------------------------------+
extern string TimeFrame			= "Current time frame";
extern int    PeriodRSI			= 13;
extern int    PeriodStoch		= 8;
extern int    PeriodSK			= 5;
extern int    PeriodSD			= 3;
extern ENUM_MA_METHOD MAMode	= 0;
//+------------------------------------------------------------------+
double SK[];
double SD[];
double StoRSI[];
double RSI[];
string IndicatorFileName;
int    timeFrame;
bool   returnBars = false;
//+------------------------------------------------------------------+
int init()
{
	IndicatorBuffers(4);
	SetIndexBuffer( 0, SK );
	SetIndexBuffer( 1, SD );
	SetIndexBuffer( 2, StoRSI );
	SetIndexBuffer( 3, RSI );
	
	if( TimeFrame == "getBarsCount" )
	{
		returnBars=true;
		return(0);
	}   
	timeFrame = stringToTimeFrame(TimeFrame);
	
	IndicatorShortName( "DTOSC ("+PeriodRSI+","+PeriodStoch+","+PeriodSK+","+PeriodSD+")" );
	IndicatorFileName = WindowExpertName();
	
	return(0);
}
int deinit() { return(0); }
//+------------------------------------------------------------------+
int start()
{
	int i,limit;
	int counted_bars = IndicatorCounted();
	
	if( counted_bars < 0 ) return(-1);
	if( counted_bars > 0 ) counted_bars--;
	limit = Bars - counted_bars;
	
	if( returnBars ){ SK[0] = limit; return(0); }
	if( timeFrame != Period() )
	{
		limit = MathMax( limit, MathMin( Bars, iCustom( NULL, timeFrame, IndicatorFileName, "getBarsCount", 0, 0 ) * timeFrame / Period() ) );
			
		for( i = 0; i < limit; i++ )
		{
			int y = iBarShift( NULL, timeFrame, Time[i] );
			SK[i] = iCustom( NULL, timeFrame, IndicatorFileName, "", PeriodRSI, PeriodStoch, PeriodSK, PeriodSD, MAMode, 0, y );
			SD[i] = iCustom( NULL, timeFrame, IndicatorFileName, "", PeriodRSI, PeriodStoch, PeriodSK, PeriodSD, MAMode, 1, y );
		}     
		
		return(0);         
	}
				
	for( i = limit; i >= 0; i-- )
	{
		RSI[i] = iRSI( NULL, 0, PeriodRSI, PRICE_CLOSE, i );
		double LLV = RSI[ ArrayMinimum( RSI, PeriodStoch, i ) ];
		double HHV = RSI[ ArrayMaximum( RSI, PeriodStoch, i ) ];
		if( ( HHV - LLV ) != 0 )
			StoRSI[i] = 100.0 * ( ( RSI[i] - LLV ) / ( HHV - LLV ) );
		else  StoRSI[i] = 0;
	}
	for( i = limit; i >= 0; i-- ) SK[i] = iMAOnArray( StoRSI, 0, PeriodSK, 0, MAMode, i );
	for( i = limit; i >= 0; i-- ) SD[i] = iMAOnArray( SK, 0, PeriodSD, 0, MAMode, i );
	
	return(0);
}
//+------------------------------------------------------------------+
int stringToTimeFrame( string tfs )
{
	for( int l = StringLen( tfs ) - 1; l >= 0; l-- )
	{
		int charcode = StringGetChar( tfs, l );
			if( ( charcode > 96 && charcode < 123 ) || ( charcode > 223 && charcode < 256 ) )
				tfs = StringSetChar( tfs, l, charcode - 32 );
			else 
				if( charcode > -33 && charcode < 0 )
					tfs = StringSetChar( tfs, l, charcode + 224 );
	}
	
	int tf=0;
	if( tfs=="M1" || tfs=="1" )     tf=PERIOD_M1;
	if( tfs=="M5" || tfs=="5" )     tf=PERIOD_M5;
	if( tfs=="M15"|| tfs=="15" )    tf=PERIOD_M15;
	if( tfs=="M30"|| tfs=="30" )    tf=PERIOD_M30;
	if( tfs=="H1" || tfs=="60" )    tf=PERIOD_H1;
	if( tfs=="H4" || tfs=="240" )   tf=PERIOD_H4;
	if( tfs=="D1" || tfs=="1440" )  tf=PERIOD_D1;
	if( tfs=="W1" || tfs=="10080" ) tf=PERIOD_W1;
	if( tfs=="MN" || tfs=="43200" ) tf=PERIOD_MN1;
	if( tf<Period())                tf=Period();
	
	return(tf);
}