Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 740

 
Taras Slobodyanik:

Do initialisation of variables in OnInit (including redrawing objects)


Explain what this is about.

I added it to OnInit after creating a label

    ObjectSetInteger(0, objname, OBJPROP_YDISTANCE, y);
    ChartRedraw();


But the result is the same.

 
psyman:


Explain what this is about.

I added it to OnInit after creating the tag


but the result is the same.

need to see the code, what's there...

ps. the code above works fine (I don't see any glitches)
(https://www.mql5.com/ru/forum/160683/page739#comment_10290585)


oops. although i replaced the Bid variable with Bid0

Любые вопросы новичков по MQL4, помощь и обсуждение по алгоритмам и кодам
Любые вопросы новичков по MQL4, помощь и обсуждение по алгоритмам и кодам
  • 2019.01.17
  • www.mql5.com
В этой ветке я хочу начать свою помощь тем, кто действительно хочет разобраться и научиться программированию на новом MQL4 и желает легко перейти н...
 
Good afternoon, how do I write the order to be opened when the price touches a muvingian?
 
Ivan Rozhkov:
Good afternoon, how do I write the order to be opened when the price touches a muvingian?
if ((prev_bid>ma && curr_bid<=ma) || (prev_bid<ma && curr_bid>=ma))
   //открывается ордер
 
Taras Slobodyanik:

I have to look at the code to see what's there...

psst. The code above works fine (no glitches)
(https://www.mql5.com/ru/forum/160683/page739#comment_10290585)


oops. although I replaced the Bid variable with Bid0

Forgot to add that this is me testing in 5. In MT4 the vertical marker does move quickly into place when you open the chart, no need to wait for a tick or move the mouse. A few times there were glitches with horizontal shift, managed to overcome only by recompiling the indicator.

 
psyman:


Tried it, you have to move the mouse to get the mark back. It's the same crutch, but in a different hand :-)

And switching between charts forCHARTEVENT_CHART_CHANGE is not an event.

Can anyone think of a way to overcome this anomaly?


Print displays a message to me if I switch a chart window, both forwards and backwards switching (I'm on MT4, but it's not written in MT4 docs that chart switching can be caught as aCHARTEVENT_CHART_CHANGE event )

void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
  if(id==CHARTEVENT_CHART_CHANGE) Print("graph");
}

But yes, it's still a crutch method. We need to find out why the Y coordinate is different when switching to another chart. But looking at the code, we can conclude that theChartTimePriceToXY gives different results for some reason.

 
psyman:

I forgot to add that I'm testing this in 5. In MT4 the vertical marker really moves quickly into place when the chart is opened, no need to wait for a tick or move the mouse. A few times there were glitches with horizontal shift, managed to overcome only by recompiling the indicator.

Well, in Five there are a lot of "features" - from the queue on the chart to the creation of bars from the minutes, and the triggering of the previous deinit after the current one)

So you need to check the bars/graph/objects - at what point an error occurs and correct it.

 
Taras Slobodyanik:


in general - you need to check the bars/graphics/objects - at what point an error occurs and correct it.


How to do this, where to start?

 
psyman:


How to do it, where to start?

do a print everywhere, i.e. we print the bid (you can also print the current date) in oninit and then in the calculation as well...
I can telepathise that the story does not build up immediately and the bars appear later

 
Taras Slobodyanik:

do a print everywhere, i.e. in oninit print the bid (you can also print the current date) and then when calculating too...
I telepathize that this story does not have time to build immediately and the bars appear later

For the next code, a story in pictures:

//+------------------------------------------------------------------+
//|                                              !_clr_indicator.mq5 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window


string objname="Bid";
input color Bid_color = clrCrimson;

int OnInit()
  {

int x,y;
double Bid0 = SymbolInfoDouble(Symbol(), SYMBOL_BID);
int width = (int)ChartGetInteger(0,CHART_WIDTH_IN_PIXELS,0);  //ширина графика



        Print("1 Bid0 = ", Bid0,  " x = ", x, " y = ", y," TimeCurrent()=", TimeCurrent() );

   ObjectCreate(0, objname, OBJ_LABEL,0, 0, 0);
   
   ObjectSetString(0, objname, OBJPROP_TEXT, DoubleToString(Bid0, _Digits-1));
        ObjectSetInteger(0, objname, OBJPROP_FONTSIZE, 12);
        ObjectSetInteger(0, objname, OBJPROP_COLOR, Bid_color);
        ObjectSetString(0, objname, OBJPROP_FONT, "Verdana");

        ChartTimePriceToXY(0, 0, TimeCurrent(), Bid0, x, y);
        Print("2 Bid0 = ", Bid0,  " x = ", x, " y = ", y," TimeCurrent()=", TimeCurrent() );
         
        ObjectSetInteger(0, objname, OBJPROP_XDISTANCE, width-60);

   ObjectSetInteger(0, objname, OBJPROP_YDISTANCE, y);

   

   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[])
  {

return(rates_total);

  }

void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam) 
  { 

int x2,y2;
double Bid2 = SymbolInfoDouble(Symbol(), SYMBOL_BID);
double  cprice;
datetime ctime;
int      cwindow=0;



if(id==CHARTEVENT_CHART_CHANGE) 
   {
   



           ChartTimePriceToXY(0, 0, TimeCurrent(), Bid2, x2, y2); 
      
      Print("5 Bid2 = ", Bid2,  " x2 = ", x2, " y2 = ", y2," TimeCurrent2()=", TimeCurrent() );
      
        ObjectSetInteger(0, objname, OBJPROP_YDISTANCE, y2);
   
   ChartXYToTimePrice(0,x2,x2,cwindow, ctime, cprice);
   

   }



  }



void OnDeinit(const int reason) 
   { 

      ObjectDelete(0,objname);
   } 
 
 


Disconnected the network, adding an indicator

I switch to the next chart


y2 changed value, I go back


y2 has returned its value, but the marker is counted by the "gone" value! I spin the mouse wheel.

The marker is back in its place!

Reason: