-
Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
Messages Editor -
int ArrowObject1 = ObjectCreate("Arrow 1", OBJ_ARROW, 0, ArrowDate1, ArrowPrice1Normalized);
The function does not return an int. Perhaps you should read the manual.ObjectCreate - Object Functions - MQL4 ReferenceHow To Ask Questions The Smart Way. (2004)
How To Interpret Answers.
RTFM and STFW: How To Tell You've Seriously Screwed Up. -
ObjectSet(ArrowObject2, OBJPROP_ARROWCODE, 3); [Arrow code on the chart ends up as 241]
Invalid object name.
-
Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
Messages Editor - The function does not return an int. Perhaps you should read the manual.ObjectCreate - Object Functions - MQL4 Reference
How To Ask Questions The Smart Way. (2004)
How To Interpret Answers.
RTFM and STFW: How To Tell You've Seriously Screwed Up. -
Invalid object name.
Thanks William. I've completed it and it works for what I need (A part of a trade logging process, particularly when trade data isn't avaialble to pull from the Mt4 terminal)
It will plot:
- an arrow at the open price
- an arrow at the close price
- a line between them
- an arrow where the SL would be
- an arrow where the TP would be
Exactly the same as if you were to drag a trade from the terminal's history onto a chart window.
A limitation is it will only plot one trade on a chart. I expect because trying to do two would duplicate the names of the objects.
I've pasted the code here in case anyone has a use for it:
Thanks again.
#property indicator_chart_window #property indicator_buffers 0 #property indicator_plots 0 // Set Trade Open Parameters extern datetime EntryDate = D'2023.03.03 00:00'; extern double EntryPrice = 144.8730; // Set Trade Close Parameters extern datetime ExitDate = D'2023.03.03 05:00'; extern double ExitPrice = 144.8530; //Set Trade TP Parameters extern double TP = 144.1800; //Set Trade SL Parameters extern double SL = 145.1190; int start() { // Draw the first arrow double EntryPriceNormalized = NormalizeDouble(EntryPrice, Digits); string ArrowObject1 = ObjectCreate("Arrow 1", OBJ_ARROW, 0, EntryDate, EntryPriceNormalized); ObjectSet("Arrow 1", OBJPROP_ARROWCODE, 1); ObjectSet("Arrow 1", OBJPROP_COLOR, clrRed); // Draw the second arrow string ExitPriceNormalized = NormalizeDouble(ExitPrice, Digits); long ArrowObject2 = ObjectCreate("Arrow 2", OBJ_ARROW, 0, ExitDate, ExitPriceNormalized); ObjectSet("Arrow 2", OBJPROP_ARROWCODE, 3); ObjectSet("Arrow 2", OBJPROP_COLOR, clrGoldenrod); // Draw the line connecting the arrows string LineObject = ObjectCreate("Line", OBJ_TREND, 0, EntryDate, EntryPriceNormalized, ExitDate, ExitPriceNormalized); ObjectSet("Line", OBJPROP_STYLE, STYLE_DOT); ObjectSet("Line", OBJPROP_COLOR, clrRed); ObjectSet("Line", OBJPROP_RAY, false); // Draw the TP arrow double TPNormalized = NormalizeDouble(TP, Digits); string ArrowObject3 = ObjectCreate("Arrow 3", OBJ_ARROW, 0, EntryDate, TPNormalized); ObjectSet("Arrow 3", OBJPROP_ARROWCODE, 4); ObjectSet("Arrow 3", OBJPROP_COLOR, clrBlue); // Draw the SL arrow double SLNormalized = NormalizeDouble(SL, Digits); string ArrowObject4 = ObjectCreate("Arrow 4", OBJ_ARROW, 0, EntryDate, SLNormalized); ObjectSet("Arrow 4", OBJPROP_ARROWCODE, 4); ObjectSet("Arrow 4", OBJPROP_COLOR, clrRed); return(0); }
I did RTFM William. It's like reading a foreign language to me and I had to play around - the code I shared somehow worked. Thanks for pointing out the error. I've updated it as below:
Thanks again for your guidance.
#property indicator_chart_window #property indicator_buffers 0 #property indicator_plots 0 // Set Trade Open Parameters extern datetime EntryDate = D'2023.03.03 00:00'; extern double EntryPrice = 144.8730; // Set Trade Close Parameters extern datetime ExitDate = D'2023.03.03 05:00'; extern double ExitPrice = 144.8530; //Set Trade TP Parameters extern double TP = 144.1800; //Set Trade SL Parameters extern double SL = 145.1190; int start() { // Draw the first arrow double EntryPriceNormalized = NormalizeDouble(EntryPrice, Digits); string ArrowObject1 = ObjectCreate("Arrow 1", OBJ_ARROW, 0, EntryDate, EntryPriceNormalized); ObjectSet("Arrow 1", OBJPROP_ARROWCODE, 1); ObjectSet("Arrow 1", OBJPROP_COLOR, clrRed); // Draw the second arrow double ExitPriceNormalized = NormalizeDouble(ExitPrice, Digits); string ArrowObject2 = ObjectCreate("Arrow 2", OBJ_ARROW, 0, ExitDate, ExitPriceNormalized); ObjectSet("Arrow 2", OBJPROP_ARROWCODE, 3); ObjectSet("Arrow 2", OBJPROP_COLOR, clrGoldenrod); // Draw the line connecting the arrows string LineObject = ObjectCreate("Line", OBJ_TREND, 0, EntryDate, EntryPriceNormalized, ExitDate, ExitPriceNormalized); ObjectSet("Line", OBJPROP_STYLE, STYLE_DOT); ObjectSet("Line", OBJPROP_COLOR, clrRed); ObjectSet("Line", OBJPROP_RAY, false); // Draw the TP arrow double TPNormalized = NormalizeDouble(TP, Digits); string ArrowObject3 = ObjectCreate("Arrow 3", OBJ_ARROW, 0, EntryDate, TPNormalized); ObjectSet("Arrow 3", OBJPROP_ARROWCODE, 4); ObjectSet("Arrow 3", OBJPROP_COLOR, clrBlue); // Draw the SL arrow double SLNormalized = NormalizeDouble(SL, Digits); string ArrowObject4 = ObjectCreate("Arrow 4", OBJ_ARROW, 0, EntryDate, SLNormalized); ObjectSet("Arrow 4", OBJPROP_ARROWCODE, 4); ObjectSet("Arrow 4", OBJPROP_COLOR, clrRed); return(0); }
ObjectCreate returns boolean value - true or false.
Excerpt from the documentation William recommended you to read: " and the return of true means a successful creation of an object; otherwise false is returned".
Things wrong with your code:
- it uses "old" syntax, doesn't have "property strict", doesn't have "OnCalculate()"
- best solution is to use MetaEditor's wizard to create empty indicator then copy parts of the existing code
- indicator doesn't remove objects when it exits
- delete objects you created in OnDeinit() function
- you do not check if ObjectCreate() function was successful - check the return value and if it is false, then use GetLastErrorCode to find out what caused the problem
- cause of error is probably existing object - you created object, stopped an indicator but object still exists on the chart. Next time you try to create object with the same name, it won't work.
- create string constants for object names - you are repeating those names in multiple places, replace literals with constants
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I've created this so far, but for some reason the ObjectSet is not working properly. My comments are in []