September 23, 2007
Numbering Bars
When you are developing systems and discussing charts with other traders by email, it is often convenient to be able to refer to the bars on your chart by number, date, or time. The code below can be appended to the end of your code to give you these options:
The b < Barcount check prevents out of range errors when you have AmiBroker Preferences set to give you a number of Blank bars at the right edge of your chart.
Plot(C,"",1,128); _SECTION_BEGIN("BAR LABELING"); ShowBars = ParamToggle("Label Bars","NO|YES",0); LabelCo = ParamColor("Label",colorYellow); Label = ParamList("Bar Label","NUMBER|TIMENUM|DATENUM",0); GraphXSpace = 10; if( Showbars ) { DN=DateNum(); TN=TimeNum(); FirstVisibleBar = Status( "FirstVisibleBar" ); Lastvisiblebar = Status("LastVisibleBar"); for( b = Firstvisiblebar, BN=0; b <= Lastvisiblebar AND b < BarCount; b++) { if( Label == "NUMBER" ) BarLabel = NumToStr(BN++,1.0); else if ( Label == "TIMENUM" ) BarLabel = NumToStr(TN[b],1.0); else BarLabel= NumToStr(DN[b],1.0); PlotText("\n^\n"+BarLabel,b,L[b],LabelCo); } } _SECTION_END();
The labeled charts look like this:
Below a more streamlined version using the Switch():
_SECTION_BEGIN( "BAR LABELING" ); Plot( C, "", 1, 128 ); ShowBars = ParamToggle( "Label Bars", "NO|YES", 0 ); LabelCo = ParamColor( "Label", colorYellow ); Label = ParamList( "BarLabel", "NUMBER|TimeNum|DateNum|BARNUM|WEEKDAYNUM|WEEKDAY", 0 ); GraphXSpace = 10; if ( Showbars ) { DN = DateNum(); TN = TimeNum(); WD = DayOfWeek(); WDList = "SUN,MON,TUE,WED,THU,FRI,SAT"; FirstVisibleBar = Status( "FirstVisibleBar" ); Lastvisiblebar = Status( "LastVisibleBar" ); for ( b = Firstvisiblebar, BN = 0; b <= Lastvisiblebar AND b < BarCount; b++ ) { switch ( Label ) { case "NUMBER": BarLabel = NumToStr( BN++, 1.0 ); break; case "TIMENUM": BarLabel = NumToStr( TN[b], 1.0 ); break; case "BARNUM": BarLabel = NumToStr( b, 1.0 ); break; case "WEEKDAYNUM": BarLabel = NumToStr( WD[b], 1.0 ); break; case "WEEKDAY": BarLabel = StrExtract( WDList, WD[b] ); break; default: BarLabel = NumToStr( DN[b], 1.0 ); break; } PlotText( "\n^\n" + BarLabel, b, L[b] - ( H[b] - L[b] ), LabelCo ); } } _SECTION_END();
Edited by Al Venosa.
Filed by Herman at 4:10 pm under Debugging AFL
Comments Off on Numbering Bars