Have you ever needed to copy and paste some graphical objects between open charts? I think you have. Surprisingly, MetaTrader does not provide “Copy & Paste” feature for objects. The only thing that allows you to achieve more or less similar effect is templates (tpl-files). But the template stores the full chart state, including indicators, settings and a lot of auxiliary stuff, that may be irrelevant and unnecessary for required replication procedure.
That’s why I developed this indicator ChartObjectsCopyPaste.mq5. It copies selected objects to the clipboard, from where they can be pasted onto other chart(s). No strings attached.
The indicator is based on another indicator published in the algotrading book – ObjectGroupEdit.mq5.
Please follow the link to find out more about all the underlying classes (ObjectMonitor, MapArray and others) working behind the scenes.
This indicator has no buffers. It should be attached at least to 2 charts: source chart from which you want to copy objects, and target chart where you plan to paste them.
While running on a chart, indicator tracks existing graphical objects and makes notes about which ones are selected.
As usual “Copy & Paste” commands work in pair:
- Press Ctrl+Q hotkey to copy all selected objects into Windows clipboard as a text (you can save and view it in any text editor, you can find an example below).
- On a target chart press Ctrl+J to paste all objects from the clipboard.
Why Ctrl+Q/Ctrl+J? These are just 2 vacant key combinations voluntarily chosen from very few. This is because MetaTrader intercepts and does not propagate many hotkeys to MQL-programs. Particularly this is so for the standard Ctrl+C/Ctrl+V/Ctrl+Ins/Shift+Ins, which are not working in this context.
As the source code is available, you can change the hotkeys to other combinations.
Since the indicator uses system DLLs to access Windows clipboard, you should allow DLL imports in the Properties dialog, on the Dependencies tab.
Due to the fact that the Codebase does not allow DLL imports, the related to clipboard code is wrapped into conditional preprocessor directive #ifdef DLL_LINK, so please uncomment the line
#define DLL_LINK
before compilation. Otherwise, you’ll see how clipboard function stubs will show you alerts in responce to pressing hotkeys, and no actual actions will be performed!
- MakeAllSelectable – flag to make all objects selectable (which is normally false for those objects created programmatically);
- LogDetails – flag to output all properties of transferred objects to the log;
Here is an example of clipboard text with 2 objects:
OBJ_VLINEÂ Â Â Â Â Â H1 Vertical Line 5578Â Â 0Â Â Â Â Â Â 0 enum ENUM_OBJECT_PROPERTY_INTEGER 0Â Â Â Â Â Â OBJPROP_COLORÂ Â 55295 1Â Â Â Â Â Â OBJPROP_STYLEÂ Â 2 2Â Â Â Â Â Â OBJPROP_WIDTHÂ Â 1 3Â Â Â Â Â Â OBJPROP_BACKÂ Â Â Â 0 4Â Â Â Â Â Â OBJPROP_SELECTEDÂ Â Â Â Â Â Â Â 1 7Â Â Â Â Â Â OBJPROP_TYPEÂ Â Â Â 0 8Â Â Â Â Â Â OBJPROP_TIMEÂ Â Â Â 1726739940 10Â Â Â Â Â Â OBJPROP_SELECTABLEÂ Â Â Â Â Â 1 11Â Â Â Â Â Â OBJPROP_CREATETIMEÂ Â Â Â Â Â 1726847009 12Â Â Â Â Â Â OBJPROP_TIMEFRAMESÂ Â Â Â Â Â 2097151 200Â Â Â Â OBJPROP_LEVELSÂ Â 0 207Â Â Â Â OBJPROP_ZORDERÂ Â 0 208Â Â Â Â OBJPROP_HIDDENÂ Â 0 1032Â Â Â Â OBJPROP_RAYÂ Â Â Â 1 enum ENUM_OBJECT_PROPERTY_DOUBLE 9Â Â Â Â Â Â OBJPROP_PRICEÂ Â 1.11449 enum ENUM_OBJECT_PROPERTY_STRING 5Â Â Â Â Â Â OBJPROP_NAMEÂ Â Â Â H1 Vertical Line 5578 6Â Â Â Â Â Â OBJPROP_TEXTÂ Â Â Â 206Â Â Â Â OBJPROP_TOOLTIP OBJ_CHANNELÂ Â Â Â H1 Equidistant Channel 40885Â Â Â Â 5Â Â Â Â Â Â 1 enum ENUM_OBJECT_PROPERTY_INTEGER 0Â Â Â Â Â Â OBJPROP_COLORÂ Â 255 1Â Â Â Â Â Â OBJPROP_STYLEÂ Â 0 2Â Â Â Â Â Â OBJPROP_WIDTHÂ Â 1 3Â Â Â Â Â Â OBJPROP_BACKÂ Â Â Â 0 4Â Â Â Â Â Â OBJPROP_SELECTEDÂ Â Â Â Â Â Â Â 1 7Â Â Â Â Â Â OBJPROP_TYPEÂ Â Â Â 5 8Â Â Â Â Â Â OBJPROP_TIMEÂ Â Â Â 1726758000 8Â Â Â Â Â Â OBJPROP_TIME.1Â Â 1726797600 8Â Â Â Â Â Â OBJPROP_TIME.2Â Â 1726758000 10Â Â Â Â Â Â OBJPROP_SELECTABLEÂ Â Â Â Â Â 1 11Â Â Â Â Â Â OBJPROP_CREATETIMEÂ Â Â Â Â Â 1726847883 12Â Â Â Â Â Â OBJPROP_TIMEFRAMESÂ Â Â Â Â Â 2097151 200Â Â Â Â OBJPROP_LEVELSÂ Â 0 207Â Â Â Â OBJPROP_ZORDERÂ Â 0 208Â Â Â Â OBJPROP_HIDDENÂ Â 0 1003Â Â Â Â OBJPROP_RAY_LEFTÂ Â Â Â Â Â Â Â 0 1004Â Â Â Â OBJPROP_RAY_RIGHTÂ Â Â Â Â Â 0 1031Â Â Â Â OBJPROP_FILLÂ Â Â Â 0 enum ENUM_OBJECT_PROPERTY_DOUBLE 9Â Â Â Â Â Â OBJPROP_PRICEÂ Â -28.113879003558715 9Â Â Â Â Â Â OBJPROP_PRICE.1 -21.708185053380777 9Â Â Â Â Â Â OBJPROP_PRICE.2 -48.04270462633452 enum ENUM_OBJECT_PROPERTY_STRING 5Â Â Â Â Â Â OBJPROP_NAMEÂ Â Â Â H1 Equidistant Channel 40885 6Â Â Â Â Â Â OBJPROP_TEXTÂ Â Â Â 206Â Â Â Â OBJPROP_TOOLTIP
And here are these objects pasted on the chart: