GetPaneHeights

Download script
Author: Copyright (c) 2009, kartz
GetPaneHeights

This script is a specialized utility designed to help MetaTrader charts "understand" their own visual layout. While most trading tools look only at price data (like highs, lows, and volumes), this tool looks at the actual physical screen space of your trading window to measure how much room each section occupies in pixels.

How it Works

The script performs a systematic scan of your trading screen to determine the exact height of different "panes." In MetaTrader, panes are the distinct horizontal sections of a chart—for example, the main price chart is usually at the top (Pane 0), followed by various indicators (like RSI or MACD) in sections below it.

Instead of guessing how big these sections are based on percentage points, the script follows this logic:

  1. Identifying the Window: It first locates the specific window handle for your current symbol and timeframe to ensure it is measuring the correct area.
  2. Scanning for Borders: It looks at the very edge of the window to find the visual boundaries (the "frames") that separate one indicator from another or from the main chart.
  3. Measuring Pixels: Once it identifies these borders, it calculates the vertical distance in pixels between them.
  4. Mapping the Layout: It compiles a list of these heights, identifying exactly how tall each section is from top to bottom.

Why This Is Useful

For a standard user, this logic is essential for "Visual Precision." Without knowing the exact pixel height of a pane, it is very difficult to place visual objects—like text labels, alerts, or arrows—in a consistent spot.

By providing the exact pixel heights, this script allows other trading tools to:

  • Place an arrow exactly 10 pixels above a price candle, regardless of how much you zoom in or out.
  • Ensure that text labels don't overlap with indicator lines.
  • Maintain a consistent visual layout even if the user resizes their window or changes their screen resolution.

Note for Users: For this script to work accurately, the trading window must be fully visible. If another window (like a browser or a folder) is overlapping the edge of your chart, the script will not be able to "see" the boundaries and may return an error.

Miscellaneous
It issuies visual alerts to the screen
6 Views
1 Downloads
0 Favorites
GetPaneHeights
/*
    GetPaneHeights: get heights in pixel of all panes in a window

    FOR WHAT:

        With the use of the built-in functions "WindowPriceMin/Max()"
        together, you can calculate appropriate spacing/margin to
        text/arrow objects (OBJ_TEXT/OBJ_ARROW), whatever size the
        window is.  See the EXAMPLE section below.

    SYNOPSIS:

        #import "GetPaneHeights.ex4"
            int GetPaneHeights(int &height[]);
        #import

        height[i] = the height in pixel of pane "i"
            i = 0: main chart
            i = 1: indicator #1
            i = 2: indicator #2
            ... (in top-to-bottom order)

        The size of "height[]" must be large enough to contain all panes.
        This function returns the number of panes.
        If it fails, the return value is zero.

    NOTE:

        This function scans the device context (DC) of a window,
        so if (the leftmost piece of) the window is covered by another
        window, then this function will fail.

    EXAMPLE:

        #import "GetPaneHeights.ex4"
            int GetPaneHeights(int &height[]);
        #import

        int         i, panes, height[10];
        double      spacing;
        string      sign;

        panes = GetPaneHeights(height);
        for (i = 0; i < panes; i++)
            Print("height[", i, "] = ", height[i], " pixels");

        // Analyzing the chart... Hmm... it's time to sell.

        if (panes == 0) {
            Alert("WARNING: cannot get the pane height!");
            height[0] = 500;
        }
        sign = "sell sign";
        spacing = 5 * (WindowPriceMax(0) - WindowPriceMin(0)) / height[0];
        ObjectCreate(sign, OBJ_ARROW, 0, Time[0], High[0] + spacing);
        ObjectSet(sign, OBJPROP_ARROWCODE, ...);
        ObjectSet(sign, OBJPROP_COLOR, ...);

        // The sell sign will be located 5 pixels above the highest price
        // of the latest bar, whatever size the window is.

    Version 1.01, Copyright (c) 2009, kartz

    (Change Log)
        1.00 -> 1.01: fix a bug of recognizing the date-and-time row as a pane
                      on a machine which doesn't use font anti-aliasing.
*/

#property copyright     "Copyright (c) 2009, kartz"
#property library

#import "user32.dll"
    int     GetDC(int hwnd);                            // HDC
    int     ReleaseDC(int hwnd, int hdc);
    int     GetClientRect(int hwnd, int &rect[4]);      // BOOL
#import "gdi32.dll"
    int     GetPixel(int hdc, int x, int y);            // COLORREF
#import

#define CLR_INVALID     0xFFFFFFFF      // Windows' constant
#define TEST_XY_COORD   10

int GetPaneHeights(int &height[])
{

    int     i, x, y, h, c, cc, frame, hwnd, hdc, rect[4];

    hwnd = WindowHandle(Symbol(), Period());
    if (hwnd == 0)
        return(0);
    if (!GetClientRect(hwnd, rect))
        return(0);
    h = rect[3];
    hdc = GetDC(hwnd);
    if (hdc == 0)
        return(0);

    i = 0;
    c = GetPixel(hdc, 0, TEST_XY_COORD);
    if (c != CLR_INVALID) {
        // scan horizontally.
        for (x = 1; x <= TEST_XY_COORD; x++) {
            frame = GetPixel(hdc, x, TEST_XY_COORD);
            if ((frame == CLR_INVALID) || (frame != c))
                break;
        }
        // "x" must be 2, but scanned just in case.
        if (frame != CLR_INVALID) {
            // scan vertically along the frame.
            for (cc = CLR_INVALID, y = 0; y < h; y++, cc = c) {
                c = GetPixel(hdc, x, y);
                if (c == CLR_INVALID) {
                    i = 0;
                    break;
                }
                if (c == frame) {
                    if (cc != frame)
                        height[i] = y;
                } else if (cc == frame) {
                    height[i] = y - height[i] - 1;
                    i++;
                    if (i == WindowsTotal())
                        break;
                }
            }
        }
    }
    ReleaseDC(hwnd, hdc);
    return(i);
}

Comments