Help - ObjectSetText() output only at one position or one chart ?

 

Hi, can anyone point me in the right direction. Thanks in advance

ObjectSetText() indicator code problem

I have a problem with indicator code that, although placed on all charts, output is only at the first position or chart opened.

The code attaches a text of pip calculation (OrderStopLoss - OrderOpenPrice) under a stoploss (solid) Horizontal Line.

The Horizontal Line is generated by a script and is placed at a preset pip distance from the orderopenprice at every trade opened - if moved it adjusts the stoploss pip distance and price.

The code will also attach said text to the MT4 default dot-hash stoploss line if the script is not running - but the same problems occur.

Descriptive and incidental to the problem is that the code can place the ticket number next to the stoploss pip calculation. The pip calculation can be set to return pips or fractional pips.

Here is the problem - Example: Three charts open, each with one trade open, each with one stoploss Horizontal Line active. Each stoploss Horizontal Line should have the relevant text attached, but only one stoploss Horizontal Line has text attached.

(i=OrdersTotal()-1;i>=0;i--) results in the text appearing on the 1st chart (position) opened but not the others.

(i=OrdersTotal()-1;i>=1;i--) result in the text appearing on the 2nd chart (position) opened but not the others.

(i=OrdersTotal()-1;i>=2;i--) result in the text appearing on the 3rd chart (position) opened but not the others.

As above, the same results for multiple positions open on a single chart - the text will cycle through the active stoploss Horizontal Lines according to the int change of i in - i>=0;, i>=1; and i>=2;.

If the following code

CreateText(Pip_Distance+SL,Time[((WindowFirstVisibleBar()-0)*Move_Text_L_to_R/100)],OrderStopLoss(),SL_Color,DoubleToStr(MathAbs((OrderStopLoss()-OrderOpenPrice())/pmod),dig));

is changed to

CreateText(HL_Price+name,Time[((WindowFirstVisibleBar()-0)*Move_Text_L_to_R/100)],price,Text_Color,DoubleToStr(price,Digits));

and

for(i=OrdersTotal()-1;i>=0;i--)

removed - the code will attach a text of price to ALL Stoploss Horizontal Lines on ALL positions open on ALL charts, which is correct and also required in the case of ((OrderStopLoss()-OrderOpenPrice())/.....

Comment: Confused - help needed - for convenience full code pasted below -

extern bool Fractional_Pips = false;

extern bool Show_OrderTicket = true;

extern color SL_Color = Red;

extern int Text_Size =12;

extern int Move_Text_L_to_R =10;


string Npips=" pips";

string Fpips=" fpips";


string Pip_Distance="Pip+Distance";

double pmod;

int p2p;

int dig;

int Xpips;

int i;


int init()


{

if (Fractional_Pips==true)

{pmod=Point;p2p=1;dig=0;}

else

{pmod=Point*10;p2p=10;dig=1;}

if (Digits==0)

{pmod=Point;p2p=1;dig=0;Xpips=1;}

return(0);

}

int deinit()

{

DeleteLabels(Pip_Distance);

return(0);

}


int start()

{

int counted_bars=IndicatorCounted();

int obj_total=ObjectsTotal();

string SL;

DeleteLabels(Pip_Distance);

for(int i=0;i<obj_total;i++)

{

SL=ObjectName(i);

if(ObjectType(SL)==OBJ_HLINE)

{

double price = ObjectGet(SL, OBJPROP_PRICE1);

CreateText(Pip_Distance+SL,Time[((WindowFirstVisibleBar()-0)*Move_Text_L_to_R/100)],OrderStopLoss(),SL_Color,DoubleToStr(MathAbs((OrderStopLoss()-OrderOpenPrice())/pmod),dig));

}

}

return(0);

}

void CreateText(string SL, datetime time1, double price,color SL_Color, string text)

{

ObjectDelete(SL);

if(!ObjectCreate(SL, OBJ_TEXT,0, time1, OrderStopLoss()))

{

Print("error: cant create OBJ_TEXT! code #",GetLastError());

return(0);

}

for(i=OrdersTotal()-1;i>=0;i--)

{

if(OrderSelect(i,SELECT_BY_POS)==true)

if(Fractional_Pips==true)

if (Xpips==1)

Npips=Npips;

else

Npips=Fpips;

if (Show_OrderTicket == true)

ObjectSetText(SL, " " + "#" + OrderTicket() + " * " + text + Npips, Text_Size, "Verdana", SL_Color);

else

ObjectSetText(SL, " " + " " + text + Npips, Text_Size, "Verdana", SL_Color);

}

}

void DeleteLabels(string Pip_Distance)

{

int obj_total=ObjectsTotal();

string SL;

for(int i=0;i<obj_total;i++)

{

SL=ObjectName(i);

if(StringFind(SL, Pip_Distance,0)>-1)

{

ObjectDelete(SL);

i--;

}

}

}

 
file45:

Hi, can anyone point me in the right direction. Thanks in advance

ObjectSetText() indicator code problem

I have a problem with indicator code that, although placed on all charts, output is only at the first position or chart opened.

The code attaches a text of pip calculation (OrderStopLoss - OrderOpenPrice) under a stoploss (solid) Horizontal Line.

...

Please use the SRC button to add Source Code.

So it seems that you have to manually draw a horizontal line before the indicator can work.

Then ...

         CreateText(Pip_Distance+SL,Time[((WindowFirstVisibleBar()-0)*Move_Text_L_to_R/100)],OrderStopLoss(),SL_Color,DoubleToStr(MathAbs((OrderStopLoss()-OrderOpenPrice())/pmod),dig));

this line seems to be using OrderStopLoss() and OrderOpenPrice() before it has selected the order using the OrderSelect() function. This will create some random looking operations.

Investigation continues ...

 
file45:

Hi, can anyone point me in the right direction. Thanks in advance


This now seems to be closer to what you wanted at least ...

extern bool Fractional_Pips  = true;
extern bool Show_OrderTicket = true;

extern color SL_Color = Yellow;
extern int   Text_Size =14;
extern int   Move_Text_L_to_R =10;

string Npips= " pips";
string Fpips= " fpips";

string Pip_Distance="Pip+Distance";

double pmod;

int p2p;
int dig;
int Xpips;
int i;

//---------------------------------------------------------------------
int init(){
   
   if(Fractional_Pips==true){
      pmod=Point;
      p2p=1;
      dig=0;
   }
   else{
      pmod=Point*10;
      p2p=10;
      dig=1;
   }

   if(Digits==0){
      pmod=Point;
      p2p=1;
      dig=0;
      Xpips=1;
   }

   return(0);
}
//---------------------------------------------------------------------
int deinit(){
   DeleteLabels(Pip_Distance);
   return(0);
}
//---------------------------------------------------------------------
int start(){
   int counted_bars=IndicatorCounted();
   
   int obj_total=ObjectsTotal();
   
   string SL;
   DeleteLabels(Pip_Distance);
   
   int orders= OrdersTotal();
   if( orders==0 )
      return(0);
   
   bool valid=false;
   for( int n=0; n<orders; n++ ){
      if( OrderSelect(n,SELECT_BY_POS) ){
         if( OrderSymbol()==Symbol() ){ // we have a valid order for this symbol
            valid=true;
            break;
         }
      }
   }
   
   if( !valid )   // there are no orders open for the symbol on this chart
      return(0); 
   
   for(int i=0;i<obj_total;i++){
      SL=ObjectName(i);
      if(ObjectType(SL)==OBJ_HLINE){
         double price = ObjectGet(SL, OBJPROP_PRICE1);
         CreateText(Pip_Distance+SL,Time[((WindowFirstVisibleBar()-0)*Move_Text_L_to_R/100)],OrderStopLoss(),SL_Color,DoubleToStr(MathAbs((OrderStopLoss()-OrderOpenPrice())/pmod),dig));
      }
   }
   return(0);
}
//---------------------------------------------------------------------
void CreateText(string SL, datetime time1, double price,color SL_Color, string text){
   ObjectDelete(SL);
   
   if(!ObjectCreate(SL, OBJ_TEXT,0, time1, OrderStopLoss())){
      Print("error: cant create OBJ_TEXT! code #",GetLastError());
      return(0);
   }
   
   for( i=OrdersTotal()-1; i>=0; i-- ){

      if(OrderSelect(i,SELECT_BY_POS)==true){
         if( OrderSymbol()==Symbol() ){   // we are only interested in this chart
            if(Fractional_Pips==true){
               if(Xpips==1)
                  Npips=Npips;
               else
                  Npips=Fpips;
            }

            if (Show_OrderTicket == true)
               ObjectSetText(SL, " " + "#" + OrderTicket() + " * " + text + Npips, Text_Size, "Verdana", SL_Color);
            else
               ObjectSetText(SL, " " + " " + text + Npips, Text_Size, "Verdana", SL_Color);
         }
      }
   }
}
//---------------------------------------------------------------------
void DeleteLabels(string Pip_Distance){

   int obj_total=ObjectsTotal();

   string SL;

   for( int i=0; i<obj_total; i++ ){
      SL=ObjectName(i);
      if( StringFind(SL, Pip_Distance,0) > -1 ){
         if( ObjectDelete(SL) ){  // delete all objects called anything including substring Pip_Distance 
            obj_total--;
            i--;
         }
      }
   }
}
 
dabbler:

This now seems to be closer to what you wanted at least ...

Thanks for the additional code. Code copied, pasted and compiled. Text now attaches to all charts but, as before, to only one SL Line on any single chart - additional SL Lines are ignored. Previously the text did not attach to other charts. A welcomed improvement.

A newbie, after further labors, I suspect the code could be better structured - "This will create some random looking operations." - however, its looking more like a glass half full that half empty.

  
 


Rearranged void CreateText(.............

void CreateText(string SL, datetime time1, double price,color SL_Color, string text){
   ObjectDelete(SL);
   
   if(Fractional_Pips==true){
      if(Xpips==1)
         Npips=Npips;
   else
      Npips=Fpips;}
   
   if(!ObjectCreate(SL, OBJ_TEXT,0, time1, OrderStopLoss())){
   Print("error: cant create OBJ_TEXT! code #",GetLastError());
   return(0);}
   
   if (Show_OrderTicket == true)
      ObjectSetText(SL, " " + "#" + OrderTicket() + " * " + text + Npips, Text_Size, "Verdana", SL_Color);
   else
      ObjectSetText(SL, " " + " " + text + Npips, Text_Size, "Verdana", SL_Color);
   
   /*for(int i=OrdersTotal()-1; i>=0; i-- ){
      if(OrderSelect(i,SELECT_BY_POS)==true){
         if( OrderSymbol()==Symbol()){   // we are only interested in this chart*/    
         
   /*for(int i=0;i<OrdersTotal();i++ ){
      if(OrderSelect(i,SELECT_BY_POS)==true){
         if( OrderSymbol()==Symbol()){   // we are only interested in this chart*/       
         
   static int i = 0;
   if(OrderSelect(i,SELECT_BY_POS)==true){     
   i++;      
         //}
     //}
   }
}

Top /*...*/ gets text on one SL Line on each charts.

Under top /*...*/ gets text on two SL Lines (earliest opened) on one chart and one SL Line (earliest open) on all additional charts. (? ?)

The below code gets text on all SL Lines - but not without problem.

static int i = 0;
   if(OrderSelect(i,SELECT_BY_POS)==true){     
   i++;      
problem - the text attaches to all SL Lines on all charts, and although all tickets and calculations are correct, the text remains for a few seconds then disappears. After this disappearance one SL Line - the first opened - on each chart, retains text permanently for the duration of the trade. 
 If the above code is placed anywhere else either the tickets or pip calculations will be wrong.
 
void DeleteLabels(string Pip_Distance){
   int obj_total=ObjectsTotal();
   for( int i=0; i<obj_total; i++ ){
Count DOWN
 
WHRoeder:
Count DOWN

Not sure what you mean as it is coded in original code as given further above.

The following code

for(int i=0;i<OrdersTotal();i++ ){
   if(OrderSelect(i,SELECT_BY_POS)==true){
      if( OrderSymbol()==Symbol()){   // we are only interested in this chart       

// static int i = 0;
// if(OrderSelect(i,SELECT_BY_POS)==true){     
// i++;      

produces - see below chart: the yellow line is a stack of 3 opened market Buys, associate SL in red below - the SL Lines are drawn by the Easy Order script.

As can be seen, one SL Line is without text - all additional charts will have text at only one SL Line (with earliest date & time stamp) - additional SL Lines will be without text. So the earliest chart will always have text attached to only two of all SL Lines and additional charts will always have text attached to only one of all SL Lines. (? ?)

The object is to have text at all SL Lines for the duration of open trades trade.

static int i = 0;
if(OrderSelect(i,SELECT_BY_POS)==true){     
i++;     

The immediate above achieves said objective of text at all SL Lines but disappears after a few seconds. ( ?)

Proving a hard nut to crack !

The Easy Order script can be got here - Easy Order

 
file45:

The following code.

The above problem has been solved - thanks to contributors - credits have been included in revised code

While searching the internet for solutions I found little that would lead a learner to connect the dots, however, this thread comes up regularly in search results.

The solution may prove helpful as a template for analysis, direction and a reference for future builds. The codes is posted below.


#property copyright "Copyright @ 20011, File45 and anyone who want to add their 2 pence/cents/rupees worth"
#property indicator_chart_window 

//thanks: miaden-super moderator FOREX-TSD www.forex-tsd.com for major edits after which it worked.
// https://www.forex-tsd.com/expert-advisors-metatrader-4/20171-please-fix-indicator-ea-34.html#post425056
//thanks: dabbler - MQL4/Forum https://forum.mql4.com/45267   
//thwnks: WHRoeder - as above

extern bool Show_SL = true;
extern color SL_Color = Yellow;
extern bool Show_TP = true;
extern color TP_Color = DodgerBlue;
extern int   Text_Size =10;
extern int   Move_Text_L_or_R =0;
extern bool Fractional_Pips  = true;
extern bool Show_OrderTicket = true;

string Npips= " pips";
string Fpips= " fpips";

static string Pip_Distance="Pip+Distance";
static string text;
double pmod;

int p2p;
int dig;
int Xpips;

//---------------------------------------------------------------------
int init(){
   
   if(Fractional_Pips==true){
      pmod=Point;
      p2p=1;
      dig=0;
   }
   else{
      pmod=Point*10;
      p2p=10;
      dig=1;
   }

   if(Digits==0){
      pmod=Point;
      p2p=1;
      dig=0;
      Xpips=1;
   }
   
   return(0);
}
//---------------------------------------------------------------------
int deinit(){
   DeleteLabels(Pip_Distance);
   return(0);
}
//---------------------------------------------------------------------
int start()
{
   DeleteLabels(Pip_Distance);

   int orders= OrdersTotal();
   if( orders==0 ) return(0);
   
   for (int n=0; n<orders; n++)
   {
      if (OrderSelect(n,SELECT_BY_POS))
      if (OrderSymbol()==Symbol())
      {
         string name = Pip_Distance+OrderTicket()+"stop loss";
            if(Show_SL==true)
            CreateText(name,Time[((WindowFirstVisibleBar()-0)*Move_Text_L_or_R/100)],OrderStopLoss(),SL_Color,DoubleToStr(MathAbs((OrderStopLoss()-OrderOpenPrice())/pmod),dig));
                name = Pip_Distance+OrderTicket()+"take profit";
            if(Show_TP==true)
            CreateText(name,Time[((WindowFirstVisibleBar()-0)*Move_Text_L_or_R/100)],OrderTakeProfit(),TP_Color,DoubleToStr(MathAbs((OrderTakeProfit()-OrderOpenPrice())/pmod),dig));
      }
   }
   return(0);
}

//---------------------------------------------------------------------
//
//---------------------------------------------------------------------

void CreateText(string SL, datetime time1, double price, color theColor, string text)
{
   if (price==0) return;
   if (Fractional_Pips==true)
      {
         if(Xpips==1)
               Npips=Npips;
         else  Npips=Fpips;
      }
   
   ObjectCreate(SL, OBJ_TEXT,0, time1, price);
   if (Show_OrderTicket == true)
   // this long gap takes the text to the right side of the screen
         ObjectSetText(SL, "                                        " + "#" + OrderTicket() + " * " + text + Npips, Text_Size, "Verdana", theColor);
   else  ObjectSetText(SL, "                                        " + " " + text + Npips, Text_Size, "Verdana", theColor);
}

//---------------------------------------------------------------------
//
//---------------------------------------------------------------------

void DeleteLabels(string Pip_Distance)
{
   for( int i=ObjectsTotal(); i>=0; i-- )
   {
      string SL=ObjectName(i); if (StringFind(SL,Pip_Distance,0) > -1 ) ObjectDelete(SL);
   }
}