Donchian Channel Indicator

Posted By: hughbriss

Donchian Channel Indicator - 10/23/12 08:12

Hi JCL,

I am having trouble getting some indicators to work, could you run through with me how to use them?

DChannel(int TimePeriod): var
Donchian Channel; from the minimum and maximum value of the High and Low var* over the time period. Returns: rRealUpperBand, rRealLowerBand. The function internally creates series and thus must be called in a fixed order in the script

So I have tried to declare the value of DChannel as 55 for the upper band but then I can't seem to use rRealUpperBand in the script?

Also stochastics is another one I have trouble with. Could you please show me how to use this type of indicator? Thanks.

Maybe a short script using them would help?
Posted By: Guiom

Re: Donchian Channel Indicator - 10/23/12 11:37

Hi hughbriss,

I have written a script to deal with the basic donchian channel strategy and I will post it when i am back home in a few hours, unless someones posts something before then

Guiom
Posted By: hughbriss

Re: Donchian Channel Indicator - 10/23/12 12:36

Excellent, thank you.
Posted By: Guiom

Re: Donchian Channel Indicator - 10/23/12 15:36

Here it is. Basic donchian channel trading, enter one position on breakout of long period channel and exit when crossing the shorther channel. Not sure how to code the add ons the way you've done it with your DC trading method tho...

Guiom

Code:
function run()
{
	set(PARAMETERS|LOGFILE|TESTNOW);  // generate and use optimized parameters
	StartDate = 2006;
	BarPeriod = 1440;	// daily bars
	NumWFOCycles = 8; // activate WFO
	NumBarCycles = 4;	// 4 times oversampling
	
		var *Price_Close = series(priceClose());
		var *Donchian_Entry = series(DChannel(optimize(30,35,70,5)));
		var *DE_Up = series(rRealUpperBand);
		var *DE_Down = series(rRealLowerBand);
		var *Donchian_Exit = series(DChannel(optimize(5,5,30,5)));
		var *DEx_Up = series(rRealUpperBand);
		var *DEx_Down = series(rRealLowerBand);

	while(asset(loop("AUD/USD","EUR/USD","GBP/USD","GER30","NAS100","SPX500","UK100","UKOil","US30","USD/CAD","USD/CHF","USD/JPY","USDOLLAR","USOil","XAG/USD","XAU/USD")))
	{
		Stop = optimize(2,1,10)*ATR(20);
		
		if(Train)
			Lots = 1;
		else if(OptimalFLong > 0) {
			Lots = 1;			 
			Margin = clamp((WinLong-LossLong) * OptimalFLong/2, 50, 10000);
		} else if(OptimalFShort > 0) {
			Lots = 1;
			Margin = clamp((WinShort-LossShort) * OptimalFShort/2, 50, 10000);
		} else
			Lots = 0; // switch off trading
		
		if(crossOver(Price_Close,DE_Up[1]) and numLong(0)==0)
			enterLong();
		else if(crossUnder(Price_Close,DE_Down[1]) and numShort(0)==0)
			enterShort();
		else if(crossOver(Price_Close,DEx_Up[1]) and numShort(0)>=1)
			exitShort();
		else if(crossUnder(Price_Close,DEx_Down[1]) and numLong(0)>=1)
			exitLong();
	}	

}

Posted By: jcl

Re: Donchian Channel Indicator - 10/23/12 16:26

Thanks for posting - and some remarks if you don't mind:

- The Donchian_Entry series is not needed, it is identical to DE_Up (same for Donchian_Exit)

- For the entry series, the default optimize value is below its lower limit.

- OptimalF is always 1 because you have FACTORS not set.

- CrossOver/Under checks the crossing of the price not with a curve, but with a horizontal line. I'm not sure if that is what you intended. If you want to compare the price curve with the Donchian curve delayed by 1 bar, use instead:

if(crossOver(Price_Close,DE_Up+1)...

DE_Up[1] = the Donchian up value at the previous bar
DE_Up+1 = the Donchian up series delayed by 1 bar.
Posted By: hughbriss

Re: Donchian Channel Indicator - 10/23/12 17:06

Thanks Guiom and JCL, the plan is coming together. I will attempt to code it and see how I go.

By the way 55/20 is the most profitable all round settings for the donch channels. The add ons I have found to be profitable are every time the 55 is broken and the donch channel has been flat for at least 3 bars although I think I will probably have to look into some sort of ATR entry for 3 or 4 entries and then stop adding on, very much like the original turtle rules.
Posted By: Guiom

Re: Donchian Channel Indicator - 10/23/12 17:27

Originally Posted By: jcl
Thanks for posting - and some remarks if you don't mind:

I don't mind at all, thanks for highlighting the obvious errors...

Quote:
- CrossOver/Under checks the crossing of the price not with a curve, but with a horizontal line. I'm not sure if that is what you intended. If you want to compare the price curve with the Donchian curve delayed by 1 bar, use instead:

if(crossOver(Price_Close,DE_Up+1)...

DE_Up[1] = the Donchian up value at the previous bar
DE_Up+1 = the Donchian up series delayed by 1 bar.


Is crossOver(Price_Close,DE_Up[1]) the same as Price_Close>DE_Up[1]?

Thanks
Posted By: jcl

Re: Donchian Channel Indicator - 10/24/12 08:33

No, it's not the same. It's the crossing of the Price_Close curve with a horizontal line that has the position of DE_Up at the previous bar.

Price_Close>DE_Up[1] would be a wrong expression. For comparing two variables from two series, use for instance Price_Close[0]>DE_Up[1].
Posted By: TankWolf

Re: Donchian Channel Indicator - 10/25/12 04:29

Thought I would just post this here to avoid starting another thread.

With the ADX() indicator when I check the help manual it says the following:
Quote:

ADX(int TimePeriod): var
Average Directional Movement Index. Moving average of the DX (see below). The returned values range from 0 to 100.


I have some trading strategies that uses the ADX but also the +DI and -DI values. By looking at what the ADX indicator returns it would seem that those values arn't returned is this true and if they arnt is there any way to return them?
Posted By: jcl

Re: Donchian Channel Indicator - 10/25/12 07:14

You can see the the description and source code of all those old indicators at http://tadoc.org/. The +DI and -DI are indeed internally calculated, but apparently nowhere stored, so that you have no access on them.
Posted By: TankWolf

Re: Donchian Channel Indicator - 10/25/12 07:23

Hmmm well that sucks, I guess I wont be able to use the strategies that rely on the +DI & -DI crossing then. It seems weird that those values havnt been stored when they are a standard part of the indicator when displayed on most trading platforms.
Posted By: jcl

Re: Donchian Channel Indicator - 10/25/12 08:10

Well, according to the algorithm the +DI & -DI crossing is just the zero crossing of the DX, so you should be able to use those strategies. That's probably the reason why the TA-Lib author did not care to store +DI & -DI separately.
Posted By: TankWolf

Re: Donchian Channel Indicator - 10/25/12 09:29

Fair enough jcl thanks for your assistance as always. laugh
Posted By: hughbriss

Re: Donchian Channel Indicator - 10/25/12 21:38

Originally Posted By: jcl
Thanks for posting - and some remarks if you don't mind:

- The Donchian_Entry series is not needed, it is identical to DE_Up (same for Donchian_Exit)

- For the entry series, the default optimize value is below its lower limit.

- OptimalF is always 1 because you have FACTORS not set.

- CrossOver/Under checks the crossing of the price not with a curve, but with a horizontal line. I'm not sure if that is what you intended. If you want to compare the price curve with the Donchian curve delayed by 1 bar, use instead:

if(crossOver(Price_Close,DE_Up+1)...

DE_Up[1] = the Donchian up value at the previous bar
DE_Up+1 = the Donchian up series delayed by 1 bar.


If the DChannel series is not required then where would you specify the length of the Donchian Channel?
Posted By: jcl

Re: Donchian Channel Indicator - 10/26/12 06:07

Its parameter specifies the DChannel length. You can find DChannel and all other indicators described in the manual under "Functions / Analysis / Indicators".
Posted By: hughbriss

Re: Donchian Channel Indicator - 10/26/12 09:25

Ok. In but in the original code he specifies DChannel as a series and you say that it is not required to do so and yet it is in that expression that he is defining the length of the channel.

Please could you just post a couple of lines of code of how you would set this indicator up? Thanks.
Posted By: jcl

Re: Donchian Channel Indicator - 10/26/12 09:31

DChannel(length);
var *DE_Up = series(rRealUpperBand);
var *DE_Down = series(rRealLowerBand);
Posted By: hughbriss

Re: Donchian Channel Indicator - 10/26/12 11:51

That makes perfect sense. Thank you for your help.
Posted By: hughbriss

Re: Donchian Channel Indicator - 10/26/12 19:42

Hmmm, what if you want to use two different donch channels, one for entry and one for exit? I suppose you'd declare the DChannel, then define the variables using the rRealUpper/LowerBand and then declare the DChannel as the other length and then set up the other rReal bands? As long as they are in the right order it will do it sequentially correct?

So...

DChannel(55);
var *DE_Up = series(rRealUpperBand);
var *DE_Down = series(rRealLowerBand);

Dchannel(20);
var *DEX_Up = series(rRealUpperBand);
var *DEX_Down = series(rRealLowerBand);

I will have a go to implement this and if it doesn't work I'll go back to the original code and work with that.
Posted By: hughbriss

Re: Donchian Channel Indicator - 10/26/12 20:03

I came up with this but it doesn't appear to work which is a shame because logically in my head it looks fine.

function run()

{
BarPeriod = 1440;

var *ClosePrice = series(priceClose());
DChannel(55);

var *dcentup = series(rRealUpperBand);

var *dcentdown = series(rRealLowerBand);


DChannel(20);

var *dcexup = series(rRealUpperBand);

var *dcexdown = series(rRealLowerBand);

if(numLong = 0 && ClosePrice[0] > dcentup[0])
enterLong();
if(numShort = 0 && ClosePrice[0] < dcentdown[0])
enterShort();
if(numLong > 0 && ClosePrice[0] < dcexdown[0])
exitLong();
if(numShort > 0 && ClosePrice[0] > dcexup[0])
exitShort();
}

JCL, do you just get completely exasperated by people who can't work this stuff out even though there are a couple of lines in the manual that explain it all perfectly (to you)? :-)

I swear you could have written some of it in German and I would have had more chance of understanding it! I will get there though, I am determined to work this thing out.
Posted By: hughbriss

Re: Donchian Channel Indicator - 10/26/12 21:17

D'oh! Ok, I've worked it out... I need to use [1] rather than [0]. You see, I'm not that stupid, just a little slow...
Posted By: Spirit

Re: Donchian Channel Indicator - 10/27/12 07:18

Hmm I see nothing wrong in your code and dont see why you think you need to use [1], but maybe you wanted a crossOver() of the price instead of just comparing it? Your comparing will enter trades all the time on every bar, but crossover only enters it when the price crosses over the border.
Posted By: hughbriss

Re: Donchian Channel Indicator - 10/27/12 10:03

Originally Posted By: Spirit
Hmm I see nothing wrong in your code and dont see why you think you need to use [1], but maybe you wanted a crossOver() of the price instead of just comparing it? Your comparing will enter trades all the time on every bar, but crossover only enters it when the price crosses over the border.


Using the [1] will compare the previous value of the DChannel rather than the current one which by necessity will have risen or fallen in line with the price and therefore will never be exceeded.

The trade count criteria prevent multiple trades being opened.
Posted By: hughbriss

Re: Donchian Channel Indicator - 10/28/12 20:13

Maybe anyone watching may have already worked this out but I was using = in the if statements when I needed ==

This will come to me slowly. If you never give up at anything no matter how bleak things look you will eventually get there.
Posted By: jcl

Re: Donchian Channel Indicator - 10/29/12 14:52

Not only that, you also used "numLong" instead of "numLong(0)". Both are valid C expressions, but numLong is the pointer of the numLong() function.

We have removed numLong() from the documentation because it is not needed. The number of trades and all other trade statistics are directly available as variables:

http://manual.zorro-trader.com/winloss.htm
Posted By: hughbriss

Re: Donchian Channel Indicator - 10/30/12 08:20

Yes, I worked that out as well eventually.

Another question. I know that with an ma cross system as soon as you start trading short the long trades are exited. With thi donch channel system where you are using a different criteria for the entry and exit would the exitLong() function close all open long trades if you had added on or would I need to put in some kind of loop to count the number of open longs and keep closing them until they were all closed?
Posted By: jcl

Re: Donchian Channel Indicator - 10/30/12 10:35

exitLong() closes all open long trades with the same asset and algo, so you need no loop.
Posted By: TankWolf

Re: Donchian Channel Indicator - 10/30/12 23:59

Originally Posted By: jcl
Well, according to the algorithm the +DI & -DI crossing is just the zero crossing of the DX, so you should be able to use those strategies. That's probably the reason why the TA-Lib author did not care to store +DI & -DI separately.


Quote:

775 /* Generated */ if( !TA_IS_ZERO(prevTR) )
776 /* Generated */ {
777 /* Generated */ minusDI = round_pos(100.0*(prevMinusDM/prevTR));
778 /* Generated */ plusDI = round_pos(100.0*(prevPlusDM/prevTR));
779 /* Generated */ tempReal = minusDI+plusDI;
780 /* Generated */ if( !TA_IS_ZERO(tempReal) )
781 /* Generated */ {
782 /* Generated */ tempReal = round_pos(100.0*(std_fabs(minusDI-plusDI)/tempReal));


jcl Ive tried to work out how to use the +DI and -DI values but I just cant seem to work it out, its a vital aspect to one of my strategies that I check that the +DI and -DI have crossed before a trade can be taken. Any further help would be appreciated or anyone for that matter. tongue
Posted By: jcl

Re: Donchian Channel Indicator - 10/31/12 07:41

var *DX20 = series(DX(20));

if(CrossOver(DX20,0.0)) ... // +DI crossed -DI

if(CrossUnder(DX20,0.0)) ... // -DI crossed +DI

Hope this helps.
Posted By: TankWolf

Re: Donchian Channel Indicator - 10/31/12 09:14

Im trying that and no trades are excuteing at all now.

The DX value reads:
Quote:

DX(int TimePeriod): var
Directional Movement Index by Welles Wilder (the guy who claimed that "the interaction of sun, moon, and earth is the basis of all market movement"). If sun, moon, and earth don't work, use this indicator. The values range from 0 to 100, but rarely get above 60. A high return value is supposed to indicate a strong trend, a low value a weak trend.


So if the DX value can only return a value between 0 and 100 how can it ever cross under or over 0?
Posted By: jcl

Re: Donchian Channel Indicator - 10/31/12 11:16

You're absolutely right: The 0..100 range contradicts the website that says DX = (+DI - -DI)/(+DI + -DI). This would not generate a 0..100 range, but a -1..+1 range. So one of them is obviously wrong.

Although I don't think that DX, +DI, or -DI can generate very useful trade signals, that issue has to be solved. I'll check the source and will find out which algorithm is really used.
Posted By: jcl

Re: Donchian Channel Indicator - 10/31/12 12:17

Ok, TA-Lib indeed uses a different formula:

DX = 100*abs(+DI - -DI)/(+DI + -DI)

With this formula it's not possible to determine which crosses which. You're encouraged to write your own version of the +DI and -DI indicators if you need them. If you encounter any problems, post in the script forum and I'll help.
Posted By: TankWolf

Re: Donchian Channel Indicator - 11/01/12 01:26

Hmmm okies I see, by the way I did notice these other indicators just now maybe they can be used?

Quote:

MinusDI(int TimePeriod): var
Minus Directional Indicator.

MinusDM(int TimePeriod): var
Minus Directional Movement.

PlusDI(int TimePeriod): var
Plus Directional Indicator.

PlusDM(int TimePeriod): var
Plus Directional Movement.


There is no real description on what they return or do though but looking at the TALib for these indicators it seems maybe these are to be used instead?
Posted By: jcl

Re: Donchian Channel Indicator - 11/01/12 09:42

Yes, that's it. The TA-Lib calculates them as separate indicators.
Posted By: maudur

Re: Donchian Channel Indicator - 11/05/12 20:23

Originally Posted By: jcl
Not only that, you also used "numLong" instead of "numLong(0)". Both are valid C expressions, but numLong is the pointer of the numLong() function.

We have removed numLong() from the documentation because it is not needed. The number of trades and all other trade statistics are directly available as variables:

http://manual.zorro-trader.com/winloss.htm


I've used NumOpenLong but I got the following message:

'NumOpenLong' undeclared identifier.

I need to include some specific line at start?

Thanks.
Posted By: SFF

Re: Donchian Channel Indicator - 02/04/13 09:58

I tested that script below URL with another parameter and It said this error.
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=410020#Post410020

BADFREE2: SYS

What is this?
Posted By: jcl

Re: Donchian Channel Indicator - 02/04/13 10:04

That script had wrong variable names. It's "NumOpenLong" and "NumOpenShort", not "numShort".
© 2024 lite-C Forums