createobject at different bars

 

Hello,

I'm experimenting with a short code, just to learn how to place objects at different bars in a chart.

I just wanted to place a vertical line at each bar in the chart (later I'm gonna add conditions for where a vertical line should appear)

Now, when I place this "indicator" on a chart, it apparently creates many (not visible) objects with individual names (if you use the show/list objects function),

but they are all placed like on the 1970/01/01 so that there are no vertical lines from the first to the last bar in the chart as intended.

If anyone has an idea how to correct the progr. mistake, I'd be very thankful in advance.


So my (for the experts surely very simple) code is:


#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int shift;
int limit;
extern int Window;
extern color ColourUp=Blue,ColourDown=Orange;
extern int ScaleVertLineUp=5,ScaleVertLineDown=5;
extern int InputOBJPROP_STYLE=0;
extern string VertLineUp = "VertLineUp";
extern string VertLineDown = "VertLineDown";

int init()
{ limit=Bars;
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
//----
for(shift=limit; shift>=0; shift--)
{
double BarMarker = Time[shift];
string VertUp = VertLineUp + BarMarker;
string VertDown = VertLineDown + BarMarker;
ObjectCreate(VertUp,OBJ_VLINE,Window,shift,shift);ObjectSet(VertUp,OBJPROP_COLOR,ColourUp);


// ObjectSet(VertUp,OBJPROP_STYLE,0}; this line is skipped because a compiling error message

}


//----
return(0);
}
//+------------------------------------------------------------------+

 

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. int init(){ limit=Bars; }
    int start(){
       int counted_bars=IndicatorCounted();
       for(shift=limit; shift>=0; shift--) ...
    Why are you calling IndicatorCounted() when you're not using the value? Why are you reprocessing all bars (up to the original bar count?) Do it right.
    int start(){
       int counted_bars=IndicatorCounted();
    // if(counted_bars < LOOKBACK) counted_bars = LOOKBACK; // SMA(x) has look back of x
       for(shift=Bars - 1 - counted_bars; shift>=0; shift--) ... // Bars is not constant.

  3. ObjectCreate(VertUp,OBJ_VLINE,Window,shift,shift);
    RTFM. what is the fourth argument to ObjectCreate and what are you passing?
  4. What are Function return values ? How do I use them ? - MQL4 forum
 
//+------------------------------------------------------------------+
//|                                                     000shift.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int shift;
int limit;
extern int       Window;
extern color     ColourUp=Blue,ColourDown=Orange;
extern int       ScaleVertLineUp=5,ScaleVertLineDown=5;
extern int       InputOBJPROP_STYLE=0;
extern string VertLineUp  = "VertLineUp";
extern string VertLineDown  = "VertLineDown";    

int init()
  {  limit=Bars;
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
  
//--------------
int start(){
   int counted_bars=IndicatorCounted();
// if(counted_bars < LOOKBACK) counted_bars = LOOKBACK; // SMA(x) has look back of x
   for(shift=Bars - 1 - counted_bars; shift>=0; shift--)
  
    { 
     double BarMarker = Time[shift];                    
     string VertUp = VertLineUp + BarMarker;
     string VertDown = VertLineDown + BarMarker;
     ObjectCreate(VertUp,OBJ_VLINE,Window,shift,shift);ObjectSet(VertUp,OBJPROP_COLOR,ColourUp);
     ObjectSet(VertUp,OBJPROP_STYLE,0); 
    }
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
WHRoeder:

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Why are you calling IndicatorCounted() when you're not using the value? Why are you reprocessing all bars (up to the original bar count?) Do it right.
  3. RTFM. what is the fourth argument to ObjectCreate and what are you passing?
  4. What are Function return values ? How do I use them ? - MQL4 forum

Thanks a lot WH!

I integrated some of your programming lines.

But the thing I'm looking for, is a simple (initial) procedure, how to simply place a vertical Line (object) on every bar which is displayed. (later I'll use certain conditions where a vertical line should appear, so this is just a preparation)

the objects (vertical lines, I'm getting are numerous, but they seem to be all located on 1970/01/01 (probably the first available date in MT4), so my mistake may be that the positioning of the vertical lines by using the "shift" parameter in "createobject" is wrong? .. or should I use some kinda Time[shift] parameter there?

 
  1. Can't follow directions. What part of EDIT your post was not clear?
  2. Can't answer questions. What part of "what is the fourth argument" was unclear? Had you bothered to read the link and answer the question you would have the answers to your two questions.
 

btw, I solved one part of the issue

for example a line like : ObjectCreate("line",OBJ_VLINE,0,Time[0],0) puts a vertical line at the last bar correctly

but in the context below only 1 vertical line at the last bar appears (which is at shift+1 btw), so the other vertical lines from "Bars" down to/until "shift>=0"

are not there? how to solve this? At the present time, forex is closed,so MT is not receiving any ticks, but I think that "for" condition below should be executed anyway?!


for(shift=Bars; shift>=0; shift--)
{
double BarMarker = Time[shift+1];
string VertUp = VertLineUp + BarMarker;
string VertDown = VertLineDown + BarMarker;
ObjectCreate(VertUp,OBJ_VLINE,Window,Time[shift+1],0);ObjectSet(VertUp,OBJPROP_COLOR,ColourUp);

ObjectSet(VertUp,OBJPROP_STYLE,0);

} // unfortunately creates only 1 line at the last bar

 
fxnew:

btw, I solved one part of the issue

for example a line like : ObjectCreate("line",OBJ_VLINE,0,Time[0],0) puts a vertical line at the last bar correctly

but in the context below only 1 vertical line at the last bar appears (which is at shift+1 btw), so the other vertical lines from "Bars" down to/until "shift>=0"

are not there? how to solve this? At the present time, forex is closed,so MT is not receiving any ticks, but I think that "for" condition below should be executed anyway?!

<CODE REMOVED>

You were already asked politely . . .

Please edit your post . . . please use the SRC button to post code: How to use the SRC button.

 
for(shift=Bars; shift>=0; shift--)
  
   { 
     double BarMarker = Time[shift+1];                    
     string VertUp = VertLineUp + BarMarker;
     string VertDown = VertLineDown + BarMarker;
     ObjectCreate(VertUp,OBJ_VLINE,Window,Time[shift+1],0);ObjectSet(VertUp,OBJPROP_COLOR,ColourUp);
     ObjectSet(VertUp,OBJPROP_STYLE,0); 
   }
 

btw, I solved one part of the issue

for example a line like : ObjectCreate("line",OBJ_VLINE,0,Time[0],0) puts a vertical line at the last bar correctly

but in the code inserted only 1 vertical line at the last bar appears (which is at shift+1 btw), so the other vertical lines from "Bars" down to/until "shift>=0"

are not there? how to solve this? At the present time, forex is closed,so MT is not receiving any ticks, but I think that "for" condition below should be executed anyway?!
 
fxnew:

btw, I solved one part of the issue

for example a line like : ObjectCreate("line",OBJ_VLINE,0,Time[0],0) puts a vertical line at the last bar correctly

but in the code inserted only 1 vertical line at the last bar appears (which is at shift+1 btw), so the other vertical lines from "Bars" down to/until "shift>=0"

are not there? how to solve this? At the present time, forex is closed,so MT is not receiving any ticks, but I think that "for" condition below should be executed anyway?!
Please EDIT your post instead of creating a new one . . . you have been asked politely, is it too much to ask ?
 
fxnew:

btw, I solved one part of the issue

for example a line like : ObjectCreate("line",OBJ_VLINE,0,Time[0],0) puts a vertical line at the last bar correctly

but in the code inserted only 1 vertical line at the last bar appears (which is at shift+1 btw), so the other vertical lines from "Bars" down to/until "shift>=0"

are not there? how to solve this? At the present time, forex is closed,so MT is not receiving any ticks, but I think that "for" condition below should be executed anyway?!
How can you create multiple lines with the same Object name ?
Reason: