not sure how to insert a variable into a label

 

Hello Forum, with help I previously got an indicator to work which simply put a small box on the chart to illustrate the current spread between bid and ask.

With labels at the Ask and Bid levels

Now I am trying to work out how to insert the actual values of the Ask and Bid prices into the label

Have never done anything like this before, so not even sure if I am on the right track.

Looking at the documentation, I thought I might have to set up an Alert, but don't know how to position these.

Hoping for some help and a nudge in the right direction.

The working code of original indicator is attached and my attempt to insert the price values are below (not sure if  could put two code examples in one post)

I thought perhaps I could insert a variable into the label, but although compiling, obviously it is not working

Any help would be greatly appreciated

Perhaps there is a simple example somehwere that I could look at (Documentation, CodeBase??)....

//+------------------------------------------------------------------+
//|                                                                  |
//|                 Copyright © 1999-2008, MetaQuotes Software Corp. |
//|                                         http://www.metaquotes.ru |
//+------------------------------------------------------------------+

//---- indicator settings

#property  indicator_chart_window
#property  indicator_buffers 0
//----

//---- indicator parameters

extern string          help                     = "bars forward positions Level Line";
extern double          bars_forward             = 1;      //Time[0] + numberOfSecondsIntoTheFuture is Time[0]+q*Period()*60; // q bars into the future
extern double          BoxLength                = 3;      // sets length of Level Line
extern string          label_adjust_help        = "For Forex *10E-5";
extern double          LabelAdjHorizontal       = 2;      // positions label above or under shaded box depicting spread   
extern double          AskLabelAdjUp            = 0.3;    //
extern double          BidLabelAdjDown          = 0;      //
extern int             font_size                = 8;      // sets font size
extern string          help1                    = "set to true to show box";
extern bool            ShowBox                  = true;   // set true to display box
extern bool            ShowLabel                = true;   // set true to display label
double                 AskText,BidText;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
   {
   IndicatorShortName("Bid-Ask Spread");
   
   
   //---- initialization done
   return(0);
   }    
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
   {
    ObjectDelete("Box");
    ObjectDelete("Box_label");
    ObjectDelete("Box_label2");         
   return(0);
   }
//+------------------------------------------------------------------+
//| Other timeframe line levels                                           |
//+------------------------------------------------------------------+
int start()  
 {
 AskText=Ask;
 BidText=Bid;
//-
//-----Draw Box Rectangle");
//--
       if (ShowBox) // draw if true
       {
       if (ObjectFind   ("Box") == -1) 
           ObjectCreate ("Box", OBJ_RECTANGLE, 0, 0, 0, 0, 0);
         
           ObjectSet    ("Box", OBJPROP_TIME1,       Time[0] +             bars_forward * 60 * Period() );
           ObjectSet    ("Box", OBJPROP_TIME2,       Time[0] + BoxLength * bars_forward * 60 * Period() );
      
           ObjectSet    ("Box", OBJPROP_PRICE1,      Ask );
           ObjectSet    ("Box", OBJPROP_PRICE2,      Bid ); 
      
           ObjectSet    ("Box", OBJPROP_RAY,         false);
           ObjectSet    ("Box", OBJPROP_COLOR,       PeachPuff);
           ObjectSet    ("Box", OBJPROP_WIDTH,       3);
       }
  //-- Draw Box Label -----------------------------------------      
       if (ShowBox) // draw if true
       {
       if (ObjectFind   ("Box_label") == -1) 
           ObjectCreate ("Box_label",OBJ_TEXT, 0, 0, 0);
      
           ObjectSet    ("Box_label", OBJPROP_TIME1, Time[0] + LabelAdjHorizontal *bars_forward * 60 * Period() );
           ObjectSet    ("Box_label", OBJPROP_PRICE1, (Ask+AskLabelAdjUp) );

           ObjectSet    ("Box_label", OBJPROP_COLOR, Black);
           ObjectSetText("Box_label", AskText, font_size, "Arial", Black); 
       }
       
  //-- Draw Box Label2 -----------------------------------------      
       if (ShowBox) // draw if true
       {
       if (ObjectFind   ("Box_label2") == -1) 
           ObjectCreate ("Box_label2", OBJ_TEXT, 0, 0, 0);
      
           ObjectSet    ("Box_label2", OBJPROP_TIME1, Time[0] + LabelAdjHorizontal *bars_forward * 60 * Period() );
           ObjectSet    ("Box_label2", OBJPROP_PRICE1,( Bid-BidLabelAdjDown) );

           ObjectSet    ("Box_label2", OBJPROP_COLOR, Black);
           ObjectSetText("Box_label2", BidText, font_size, "Arial", Black); 
       }     
       
   //----
   return(0);
   }
//+------------------------------------------------------------------+
 
pullend:

Hello Forum, with help I previously got an indicator to work which simply put a small box on the chart to illustrate the current spread between bid and ask.

With labels at the Ask and Bid levels

Now I am trying to work out how to insert the actual values of the Ask and Bid prices into the label

Have never done anything like this before, so not even sure if I am on the right track.

Looking at the documentation, I thought I might have to set up an Alert, but don't know how to position these.

Hoping for some help and a nudge in the right direction.

The working code of original indicator is attached and my attempt to insert the price values are below (not sure if  could put two code examples in one post)

I thought perhaps I could insert a variable into the label, but although compiling, obviously it is not working

You aren't creating a Label Object you are creating a Text Object,  the distinction is important. A label is positioned using pixel values,  text is positioned using datetime and price.

Look at the documentation and you will find that  ObjectSetText() takes a string as it's second parameter,  you are passing BidText or AskText and these are both doubles,  are you expecting the function to cast these values to strings ?  it might happen,  you will have to test it.  Why not just pass a string though ?  use DoubleToString(price,  Digits) to convert your Ask and Bid prices to strings . . .

 

If you want to know what is happening run your code and check the Objects on the chart,  Ctrl + B,   from here you will see if your Object has been created,  if it has you can check it's value and its position, then you will know if it is correct or not,  if it's wrong you will have some idea of where it is and you should then be in a much better position to fix your code.

 
RaptorUK:

You aren't creating a Label Object you are creating a Text Object,  the distinction is important. A label is positioned using pixel values,  text is positioned using datetime and price.

Look at the documentation and you will find that  ObjectSetText() takes a string as it's second parameter,  you are passing BidText or AskText and these are both doubles,  are you expecting the function to cast these values to strings ?  it might happen,  you will have to test it.  Why not just pass a string though ?  use DoubleToString(price,  Digits) to convert your Ask and Bid prices to strings . . .

 

If you want to know what is happening run your code and check the Objects on the chart,  Ctrl + B,   from here you will see if your Object has been created,  if it has you can check it's value and its position, then you will know if it is correct or not,  if it's wrong you will have some idea of where it is and you should then be in a much better position to fix your code.


Thanks Raptor ......AGAIN!!

I wasn't familiar with DoubleToStr, but now am, and it delivers exactly what I wanted.

Working code attached in case others interested:

//+------------------------------------------------------------------+
//|                                                                  |
//|                 Copyright © 1999-2008, MetaQuotes Software Corp. |
//|                                         http://www.metaquotes.ru |
//+------------------------------------------------------------------+

//---- indicator settings

#property  indicator_chart_window
#property  indicator_buffers 0
//----

//---- indicator parameters

extern string          help                     = "bars forward positions Level Line";
extern double          bars_forward             = 1;      //Time[0] + numberOfSecondsIntoTheFuture is Time[0]+q*Period()*60; // q bars into the future
extern double          BoxLength                = 3;      // sets length of Level Line
extern string          label_adjust_help        = "For Forex *10E-5";
extern double          LabelAdjHorizontal       = 2;      // positions label above or under shaded box depicting spread   
extern double          AskLabelAdjUp            = 0.3;    //
extern double          BidLabelAdjDown          = 0;      //
extern int             font_size                = 8;      // sets font size
extern string          help1                    = "set to true to show box";
extern bool            ShowBox                  = true;   // set true to display box
extern bool            ShowLabel                = true;   // set true to display label
double                 AskText,BidText;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
   {
   IndicatorShortName("Bid-Ask Spread");
   
   
   //---- initialization done
   return(0);
   }    
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
   {
    ObjectDelete("Box");
    ObjectDelete("Box_label");
    ObjectDelete("Box_label2");         
   return(0);
   }
//+------------------------------------------------------------------+
//| Other timeframe line levels                                           |
//+------------------------------------------------------------------+
int start()
       
       {
       AskText=Ask;
       BidText=Bid;
//-
//-----Draw Box Rectangle");
//--
       if (ShowBox) // draw if true
       {
       if (ObjectFind   ("Box") == -1) 
           ObjectCreate ("Box", OBJ_RECTANGLE, 0, 0, 0, 0, 0);
         
           ObjectSet    ("Box", OBJPROP_TIME1,       Time[0] +             bars_forward * 60 * Period() );
           ObjectSet    ("Box", OBJPROP_TIME2,       Time[0] + BoxLength * bars_forward * 60 * Period() );
      
           ObjectSet    ("Box", OBJPROP_PRICE1,      Ask );
           ObjectSet    ("Box", OBJPROP_PRICE2,      Bid );
      
           ObjectSet    ("Box", OBJPROP_RAY,         false);
           ObjectSet    ("Box", OBJPROP_COLOR,       PeachPuff);
           ObjectSet    ("Box", OBJPROP_WIDTH,       3);
       }
  //-- Draw Box Label -----------------------------------------      
       if (ShowBox) // draw if true
       {
       if (ObjectFind   ("Box_label") == -1) 
           ObjectCreate ("Box_label", OBJ_TEXT, 0, 0, 0);
      
           ObjectSet    ("Box_label", OBJPROP_TIME1, Time[0] + LabelAdjHorizontal *bars_forward * 60 * Period() );
           ObjectSet    ("Box_label", OBJPROP_PRICE1, (Ask+AskLabelAdjUp) );

           ObjectSet    ("Box_label", OBJPROP_COLOR, Black);
           ObjectSetText("Box_label", DoubleToStr(AskText,4), font_size, "Arial", Black); 
       }
       
  //-- Draw Box Label2 -----------------------------------------      
       if (ShowBox) // draw if true
       {
       if (ObjectFind   ("Box_label2") == -1) 
           ObjectCreate ("Box_label2", OBJ_TEXT, 0, 0, 0);
      
           ObjectSet    ("Box_label2", OBJPROP_TIME1, Time[0] + LabelAdjHorizontal *bars_forward * 60 * Period() );
           ObjectSet    ("Box_label2", OBJPROP_PRICE1,( Bid-BidLabelAdjDown) );

           ObjectSet    ("Box_label2", OBJPROP_COLOR, Black);
           ObjectSetText("Box_label2", DoubleToStr(BidText,4), font_size, "Arial", Black); 
       }     
       
  
   //----
   return(0);
   }
//+------------------------------------------------------------------+
Reason: