Introducing Order Markers

In the previous post I developed a method to link prices between the TDash window and the Main Chart window. In this post I show how I used this technique to develop “Order Markers” that can be used to modify orders, display order status, and cancel orders.

In the final TDash Order Markers will be created automatically whenever an order is found Pending. The initial orders will be placed with another tool (the QBar) that will be covered in a subsequent post.

To test the Order markers you need to create two side-by-side windows in AmiBroker. If you use multiple monitors you can place the Main Chart at the right side of the left monitor, and TDash on left side of the right monitor. This will work fine and gives you a lot of space to work with. However, depending on the monitor sizes and their horizontal positions, the Markers may not appear perfectly at the same height from your desk, however the prices shown and plotted will be accurate.

To install the programs, first open and copy this TDashInclude.afl include file (not listed below due to its size) to the AmiBroker default Include folder. The TDashInclude.afl file is a development version and contains a lot of code that you don’t need at this time – please ignore the unused functions. The include will be cleaned up when the functions all work as they should. Next Apply MainMarkers.afl (listed below) to the main chart at the left, and Apply TDashMarkers.afl (listed below) to the TDash window at the right.

The demo code presented here creates three Order Markers, each can be moved by dragging it to a new price level. At start-up the Order markers will be parked at the top of the TDash window, just click on them to activate them. Remember that in the final TDash inactive Order Markers will not show, only Order Markers for Pending orders will show.

The price line and value displayed on the main chart will track the Order Marker, and the Order Markers will track a changing Y-axis on the Main chart. To allow setting an exact price Increment (+) and Decrement (-) controls are provided on the Marker. To cancel the order click the Cancel control (X). In this test cancel will only gray-out the marker and park it at the top of the TDash window, in the final program a canceled Marker will disappear after the cancellation has been confirmed.

It should look and work as shown in this video below.

// TDashMainMarkers.afl

PersistentPath StaticVarGetText"~PersistentPath" ); // Global
if ( PersistentPath == "" ) 
{
    PersistentPath "PersistentVariables\\";
    fmkdirPersistentPath );
    StaticVarSetText"~PersistentPath"PersistentPath );
}

function PersistentVarRemoveVarName )
{
    global PersistentPath;
    Fn PersistentPath VarName ".pva";
    fh fdeleteFn ) ;
    return fh;
}

function PersistentVarGetVarName )
{
    global PersistentPath;
    fh fopenPersistentPath VarName ".pva""r" );
    if ( fh )
    {
        String fgetsfh );
        fclosefh );
        Number StrToNum( String );
    }
    else Number Null;
    return Number;
}

function PersistentVarSetVarNameNumber )
{
    global PersistentPath;
    String NumToStrNumber );
    fh fopenPersistentPath VarName ".pva""w" );
    if ( fh )
    {
        fputsStringfh );
        fclosefh );
    }
    return fh;
}

function MAinLinkWitTDashMArkername )
{
    local xMY;
    // On first run try to get persistent price
    if ( IsNullStaticVarGet"~MarkerInit_" MArkerName ) ) )
    {
        Price PersistentVarGet"MainChartPrice_" MarkerName );
        if ( Price )
        {
            Miny Status"axisminy" );
            Maxy Status"axismaxy" );
            PxChartRange Status"pxchartheight" );
            Pricerange MaxY MinY;
            TDashYPixels = ( MaxY Price ) * PxChartRange Pricerange 5;
            StaticVarSet"~TDashYPixels_" MarkerNameTDashYPixels );
            StaticVarSet"~MainChartPrice_" MarkerNamePrice );
            StaticVarSet"~Exists_" MarkerNameTrue );
        }
        StaticVarSet"~MarkerInit_" MArkerNameTrue );
    }

    MArkerEnabled NzStaticVarGet"~Exists_" MarkerName ) );
    if ( MArkerEnabled )
    {
        pxWidth Status"pxWidth" );
        Miny Status"axisminy" );
        Maxy Status"axismaxy" );
        pxchartright Status"pxchartright" );
        pxchartbottom Status"pxchartbottom" );
        pxcharttop Status"pxcharttop" );
        PxChartRange Status"pxchartheight" );
        pxheight Status"pxheight" );
        Pricerange MaxY MinY;

        // TDash releases get TDash y-pixel value and convert to price
        if ( NzStaticVarGet"~LeftButtonRelease" ) ) )         
        {
            TDashYPixels NzStaticVarGet"~TDashYPixels_" MarkerName ) );        // Y pixels from TDash window
            Price Maxy - ( Pricerange PxChartRange * ( TDashYPixels ) );
            StaticVarSet"~LeftButtonRelease"False );
            StaticVarSet"~MainChartPrice_" MarkerNamePrice );
        }
        else
            if ( NzStaticVarGet"~NowDragging_" MarkerName ) ) == )     // TDash marker tracks main window
            {
                Price NzStaticVarGet"~MainChartPrice_" MarkerName ) );
                TDashYPixels = ( MaxY Price ) * PxChartRange Pricerange 5;
                StaticVarSet"~TDashYPixels_" MarkerNameTDashYPixels );         // Y pixels calculated from price in main chart
            }
            else
                if ( NzStaticVarGet"~NowDragging_" MarkerName ) ) )             // Main window tracks TDash window
                {
                    TDashYPixels NzStaticVarGet"~TDashYPixels_" MarkerName ) );
                    Price Maxy - ( Pricerange PxChartRange * ( TDashYPixels ) );
                    StaticVarSet"~MainChartPrice_" MarkerNamePrice );  // Price for main chart calculated from TDash y pixels
                }

        Price NzStaticVarGet"~MainChartPrice_" MarkerName ) );

        MArkerColor  NzStaticVarGet"~MarkerColor_" MarkerName ) );
        MArkerTextColor NzStaticVarGet"~MarkerTextColor_" MarkerName ) );
        MArkerPenColor NzStaticVarGet"~MarkerPenColor_" MarkerName ) );

        GfxSetBkMode);
        GfxSelectPenMArkerColor1);
        GfxMoveTo0TDashYPixels );
        GfxLineTopxwidthTDashYPixels );

        GfxSetBkMode);
        GfxSetBkColorMArkerColor );
        GfxSetTextColorMArkerTextColor );
        GfxSelectFont"Lucida Console"FontSize 10FontWeight 700 );
        GfxDrawTextNumToStrPrice1.2False ), pxchartrightTDashYPixels FontSizepxwidthTDashYPixels Fontsize37 );
    }
}

function DrawQBarPriceLine()
{
    if( NzStaticVarGet"~QBarVisible" ) ) )
    {
        pxWidth Status"pxWidth" );
        Miny Status"axisminy" );
        Maxy Status"axismaxy" );
        pxchartright Status"pxchartright" );
        pxchartbottom Status"pxchartbottom" );
        pxcharttop Status"pxcharttop" );
        PxChartRange Status"pxchartheight" );
        pxheight Status"pxheight" );
        Pricerange MaxY MinY;

    QBarColor  NzStaticVarGet"~QBarColor" ) );
    QBarTextColor NzStaticVarGet"~QBarTextColor" ) );
    QBarPenColor NzStaticVarGet"~QBarPenColor" ) );

    QBarColor NzStaticVarGet"~ActionColor" ) );
    QBarTextColor NzStaticVarGet"~ActionTextColor" ) );

    QBarYPixel NzStaticVarGet"~QBarYPixel" ) );        // Y pixels from TDash window
    Price Maxy - ( Pricerange PxChartRange * ( QBarYPixel ) );
    GfxSetBkMode);
    GfxSelectPenQBarColor1);
    GfxMoveTo0QBarYPixel );
    GfxLineTopxwidthQBarYPixel );

    GfxSetBkMode);
    GfxSetBkColorQBarColor );
    GfxSetTextColorQBarTextColor );
    GfxSelectFont"Lucida Console"FontSize 10FontWeight 700 );
    GfxDrawTextNumToStrPrice1.2False ), pxchartrightQBarYPixel FontSizepxwidthQBarYPixel Fontsize37 );
    }
}


_SECTION_BEGIN"Global Parameters" );
if ( ParamTrigger"Clear StaticVar (TEST)""CLEAR" ) ) // to simulate start up
{
    StaticVarRemove"*" );
}

if ( ParamTrigger"Clear Persistent Var""CLEAR" ) ) // to simulate start up
{
    PersistentVarRemove"MainChartPrice_" "TARGET" );
    PersistentVarRemove"MainChartPrice_" "MAIN" );
    PersistentVarRemove"MainChartPrice_" "STPLOSS" );
}
_SECTION_END();

GfxSetOverlayMode);
MAinLinkWitTDash"BUY" );
MAinLinkWitTDash"MAIN" );
MAinLinkWitTDash"SELL" );
DrawQBarPriceLine();
PlotC""colorBlackstyleBar );
RequestTimedRefresh0.1 );
// TDashMarkers
#pragma nocache
#include <TDashInclude.afl>

_SECTION_BEGIN"TDASH MARKERS" );
setBackGroundColorParamColor"TDash background"colorLightBlue ) );

// Set default font size
FontSize NzStaticVarGet"~FontSize" ) );
FontName "ARIAL";
Fontweight 700;
GfxSelectFontFontNameFontSizeFontWeight );

// Columns. Most parameters are for experimentation and can be hard-coded
NumberCols Param"Number Button Columns"8120);
ButtonWidth Status"pxwidth" ) / NumberCols;
MaxButtonWidth Param"Max Button Width"1501500);
ButtonWidth MinButtonWidthMaxButtonWidth );
MaxMarkerWidth Param"Max Button Width"1501500);
MarkerWidth MinButtonWidthMaxMarkerWidth );

// Rows
NumberRows Param"Number Button Rows"20150);
ButtonHeight Status"pxheight" ) / NumberRows;
MaxButtonHeight Param"Max Button Height"501100);
ButtonHeight MinButtonHeightMaxButtonHeight );
MinButtonHeight Param"Min Button Height"501100);
ButtonHeight MaxButtonHeightMinButtonHeight );

MarkerHeight ButtonHeight;

TipsOn ParamToggle"Help Tips""HIDE|SHOW");
ShowTips ParamToggle"Help Tips""HIDE|SHOW");
ClickSound ParamToggle"Click Sound""OFF|ON");

SetChartOptions0chartHideQuoteMarker );
GfxSetOverlayMode2  );

// mouse
MX GetCursorXPosition);
MY GetCursorYPosition);
LeftClick NzStaticVarGet"~LeftClick" ) ); //Left Click SV set at end of code
LeftDown GetCursorMouseButtons() == 1;

OnTDash     = !IsNullMX ) AND !IsNullMY );
LeftClick LeftClick AND OnTDash;    // Only accept mouse clicks when cursor is inside TDash window
LeftDown = ( LeftDown OR LeftClick ) AND OnTDash;

PrevLeftButtonState NzStaticVarGet"~LeftButtonState" ) );
LeftButtonRelease LeftDown PrevLeftButtonState AND OnTDash;
StaticVarSet"~LeftButtonState"LeftDown ); // Release is also detected outside TDash window

// testing markers
MArkerTip "Drag Price Marker to desired price Level. Click 'X' to cancel, '+' to increment, and '-' to decrement";
ServiceMarkers"TARGET"colorBrightGreencolorBlackcolorBlackMArkerTip );
ServiceMarkers"MAIN"colorBluecolorWhitecolorBlackMArkerTip   );
ServiceMarkers"STPLOSS"colorRedcolorBlackcolorBlackMArkerTip   );

// testing
AddRowTogfxTitle"                TDashAction: "+VarGetText"TDashAction"), colorBlackFontSize=10 );

// The following lines are always located at the end of the code 
ShowTipBrushColor colorYellowOutlineColor colorBlackTextColor colorBlackTipWidth 150TipHeight 1008TipsOn );
LeftClick GetCursorMouseButtons() == 9;
StaticVarSet"~LeftClick"LeftClick );
RequestTimedRefresh0.1 );
_SECTION_END();

Comments are closed.