Sei sulla pagina 1di 6

23/1/2010 AmiBroker Users’ Knowledge Base » Butt…

Users' Knowledge Base


Share your experience, code and everything with other AmiBroker
Users’.

March 9, 2008

Button Rotate
A ButtonRotate function is like a ParamToggle() but with multiple states. The ButtonRotate function returns the
label displayed on the button, and selects the next label each time you click the button. In the example below the
ButtonRotate is used to select the next action, which can be Buy, Sell, Short, Cover, Cash, or Reverse. The end
result of using this function is similar to using the ParamList() however it is much quicker to use. When the action
is selected the order can be transmitted using the Transmit ButtonTrigger().The function returns the displayed
label; sometimes the label can be used directly in PlaceOrder(), at other times you may have to use an if()
comparison to know which action to perform. For debugging purposes the Title shows the returned values:

The ButtonRotate() is listed below for discussion. There is no need to copy this because it is included in the
Include file at the end of this post.

1 function ButtonRotate( LabelStr, BackColorStr, TextColorStr )


2 {
3 global ColNumber, RowNumber, ColName, ColExpanded;
4
5 if ( ColExpanded )
6 {
7 ColName = VarGetText( "ColName" );
8 RowNumber = Nz( kStaticVarGet( "RowNumber" + ColName ) ) + 1;
9 kStaticVarSet( "RowNumber" + ColName, RowNumber );
10 Rotate = GetButtonClick( ColNumber, RowNumber );
11 if ( Rotate OR IsNull( StaticVarGet("RotateInit"+ ColName + RowNumber ) ) )
12 {
13 RotateIndex = Nz( kStaticVarGet( "RotateIndex" + ColName + RowNumber ) );
14 if ( StrExtract( LabelStr, RotateIndex + 1) != "" ) RotateIndex++;
15 else RotateIndex = 0;
16 kStaticVarSet( "RotateIndex" + ColName + RowNumber, RotateIndex );
17
18 Label = StrExtract( LabelStr, RotateIndex );
19
20 if ( StrExtract( BackColorStr, RotateIndex ) == "" ) BackColor = StrToNum( StrExtract( BackColorStr, 0 ) );
21 else BackColor = StrToNum( StrExtract( BackColorStr, RotateIndex ) );
22
23 if ( StrExtract( TextColorStr, RotateIndex ) == "" ) TextColor = StrToNum( StrExtract( TextColorStr, 0 ) );
24 else TextColor = StrToNum( StrExtract( TextColorStr, RotateIndex ) );
25
26 kStaticVarSetText( "Label" + ColName + RowNumber, Label );
27 kStaticVarSet( "TextColor" + ColName + RowNumber, TextColor );
28 kStaticVarSet( "BackColor" + ColName + RowNumber, BackColor );
29 StaticVarSet("RotateInit"+ ColName + RowNumber, True);
30 }
31 }
32 Label = kStaticVarGetText( "Label" + ColName + RowNumber);
33 return Label;
34 }

Referring to the above code you’ll see the usual ColExpanded variable that determines whether this button will
be displayed. A RotateInit var is used to detect whether the button has been initialized, i.e., whether is was
assigned colors and text. Each time the function is called the RotateIndex incremented. This index is used to
extract the proper label and color from the csv encoded options in the string argument for the function.

amibroker.org/userkb/…/button-rotate/ 1/6
23/1/2010 AmiBroker Users’ Knowledge Base » Butt…
The code below demonstrates how the ButtonRotate is used. Note that for brevity I used digits to indicate
colors. You can also use constants like ColorRed, ColorBlue, etc.

1 #include <ControlPanelInclude-004.afl>
2
3 global ColNumber;
4 RequestTimedRefresh(1);
5 ButtonHeight = Param("Button Height",20,5,200,1);
6 ButtonWidth = Param("Button Width",120,5,200,1);
7 PanelYoffset = Param("Button Row Offset (px)",10,0,Status("pxheight"),1);
8 PanelXoffset = Param("Button Column Offset (px)",10,0,Status("pxwidth"),1);
9 FontRatio = Param("Font: ButtonHeight ratio",2,1,20,0.1);
10 DoubleClickInterval = Param("Double Click Max. Interval",330,1,1000,1);
11
12 ButtonColumnBegin( "1" );
13 ButtonHeader( "HEADER", colorBlue, colorBlue,colorWhite);
14 ButtonText( "TRADING ENABLED", colorYellow, colorBlue);
15
16 Action=ButtonRotate( "BUY,SELL,SHORT,COVER,CASH,REVERSE", "6,5,1,3,2,4", "2,3,4,5,6,1" );
17 Transmit = ButtonTrigger( "TRANSMIT", colorBrightGreen, colorRed, colorBlack);
18 ButtonColumnEnd( );
19
20 ClickCoordinates = Nz(StaticVarGet("ClickCoordinates"));
21 switch( ClickCoordinates )
22 {
23 case 101:
24 // Perform Button task 101 here
25 break;
26 case 102:
27 // Perform Button task 102 here
28 break;
29 // etc.
30 }
31
32 Plot(C,"",1,128);
33
34 Title = "\n"+
35 "Button Coordinates: "+ClickCoordinates+"\n"+
36 "Action: "+Action+"\n"+
37 "Transmit: "+Transmit;

As always, here follows the revised Include file with the ButtonRotate() included.

1 // ControlPanelInclude-004.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 NewColumn()
30 {
31 VarSet("ColNumber", 0);
32 }
33
34 function GetButtonClick( ColNumber, RowNumber )
35 {
36 global PanelYoffset, PanelXoffset, ButtonHeight, ButtonWidth;

amibroker.org/userkb/…/button-rotate/ 2/6
23/1/2010 AmiBroker Users’ Knowledge Base » Butt…
37 LButtonDown = GetCursorMouseButtons() == 9;
38 Click = False;
39 if( LButtonDown )
40 {
41 ULButtonX = PanelXoffset + (ColNumber-1) * ButtonWidth;
42 LRButtonX = ULButtonX + ButtonWidth;
43 ULButtonY = (RowNumber -1) * ButtonHeight + PanelYoffset;
44 LRButtonY = ULButtonY + ButtonHeight;
45 MouseCoord = Nz(StaticVarGet("ClickCoordinates"));
46 if( MouseCoord == 0 AND LButtonDown )
47 {
48 MousePx = GetCursorXPosition( 1 );
49 MousePy = GetCursorYPosition( 1 );
50 if( MousePx > ULButtonX AND MousePx < LRButtonX AND MousePy > ULButtonY AND MousePy < LRButtonY )
51 {
52 StaticVarSet("ClickCoordinates",ColNumber*100+RowNumber);
53 Click = 1;
54 }
55 }
56 }
57 return Click;
58 }
59
60 function ButtonColumnBegin( ColName )
61 {
62 global FontRatio, ColName, ColNumber, ButtonHeight, ButtonWidth, PanelXoffset, PanelYoffset, Colname;
63 ColNumber = VarGet("ColNumber");
64 if( IsEmpty( ColNumber ) )
65 {
66 VarSet("ColNumber",1);
67 StaticVarSet("ClickCoordinates",0);
68 }
69 else VarSet("ColNumber", ++ColNumber);
70 ColName = ColName+GetChartID();
71 kStaticVarSet("RowNumber"+ColName, 0);
72 VarSetText("ColName",ColName);
73 GfxSetOverlayMode( 0 );
74 GfxSelectFont( "Tahoma", ButtonHeight/FontRatio, 800 );
75 GfxSelectPen( colorBlack );
76 GfxSetBkMode( 1 );
77 }
78
79 function ButtonHeader( Label, backColor1, BackColor2, TextColor)
80 {
81 global ColNumber, RowNumber, ColExpanded, Colname;
82 RowNumber = Nz(kStaticVarGet("RowNumber"+ColName))+1;
83 kStaticVarSet("RowNumber"+ColName, RowNumber);
84 SingleClick = GetButtonClick( ColNumber, RowNumber );
85 BackColor = backColor1;
86 ColExpanded = Nz(kStaticVarGet(ColName+"ColExpanded"));
87 if( SingleClick )
88 {
89 BackColor = backColor2;
90 ColExpanded = Nz(kStaticVarGet(ColName+"ColExpanded"));
91 if( ColExpanded ) kStaticVarSet(ColName+"ColExpanded", False);
92 else kStaticVarSet(ColName+"ColExpanded", True);
93 }
94 ColExpanded = Nz(kStaticVarGet(ColName+"ColExpanded"));
95 kStaticVarSetText("Label"+ColName+RowNumber, Label);
96 kStaticVarSet("TextColor"+ColName+RowNumber, TextColor);
97 kStaticVarSet("BackColor"+ColName+RowNumber, backColor);
98 }
99
100 function ButtonText( Label, backColor, TextColor)
101 {
102 global ColNumber, RowNumber, Colname;
103 ColExpanded = Nz(kStaticVarGet(ColName+"ColExpanded"));
104 if( ColExpanded )
105 {
106 ColName = VarGetText("ColName");
107 RowNumber = Nz(kStaticVarGet("RowNumber"+ColName))+1;
108 kStaticVarSet("RowNumber"+ColName, RowNumber);
109 kStaticVarSetText("Label"+ColName+RowNumber, Label);
110 kStaticVarSet("TextColor"+ColName+RowNumber, TextColor);
111 kStaticVarSet("BackColor"+ColName+RowNumber, backColor);
112 }
113 }
114
115 function ButtonTrigger( Label, backColor1, BackColor2, TextColor)
116 {
117 global ColNumber, RowNumber, ColName;
118 ColExpanded = Nz(kStaticVarGet(ColName+"ColExpanded"));
119 if( ColExpanded )

amibroker.org/userkb/…/button-rotate/ 3/6
23/1/2010 AmiBroker Users’ Knowledge Base » Butt…
120 {
121 ColName = VarGetText("ColName");
122 RowNumber = Nz(kStaticVarGet("RowNumber"+ColName))+1;
123 kStaticVarSet("RowNumber"+ColName, RowNumber);
124 Trigger = GetButtonClick( ColNumber, RowNumber );
125 if( Trigger ) BackColor = backColor2; else BackColor = backColor1;
126 kStaticVarSetText("Label"+ColName+RowNumber, Label);
127 kStaticVarSet("TextColor"+ColName+RowNumber, TextColor);
128 kStaticVarSet("BackColor"+ColName+RowNumber, backColor);
129 }
130 else Trigger = 0;
131 return Trigger;
132 }
133
134 function ButtonRotate( LabelStr, BackColorStr, TextColorStr )
135 {
136 global ColNumber, RowNumber, ColName, ColExpanded;
137
138 if ( ColExpanded )
139 {
140 ColName = VarGetText( "ColName" );
141 RowNumber = Nz( kStaticVarGet( "RowNumber" + ColName ) ) + 1;
142 kStaticVarSet( "RowNumber" + ColName, RowNumber );
143 Rotate = GetButtonClick( ColNumber, RowNumber );
144 if ( Rotate OR IsNull( StaticVarGet("RotateInit"+ ColName + RowNumber ) ) )
145 {
146 RotateIndex = Nz( kStaticVarGet( "RotateIndex" + ColName + RowNumber ) );
147 if ( StrExtract( LabelStr, RotateIndex + 1) != "" ) RotateIndex++;
148 else RotateIndex = 0;
149 kStaticVarSet( "RotateIndex" + ColName + RowNumber, RotateIndex );
150
151 Label = StrExtract( LabelStr, RotateIndex );
152
153 if ( StrExtract( BackColorStr, RotateIndex ) == "" ) BackColor = StrToNum( StrExtract( BackColorStr, 0 ) );
154 else BackColor = StrToNum( StrExtract( BackColorStr, RotateIndex ) );
155
156 if ( StrExtract( TextColorStr, RotateIndex ) == "" ) TextColor = StrToNum( StrExtract( TextColorStr, 0 ) );
157 else TextColor = StrToNum( StrExtract( TextColorStr, RotateIndex ) );
158
159 kStaticVarSetText( "Label" + ColName + RowNumber, Label );
160 kStaticVarSet( "TextColor" + ColName + RowNumber, TextColor );
161 kStaticVarSet( "BackColor" + ColName + RowNumber, BackColor );
162 StaticVarSet("RotateInit"+ ColName + RowNumber, True);
163 }
164 }
165 Label = kStaticVarGetText( "Label" + ColName + RowNumber);
166 return Label;
167 }
168
169
170 function ButtonColumnEnd()
171 {
172 global ButtonHeight, ButtonWidth, PanelYoffset, PanelXoffset, ColNumber, RowNumber, ColName;
173 ChartIDStr = NumToStr(GetChartID(),1.0,False);
174 ULButtonX = PanelXoffset + (ColNumber-1) * ButtonWidth;
175 LRButtonX = ULButtonX + ButtonWidth;
176 for( Row = 1; Row <= RowNumber; Row++ )
177 {
178 ULButtonY = (Row-1) * ButtonHeight + PanelYoffset;
179 LRButtonY = ULButtonY + ButtonHeight;
180 Label = kStaticVarGetText("Label"+ColName+Row);
181 TextColor = Nz(kStaticVarGet("TextColor"+ColName+Row));
182 BackColor = Nz(kStaticVarGet("BackColor"+ColName+Row));
183 GfxSelectSolidBrush( BackColor);
184 GfxRectangle( ULButtonX, ULButtonY, LRButtonX, LRButtonY );
185 GfxSetBkColor( BackColor);
186 GfxSetTextColor( TextColor );
187 GfxDrawText( Label, ULButtonX, ULButtonY, LRButtonX, LRButtonY, 32 | 1 | 4);
188 }
189 }

Filed by Herman at 10:52 pm under Real-Time Control-Panels

(4 votes, average: 5 out of 5)


Loading ...

No comments yet. Be the first.


Leave a reply
amibroker.org/userkb/…/button-rotate/ 4/6
23/1/2010 AmiBroker Users’ Knowledge Base » Butt…
name (required)

email (will not be shown) (required)

website

Submit Comment

Search

Info
Table Of Contents
Contributor Agreement
About
Author Guide
Contacts
FAQ
Terms of Use

Recent Posts
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)

amibroker.org/userkb/…/button-rotate/ 5/6
23/1/2010 AmiBroker Users’ Knowledge Base » Butt…
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

Copyright (C)2006 AmiBroker.com.


This site uses WordPress Page generated in 0.552 seconds.

amibroker.org/userkb/…/button-rotate/ 6/6

Potrebbero piacerti anche