Loading…

Colour Chart By Position State

At a glance, a busy terminal with a dozen charts open does not tell you which ones actually have exposure right now. This script answers that question for the chart it is run on: it looks at the open market orders on that chart's symbol only, and classifies the state as long, short, hedged or flat. Long means only buys are open, short means only sells, hedged means both, and flat means neither. The naive version of a script like this changes the chart background and stops thinking about it. That is fine on the first run. It becomes a problem the moment the trader wants their normal chart back, because a tint applied on top of an unknown starting colour cannot be undone - there is nothing to undo it TO. Once the original colour is gone, "remove the tint" has no correct answer, only guesses. This script avoids that trap by treating the original colour as something to capture once and never again. The first time it is ever asked to tint a given chart, it reads that chart's current background colour and stores it in a terminal global variable before changing anything. Every later run finds that value already saved and leaves it alone, so a chart that has been tinted five times in a row still has its true, original colour on record, not whatever colour was showing the last time the script happened to run. Setting InpRestoreOriginal to true reads that saved value back and puts it on the chart, with no classification involved at all - it is a dedicated undo, not a side effect of running the script again. The label mode is the safer of the two display options and is the default: it adds one small object in the corner of the chart, which is easy to see and easy to ignore, and it never changes anything the trader configured themselves. The background mode is more visible from across a wall of monitors, but it edits a setting the trader set on purpose, which is a bigger thing to do without being asked - hence the corner label being the default, not the tint. Notes and limitations - Per-symbol, per-chart, not account-wide. Only orders or positions whose symbol matches the chart's own symbol are counted (OrderSymbol() on MQL4, POSITION_SYMBOL on MQL5). A hedged EURUSD book and a clean long GBPUSD book are two different charts with two different readings; this script never summarises the whole account on one chart. - On a netting account (MQL5 only), HEDGED can never be reported for the reason explained in MQL4 VERSUS MQL5 DIFFERENCES - there is structurally only ever one position per symbol, so the state is always flat, long or short there. A hedging-mode MQL5 account, and MQL4 always, can show all four states. - The saved original colour is keyed to this chart's own ID (ChartID()), not to the symbol. Two charts showing the same symbol are still two separate windows and can have been given two different colours by the trader, so each chart gets its own independent saved value; restoring one chart never touches another. Running the script on several charts creates one small global variable per chart, all named with a "cbp_bg_orig_" prefix. - A chart's ID is assigned by the terminal for the life of that chart window. If a chart is closed and later reopened as a new window - a fresh profile, for instance - it receives a new ID, and any global variable saved under its old ID is simply left behind, unused. It is not deleted automatically; it does no harm sitting there, but it will not restore a colour to the new window on its own. - Global variables are stored by the terminal and persist across restarts, so the saved original colour survives closing and reopening the platform, as long as the chart keeps the same ID. - In label mode, flat is shown as "FLAT" in a neutral colour rather than being left blank, so a stale-looking chart and a genuinely flat one are never confused with each other. - In background mode, a flat classification restores the saved original colour rather than applying a fourth tint - there is no exposure to highlight, so the chart reverts to how it normally looks. - The label is a chart object named "cbp_state" and is left in place after the script finishes running, by design - the whole point is a reading that stays visible, not one that vanishes the instant the script ends. Running the script again updates it rather than adding a duplicate. To remove it by hand, delete the object named "cbp_state" from the chart's object list. - This script only reads orders and chart settings; it never places, closes or modifies a trade.

Script · MT5 · v1.0 · Free

About Colour Chart By Position State

Colour Chart By Position State is a script for MetaTrader 5. It can alert you with a pop-up, sound, email or push message. It has 2 settings, such as Mode and Restore original. MQL source code included.

Files in the download: ColourByPosition.ex5, ColourByPosition.mq5, ColourByPosition.txt.

Settings

SettingDefaultWhat it does
ModeMODE_LABELDisplay method
Restore originalfalseRestore original background only (no classification), then exit

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Hedging · Alerts · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria