How optimize objectget() / ObjectGetValueByShift() function in EA - page 2

 
LONNV #:

thanks bro,

if for downtrend , there will be two high points,

1st high point is greater than 2nd higher point ,

So need to substractas follow ,

right?

or need to be negative sign infront of mNumerator ?

The way I had it, for mNumerator, I assumed an uptrend line and did the math expecting a positive value (i.e., price2 - price1)

I tried reversing the logic in mNumerator, but it didn't work. I haven't delved into why that is.

I'm going to leave my code below. It works for both uptrends and downtrends. Default index to check is 0, relevant code is in the CalcValueByShift function.


#property strict
#property indicator_chart_window

#include <ChartObjects/ChartObjectsLines.mqh>

CChartObjectTrend Trend;
CChartObjectHLine HLine;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit() {
  return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[]) {
  if (prev_calculated == 0) {
    Create();
    CalcValueByShift();
  } else if (rates_total - prev_calculated == 1) {
    CalcValueByShift();
  }
  
  return rates_total;                
}
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam) {
  switch (id) {
    case CHARTEVENT_OBJECT_DRAG : {
      CalcValueByShift();
      break;
    }
  }
}

void Create() {
  const double price1   = Bid;
  const double price2   = Bid + (500 * _Point);
  const datetime time1  = iTime(NULL, 0, 10);
  const datetime time2  = iTime(NULL, 0, 5);
  const bool wasCreated = Trend.Create(0, "test", 0, time1, price1, time2, price2);
  
  if (wasCreated) {
    Trend.Selectable(true);
    Trend.Selected(true);
    Trend.Detach();
    HLine.Create(0, "test1", 0, 0.0);
  }
  
}

void CalcValueByShift(const int index = 0) {
  const bool doesTrendlineExist = Trend.Attach(0, "test", 0, 2);
  
  if (!doesTrendlineExist) {
    ChartIndicatorDelete(0, 0, "CalcValueByShift");
    return;
  }
  
  const double m1         = Trend.Price(1) - Trend.Price(0);
  const int m2            = iBarShift(NULL, 0, Trend.Time(0)) - iBarShift(NULL, 0, Trend.Time(1));  
  const bool isZeroDivide = m2 == 0;
  
  if (isZeroDivide)
    return;
  
  const double m  = m1 / m2;  
  const int x     = iBarShift(NULL, 0, Trend.Time(0)) - index;
  const double b  = Trend.Price(0);
  const double y  = (m * x) + b;
  
  HLine.Price(0, y);
  
  Comment("m1: ", DoubleToStr(m1, Digits),
          "\nm2: ", m2,
          "\nm: ", m,
          "\nx: ", x, 
          "\nb: ", b,
          "\ny: ", DoubleToStr(y, Digits));
}
 
Alexander Martinez #:

The way I had it, for mNumerator, I assumed an uptrend line and did the math expecting a positive value (i.e., price2 - price1)

I tried reversing the logic in mNumerator, but it didn't work. I haven't delved into why that is.

I'm going to leave my code below. It works for both uptrends and downtrends. Default index to check is 0, relevant code is in the CalcValueByShift function.


thanks bro, this is 1st question and answer for trendline formula in mql4 without using objects.

good for new comers in community.

 
LONNV #:

thanks bro, this is 1st question and answer for trendline formula in mql4 without using objects.

good for new comers in community.

No problem, hope you got everything sorted out and GL. :)