Académique Documents
Professionnel Documents
Culture Documents
March 8, 2008
There have been many requests for on-chart custom Command Buttons. Command Buttons are mouse-click
sensitive buttons or menu-items that, when clicked on, will execute a specific section of AFL code. This post
introduces Command Buttons of the Trigger-type that respond to Left-Button clicks. This Trigger Button can be
used in the same way you would use the ParamTrigger() function. Note that the first button does not respond to
mouse-clicks; it is not a trigger button. The TextCell was designed to display text only, for example to display
status information for your trading system. Here is an example of a simple horizontal layout:
To display the buttons horizontally lengthens the code a little because the code is optimized for vertical button
columns. Here is the code that places the above button array on your chart:
1 #include <ControlPanelInclude-001.afl>
2
3 global ColNumber;
4 RequestTimedRefresh(1);
5 CellHeight = Param("Cell Height",20,5,200,1);
6 CellWidth = Param("Cell Width",120,5,200,1);
7 PanelYoffset = Param("Cell Row Offset (px)",10,0,Status("pxheight"),1);
8 PanelXoffset = Param("Cell Column Offset (px)",10,0,Status("pxwidth"),1);
9 FontRatio = Param("Font: CellHeight ratio",2,1,20,0.1);
10
11 Column_Begin( "1" );
12 TextCell( "AUTO-TRADING", colorRed, colorBlack);
13 Column_End( );
14
15 Column_Begin( "2" );
16 Reset = TriggerCell( "START SESSION", colorBrightGreen, colorRed, colorBlack);
17 Column_End( );
18
19 Column_Begin( "3" );
20 CancelAll = TriggerCell( "CANCEL ALL", colorBrightGreen, colorRed, colorBlack);
21 Column_End( );
22
23 Column_Begin( "4" );
24 CloseAll = TriggerCell( "CLOSE ALL", colorBrightGreen, colorRed, colorBlack);
25 Column_End( );
26
27 Column_Begin( "5");
28 EndSession = TriggerCell( "END SESSION", colorBrightGreen, colorRed, colorBlack);
29 Column_End( );
30
31 ClickCoordinates = Nz(StaticVarGet("ClickCoordinates"));
32 switch( ClickCoordinates )
33 {
34 case 201:
amibroker.org/…/command-buttons-trig… 1/5
23/1/2010 AmiBroker Users’ Knowledge Base » Co…
35 Say( "201");
36 break;
37 case 301:
38 Say( "301");
39 break;
40 case 401:
41 Say( "401");
42 break;
43 case 501:
44 Say( "501");
45 break;
46 }
47
48 Plot(C,"",1,128);
49
50 Title = "CLICK COORDINATES: "+ClickCoordinates;
The Trigger function returns a trigger, i.e., a True state that lasts only for the current refresh and that returns False
at the next pass through the code. A Triggername is assigned to each button and is used to key the static
variables. Backcolor1 is the normal color of the button. Backcolor2 is the color the button takes on when it is
clicked on; this gives a visual confirmation that the click was registered. If a button is clicked on, the button
coordinates (vertical position, horizontal position) are returned in compressed for as
ColNumber*100+RowNumber.
Trigger action can be invoked in two ways: by checking the value returned by the trigger functions, and by
processing the click-coordinates in a Switch() statement. Each method may have advantages depending on the
application.
Below a listing of the revised Include file, please copy to your default include folder.
1 // ControlPanelInclude-001.afl
2 procedure kStaticVarSet( SName, SValue )
3 {
4 ChartID = GetChartID();
5 InIndicator = Status("Action") == 1;
6 if( InIndicator ) StaticVarSet(Sname+ChartID, Svalue);
7 }
8
9 function kStaticVarGet( SName )
10 {
11 ChartID = GetChartID();
12 Var = StaticVarGet(Sname+ChartID);
13 return Var;
14 }
15
16 procedure kStaticVarSetText( SName, SValue )
17 {
18 ChartID = GetChartID();
19 InIndicator = Status("Action") == 1;
20 if( InIndicator ) StaticVarSetText(Sname+ChartID, Svalue);
21 }
22
23 function kStaticVarGetText( SName )
24 {
25 ChartID = GetChartID();
26 return StaticVarGetText(Sname+ChartID);
27 }
28
29 function Column_Begin( ColName )
30 {
31 global FontRatio, ColName, ColNumber, CellHeight, CellWidth, PanelXoffset, PanelYoffset;
32 ColNumber = VarGet("ColNumber");
33 if( IsEmpty( ColNumber ) )
34 {
35 VarSet("ColNumber",1);
36 StaticVarSet("ClickCoordinates",0);
37 }
38 else VarSet("ColNumber", ++ColNumber);
39 ColName = ColName+GetChartID();
40 GfxSetOverlayMode( 0 );
41 GfxSelectFont( "Tahoma", CellHeight/FontRatio, 800 );
42 GfxSelectPen( colorBlack );
43 GfxSetBkMode( 1 );
amibroker.org/…/command-buttons-trig… 2/5
23/1/2010 AmiBroker Users’ Knowledge Base » Co…
44 kStaticVarSet("RowNumber"+ColName, 0);
45 VarSetText("ColName",ColName);
46 return ColNumber;
47 }
48
49 function Column_End( )
50 {
51 global CellHeight, CellWidth, PanelYoffset, PanelXoffset, ColNumber, RowNumber;
52 ChartIDStr = NumToStr(GetChartID(),1.0,False);
53 ColName = VarGetText("ColName");
54 ULCellX = PanelXoffset + (ColNumber-1) * CellWidth;
55 LRCellX = ULCellX + CellWidth;
56 for( Row = 1; Row <= RowNumber; Row++ )
57 {
58 ULCellY = (Row-1) * CellHeight + PanelYoffset;
59 LRCellY = ULCellY + CellHeight;
60 TextCell = kStaticVarGetText("TextCell"+ColName+Row);
61 TextColor = Nz(kStaticVarGet("TextColor"+ColName+Row));
62 BackColor = Nz(kStaticVarGet("BackColor"+ColName+Row));
63 GfxSelectSolidBrush( BackColor);
64 GfxRectangle( ULCellX, ULCellY, LRCellX, LRCellY );
65 GfxSetBkColor( BackColor);
66 GfxSetTextColor( TextColor );
67 GfxDrawText( TextCell, ULCellX, ULCellY, LRCellX, LRCellY, 32 | 1 | 4);
68 }
69 }
70
71 function TextCell( TextCell, backColor, TextColor)
72 {
73 global ColNumber, RowNumber;;
74 ColName = VarGetText("ColName");
75 RowNumber = Nz(kStaticVarGet("RowNumber"+ColName))+1;
76 kStaticVarSet("RowNumber"+ColName, RowNumber);
77 kStaticVarSetText("TextCell"+ColName+RowNumber, TextCell);
78 kStaticVarSet("TextColor"+ColName+RowNumber, TextColor);
79 kStaticVarSet("BackColor"+ColName+RowNumber, backColor);
80 }
81
82 function NewColumn()
83 {
84 VarSet("ColNumber", 0);
85 }
86
87 function CheckMouseClick( ColNumber, RowNumber )
88 {
89 global PanelYoffset, PanelXoffset, CellHeight, CellWidth;
90 LButtonDown = GetCursorMouseButtons() == 9;
91 Click = 0;
92 if( LButtonDown )
93 {
94 ULCellX = PanelXoffset + (ColNumber-1) * CellWidth;
95 LRCellX = ULCellX + CellWidth;
96 ULCellY = (RowNumber -1) * CellHeight + PanelYoffset;
97 LRCellY = ULCellY + CellHeight;
98 MouseCoord = Nz(StaticVarGet("ClickCoordinates"));
99 if( MouseCoord == 0 AND LButtonDown )
100 {
101 MousePx = GetCursorXPosition( 1 );
102 MousePy = GetCursorYPosition( 1 );
103 if( MousePx > ULCellX AND MousePx < LRCellX AND MousePy > ULCellY AND MousePy < LRCellY )
104 {
105 StaticVarSet("ClickCoordinates",ColNumber*100+RowNumber);
106 Click = 1;
107 }
108 }
109 }
110 return Click;
111 }
112
113 function TriggerCell( Label, backColor1, BackColor2, TextColor)
114 {
115 global ColNumber, RowNumber;;
116 ColName = VarGetText("ColName");
117 RowNumber = Nz(kStaticVarGet("RowNumber"+ColName))+1;
118 kStaticVarSet("RowNumber"+ColName, RowNumber);
119 Trigger = CheckMouseClick( ColNumber, RowNumber );
120 if( Trigger ) BackColor = backColor2; else BackColor = backColor1;
121 kStaticVarSetText("TextCell"+ColName+RowNumber, Label);
amibroker.org/…/command-buttons-trig… 3/5
23/1/2010 AmiBroker Users’ Knowledge Base » Co…
122 kStaticVarSet("TextColor"+ColName+RowNumber, TextColor);
123 kStaticVarSet("BackColor"+ColName+RowNumber, backColor);
124 return Trigger;
125 }
2. Tomasz Janeczko
March 10th, 2008 | 6:01 pm
Nice post. If only image posted was narrower it would be much better. You may consider scaling it down.
3. Herman
March 10th, 2008 | 6:37 pm
Leave a reply
name (required)
website
Submit Comment
Search
Info
Table Of Contents
Contributor Agreement
About
Author Guide
Contacts
FAQ
Terms of Use
Recent Posts
amibroker.org/…/command-buttons-trig… 4/5
23/1/2010 AmiBroker Users’ Knowledge Base » Co…
Plotting trades on chart
Date and Time to Number Conversions
Popup Window: Preventing pile-ups
Wordpress upgrade
US-Stocks Database (v2)
Recent Comments
Herman on High-Precision Delay and Interval Timing
Tomasz Janeczko on High-Precision Delay and Interval Timing
Tomasz Janeczko on Static Variables in RT Systems
Yofa on Real-Time Bar Period Timing
Anthony Abry on Introduction to Trading Systems - Management
Categories
Select Category
Archives
February 2009 (2)
August 2008 (1)
April 2008 (1)
March 2008 (20)
February 2008 (6)
January 2008 (1)
December 2007 (4)
November 2007 (18)
October 2007 (17)
September 2007 (17)
August 2007 (26)
July 2007 (20)
June 2007 (17)
May 2007 (8)
April 2007 (16)
January 2007 (1)
Meta
Log in
Entries RSS
Comments RSS
WordPress.org
amibroker.org/…/command-buttons-trig… 5/5