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

Comments are closed.