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:

rotatebutton.png

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.

function ButtonRotateLabelStrBackColorStrTextColorStr )
{
    global ColNumberRowNumberColNameColExpanded;

    if ( ColExpanded )
    {
        ColName VarGetText"ColName" );
        RowNumber NzkStaticVarGet"RowNumber" ColName ) ) + 1;
        kStaticVarSet"RowNumber" ColNameRowNumber );
        Rotate GetButtonClickColNumberRowNumber );
        if ( Rotate OR IsNullStaticVarGet("RotateInit"ColName RowNumber ) ) )
        {
            RotateIndex NzkStaticVarGet"RotateIndex" ColName RowNumber ) );
            if ( StrExtractLabelStrRotateIndex 1) != "" RotateIndex++;
            else RotateIndex 0;
            kStaticVarSet"RotateIndex" ColName RowNumberRotateIndex );

            Label StrExtractLabelStrRotateIndex );

            if ( StrExtractBackColorStrRotateIndex ) == "" BackColor StrToNumStrExtractBackColorStr) );
            else BackColor StrToNumStrExtractBackColorStrRotateIndex ) );

            if ( StrExtractTextColorStrRotateIndex ) == "" TextColor StrToNumStrExtractTextColorStr) );
            else TextColor =     StrToNumStrExtractTextColorStrRotateIndex ) );

            kStaticVarSetText"Label" ColName RowNumberLabel );
            kStaticVarSet"TextColor" ColName RowNumberTextColor );
            kStaticVarSet"BackColor" ColName RowNumberBackColor );
                StaticVarSet("RotateInit"ColName RowNumberTrue);
        }
    }
    Label     kStaticVarGetText"Label" ColName RowNumber);
    return Label;
}

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.

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.

#include <ControlPanelInclude-004.afl>

global ColNumber;
RequestTimedRefresh(1);
ButtonHeight            Param("Button Height",20,5,200,1); 
ButtonWidth             Param("Button Width",120,5,200,1); 
PanelYoffset             Param("Button Row Offset (px)",10,0,Status("pxheight"),1); 
PanelXoffset            Param("Button Column Offset (px)",10,0,Status("pxwidth"),1); 
FontRatio               Param("Font: ButtonHeight ratio",2,1,20,0.1);
DoubleClickInterval    Param("Double Click Max. Interval",330,1,1000,1);

ButtonColumnBegin"1" );
ButtonHeader"HEADER"colorBluecolorBlue,colorWhite);
ButtonText"TRADING ENABLED"colorYellowcolorBlue);

Action=ButtonRotate"BUY,SELL,SHORT,COVER,CASH,REVERSE""6,5,1,3,2,4""2,3,4,5,6,1" );
Transmit ButtonTrigger"TRANSMIT"colorBrightGreencolorRedcolorBlack);
ButtonColumnEnd( );

ClickCoordinates Nz(StaticVarGet("ClickCoordinates"));
switch( ClickCoordinates )
    {
    case 101:
    // Perform Button task 101 here
    break;
    case 102:
    // Perform Button task 102 here
    break;
    // etc.
    }

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

Title "\n"+
"Button Coordinates: "+ClickCoordinates+"\n"+
"Action: "+Action+"\n"+

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

// ControlPanelInclude-004.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 NewColumn()
    {
    VarSet("ColNumber"0);
    }

function GetButtonClickColNumberRowNumber )
    {
    global PanelYoffsetPanelXoffsetButtonHeightButtonWidth;
    LButtonDown GetCursorMouseButtons() == 9;
    Click False;
    if( LButtonDown )
        {
        ULButtonX     PanelXoffset + (ColNumber-1) * ButtonWidth;
        LRButtonX    ULButtonX ButtonWidth;
        ULButtonY     = (RowNumber -1) * ButtonHeight PanelYoffset;
        LRButtonY    ULButtonY ButtonHeight;
        MouseCoord     Nz(StaticVarGet("ClickCoordinates"));
        if( MouseCoord == AND LButtonDown )
            {
            MousePx GetCursorXPosition);
            MousePy GetCursorYPosition);
            if( MousePx ULButtonX AND MousePx LRButtonX AND MousePy ULButtonY AND MousePy LRButtonY )
                {
                StaticVarSet("ClickCoordinates",ColNumber*100+RowNumber);
                Click 1;
                }
            }
        }
    return Click;
    }

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

function ButtonHeaderLabelbackColor1BackColor2TextColor)
    {
    global ColNumberRowNumberColExpandedColname;
    RowNumber Nz(kStaticVarGet("RowNumber"+ColName))+1;
    kStaticVarSet("RowNumber"+ColNameRowNumber);
    SingleClick GetButtonClickColNumberRowNumber );
    BackColor backColor1;
    ColExpanded Nz(kStaticVarGet(ColName+"ColExpanded"));
    if( SingleClick ) 
        {
        BackColor backColor2; 
        ColExpanded Nz(kStaticVarGet(ColName+"ColExpanded"));
        if( ColExpanded kStaticVarSet(ColName+"ColExpanded"False);
        else kStaticVarSet(ColName+"ColExpanded"True);
        }
    ColExpanded Nz(kStaticVarGet(ColName+"ColExpanded"));
    kStaticVarSetText("Label"+ColName+RowNumberLabel);
    kStaticVarSet("TextColor"+ColName+RowNumberTextColor);
    kStaticVarSet("BackColor"+ColName+RowNumberbackColor);
    }

function ButtonTextLabelbackColorTextColor)
    {
    global ColNumberRowNumberColname;
    ColExpanded Nz(kStaticVarGet(ColName+"ColExpanded"));
    if( ColExpanded )
        {
        ColName VarGetText("ColName");
        RowNumber Nz(kStaticVarGet("RowNumber"+ColName))+1;
        kStaticVarSet("RowNumber"+ColNameRowNumber);
        kStaticVarSetText("Label"+ColName+RowNumberLabel);
        kStaticVarSet("TextColor"+ColName+RowNumberTextColor);
        kStaticVarSet("BackColor"+ColName+RowNumberbackColor);
        }
    }

function ButtonTriggerLabelbackColor1BackColor2TextColor)
    {
    global ColNumberRowNumberColName;
    ColExpanded Nz(kStaticVarGet(ColName+"ColExpanded"));
    if( ColExpanded )
        {
        ColName VarGetText("ColName");
        RowNumber Nz(kStaticVarGet("RowNumber"+ColName))+1;
        kStaticVarSet("RowNumber"+ColNameRowNumber);
        Trigger GetButtonClickColNumberRowNumber );
        if( Trigger BackColor backColor2; else BackColor backColor1;
        kStaticVarSetText("Label"+ColName+RowNumberLabel);
        kStaticVarSet("TextColor"+ColName+RowNumberTextColor);
        kStaticVarSet("BackColor"+ColName+RowNumberbackColor);
        }
    else Trigger 0;
    return Trigger;
    }

function ButtonRotateLabelStrBackColorStrTextColorStr )
{
    global ColNumberRowNumberColNameColExpanded;

    if ( ColExpanded )
    {
        ColName VarGetText"ColName" );
        RowNumber NzkStaticVarGet"RowNumber" ColName ) ) + 1;
        kStaticVarSet"RowNumber" ColNameRowNumber );
        Rotate GetButtonClickColNumberRowNumber );
        if ( Rotate OR IsNullStaticVarGet("RotateInit"ColName RowNumber ) ) )
        {
            RotateIndex NzkStaticVarGet"RotateIndex" ColName RowNumber ) );
            if ( StrExtractLabelStrRotateIndex 1) != "" RotateIndex++;
            else RotateIndex 0;
            kStaticVarSet"RotateIndex" ColName RowNumberRotateIndex );

            Label StrExtractLabelStrRotateIndex );

            if ( StrExtractBackColorStrRotateIndex ) == "" BackColor StrToNumStrExtractBackColorStr) );
            else BackColor StrToNumStrExtractBackColorStrRotateIndex ) );

            if ( StrExtractTextColorStrRotateIndex ) == "" TextColor StrToNumStrExtractTextColorStr) );
            else TextColor =     StrToNumStrExtractTextColorStrRotateIndex ) );

            kStaticVarSetText"Label" ColName RowNumberLabel );
            kStaticVarSet"TextColor" ColName RowNumberTextColor );
            kStaticVarSet"BackColor" ColName RowNumberBackColor );
                StaticVarSet("RotateInit"ColName RowNumberTrue);
        }
    }
    Label     kStaticVarGetText"Label" ColName RowNumber);
    return Label;
}


function ButtonColumnEnd()
    {
    global ButtonHeightButtonWidthPanelYoffsetPanelXoffsetColNumberRowNumberColName;
    ChartIDStr     NumToStr(GetChartID(),1.0,False);
    ULButtonX         PanelXoffset + (ColNumber-1) * ButtonWidth;
    LRButtonX        ULButtonX ButtonWidth;
    for( Row 1Row <= RowNumberRow++ ) 
        {
        ULButtonY         = (Row-1) * ButtonHeight PanelYoffset;
        LRButtonY        ULButtonY ButtonHeight;
        Label     kStaticVarGetText("Label"+ColName+Row);
        TextColor     Nz(kStaticVarGet("TextColor"+ColName+Row));
        BackColor     Nz(kStaticVarGet("BackColor"+ColName+Row));
        GfxSelectSolidBrushBackColor);
        GfxRectangleULButtonXULButtonYLRButtonXLRButtonY ); 
        GfxSetBkColorBackColor);
        GfxSetTextColorTextColor );
        GfxDrawTextLabelULButtonXULButtonYLRButtonXLRButtonY32 4);
        }
1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 5.00 out of 5)
Loading...Loading...

Button Header (Collapse/Expand)

Note: A number of variables and function names were changed to provide more consistent naming; please upgrade your code and Include file. I regret that such changes may happen rather often, however, the only alternative would be to complete all posts for this topic and publish them all at once. Since there is no guarantee that all code will ever be fully completed (most of my development work never stops), I think it is better to publish whatever is functional, as if this were a development blog.

The purpose of the Button Header is to name button columns (this is needed to key static variables) and provide a Collapse/Expand function for Button Columns. Clicking on a Button Header will alternatively Collapse and Expand button columns. This allows you to quickly expose chart sections that were hidden by the Control Panel. A collapsed Button Column will look like this:

clip_image002

Clicking on the Header Button will expand the column to look as show below:

clip_image002[5]

A few variables are displayed in the Title to facilitate debugging. The following listing shows the test code used to display the above Button Column. Note again that there are two ways to process button clicks: using the values returned by the button functions, or using the Switch() statement.

#include <ControlPanelInclude-003.afl>

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

ButtonColumnBegin"1" );
ButtonHeader"COLUMN HEADER1"colorBluecolorLightBlue,colorWhite);
ButtonText"AUTO-TRADING ON"colorBluecolorWhite);
Reset ButtonTrigger"START SESSION"colorBrightGreencolorRedcolorBlack);
CancelAll ButtonTrigger"CANCEL ALL"colorBrightGreencolorRedcolorBlack);
CloseAll ButtonTrigger"CLOSE ALL"colorBrightGreencolorRedcolorBlack);
EndSession ButtonTrigger"END SESSION"colorBrightGreencolorRedcolorBlack);
ButtonColumnEnd( );

ClickCoordinates Nz(StaticVarGet("ClickCoordinates"));
switch( ClickCoordinates )
    {
    case 101:
    Say"1 1");
    break;
    case 102:
    Say"1 2");
    break;
    case 103:
    Say"1 3");
    break;
    case 104:
    Say"1 4");
    break;
    case 105:
    Say"1 5");
    break;
    case 106:
    Say"1 6");
    break;
    }

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

Title "\n"+
"  Click Coordinates: "+ClickCoordinates+"\n"+
"Column Expanded Var: "+Nz(kStaticVarGet(ColName+"ColExpanded"));

The ButtonHeader() is similar to the ButtonText() function but has a Collapse/Expand variable added. Here is the code for the new ButtonHeader() function:

function ButtonHeaderLabelbackColor1BackColor2TextColor)
    {
    global ColNumberRowNumberColExpandedColname;
    RowNumber Nz(kStaticVarGet("RowNumber"+ColName))+1;
    kStaticVarSet("RowNumber"+ColNameRowNumber);
    Trigger GetMouseClickColNumberRowNumber );
    if( Trigger ) 
        {
        BackColor backColor2; 
        ColExpanded Nz(kStaticVarGet(ColName+"ColExpanded"));
        if( ColExpanded kStaticVarSet(ColName+"ColExpanded"False);
        else kStaticVarSet(ColName+"ColExpanded"True);
        }
    else BackColor backColor1;
    ColExpanded Nz(kStaticVarGet(ColName+"ColExpanded"));
    kStaticVarSetText("TextButton"+ColName+RowNumberLabel);
    kStaticVarSet("TextColor"+ColName+RowNumberTextColor);
    kStaticVarSet("BackColor"+ColName+RowNumberbackColor);
    }

In the above code, when the left Mouse button is clicked, a static variable named ColExpanded is toggled between True and False. All earlier button functions have been modified to only execute their internal code only if this variable is True. This way Buttons will only display if the variable ColExpanded it True. The listing below shows how the TriggerButton() was modified. This and the HeaderButton functions are located in the Include file at the end of this post; there is no need to copy them separately.

function ButtonTriggerLabelbackColor1BackColor2TextColor)
    {
    global ColNumberRowNumberColName;
    ColExpanded Nz(kStaticVarGet(ColName+"ColExpanded"));
    if( ColExpanded )
        {
        ColName VarGetText("ColName");
        RowNumber Nz(kStaticVarGet("RowNumber"+ColName))+1;
        kStaticVarSet("RowNumber"+ColNameRowNumber);
        Trigger GetMouseClickColNumberRowNumber );
        if( Trigger BackColor backColor2; else BackColor backColor1;
        kStaticVarSetText("TextButton"+ColName+RowNumberLabel);
        kStaticVarSet("TextColor"+ColName+RowNumberTextColor);
        kStaticVarSet("BackColor"+ColName+RowNumberbackColor);
        }
    else Trigger 0;
    return Trigger;
    }  

The new ControlPanelInclude-003.afl Include file is listed below, it must be copied to your default Include folder for the above code to work.

// ControlPanelInclude-003.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 NewColumn()
    {
    VarSet("ColNumber"0);
    }

function GetMouseClickColNumberRowNumber )
    {
    global PanelYoffsetPanelXoffsetButtonHeightButtonWidth;
    LButtonDown GetCursorMouseButtons() == 9;
    Click 0;
    if( LButtonDown )
        {
        ULButtonX         PanelXoffset + (ColNumber-1) * ButtonWidth;
        LRButtonX        ULButtonX ButtonWidth;
        ULButtonY         = (RowNumber -1) * ButtonHeight PanelYoffset;
        LRButtonY        ULButtonY ButtonHeight;
        MouseCoord Nz(StaticVarGet("ClickCoordinates"));
        if( MouseCoord == AND LButtonDown )
            {
            MousePx GetCursorXPosition);
            MousePy GetCursorYPosition);
            if( MousePx ULButtonX AND MousePx LRButtonX AND MousePy ULButtonY AND MousePy LRButtonY )
                {
                StaticVarSet("ClickCoordinates",ColNumber*100+RowNumber);
                Click 1;
                }
            }
        }
    return Click;
    }

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

function ButtonTextTextButtonbackColorTextColor)
    {
    global ColNumberRowNumberColname;
    ColExpanded Nz(kStaticVarGet(ColName+"ColExpanded"));
    if( ColExpanded )
        {
        ColName VarGetText("ColName");
        RowNumber Nz(kStaticVarGet("RowNumber"+ColName))+1;
        kStaticVarSet("RowNumber"+ColNameRowNumber);
        kStaticVarSetText("TextButton"+ColName+RowNumberTextButton);
        kStaticVarSet("TextColor"+ColName+RowNumberTextColor);
        kStaticVarSet("BackColor"+ColName+RowNumberbackColor);
        }
    }

function ButtonTriggerLabelbackColor1BackColor2TextColor)
    {
    global ColNumberRowNumberColName;
    ColExpanded Nz(kStaticVarGet(ColName+"ColExpanded"));
    if( ColExpanded )
        {
        ColName VarGetText("ColName");
        RowNumber Nz(kStaticVarGet("RowNumber"+ColName))+1;
        kStaticVarSet("RowNumber"+ColNameRowNumber);
        Trigger GetMouseClickColNumberRowNumber );
        if( Trigger BackColor backColor2; else BackColor backColor1;
        kStaticVarSetText("TextButton"+ColName+RowNumberLabel);
        kStaticVarSet("TextColor"+ColName+RowNumberTextColor);
        kStaticVarSet("BackColor"+ColName+RowNumberbackColor);
        }
    else Trigger 0;
    return Trigger;
    }

function ButtonHeaderLabelbackColor1BackColor2TextColor)
    {
    global ColNumberRowNumberColExpandedColname;
    RowNumber Nz(kStaticVarGet("RowNumber"+ColName))+1;
    kStaticVarSet("RowNumber"+ColNameRowNumber);
    Trigger GetMouseClickColNumberRowNumber );
    if( Trigger ) 
        {
        BackColor backColor2; 
        ColExpanded Nz(kStaticVarGet(ColName+"ColExpanded"));
        if( ColExpanded kStaticVarSet(ColName+"ColExpanded"False);
        else kStaticVarSet(ColName+"ColExpanded"True);
        }
    else BackColor backColor1;
    ColExpanded Nz(kStaticVarGet(ColName+"ColExpanded"));
    kStaticVarSetText("TextButton"+ColName+RowNumberLabel);
    kStaticVarSet("TextColor"+ColName+RowNumberTextColor);
    kStaticVarSet("BackColor"+ColName+RowNumberbackColor);
    }

function ButtonColumnEnd()
    {
    global ButtonHeightButtonWidthPanelYoffsetPanelXoffsetColNumberRowNumberColName;
    ChartIDStr     NumToStr(GetChartID(),1.0,False);
    ULButtonX         PanelXoffset + (ColNumber-1) * ButtonWidth;
    LRButtonX        ULButtonX ButtonWidth;
    for( Row 1Row <= RowNumberRow++ ) 
        {
        ULButtonY         = (Row-1) * ButtonHeight PanelYoffset;
        LRButtonY        ULButtonY ButtonHeight;
        Label     kStaticVarGetText("TextButton"+ColName+Row);
        TextColor     Nz(kStaticVarGet("TextColor"+ColName+Row));
        BackColor     Nz(kStaticVarGet("BackColor"+ColName+Row));
        GfxSelectSolidBrushBackColor);
        GfxRectangleULButtonXULButtonYLRButtonXLRButtonY ); 
        GfxSetBkColorBackColor);
        GfxSetTextColorTextColor );
        GfxDrawTextLabelULButtonXULButtonYLRButtonXLRButtonY32 4);
        }
    }
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5.00 out of 5)
Loading...Loading...

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;
    }  
1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 4.80 out of 5)
Loading...Loading...

A Basic Messaging Panel

Also see Moving Low Level Graphics (GFX) Objects on your Charts

This first post introduces you to the basic techniques on how to create a matrix of text cells to display messages and system status on your chart. At this stage the cells are not mouse-click sensitive; this and other features will be added later. In this example the number of columns, rows, the font size, and the location of the message panel, can be set from the Param window. In your final application you might want to make these parameters constant and remove the corresponding Param statements. You can combine any number of Panels, here is an example for multiple panels layouts:

image

The following simple panel is produced by the code in this post.

image

The code below will place the above Message panel on your chart. A code description follows below listing.


#include &lt;MessagePanelInclude.afl&gt;

global ColNumber;

CellHeight Param("Cell Height",20,5,200,5);

CellWidth Param("Cell Width",120,5,200,5);

PanelYoffset Param("Cell Row Offset (px)",10,0,Status("pxheight"),5);

PanelXoffset Param("Cell Column Offset (px)",10,0,Status("pxwidth"),5);

FontRatio Param("Font:CellHeight ratio",2,1,20,0.1);

MsgCol_Begin"COLUMNNAME 1"CellHeightCellWidthPanelXoffsetPanelYoffset );

TextCell"Text Message 1"BackgroundColor=1TextColor=2);

TextCell"Text Message 2"23);

TextCell"Text Message 3"84);

TextCell"Text Message 4"75);

TextCell"Text Message 5"46);

TextCell"Text Message 6"37);

TextCell"Text Message 7"28);

MsgCol_End();

MsgCol_Begin" COLUMNNAME 2"CellHeightCellWidthPanelXoffsetPanelYoffset );

TextCell"Text Message 1"92);

TextCell"Text Message 2"83);

TextCell"Text Message 3"74);

TextCell"Text Message 4"65);

TextCell"Text Message 5"56);

TextCell"Text Message 6"47);

TextCell"Text Message 7"38);

MsgCol_End();

Message Panel code above calls the Include file listed at the end of this post. The The MsgCol_Begin() function defines the name and dimensions for the current column:

MsgCol_Begin"COLUMNNAME 1"CellHeightCellWidthPanelXoffsetPanelYoffset );

You can create many columns. The ColName is used in Static variables to allow the code to know to which Column the TextCells belong. The last four arguments are self-explanatory. The Overlay and background modes can be changed inside this function, or you can extract these GFX statements and execute them separately.

The TextCell() function adds one TextCell to the column. It defines the Message, Background color, and TextColor to be used:


TextCell"Text Message 1"colorBlackcolorWhite);

The MsgCol_End() terminates the current column and draws the GFX pgraphics to the chart.

The Include file makes extensive use of static variables that are wrapped in kStaticVar…() to make the static variables unique to the current ChartID. This is needed to call the functions from different panes and windows without interfering with one another. For more on this see Keying Static Variables.


// This is the Include file MessagePanelInclude.afl, copy to your default Include folder

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 MsgCol_BeginColNameCellHeightCellWidthPanelXoffsetPanelYoffset )

{

global FontRatioColNameColNumber;

ColNumber VarGet("ColNumber");

if( IsEmptyColNumber ) ) VarSet("ColNumber",1);

else VarSet("ColNumber", ++ColNumber);

ColName ColName+GetChartID();

GfxSetOverlayMode);

GfxSelectFont"Tahoma"CellHeight/FontRatio800 );

GfxSelectPencolorBlack );

GfxSetBkMode);

kStaticVarSet("PanelItemNum"+ColName0);

VarSetText("ColName",ColName);

return ColNumber;

}

function MsgCol_End( )

{

global CellHeightCellWidthPanelYoffsetPanelXoffsetColNumberPanelItemNum;

ChartIDStr NumToStr(GetChartID(),1.0,False);

ColName VarGetText("ColName");

ULCellX PanelXoffset + (ColNumber-1) * CellWidth;

LRCellX ULCellX CellWidth;

for( Row 1Row &lt;= PanelItemNumRow++ )

{

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));

if( Row==1TextCell StrReplaceTextCellChartIDStr"");

GfxSelectSolidBrushBackColor);

GfxRectangleULCellXULCellYLRCellXLRCellY );

GfxSetBkColorBackColor);

GfxSetTextColorTextColor );

GfxDrawTextTextCellULCellXULCellYLRCellXLRCellY32 4);

}

}

function TextCellTextCellbackColorTextColor)

{

global InitializePanelsPanelVisible;

ColName VarGetText("ColName");

PanelItemNum Nz(kStaticVarGet("PanelItemNum"+ColName))+1;

kStaticVarSet("PanelItemNum"+ColNamePanelItemNum);

kStaticVarSet("CellState"+ColName+PanelItemNumFalse);

Label StrExtract(TextCell,0);

kStaticVarSetText("TextCell"+ColName+PanelItemNumLabel);

kStaticVarSet("TextColor"+ColName+PanelItemNumTextColor);

kStaticVarSet("BackColor"+ColName+PanelItemNumbackColor);

}

function Newpanel()

{

VarSet("ColNumber"0);

}

For those who want to experiment with multi-panel layouts here is the code used to produce the xample shown earlier:

#include

global ColNumber;
_SECTION_BEGIN("MESSAGE PANEL 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);

MsgCol_Begin"COLUMNNAME  1"CellHeightCellWidthPanelXoffsetPanelYoffset );
TextCell"PANEL 1"colorBlackcolorWhite);
TextCell"Text Message 2"23);
TextCell"Text Message 3"84);
TextCell"Text Message 4"75);
TextCell"Text Message 5"46);
TextCell"Text Message 6"37);
TextCell"Text Message 7"28);
TextCell"Text Message 8"92);
TextCell"Text Message 9"83);
TextCell"Text Message 10"74);
TextCell"Text Message 11"65);
TextCell"Text Message 12"56);
TextCell"Text Message 13"47);
TextCell"Text Message 14"38);
MsgCol_End();
_SECTION_END();

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

Newpanel();
MsgCol_Begin"COLUMNNAME  1"CellHeightCellWidthPanelXoffsetPanelYoffset );
TextCell"PANEL 2"1colorWhite);
MsgCol_End();
MsgCol_Begin"COLUMNNAME  1"CellHeightCellWidthPanelXoffsetPanelYoffset );
TextCell"Text Message B"21);
MsgCol_End();
MsgCol_Begin"COLUMNNAME  1"CellHeightCellWidthPanelXoffsetPanelYoffset );
TextCell"Text Message C"4colorWhite);
MsgCol_End();
MsgCol_Begin"COLUMNNAME  1"CellHeightCellWidthPanelXoffsetPanelYoffset );
TextCell"Text Message D"5colorWhite);
MsgCol_End();
MsgCol_Begin"COLUMNNAME  1"CellHeightCellWidthPanelXoffsetPanelYoffset );
TextCell"Text Message E"4colorWhite);
MsgCol_End();
MsgCol_Begin"COLUMNNAME  1"CellHeightCellWidthPanelXoffsetPanelYoffset );
TextCell"Text Message F"5colorWhite);
MsgCol_End();
_SECTION_END();

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

MsgCol_Begin"COLUMNNAME  1"CellHeightCellWidthPanelXoffsetPanelYoffset );
TextCell"PANEL 3"colorBlackcolorWhite);
TextCell"Text Message 22"23);
TextCell"Text Message 23"84);
TextCell"Text Message 24"75);
TextCell"Text Message 25"46);
TextCell"Text Message 26"37);
TextCell"Text Message 27"28);
MsgCol_End();

MsgCol_Begin"COLUMNNAME  2"CellHeightCellWidthPanelXoffsetPanelYoffset );
TextCell"Text Message 28"92);
TextCell"Text Message 29"83);
TextCell"Text Message 30"74);
TextCell"Text Message 31"65);
TextCell"Text Message 32"56);
TextCell"Text Message 33"47);
TextCell"Text Message 34"38);
MsgCol_End();
_SECTION_END();
1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 4.60 out of 5)
Loading...Loading...

Introduction to Real-Time Control-Panels

IMPORTANT NOTES:

1) Posts in this category are published as code is developed and you should use the most recent version posted. This code is shared without extensive field testing and may contain errors or function different than you expect. Always verify that the code does what you want it to do before using it in a real application. This publishing method is used to give you a chance to use it at the current stage of development, else you would have to wait until all functions are completed – and that time might never come :-)

2) All code on the UKB takes advantage of the latest AFL functions. If you encounter problems, and before posting questions, please verify that you are running the latest AmiBroker beta software. New releases are usually announced in the AmiBroker Development Log. The code you find here is modified too frequently to add/maintain version() statements to/in all programs published.

3) The code covered in this series of posts make extensive use of GFX functions please read the posts in the GFX Programming category.

==========

In Real-Time trading it is handy to use "Control Panels". Control Panels are custom button layouts that can be used to change system settings, and give Real-Time visual feedback on the state of the system. Clicking any of the buttons lets you execute specific code with a single mouse click. While the AmiBroker Parameter window is very useful for use with Indicators it lacks important features needed in Real-Time. Posts in this category will cover the development of a custom menu system for use in Real-Time applications. It will offer: relocatable and collapsible menus, dynamic coloring, multi-state toggles, dynamic labels, sizable buttons and fonts, status indicators, unlimited menu columns, etc. Of course you can conceive and add your own special functions. The main code can be placed in an Include file and menus and buttons can be added to your code with statements somewhat similar to the AmiBroker Param(). The code below generates the first column for the test menu shown in the image below the code.

Menu_Begin( &quot;MENUS&quot;, ButtonHeightButtonWidthMenuXoffsetMenuYoffset );       
MenuMinimized MenuHeaderBLUWHT);        
ExpandAll MenuTrigger( &quot;EXPAND ALL&quot;, ColorTriggerColorTriggerOnBLK );        
SaveMenus MenuTrigger( &quot;SAVE&quot;, ColorTriggerColorTriggerOnBLK );        
LoadMenus MenuTrigger( &quot;LOAD&quot;, ColorTriggerColorTriggerOnBLK );        
MovingMenu ButtonRotate( &quot;MOVING,DRAG MENU&quot;, &quot;4,1&quot;, &quot;7,2&quot;);       
Menu_End();</p>

A Rotate button changes state with each click. It can function as a ParamToggle() but can also be used to rotate through multiple states like Buy, Sell, Short, and Cover just by clicking the button multiple times. Each click can change the Button label and color. Here is a typical menu layout:

image

Menus can be collapsed by Double-Clicking on any of the Blue headers. This means you can uncover hidden chart sections instantly, without having to move the menu block. In its collapsed state the menu looks like this:

image

You can also collapse individual menus with a single click on the blue header, as shown below.

image

Posts in this series will progress through various prerequisites and building blocks to help you design your own menu layout.

1 Star2 Stars3 Stars4 Stars5 Stars (6 votes, average: 5.00 out of 5)
Loading...Loading...