GFX drawing move with scrolling /zooming of chart

Hello, I have a code which draws 2 horizontal lines on a chart.

I have this problem with scrolling or zoom of chart, the drawings also move. How do I STICK the drawing on the chart so that they are fixed ???

// Define unique ID for the FIBS toggle button
idFibsButton = 1;  

// Prefix for static variable naming to avoid conflicts
Prefix = "FibsTool_";

// Function to create the FIBS toggle button
function CreateGUI() 
{ 
    GuiToggle("FIBS OFF", idFibsButton, 10, 60, 100, 30, notifyClicked);
    
    // Change the button text when toggled ON
    if (GuiGetCheck(idFibsButton)) 
    {
        GuiSetText("FIBS ON", idFibsButton);
    } 
    else 
    {
        GuiSetText("FIBS OFF", idFibsButton);
    }
} 

// Function to handle button toggle events
function HandleEvents() 
{ 
    for (n = 0; id = GuiGetEvent(n, 0); n++) 
    { 
        code = GuiGetEvent(n, 1); 
        
        switch (id) 
        { 
            case idFibsButton: 
                _TRACE("FIBS button toggled");

                if (GuiGetCheck(idFibsButton)) 
                {
                    StaticVarSet(Prefix + "DrawLines", 1);  // Enable drawing mode
                    StaticVarSet(Prefix + "StartBar", -1);   // Reset start bar
                    StaticVarSet(Prefix + "isDraggingLine1", 0);
                    StaticVarSet(Prefix + "isDraggingLine2", 0);
                } 
                else 
                {
                    StaticVarSet(Prefix + "DrawLines", 0);  // Disable drawing mode
                    StaticVarRemove(Prefix + "*");           // Clear all related static variables
                }

                RequestMouseMoveRefresh();  // Refresh chart immediately
                break; 

            default: 
                break; 
        } 
    } 
}

// Function to handle mouse click on the chart
function HandleMouseClick() 
{
    b = GetCursorMouseButtons();
    DrawLines = Nz(StaticVarGet(Prefix + "DrawLines"), 0);
    
    if (b & 8 && DrawLines == 1)  // Left mouse click detected and drawing is ON
    {
        x = GetCursorXPosition(0);  // X position as Date/Time
        y = GetCursorYPosition(0);  // Y position as price
        
        _TRACE("Mouse Clicked at BarIndex: " + x + ", Price: " + y);
        
        StaticVarSet(Prefix + "StartBar", Lookup(BarIndex(), x));
        StaticVarSet(Prefix + "Line1Y", y * 1.01);  // 1% above clicked price
        StaticVarSet(Prefix + "Line2Y", y);          // Lower line exactly at clicked price
        
        // Auto-disable FIBS mode after setting the lines
        GuiSetCheck(idFibsButton, 0);  // Turn off the "FIBS" button
        StaticVarSet(Prefix + "DrawLines", 0);  // Exit drawing mode
        RequestMouseMoveRefresh();  // Refresh to complete the operation
    }
}

// Function to draw horizontal lines and handle dragging
function DrawAndHandleLines() 
{
    DrawLines = Nz(StaticVarGet(Prefix + "DrawLines"), 0);
    StartBar = Nz(StaticVarGet(Prefix + "StartBar"), -1);
    Line1Y = Nz(StaticVarGet(Prefix + "Line1Y"), 0);
    Line2Y = Nz(StaticVarGet(Prefix + "Line2Y"), 0);

    if (StartBar >= 0)  // Draw only if start bar is set
    {
        endBar = min(StartBar + 50, BarCount - 1);  // Ensure we don't exceed chart limits
        
        GfxSetCoordsMode(1);  // Bar index and Price mode
        GfxSetBkMode(1);       // Transparent background
        
        // Draw first horizontal line (Red)
        GfxSelectPen(colorRed, 2);  
        GfxMoveTo(StartBar, Line1Y);
        GfxLineTo(endBar, Line1Y);
        
        // Draw second horizontal line (Green)
        GfxSelectPen(colorGreen, 2);  
        GfxMoveTo(StartBar, Line2Y);
        GfxLineTo(endBar, Line2Y);

        // Mouse position and dragging state
        mb = GetCursorMouseButtons();
        my = GetCursorYPosition(0);  // Mouse Y position as price
        hoverRadius = 0.5 * SelectedValue(ATR(14));  // Sensitivity for hovering over handles

        // Dragging only allowed when FIBS is OFF (for repositioning)
        if (!GuiGetCheck(idFibsButton)) 
        {
            // Handle dragging for the upper (red) line
            if (abs(my - Line1Y) < hoverRadius) 
            {
                GfxSelectPen(colorYellow, 1);
                GfxSelectSolidBrush(colorYellow);
                GfxCircle(endBar, Line1Y, 5);  // Draw hover circle
                
                if (mb & 1) 
                {
                    StaticVarSet(Prefix + "Line1Y", my);  // Move upper line
                    RequestMouseMoveRefresh();
                }
            }

            // Handle dragging for the lower (green) line
            if (abs(my - Line2Y) < hoverRadius) 
            {
                GfxSelectPen(colorYellow, 1);
                GfxSelectSolidBrush(colorYellow);
                GfxCircle(endBar, Line2Y, 5);  // Draw hover circle
                
                if (mb & 1) 
                {
                    StaticVarSet(Prefix + "Line2Y", my);  // Move lower line
                    RequestMouseMoveRefresh();
                }
            }
        }
    }
}

// Initialize GUI, handle events, process mouse click, and draw lines with handles
CreateGUI(); 
HandleEvents();
HandleMouseClick();
DrawAndHandleLines();

I am using GetcursorXPosition(0), (getting x in Date time) and then getting its Barindex() with using

StaticVarSet(Prefix + "StartBar", Lookup(BarIndex(), x));

SO should this not ANCHOR my drawing at that barindex() ??

I dont understand please guide how to anchor my drawings to the chart so that they do not move when I scroll chart ???

Hi
you are using Lookup() function incorrect in a wrong place
Just change 2 lines in your code, and job done :grinning:.

Line 70

   //     StaticVarSet(Prefix + "StartBar", Lookup(BarIndex(), x));
 // line 70 replaced with 
        StaticVarSet(Prefix + "StartBar",  x);

Line 85

  //  StartBar = Nz(StaticVarGet(Prefix + "StartBar"), -1);
// line 85 replaced with

    StartBarX = Nz(StaticVarGet(Prefix + "StartBar"), 0);
    StartBar = Lookup( BarIndex(),StartBarX);
1 Like

This is working amazingly well !

Thanks PanoS for the help !

1 Like