March 8, 2008
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:
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", colorRed, colorBlack); Column_End( ); Column_Begin( "2" ); Reset = TriggerCell( "START SESSION", colorBrightGreen, colorRed, colorBlack); Column_End( ); Column_Begin( "3" ); CancelAll = TriggerCell( "CANCEL ALL", colorBrightGreen, colorRed, colorBlack); Column_End( ); Column_Begin( "4" ); CloseAll = TriggerCell( "CLOSE ALL", colorBrightGreen, colorRed, colorBlack); Column_End( ); Column_Begin( "5"); EndSession = TriggerCell( "END SESSION", colorBrightGreen, colorRed, colorBlack); 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 kStaticVarSet( SName, SValue ) { ChartID = GetChartID(); InIndicator = Status("Action") == 1; if( InIndicator ) StaticVarSet(Sname+ChartID, Svalue); } function kStaticVarGet( SName ) { ChartID = GetChartID(); Var = StaticVarGet(Sname+ChartID); return Var; } procedure kStaticVarSetText( SName, SValue ) { ChartID = GetChartID(); InIndicator = Status("Action") == 1; if( InIndicator ) StaticVarSetText(Sname+ChartID, Svalue); } function kStaticVarGetText( SName ) { ChartID = GetChartID(); return StaticVarGetText(Sname+ChartID); } function Column_Begin( ColName ) { global FontRatio, ColName, ColNumber, CellHeight, CellWidth, PanelXoffset, PanelYoffset; ColNumber = VarGet("ColNumber"); if( IsEmpty( ColNumber ) ) { VarSet("ColNumber",1); StaticVarSet("ClickCoordinates",0); } else VarSet("ColNumber", ++ColNumber); ColName = ColName+GetChartID(); GfxSetOverlayMode( 0 ); GfxSelectFont( "Tahoma", CellHeight/FontRatio, 800 ); GfxSelectPen( colorBlack ); GfxSetBkMode( 1 ); kStaticVarSet("RowNumber"+ColName, 0); VarSetText("ColName",ColName); return ColNumber; } function Column_End( ) { global CellHeight, CellWidth, PanelYoffset, PanelXoffset, ColNumber, RowNumber; ChartIDStr = NumToStr(GetChartID(),1.0,False); ColName = VarGetText("ColName"); ULCellX = PanelXoffset + (ColNumber-1) * CellWidth; LRCellX = ULCellX + CellWidth; for( Row = 1; Row <= RowNumber; Row++ ) { 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)); GfxSelectSolidBrush( BackColor); GfxRectangle( ULCellX, ULCellY, LRCellX, LRCellY ); GfxSetBkColor( BackColor); GfxSetTextColor( TextColor ); GfxDrawText( TextCell, ULCellX, ULCellY, LRCellX, LRCellY, 32 | 1 | 4); } } function TextCell( TextCell, backColor, TextColor) { global ColNumber, RowNumber;; ColName = VarGetText("ColName"); RowNumber = Nz(kStaticVarGet("RowNumber"+ColName))+1; kStaticVarSet("RowNumber"+ColName, RowNumber); kStaticVarSetText("TextCell"+ColName+RowNumber, TextCell); kStaticVarSet("TextColor"+ColName+RowNumber, TextColor); kStaticVarSet("BackColor"+ColName+RowNumber, backColor); } function NewColumn() { VarSet("ColNumber", 0); } function CheckMouseClick( ColNumber, RowNumber ) { global PanelYoffset, PanelXoffset, CellHeight, CellWidth; 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 == 0 AND LButtonDown ) { MousePx = GetCursorXPosition( 1 ); MousePy = GetCursorYPosition( 1 ); if( MousePx > ULCellX AND MousePx < LRCellX AND MousePy > ULCellY AND MousePy < LRCellY ) { StaticVarSet("ClickCoordinates",ColNumber*100+RowNumber); Click = 1; } } } return Click; } function TriggerCell( Label, backColor1, BackColor2, TextColor) { global ColNumber, RowNumber;; ColName = VarGetText("ColName"); RowNumber = Nz(kStaticVarGet("RowNumber"+ColName))+1; kStaticVarSet("RowNumber"+ColName, RowNumber); Trigger = CheckMouseClick( ColNumber, RowNumber ); if( Trigger ) BackColor = backColor2; else BackColor = backColor1; kStaticVarSetText("TextCell"+ColName+RowNumber, Label); kStaticVarSet("TextColor"+ColName+RowNumber, TextColor); kStaticVarSet("BackColor"+ColName+RowNumber, backColor); return Trigger; }
Filed by Herman at 11:50 am under Real-Time Control-Panels
Comments Off on Command Buttons (Trigger type)