{"id":1650,"date":"2008-03-08T11:50:00","date_gmt":"2008-03-08T11:50:00","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/2008\/03\/08\/command-buttons-trigger-type\/"},"modified":"2008-03-21T13:13:12","modified_gmt":"2008-03-21T13:13:12","slug":"command-buttons-trigger-type","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2008\/03\/08\/command-buttons-trigger-type\/","title":{"rendered":"Command Buttons (Trigger type)"},"content":{"rendered":"

This series of posts is actually written while the functions are being developed. It is for this reason that the functions and Include file may change with each next post. Hopefully they will be better with each revision. Please always use the latest versions. <\/p>\n

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:<\/p>\n

\ntriggerbuttons.png<\/a><\/p>\n

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:<\/p>\n

<\/span>#include <ControlPanelInclude-001.afl>\r\r<\/span>global <\/span>ColNumber<\/span>;\r<\/span>RequestTimedRefresh<\/span>(<\/span>1<\/span>);\r<\/span>CellHeight <\/span>= <\/span>Param<\/span>(<\/span>"Cell Height"<\/span>,<\/span>20<\/span>,<\/span>5<\/span>,<\/span>200<\/span>,<\/span>1<\/span>); \r<\/span>CellWidth <\/span>= <\/span>Param<\/span>(<\/span>"Cell Width"<\/span>,<\/span>120<\/span>,<\/span>5<\/span>,<\/span>200<\/span>,<\/span>1<\/span>); \r<\/span>PanelYoffset <\/span>= <\/span>Param<\/span>(<\/span>"Cell Row Offset (px)"<\/span>,<\/span>10<\/span>,<\/span>0<\/span>,<\/span>Status<\/span>(<\/span>"pxheight"<\/span>),<\/span>1<\/span>); \r<\/span>PanelXoffset <\/span>= <\/span>Param<\/span>(<\/span>"Cell Column Offset (px)"<\/span>,<\/span>10<\/span>,<\/span>0<\/span>,<\/span>Status<\/span>(<\/span>"pxwidth"<\/span>),<\/span>1<\/span>); \r<\/span>FontRatio <\/span>= <\/span>Param<\/span>(<\/span>"Font: CellHeight ratio"<\/span>,<\/span>2<\/span>,<\/span>1<\/span>,<\/span>20<\/span>,<\/span>0.1<\/span>);\r\r<\/span>Column_Begin<\/span>( <\/span>"1" <\/span>);\r<\/span>TextCell<\/span>( <\/span>"AUTO-TRADING"<\/span>, <\/span>colorRed<\/span>, <\/span>colorBlack<\/span>);\r<\/span>Column_End<\/span>( );\r\r<\/span>Column_Begin<\/span>( <\/span>"2" <\/span>);\r<\/span>Reset <\/span>= <\/span>TriggerCell<\/span>( <\/span>"START SESSION"<\/span>, <\/span>colorBrightGreen<\/span>, <\/span>colorRed<\/span>, <\/span>colorBlack<\/span>);\r<\/span>Column_End<\/span>( );\r\r<\/span>Column_Begin<\/span>( <\/span>"3" <\/span>);\r<\/span>CancelAll <\/span>= <\/span>TriggerCell<\/span>( <\/span>"CANCEL ALL"<\/span>, <\/span>colorBrightGreen<\/span>, <\/span>colorRed<\/span>, <\/span>colorBlack<\/span>);\r<\/span>Column_End<\/span>( );\r\r<\/span>Column_Begin<\/span>( <\/span>"4" <\/span>);\r<\/span>CloseAll <\/span>= <\/span>TriggerCell<\/span>( <\/span>"CLOSE ALL"<\/span>, <\/span>colorBrightGreen<\/span>, <\/span>colorRed<\/span>, <\/span>colorBlack<\/span>);\r<\/span>Column_End<\/span>( );\r\r<\/span>Column_Begin<\/span>( <\/span>"5"<\/span>);\r<\/span>EndSession <\/span>= <\/span>TriggerCell<\/span>( <\/span>"END SESSION"<\/span>, <\/span>colorBrightGreen<\/span>, <\/span>colorRed<\/span>, <\/span>colorBlack<\/span>);\r<\/span>Column_End<\/span>( );\r\r<\/span>ClickCoordinates <\/span>= <\/span>Nz<\/span>(<\/span>StaticVarGet<\/span>(<\/span>"ClickCoordinates"<\/span>));\rswitch( <\/span>ClickCoordinates <\/span>)\r    {\r    case <\/span>201<\/span>:\r    <\/span>Say<\/span>( <\/span>"201"<\/span>);\r    break;\r    case <\/span>301<\/span>:\r    <\/span>Say<\/span>( <\/span>"301"<\/span>);\r    break;\r    case <\/span>401<\/span>:\r    <\/span>Say<\/span>( <\/span>"401"<\/span>);\r    break;\r    case <\/span>501<\/span>:\r    <\/span>Say<\/span>( <\/span>"501"<\/span>);\r    break;\r    }\r\r<\/span>Plot<\/span>(<\/span>C<\/span>,<\/span>""<\/span>,<\/span>1<\/span>,<\/span>128<\/span>);\r\r<\/span>Title <\/span>= <\/span>"CLICK COORDINATES: "<\/span>+<\/span>ClickCoordinates<\/span>;<\/span><\/pre>\n

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.<\/p>\n

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.<\/p>\n

Below a listing of the revised Include file, please copy to your default include folder. <\/p>\n

<\/span>\/\/ ControlPanelInclude-001.afl\r<\/span>procedure kStaticVarSet<\/span>( <\/span>SName<\/span>, <\/span>SValue <\/span>)         \r    {\r    <\/span>ChartID <\/span>= <\/span>GetChartID<\/span>();\r    <\/span>InIndicator <\/span>= <\/span>Status<\/span>(<\/span>"Action"<\/span>) == <\/span>1<\/span>;\r    if( <\/span>InIndicator <\/span>) <\/span>StaticVarSet<\/span>(<\/span>Sname<\/span>+<\/span>ChartID<\/span>, <\/span>Svalue<\/span>); \r    }\r\rfunction <\/span>kStaticVarGet<\/span>( <\/span>SName <\/span>)                     \r    { \r    <\/span>ChartID     <\/span>= <\/span>GetChartID<\/span>();\r    Var = <\/span>StaticVarGet<\/span>(<\/span>Sname<\/span>+<\/span>ChartID<\/span>);\r    return Var;\r    }\r\r<\/span>procedure kStaticVarSetText<\/span>( <\/span>SName<\/span>, <\/span>SValue <\/span>)     \r    { \r    <\/span>ChartID     <\/span>= <\/span>GetChartID<\/span>();\r    <\/span>InIndicator <\/span>= <\/span>Status<\/span>(<\/span>"Action"<\/span>) == <\/span>1<\/span>;\r    if( <\/span>InIndicator <\/span>) <\/span>StaticVarSetText<\/span>(<\/span>Sname<\/span>+<\/span>ChartID<\/span>, <\/span>Svalue<\/span>); \r    }\r\rfunction <\/span>kStaticVarGetText<\/span>( <\/span>SName <\/span>)                 \r    { \r    <\/span>ChartID <\/span>= <\/span>GetChartID<\/span>();\r    return <\/span>StaticVarGetText<\/span>(<\/span>Sname<\/span>+<\/span>ChartID<\/span>); \r    }\r\rfunction <\/span>Column_Begin<\/span>( <\/span>ColName <\/span>) \r    {\r    global <\/span>FontRatio<\/span>, <\/span>ColName<\/span>, <\/span>ColNumber<\/span>, <\/span>CellHeight<\/span>, <\/span>CellWidth<\/span>, <\/span>PanelXoffset<\/span>, <\/span>PanelYoffset<\/span>;\r    <\/span>ColNumber <\/span>= <\/span>VarGet<\/span>(<\/span>"ColNumber"<\/span>);\r    if( <\/span>IsEmpty<\/span>( <\/span>ColNumber <\/span>) ) \r        {\r        <\/span>VarSet<\/span>(<\/span>"ColNumber"<\/span>,<\/span>1<\/span>);\r        <\/span>StaticVarSet<\/span>(<\/span>"ClickCoordinates"<\/span>,<\/span>0<\/span>);\r        }\r    else <\/span>VarSet<\/span>(<\/span>"ColNumber"<\/span>, ++<\/span>ColNumber<\/span>);\r    <\/span>ColName <\/span>= <\/span>ColName<\/span>+<\/span>GetChartID<\/span>();\r    <\/span>GfxSetOverlayMode<\/span>( <\/span>0 <\/span>);\r    <\/span>GfxSelectFont<\/span>( <\/span>"Tahoma"<\/span>, <\/span>CellHeight<\/span>\/<\/span>FontRatio<\/span>, <\/span>800 <\/span>); \r    <\/span>GfxSelectPen<\/span>( <\/span>colorBlack <\/span>); \r    <\/span>GfxSetBkMode<\/span>( <\/span>1 <\/span>);\r    <\/span>kStaticVarSet<\/span>(<\/span>"RowNumber"<\/span>+<\/span>ColName<\/span>, <\/span>0<\/span>);\r    <\/span>VarSetText<\/span>(<\/span>"ColName"<\/span>,<\/span>ColName<\/span>);\r    return <\/span>ColNumber<\/span>;\r    }\r\rfunction <\/span>Column_End<\/span>( )\r    {\r    global <\/span>CellHeight<\/span>, <\/span>CellWidth<\/span>, <\/span>PanelYoffset<\/span>, <\/span>PanelXoffset<\/span>, <\/span>ColNumber<\/span>, <\/span>RowNumber<\/span>;\r    <\/span>ChartIDStr     <\/span>= <\/span>NumToStr<\/span>(<\/span>GetChartID<\/span>(),<\/span>1.0<\/span>,<\/span>False<\/span>);\r    <\/span>ColName         <\/span>= <\/span>VarGetText<\/span>(<\/span>"ColName"<\/span>);\r    <\/span>ULCellX         <\/span>= <\/span>PanelXoffset <\/span>+ (<\/span>ColNumber<\/span>-<\/span>1<\/span>) * <\/span>CellWidth<\/span>;\r    <\/span>LRCellX        <\/span>= <\/span>ULCellX <\/span>+ <\/span>CellWidth<\/span>;\r    for( <\/span>Row <\/span>= <\/span>1<\/span>; <\/span>Row <\/span><= <\/span>RowNumber<\/span>; <\/span>Row<\/span>++ ) \r        {\r        <\/span>ULCellY         <\/span>= (<\/span>Row<\/span>-<\/span>1<\/span>) * <\/span>CellHeight <\/span>+ <\/span>PanelYoffset<\/span>;\r        <\/span>LRCellY        <\/span>= <\/span>ULCellY <\/span>+ <\/span>CellHeight<\/span>;\r        <\/span>TextCell     <\/span>= <\/span>kStaticVarGetText<\/span>(<\/span>"TextCell"<\/span>+<\/span>ColName<\/span>+<\/span>Row<\/span>);\r        <\/span>TextColor     <\/span>= <\/span>Nz<\/span>(<\/span>kStaticVarGet<\/span>(<\/span>"TextColor"<\/span>+<\/span>ColName<\/span>+<\/span>Row<\/span>));\r        <\/span>BackColor     <\/span>= <\/span>Nz<\/span>(<\/span>kStaticVarGet<\/span>(<\/span>"BackColor"<\/span>+<\/span>ColName<\/span>+<\/span>Row<\/span>));\r        <\/span>GfxSelectSolidBrush<\/span>( <\/span>BackColor<\/span>);\r        <\/span>GfxRectangle<\/span>( <\/span>ULCellX<\/span>, <\/span>ULCellY<\/span>, <\/span>LRCellX<\/span>, <\/span>LRCellY <\/span>); \r        <\/span>GfxSetBkColor<\/span>( <\/span>BackColor<\/span>);\r        <\/span>GfxSetTextColor<\/span>( <\/span>TextColor <\/span>);\r        <\/span>GfxDrawText<\/span>( <\/span>TextCell<\/span>, <\/span>ULCellX<\/span>, <\/span>ULCellY<\/span>, <\/span>LRCellX<\/span>, <\/span>LRCellY<\/span>, <\/span>32 <\/span>| <\/span>1 <\/span>| <\/span>4<\/span>);\r        }\r    }\r\rfunction <\/span>TextCell<\/span>( <\/span>TextCell<\/span>, <\/span>backColor<\/span>, <\/span>TextColor<\/span>)\r    {\r    global <\/span>ColNumber<\/span>, <\/span>RowNumber<\/span>;;\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>kStaticVarSetText<\/span>(<\/span>"TextCell"<\/span>+<\/span>ColName<\/span>+<\/span>RowNumber<\/span>, <\/span>TextCell<\/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    }\r\rfunction <\/span>NewColumn<\/span>()\r    {\r    <\/span>VarSet<\/span>(<\/span>"ColNumber"<\/span>, <\/span>0<\/span>);\r    }\r\rfunction <\/span>CheckMouseClick<\/span>( <\/span>ColNumber<\/span>, <\/span>RowNumber <\/span>)\r    {\r    global <\/span>PanelYoffset<\/span>, <\/span>PanelXoffset<\/span>, <\/span>CellHeight<\/span>, <\/span>CellWidth<\/span>;\r    <\/span>LButtonDown <\/span>= <\/span>GetCursorMouseButtons<\/span>() == <\/span>9<\/span>;\r    <\/span>Click <\/span>= <\/span>0<\/span>;\r    if( <\/span>LButtonDown <\/span>)\r        {\r        <\/span>ULCellX         <\/span>= <\/span>PanelXoffset <\/span>+ (<\/span>ColNumber<\/span>-<\/span>1<\/span>) * <\/span>CellWidth<\/span>;\r        <\/span>LRCellX        <\/span>= <\/span>ULCellX <\/span>+ <\/span>CellWidth<\/span>;\r        <\/span>ULCellY         <\/span>= (<\/span>RowNumber <\/span>-<\/span>1<\/span>) * <\/span>CellHeight <\/span>+ <\/span>PanelYoffset<\/span>;\r        <\/span>LRCellY        <\/span>= <\/span>ULCellY <\/span>+ <\/span>CellHeight<\/span>;\r        <\/span>MouseCoord <\/span>= <\/span>Nz<\/span>(<\/span>StaticVarGet<\/span>(<\/span>"ClickCoordinates"<\/span>));\r        if( <\/span>MouseCoord <\/span>== <\/span>0 <\/span>AND <\/span>LButtonDown <\/span>)\r            {\r            <\/span>MousePx <\/span>= <\/span>GetCursorXPosition<\/span>( <\/span>1 <\/span>);\r            <\/span>MousePy <\/span>= <\/span>GetCursorYPosition<\/span>( <\/span>1 <\/span>);\r            if( <\/span>MousePx <\/span>> <\/span>ULCellX <\/span>AND <\/span>MousePx <\/span>< <\/span>LRCellX <\/span>AND <\/span>MousePy <\/span>> <\/span>ULCellY <\/span>AND <\/span>MousePy <\/span>< <\/span>LRCellY <\/span>)\r                {\r                <\/span>StaticVarSet<\/span>(<\/span>"ClickCoordinates"<\/span>,<\/span>ColNumber<\/span>*<\/span>100<\/span>+<\/span>RowNumber<\/span>);\r                <\/span>Click <\/span>= <\/span>1<\/span>;\r                }\r            }\r        }\r    return <\/span>Click<\/span>;\r    }\r\rfunction <\/span>TriggerCell<\/span>( <\/span>Label<\/span>, <\/span>backColor1<\/span>, <\/span>BackColor2<\/span>, <\/span>TextColor<\/span>)\r    {\r    global <\/span>ColNumber<\/span>, <\/span>RowNumber<\/span>;;\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>Trigger <\/span>= <\/span>CheckMouseClick<\/span>( <\/span>ColNumber<\/span>, <\/span>RowNumber <\/span>);\r    if( <\/span>Trigger <\/span>) <\/span>BackColor <\/span>= <\/span>backColor2<\/span>; else <\/span>BackColor <\/span>= <\/span>backColor1<\/span>;\r    <\/span>kStaticVarSetText<\/span>(<\/span>"TextCell"<\/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    return <\/span>Trigger<\/span>;\r    }  <\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"

This series of posts is actually written while the functions are being developed. It is for this reason that the functions and Include file may change with each next post. Hopefully they will be better with each revision. Please always use the latest versions. There have been many requests for on-chart custom Command Buttons. Command […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[134],"tags":[],"_links":{"self":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/1650"}],"collection":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/comments?post=1650"}],"version-history":[{"count":0,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/1650\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/media?parent=1650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/categories?post=1650"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/tags?post=1650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}