Slider Widget
Neither platform has a native slider control, and building one from chart objects has a problem beyond drawing a track and a handle: the mouse can drop the handle at any pixel, but a lot-size or risk-percent input usually only makes sense at discrete steps. A slider that reports whatever raw pixel the drag ended on either shows a value the rest of the program then has to round anyway, or looks broken when the handle visually sits between two labelled ticks. This widget converts the handle's pixel position to the nearest step-aligned value and then snaps the handle back onto the exact pixel that value corresponds to, so what the user sees always matches a value the program would actually accept - there is never a visible gap between where the handle rests and what GetValue() returns. Like this catalogue's PanelFramework, there is no way on either platform to intercept a drag mid-motion: the terminal moves a selectable object on its own and the widget only learns about it afterwards, through CHARTEVENT_OBJECT_DRAG. HandleDrag() reads the handle's position once per event and does the pixel-to-value-to-pixel conversion in a single pass. Create() refuses to build a slider that cannot work: a track narrower than the handle has no travel distance at all, a minimum that is not less than the maximum has no direction to slide in, and a step of zero or less has no defined grid to snap to. Each of those is checked explicitly rather than producing a slider that compiles but cannot be dragged sensibly. Notes and limitations - The demo cannot generate a real mouse drag, since a script's OnStart runs once with no live event loop. It moves the handle's XDISTANCE directly, the way the terminal does internally during a drag, then calls HandleDrag() with the handle's own name - the same call a host program's OnChartEvent would make on a genuine drag. - Horizontal only. A vertical slider would need the Y axis snapped instead of X and was left out to keep this widget to one clear job. - The value label shows GetValue() formatted to the number of decimals the step implies (0.5 shows one decimal, 5 shows none); it does not support a custom number format or a unit suffix - retext m_labelName's object directly if a program needs something more specific than the default. - SetEnabled(false) stops the handle from being dragged by disabling its OBJPROP_SELECTABLE flag; it does not stop a program from calling SetValue in code, which is deliberate - a disabled control can still be driven programmatically.
Script · MT4 · v1.0 · Free
About Slider Widget
Slider Widget is a script for MetaTrader 4. It has 1 setting, such as Prefix. MQL source code included.
Files in the download: WidgetSlider.mqh, WidgetSlider.txt, WidgetSliderDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Prefix | WidgetSliderDemo_ | Object-name prefix for this demo's slider |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Risk & Lot Size · MT4 Script · MQL4 Source Code