ObjectSet not working properly

 

I've created this so far, but for some reason the ObjectSet is not working properly. My comments are in []

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots 0



extern datetime ArrowDate1 = D'2023.03.03 00:00';

extern double ArrowPrice1 = 144.8730;

extern datetime ArrowDate2 = D'2023.03.03 05:00';

extern double ArrowPrice2 = 144.8730;



int start()

{

    // Draw the first arrow

    double ArrowPrice1Normalized = NormalizeDouble(ArrowPrice1, Digits);

    int ArrowObject1 = ObjectCreate("Arrow 1", OBJ_ARROW, 0, ArrowDate1, ArrowPrice1Normalized);

    ObjectSet(ArrowObject1, OBJPROP_ARROWCODE, 1); [Arrow code on the chart ends up as 241]

    ObjectSet(ArrowObject1, OBJPROP_COLOR, clrRed); [Colour Doesn't change even if I use clrBlue] 



    // Draw the second arrow

    double ArrowPrice2Normalized = NormalizeDouble(ArrowPrice2, Digits);

    int ArrowObject2 = ObjectCreate("Arrow 2", OBJ_ARROW, 0, ArrowDate2, ArrowPrice2Normalized);

    ObjectSet(ArrowObject2, OBJPROP_ARROWCODE, 3); [Arrow code on the chart ends up as 241]

    ObjectSet(ArrowObject2, OBJPROP_COLOR, clrGoldenrod); [Colour Doesn't change even if I use clrBlue] 



    // Draw the line connecting the arrows

    int LineObject = ObjectCreate("Line", OBJ_TREND, 0, ArrowDate1, ArrowPrice1Normalized, ArrowDate2, ArrowPrice2Normalized);

    ObjectSet(LineObject, OBJPROP_STYLE, STYLE_DOT); [Style appears as full line] 

    ObjectSet(LineObject, OBJPROP_COLOR, clrRed); [Colour Doesn't change even if I use clrBlue] 

    ObjectSet(LineObject, OBJPROP_RAY, false); [Doesn't work. It always Rays] 

    return(0);

}


Documentation on MQL5: Language Basics / Preprocessor / Program Properties (#property)
Documentation on MQL5: Language Basics / Preprocessor / Program Properties (#property)
  • www.mql5.com
Program Properties (#property) - Preprocessor - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
  1. 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

  2.   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 Reference

       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

  3. ObjectSet(ArrowObject2, OBJPROP_ARROWCODE, 3); [Arrow code on the chart ends up as 241]

    Invalid object name.

 
Thanks William. I've edited the original post. I'll go away and try to figure it out. I'm not a programmer, as you can likely tell. In fact, I've never coded anything before yesterday. I spent the day going down a rabbit hole with a lot of trial and error to get to this stage. I appreciate the direction. 
 
William Roeder #:
  1. 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

  2. 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.

  3. 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);
}
 
    string ArrowObject1 = ObjectCreate("Arrow 1", OBJ_ARROW, 0, EntryDate, EntryPriceNormalized);
    long ArrowObject2 = ObjectCreate("Arrow 2", OBJ_ARROW, 0, ExitDate, ExitPriceNormalized);
You have ignored #1.2 RTFM.
 
Hi my MT4 is not displaying charts only the three candle sticks icon...what could be the problem and how do I resolve it?
 
William Roeder #:
You have ignored #1.2 RTFM.

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

Reason: