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

Top= Optimize( "CTop", 9, 2, 100, 1);
Bottom= Optimize( "CBottom", 9, 2, 100, 5 );

I get all zeros.
Thanks in Advance for any help!
Bill Zimmer

HI zim830
your code missing the following
1 What is your chart price setting?
2 What is your chart intreval?
3 What is your trade starting and ending time?
4 Which variable are going to optimize?
5 What is your buy sell short and cover conditions?
6.Remove execive signals using ExRem function
7 Plot Buy and Sell Signal Arrows
8 Select optimized value from optimization results.

You can modify this code to your requirements
Thanking you all
best wishes

_N(Title = Date()+EncodeColor(colorBlack)+ "  Edited by" + EncodeColor(colorRed)+"  Free Code"
/*+"  Open Range Breakout Afl" */+ EncodeColor(colorWhite)+   "   Open:"+ Open+" ,"+"High:"+High+" ,"+"Low:"+Low+" , "+"Close:"+Close  );
Plot( C, "Close", colorDefault, styleCandle );
_SECTION_BEGIN("Intraday ORB Exploration");
SetChartOptions( 0, chartShowArrows | chartShowDates );
SquareOffTime = 151500;
TimeFrameSet( in5Minute ); // switch to 5 minute frame 

x=Optimize( "UpDown Points", 0, 1, 30, 1 );

NewDay = Day()!= Ref(Day(), -1);
highestoftheday = HighestSince(newday,H,1);
Lowestoftheday =LowestSince(newday,L,1);

ORBM = DayOpen;

BuyPrice = LastValue(C);
SellPrice = LastValue(C);
ShortPrice = LastValue(C);
CoverPrice = LastValue(C);

Buy = C>DayOpen+x AND (TimeNum()  >= FirstTradeTime) AND TimeNum()<SquareOffTime;  
Short = C<DayOpen-x AND (TimeNum()  >= FirstTradeTime) AND TimeNum()<SquareOffTime; 

Sell = TimeNum() >= SquareOffTime;
Cover = TimeNum() >= SquareOffTime; 


Plot(ORBM, "", colorBlack, styleDots);
Plot ( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Cover, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);


Hello veepsirtt
Thank you for your response. I am not looking to back test or plot a Donchian Channel, I am looking to optimize the channel borders. Should it be 20 days, 50 days, or 20 days top and 65 days on the bottom etc.

Do you see what I am trying to do? Thanks Again

Bill Zimmer

1 Like

@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:


"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.