Command Buttons (Trigger type)

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 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:

triggerbuttons.png

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:

#include <ControlPanelInclude-001.afl>

global ColNumber;
RequestTimedRefresh(1);
CellHeight Param("Cell Height",20,5,200,1); 
CellWidth Param("Cell Width",120,5,200,1); 
PanelYoffset Param("Cell Row Offset (px)",10,0,Status("pxheight"),1); 
PanelXoffset Param("Cell Column Offset (px)",10,0,Status("pxwidth"),1); 
FontRatio Param("Font: CellHeight ratio",2,1,20,0.1);

Column_Begin"1" );
TextCell"AUTO-TRADING"colorRedcolorBlack);
Column_End( );

Column_Begin"2" );
Reset TriggerCell"START SESSION"colorBrightGreencolorRedcolorBlack);
Column_End( );

Column_Begin"3" );
CancelAll TriggerCell"CANCEL ALL"colorBrightGreencolorRedcolorBlack);
Column_End( );

Column_Begin"4" );
CloseAll TriggerCell"CLOSE ALL"colorBrightGreencolorRedcolorBlack);
Column_End( );

Column_Begin"5");
EndSession TriggerCell"END SESSION"colorBrightGreencolorRedcolorBlack);
Column_End( );

ClickCoordinates Nz(StaticVarGet("ClickCoordinates"));
switch( ClickCoordinates )
    {
    case 201:
    Say"201");
    break;
    case 301:
    Say"301");
    break;
    case 401:
    Say"401");
    break;
    case 501:
    Say"501");
    break;
    }

Plot(C,"",1,128);

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.

// ControlPanelInclude-001.afl
procedure kStaticVarSetSNameSValue )         
    {
    ChartID GetChartID();
    InIndicator Status("Action") == 1;
    if( InIndicator StaticVarSet(Sname+ChartIDSvalue); 
    }

function kStaticVarGetSName )                     
    { 
    ChartID     GetChartID();
    Var = StaticVarGet(Sname+ChartID);
    return Var;
    }

procedure kStaticVarSetTextSNameSValue )     
    { 
    ChartID     GetChartID();
    InIndicator Status("Action") == 1;
    if( InIndicator StaticVarSetText(Sname+ChartIDSvalue); 
    }

function kStaticVarGetTextSName )                 
    { 
    ChartID GetChartID();
    return StaticVarGetText(Sname+ChartID); 
    }

function Column_BeginColName ) 
    {
    global FontRatioColNameColNumberCellHeightCellWidthPanelXoffsetPanelYoffset;
    ColNumber VarGet("ColNumber");
    if( IsEmptyColNumber ) ) 
        {
        VarSet("ColNumber",1);
        StaticVarSet("ClickCoordinates",0);
        }
    else VarSet("ColNumber", ++ColNumber);
    ColName ColName+GetChartID();
    GfxSetOverlayMode);
    GfxSelectFont"Tahoma"CellHeight/FontRatio800 ); 
    GfxSelectPencolorBlack ); 
    GfxSetBkMode);
    kStaticVarSet("RowNumber"+ColName0);
    VarSetText("ColName",ColName);
    return ColNumber;
    }

function Column_End( )
    {
    global CellHeightCellWidthPanelYoffsetPanelXoffsetColNumberRowNumber;
    ChartIDStr     NumToStr(GetChartID(),1.0,False);
    ColName         VarGetText("ColName");
    ULCellX         PanelXoffset + (ColNumber-1) * CellWidth;
    LRCellX        ULCellX CellWidth;
    for( Row 1Row <= RowNumberRow++ ) 
        {
        ULCellY         = (Row-1) * CellHeight PanelYoffset;
        LRCellY        ULCellY CellHeight;
        TextCell     kStaticVarGetText("TextCell"+ColName+Row);
        TextColor     Nz(kStaticVarGet("TextColor"+ColName+Row));
        BackColor     Nz(kStaticVarGet("BackColor"+ColName+Row));
        GfxSelectSolidBrushBackColor);
        GfxRectangleULCellXULCellYLRCellXLRCellY ); 
        GfxSetBkColorBackColor);
        GfxSetTextColorTextColor );
        GfxDrawTextTextCellULCellXULCellYLRCellXLRCellY32 4);
        }
    }

function TextCellTextCellbackColorTextColor)
    {
    global ColNumberRowNumber;;
    ColName VarGetText("ColName");
    RowNumber Nz(kStaticVarGet("RowNumber"+ColName))+1;
    kStaticVarSet("RowNumber"+ColNameRowNumber);
    kStaticVarSetText("TextCell"+ColName+RowNumberTextCell);
    kStaticVarSet("TextColor"+ColName+RowNumberTextColor);
    kStaticVarSet("BackColor"+ColName+RowNumberbackColor);
    }

function NewColumn()
    {
    VarSet("ColNumber"0);
    }

function CheckMouseClickColNumberRowNumber )
    {
    global PanelYoffsetPanelXoffsetCellHeightCellWidth;
    LButtonDown GetCursorMouseButtons() == 9;
    Click 0;
    if( LButtonDown )
        {
        ULCellX         PanelXoffset + (ColNumber-1) * CellWidth;
        LRCellX        ULCellX CellWidth;
        ULCellY         = (RowNumber -1) * CellHeight PanelYoffset;
        LRCellY        ULCellY CellHeight;
        MouseCoord Nz(StaticVarGet("ClickCoordinates"));
        if( MouseCoord == AND LButtonDown )
            {
            MousePx GetCursorXPosition);
            MousePy GetCursorYPosition);
            if( MousePx ULCellX AND MousePx LRCellX AND MousePy ULCellY AND MousePy LRCellY )
                {
                StaticVarSet("ClickCoordinates",ColNumber*100+RowNumber);
                Click 1;
                }
            }
        }
    return Click;
    }

function TriggerCellLabelbackColor1BackColor2TextColor)
    {
    global ColNumberRowNumber;;
    ColName VarGetText("ColName");
    RowNumber Nz(kStaticVarGet("RowNumber"+ColName))+1;
    kStaticVarSet("RowNumber"+ColNameRowNumber);
    Trigger CheckMouseClickColNumberRowNumber );
    if( Trigger BackColor backColor2; else BackColor backColor1;
    kStaticVarSetText("TextCell"+ColName+RowNumberLabel);
    kStaticVarSet("TextColor"+ColName+RowNumberTextColor);
    kStaticVarSet("BackColor"+ColName+RowNumberbackColor);
    return Trigger;
    }  

Comments are closed.