{"id":1662,"date":"2008-03-09T22:52:13","date_gmt":"2008-03-09T22:52:13","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/2008\/03\/09\/button-rotate\/"},"modified":"2008-03-10T01:25:52","modified_gmt":"2008-03-10T01:25:52","slug":"button-rotate","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2008\/03\/09\/button-rotate\/","title":{"rendered":"Button Rotate"},"content":{"rendered":"
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: <\/p>\n
\n<\/a><\/p>\n 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.<\/p>\n Referring to the above code you’ll see the usual ColExpanded<\/i> variable that determines whether this button will be displayed. A RotateInit <\/em>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 <\/em>incremented. This index is used to extract the proper label and color from the csv encoded options in the string argument for the function.<\/p>\n 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. <\/p>\n<\/span>function <\/span>ButtonRotate<\/span>( <\/span>LabelStr<\/span>, <\/span>BackColorStr<\/span>, <\/span>TextColorStr <\/span>)\r{\r global <\/span>ColNumber<\/span>, <\/span>RowNumber<\/span>, <\/span>ColName<\/span>, <\/span>ColExpanded<\/span>;\r\r if ( <\/span>ColExpanded <\/span>)\r {\r <\/span>ColName <\/span>= <\/span>VarGetText<\/span>( <\/span>"ColName" <\/span>);\r <\/span>RowNumber <\/span>= <\/span>Nz<\/span>( <\/span>kStaticVarGet<\/span>( <\/span>"RowNumber" <\/span>+ <\/span>ColName <\/span>) ) + <\/span>1<\/span>;\r <\/span>kStaticVarSet<\/span>( <\/span>"RowNumber" <\/span>+ <\/span>ColName<\/span>, <\/span>RowNumber <\/span>);\r <\/span>Rotate <\/span>= <\/span>GetButtonClick<\/span>( <\/span>ColNumber<\/span>, <\/span>RowNumber <\/span>);\r if ( <\/span>Rotate <\/span>OR <\/span>IsNull<\/span>( <\/span>StaticVarGet<\/span>(<\/span>"RotateInit"<\/span>+ <\/span>ColName <\/span>+ <\/span>RowNumber <\/span>) ) )\r {\r <\/span>RotateIndex <\/span>= <\/span>Nz<\/span>( <\/span>kStaticVarGet<\/span>( <\/span>"RotateIndex" <\/span>+ <\/span>ColName <\/span>+ <\/span>RowNumber <\/span>) );\r if ( <\/span>StrExtract<\/span>( <\/span>LabelStr<\/span>, <\/span>RotateIndex <\/span>+ <\/span>1<\/span>) != <\/span>"" <\/span>) <\/span>RotateIndex<\/span>++;\r else <\/span>RotateIndex <\/span>= <\/span>0<\/span>;\r <\/span>kStaticVarSet<\/span>( <\/span>"RotateIndex" <\/span>+ <\/span>ColName <\/span>+ <\/span>RowNumber<\/span>, <\/span>RotateIndex <\/span>);\r\r <\/span>Label <\/span>= <\/span>StrExtract<\/span>( <\/span>LabelStr<\/span>, <\/span>RotateIndex <\/span>);\r\r if ( <\/span>StrExtract<\/span>( <\/span>BackColorStr<\/span>, <\/span>RotateIndex <\/span>) == <\/span>"" <\/span>) <\/span>BackColor <\/span>= <\/span>StrToNum<\/span>( <\/span>StrExtract<\/span>( <\/span>BackColorStr<\/span>, <\/span>0 <\/span>) );\r else <\/span>BackColor <\/span>= <\/span>StrToNum<\/span>( <\/span>StrExtract<\/span>( <\/span>BackColorStr<\/span>, <\/span>RotateIndex <\/span>) );\r\r if ( <\/span>StrExtract<\/span>( <\/span>TextColorStr<\/span>, <\/span>RotateIndex <\/span>) == <\/span>"" <\/span>) <\/span>TextColor <\/span>= <\/span>StrToNum<\/span>( <\/span>StrExtract<\/span>( <\/span>TextColorStr<\/span>, <\/span>0 <\/span>) );\r else <\/span>TextColor <\/span>= <\/span>StrToNum<\/span>( <\/span>StrExtract<\/span>( <\/span>TextColorStr<\/span>, <\/span>RotateIndex <\/span>) );\r\r <\/span>kStaticVarSetText<\/span>( <\/span>"Label" <\/span>+ <\/span>ColName <\/span>+ <\/span>RowNumber<\/span>, <\/span>Label <\/span>);\r <\/span>kStaticVarSet<\/span>( <\/span>"TextColor" <\/span>+ <\/span>ColName <\/span>+ <\/span>RowNumber<\/span>, <\/span>TextColor <\/span>);\r <\/span>kStaticVarSet<\/span>( <\/span>"BackColor" <\/span>+ <\/span>ColName <\/span>+ <\/span>RowNumber<\/span>, <\/span>BackColor <\/span>);\r <\/span>StaticVarSet<\/span>(<\/span>"RotateInit"<\/span>+ <\/span>ColName <\/span>+ <\/span>RowNumber<\/span>, <\/span>True<\/span>);\r }\r }\r <\/span>Label <\/span>= <\/span>kStaticVarGetText<\/span>( <\/span>"Label" <\/span>+ <\/span>ColName <\/span>+ <\/span>RowNumber<\/span>);\r return <\/span>Label<\/span>;\r}\r<\/span><\/pre>\n
<\/span>#include <ControlPanelInclude-004.afl>\r\r<\/span>global <\/span>ColNumber<\/span>;\r<\/span>RequestTimedRefresh<\/span>(<\/span>1<\/span>);\r<\/span>ButtonHeight <\/span>= <\/span>Param<\/span>(<\/span>"Button Height"<\/span>,<\/span>20<\/span>,<\/span>5<\/span>,<\/span>200<\/span>,<\/span>1<\/span>); \r<\/span>ButtonWidth <\/span>= <\/span>Param<\/span>(<\/span>