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

 
if(TralProcent!=0)
{ NewProfProc=Profit(-1)/(AccountBalance()/100); if(NewProfProc>0 && (ProfitProcent==0 || ProfitProcent<NewProfProc)) {ProfitProcent=NormalizeDouble(NewProfProc, 2);} if(ProfitProcent>0 && ProfitProcent>(TralProcent+TralStartProcent)) {TrallingProcent=ProfitProcent-TralProcent;} //+------------------------------------------------------------------+ if(NewProfProc>0 && TrallingProcent!=0 && NewProfProc<=TrallingProcent) {CloserS(); CloserB(); ProfitProcent=0; TrallingProcent=0;}

------robot uses trawl in %balance, please advise how to add step changes here. If I understand it correctly it is needed for Tralingprocent. For example if step =2%, then trailingprocent increases by at least 2%.
 
khorosh:

Artyom Trishkin:

Objects - they can be output to any terminal window and its subwindows.

--------------------------------------------------------------------------------------------------------------------------------------

I tried it like this, but it doesn't work.

Maybe there is something wrong with the window numbering. If there is one separate window in the chart, its number ==1?

wndNum number of the window where to display

int wndNum=1;

int OnInit()
 {
 string short_name=MQLInfoString(MQL_PROGRAM_NAME);
 short_name+=": MyIndicator";
 IndicatorSetString(INDICATOR_SHORTNAME,short_name);
 wndNum=ChartWindowFind(0,short_name);
 //---
   return(INIT_SUCCEEDED);
 }

// Функция создания объекта
void SetLabel(string nm,string text,long xd,long yd,int fs,string font,int cr,int an,color cl,string tooltip,bool sel) {
 if(ObjectFind(0,nm)<0) {
    ObjectCreate(0,nm,OBJ_LABEL,wndNum,0,0);
    ObjectSetInteger(0,nm,OBJPROP_CORNER,cr);
    ObjectSetInteger(0,nm,OBJPROP_ANCHOR,an);
    ObjectSetString (0,nm,OBJPROP_FONT,font);
    ObjectSetInteger(0,nm,OBJPROP_FONTSIZE,fs);
    ObjectSetInteger(0,nm,OBJPROP_BACK,false);
    ObjectSetInteger(0,nm,OBJPROP_HIDDEN,false);
    ObjectSetInteger(0,nm,OBJPROP_SELECTABLE,sel);
    ObjectSetInteger(0,nm,OBJPROP_SELECTED,sel);
    ObjectSetString (0,nm,OBJPROP_TOOLTIP,tooltip);
  }
    ObjectSetInteger(0,nm,OBJPROP_YDISTANCE,yd);
    ObjectSetInteger(0,nm,OBJPROP_XDISTANCE,xd);
    ObjectSetString (0,nm,OBJPROP_TEXT,text);
    ObjectSetInteger(0,nm,OBJPROP_COLOR,cl); 
 }
P.S.khorosh, I can't reply in private - the chat doesn't work!
 
Vitaly Muzichenko:

wndNum number of the window where to display

P.S.khorosh, I cannot reply to you in private - the chat is not working!

Thank you, Vitaliy! And how do I attach it to the tip of the curve of an indicator which is in a separate window? I tried to set Time[0] and curve buffer value on zero bar as coordinates, but it didn't work.

 
khorosh:

Thank you, Vitaly! And how to tie it to the tip of the curve of the indicator which is in the separate window? I tried to set Time[0] and the curve buffer value on the zero bar as coordinates, but it does not work.

That's how he answered that question. The only nuance that often comes up is the wndNum value itself. The value of wndNum is not a constant. It must be constantly monitored. The user can delete the indicator subwindow, which precedes the current one. Also do not look for the indicator itself in OnInit(). This code should be moved to OnCalculate(). In any case you should always check if the subwindow index is correct.

 
Ihor Herasko:

That's how he answered the question. The only nuance that often comes up is the wndNum value itself. The value of wndNum is not a constant. It must be constantly monitored. The user can delete the indicator subwindow, which precedes the current one. Also do not look for the indicator itself in OnInit(). This code should be moved to OnCalculate(). In any case you must always check if the subwindow index is correct.

His example shows that object coordinates are initially assumed to be in pixels (static object). But I need to bind to an indicator curve. So, first we have to calculate these coordinates. It is not shown in his work. I tried it this way, but it doesn't work (in LevLabelSpread variable there is a curve buffer value on the first bar):

....
int X=0,Y=0;
   wndNum=ChartWindowFind(0,Shortname);  
   ChartTimePriceToXY( 0,wndNum,Time[1],LevLabelSpread,X,Y); Print("X=",X," Y=",Y);    
   if(ObjectFind("_Spread")<0) 
     {
      ObjectCreate(0,"_Spread", OBJ_TEXT, wndNum, 0,0);
      ObjectSetText("_Spread", "                Spread", 10, "Arial", clrYellow);
     }
   else
     {
      ObjectSetInteger(0,"_Spread",OBJPROP_YDISTANCE,Y);
      ObjectSetInteger(0,"_Spread",OBJPROP_XDISTANCE,X);
      ObjectSetString (0,"_Spread",OBJPROP_TEXT,"                Spread");
     } 

.... 
 
khorosh:

He has an example where object coordinates are initially assumed to be in pixels (static object). But I need to link it to an indicator curve. So, I have to calculate these coordinates beforehand. It is not shown in his work. I tried it this way but it doesn't work (the variable LevLabelSpread has the curve buffer value on the first bar):

Why should I calculate price in pixels for an object of TEXT type? It is positioned by bar time and price. I have it like this:

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[])
{
   int nWndIndex = ChartWindowFind();
   if (nWndIndex < 0)
      return 0;
      
   string sName = "MyText";
   if (ObjectFind(0, sName) < 0)
      ObjectCreate(0, sName, OBJ_TEXT, nWndIndex, time[rates_total - 1], <здесь значение индикатора>);
      
   ObjectSetInteger(0, sName, OBJPROP_ANCHOR, ANCHOR_LEFT);
   ObjectSetString(0, sName, OBJPROP_TEXT, "Sample text");

   return(rates_total);
}

Result:


 
Ihor Herasko:

Why calculate the price in pixels for a TEXT object? It is positioned by bar time and price. I've got it like this:

Result:


Thank you very much.

 
Ihor Herasko:


It worked out fine:


 
Please explain the meaning and purpose of the two concepts: chart handle andchart identifier. What is the difference between them and how and when they are used.
 
khorosh:
Please explain the meaning and purpose of two notions: chart handle andchart identifier. What is the difference between them and how and when they are used.

Show me how you get both. So far it seems that we are talking about the same thing.