Trouble creating an object

 

So, as illustrated in the images, if I hard code in the values, the trendline will draw. But if I use functions that return identical values, nothing happens. No error message is thrown, which as I read means the create function went to the stack but didn't necessarily execute. No object appears in the object list of the chart. I'm assuming I'm formatting the values wrong?

Files:
 
  1. Don't post pictures of code, they are generally too hard to read.

    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 #25 (2019)
              Messages Editor
          Forum rules and recommendations - General - MQL5 programming forum (2023)

  2. You used NormalizeDouble, Its use is usually wrong, as it is in your case.

    1. Floating point has an infinite number of decimals, it's you were not understanding floating point and that some numbers can't be represented exactly. (like 1/10.)
                Double-precision floating-point format - Wikipedia, the free encyclopedia

      See also The == operand. - MQL4 programming forum (2013)

    2. Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

    3. SL/TP (stops) need to be normalized to tick size (not Point) — code fails on non-currencies.
                On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum #10 (2011)

      And abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum (2012)

    4. Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on non-currencies. So do it right.
                Trailing Bar Entry EA - MQL4 programming forum (2013)
                Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum (2012)

    5. Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.
                (MT4 2013)) (MT5 2022))

    6. MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
                MT4:NormalizeDouble - MQL5 programming forum (2017)
                How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum (2017)

    7. Prices (and lots) you get from the terminal are already correct (normalized).

    8. PIP, Point, or Tick are all different in general.
                What is a TICK? - MQL4 programming forum (2014)

 
William Roeder #: quoted post removed
Lots of great info. Thanks!
 
Andrew Powers:

So, as illustrated in the images, if I hard code in the values, the trendline will draw. But if I use functions that return identical values, nothing happens. No error message is thrown, which as I read means the create function went to the stack but didn't necessarily execute. No object appears in the object list of the chart. I'm assuming I'm formatting the values w

William Roeder #: quoted post removed.

I don't seem to have the option to edit my post. I made some alterations using the information you posted. I would call the outcome an improvement, but it has also left me a bit more confused.

//Change to getXPrice() functions:
return _Point * (price/_Point);

//Draw trend function
Print("Start time: ", getStartTime(), " start price: ", DoubleToString(getStartPrice(), _Digits), " end time: ", getEndTime(), " end price: ", DoubleToString(getEndPrice(), _Digits));  
   ObjectCreate(chartId,"Good Trendline",OBJ_TREND,0,D'2018.02.11 00:00:00',1.25554,D'2022.09.25 00:00:00',0.95357);          // <--This works
   ObjectCreate(chartId,objectName,OBJ_TREND,0,D'2018.02.11 00:00:00',getStartPrice(),D'2022.09.25 00:00:00',getEndPrice());  // <--This doesn't

//Print output
2024.06.07 12:37:01.277 Core 01 2024.01.01 00:00:00   Start time: 2018.02.11 00:00:00 start price: 1.25554 end time: 2022.09.25 00:00:00 end price: 0.95357
The image has the resulting trendlines. The downward trendline from high peak, to low peak is the hardcoded trendline. The upward trendline is using the printed values.
Files:
trendlines.png  124 kb
 

Save the price values into variables before drawing the line.

Draw a line by passing variable values ​​to ObjectCreate.

Immediately after ObjectCreate, print the values ​​of the variables

 
What is chartId equal to? Is this the current chart or not?
 
Vladislav Boyko #:

Save the price values into variables before drawing the line.

Draw a line by passing variable values ​​to ObjectCreate.

Immediately after ObjectCreate, print the values ​​of the variables

Something like:

void OnStart()
  {
   test("Good Trendline", D'2018.02.11 00:00:00', 1.25554, D'2022.09.25 00:00:00', 0.95357);
   test("Not a good trendline", getStartTime(), getStartPrice(), getEndTime(), getEndPrice());
  }

#define _prcToStr(a) DoubleToString(a, _Digits)

void test(string name, datetime time1, double price1, datetime time2, double price2)
  {
   ObjectCreate(0,name,OBJ_TREND,0,time1,price1,time2,price2);
   PrintFormat("Call ObjectCreate with parameters: %s, %s, %s, %s", TimeToString(time1), _prcToStr(price1), TimeToString(time2), _prcToStr(price2));
  }

// You didn't provide the code for your functions, so let them return zeros
datetime getStartTime() { return 0; }
double getStartPrice() { return 0.0; }
datetime getEndTime() { return 0; }
double getEndPrice() { return 0.0; }
 
Vladislav Boyko #:

Something like:

Thanks for taking a look. Storing the values in variables first yields the same result. Actually, it's no longer drawing a wildly off trendline. It just doesn't draw a trendline at all. The values that get printed are identical to the values I manually input. The chartId is 0, current chart.
 
Andrew Powers #:
Thanks for taking a look. Storing the values in variables first yields the same result. Actually, it's no longer drawing a wildly off trendline. It just doesn't draw a trendline at all. The values that get printed are identical to the values I manually input. The chartId is 0, current chart.

Please provide code to reproduce. Otherwise, it is unlikely that anyone can help you.

 
Vladislav Boyko #:

Please provide code to reproduce. Otherwise, it is unlikely that anyone can help you.

So this is interesting... still having no luck with this. But I pinpointed something that prevents objects from being drawn for some reason:

bool Trend::getHighBar(Bar &bar) {
   bar = mHigh;
   return bar.isValid();
}

bool Trend::getLowBar(Bar &bar) {
   bar = mLow;
   return bar.isValid();
}

datetime Trend::getHighTime() {
   return mHigh.getTime();
}

datetime Trend::getLowTime() {
   return mLow.getTime();
}

bool Trend::getStartBar(Bar &bar) {
   if (getHighTime() == getLowTime()) {Print("Cannot get start bar if high time and low time are equal."); return false;}
   return getHighTime() < getLowTime() ? getHighBar(bar) : getLowBar(bar);
}


bool Trend::drawTrendline(long chartId=0, string objectName="Trendline") {
        Bar startBar = new Bar();
        if (!getStartBar(startBar)) {Print(__FUNCTION__, "Failed to get start bar."); return false;} //This line prevents all ObjectCreate calls from working. However, the print functions after still return good values.

        ObjectCreate(chartId,"HLine1",OBJ_HLINE,0,D'2022.09.25 00:00:00', 0.95357); //This draws if the above line is gone
        //Any attempt to store iHigh or iLow in a class member variable and then later call a get function to retrieve it fails to draw anything, no matter how I format it
        //For formatting a double I have tried _Point *MathRound(doubleValue/_Point)
        //It seems to print exactly the value I need, but refuses to draw

        Print("Object: ", ObjectFind(chartId, "HLine1")); //Prints Object: 0 whether the HLine gets drawn or not, which means it does find an object

        ChartRedraw(chartId); return true;
}
 
Andrew Powers #:

So this is interesting... still having no luck with this. But I pinpointed something that prevents objects from being drawn for some reason:

Solved. For whatever reason, it strongly disliked the double prices coming back from my Bar class. I discovered the MqlRates class and used that instead. The trendline is now drawing.