/*[[
	Name := Modified Donchian Channels
	Author := Damiani
	Link := 
	Separate Window := No
	First Color := LightSkyBlue
	First Draw Type := Line
	Use Second Data := Yes
	Second Color := LightSkyBlue
	Second Draw Type := Line
	Second Symbol := 218
]]*/


Inputs : Periods(24), Extremes(3),margins(-2),Advance(0);
// Periods: number of bars used for calculating the Donchian channel 
//Extremes: if 1 uses the highest high and the lowest low -> I do not recomend using this
//			if 0 uses the open of the extremes bar -> the open point of a bar (as well as the close
//				are the points of maximum probability of concentration of the prices during the bar
//			if 3 uses the median point most extreme open and lowest low or highest high
//margins: is the percent of the channel subtrated from the channel border before printing it, negative values are allowed
//Advance: the numbers of bars ahead

Variables : shift(0), cnt(0), loopbegin(0);
Variables : smin(0), smax(0), SsMax(0), SsMin(0);
Variables : bar(0), prevbars(0), start(0), cs(0), prevcs(0),commodt(0);

SetLoopCount(0);
cs= Periods+Extremes+margins+Advance; //checksum used to see if parameters have been changed
if cs=prevcs and (commodt=symbol)
	then start=Bars-prevbars //params haven't changed only need to calculate new bar
	else start=-1; 
commodt=symbol;
prevbars = Bars;
prevcs = cs;
if (start=1 | start=0) then bar=start else bar=bars-1;

For shift = Bar Downto 0 Begin
	if extremes =1 then
	{
		SsMax = High[Highest(MODE_HIGH,shift,Periods)];
		SsMin = Low[Lowest(MODE_LOW,shift,Periods)];
	}	
	Else if extremes = 3 then
	{
		SsMax = (Open[Highest(MODE_OPEN,shift,Periods)]+High[Highest(MODE_HIGH,shift,Periods)])/2;
		SsMin = (Open[Lowest(MODE_OPEN,shift,Periods)]+Low[Lowest(MODE_LOW,shift,Periods)])/2;
	}
	Else
	{
		SsMax = Open[Highest(MODE_OPEN,shift,Periods)];
		SsMin = Open[Lowest(MODE_OPEN,shift,Periods)];
	};
	smin = SsMin+(SsMax-SsMin)*margins/100;
	smax = SsMax-(SsMax-SsMin)*margins/100;
	SetIndexValue(shift-Periods+1-Advance,smin);
	SetIndexValue2(shift-Periods+1-Advance,smax);
//	loopbegin = loopbegin-1;
	
End;


