Trying to Optimize a Donchian Channel. Whats wrong with code?

@zim630 if you use some exploration code to evaluate the values of the arrays used in your formula you'll easily see what is wrong:

immagine

"Top" and "Bottom" values are all the same for each bar (indeed they are "scalar" values that AmiBroker can easily mix in expressions with arrays)

They are NOT the values of the Donchian channel: on a chart, they will be displayed as a horizontal line at a fixed price level (during the optimization this 2 scalars may vary from 2 to 100), but in any case, you will never get a "cross" with any price out of this range.

You need to add some code to properly define your Donchian channels (see this thread for example).

Let's call the resulting channels/arrays like in the linked example as DonchianUpper and DonchianLower.

Then in your "cross" rules, you need to use DonchianUpper / High and DonchianLower / Low.

What parameters is, therefore, possible to "optimize"? Maybe something along these lines:

TopCPeriods = Optimize( "Top Channel  periods", 9, 2, 100, 1 );
BotCPeriods = Optimize( "Bottom Channel periods", 9, 2, 100, 1 );
DonchianUpper = HHV( Ref( H, -1 ), TopCPeriods );
DonchianLower = LLV( Ref( L, -1 ), BotCPeriods );

// Buy/Sell/Short/Cover logic
// Why not doing some test also reversing Buy and Sell logic?
Buy = Cross( H, DonchianUpper );
Sell = Cross( DonchianLower, L );
// etc.

I hope these suggestions will help you to proceed with the creation of your system.

7 Likes