Need help with array problem and determining current bar.

 
HELP!!!

Hi all,
I hope somebody can help with the indicator that I'm working on. I'm using the ObjectCreate fuction to create a vertical line when certain parameters are met. I want to take it one step futher than I have it, but I don't know how. What I want to do isn't really about ObjectCreate. It's more about arrays, as you'll see...

First I'll give the logic code, then I'll explain what I want to happen. Also, I know all those iCustoms can be thinned down by putting them in variables. I will do that on the final draft....

// *** If the Slope is UP, Juice is Green, and we get Green crossing up on iTrend  
 if((iCustom(NULL,0,"Shi_Channel",2,0) > 0 && iCustom(NULL,0,"Juice",2,0) >= 0 && 
    ( // iTrend Green crossing on current bar or previous bar
    (iCustom(NULL,0,"i_Trend",0,2) < iCustom(NULL,0,"i_Trend",1,2) && iCustom(NULL,0,"i_Trend",0,1) > iCustom(NULL,0,"i_Trend",1,1) && iCustom(NULL,0,"Juice",2,1) <= 0) ||
    (iCustom(NULL,0,"i_Trend",0,1) < iCustom(NULL,0,"i_Trend",1,1) && iCustom(NULL,0,"i_Trend",0,0) > iCustom(NULL,0,"i_Trend",1,0))
    )
    )
    ||
// *** If the Slope is DOWN, Juice is Green, and we get Red crossing down on iTrend
    (iCustom(NULL,0,"Shi_Channel",2,0) < 0 && iCustom(NULL,0,"Juice",2,0) >= 0 &&
    ( // iTrend Red crossing on current bar or previous bar
    (iCustom(NULL,0,"i_Trend",1,2) < iCustom(NULL,0,"i_Trend",0,2) && iCustom(NULL,0,"i_Trend",1,1) > iCustom(NULL,0,"i_Trend",0,1) && iCustom(NULL,0,"Juice",2,1) <= 0) ||
    (iCustom(NULL,0,"i_Trend",1,1) < iCustom(NULL,0,"i_Trend",0,1) && iCustom(NULL,0,"i_Trend",1,0) > iCustom(NULL,0,"i_Trend",0,0))
    )
    )
    )
    
  {
     ObjectDelete("VL0");
     ObjectCreate("VL0",OBJ_VLINE,0,CurTime(),0); //(CurTime()-(Period()*60*2))
      ObjectSet("VL0",OBJPROP_COLOR,Yellow);
    ObjectSet("VL0",OBJPROP_WIDTH,1); 
    ObjectSet("VL0",OBJPROP_STYLE,STYLE_SOLID);
//      ObjectCreate(TimeToStr(CurTime()),OBJ_VLINE,0,CurTime(),0); //(CurTime()-(Period()*60*2))
//       ObjectSet(TimeToStr(CurTime()),OBJPROP_COLOR,Yellow);
//     ObjectSet(TimeToStr(CurTime()),OBJPROP_WIDTH,1); 
//     ObjectSet(TimeToStr(CurTime()),OBJPROP_STYLE,STYLE_SOLID);
  }



Right now it updates on every tick, and if the conditions are met, it deletes the old Vertical Line and recreates a new one. This all happens on the current bar so the user doesn't see anything changing. So far, so good.

What I want to have happen is that it only deletes the Line if the line is on the current bar. If the V Line is on a previous bar, leave it be. Here's what I mean...
Example - M5 Chart (8:58:34 - parameters met - Draw Line, 8:59:12 - parameters met - Delete previous Line, Draw Line, 9:02:17 - parameters met - Leave old Line because it is now on previous bar, Draw Line on current bar.)

I believe this would require that each line be given a unique name that gets stored in an array so that the name could be called into the ObjectDelete function, but I'm not positive how to do that, and then I definately don't know how to write the code to determine if the last line drawn is on the previous or current bar.

I want to be able to look back on a chart and see all the bars where the parameters were met. I was able to do this by using TimeToStr(CurTime()) as the unique name of the Line and never deleting anything, but what would happen is that every tick on the current bar that the parameters were met it would stack another Line on top. The user wouldn't see anything, but if you look at Objects on the chart, you'd see multiple Veritical Lines for each bar. That's why I want it to delete the old line if it is on the current bar, so that at the end of the day, there will only be 1 Vertical Line drawn on each bar that meets the parameters.

I'm sure I've repeated myself, but I hope what I wrote makes sense. I could really use some help. Thanks.

Keris

 
Hi!

I'm not sure if I understand the problem correctly, but here are some ideas anyway.

Re your last paragraph. Use TimeToStr(Time[x]) as the name for the object as that doesn't change. That way you will elegantly be able to always access the object that belongs to a bar. Even if the bar moved back into the past, it's Time[x] will be constant.

Another way could be to do vertical bars by using a histogram indicator. I'm not sure how histograms work in chart_window indicators but there must be a way. The ichimoku charts sample has a histogram to paint one of the channels. From checking the source I had the impression that it's start point is in the DRAW_HISTOGRAM type buffer and reaches up to the next chart buffer (but as said, I've never done that so far). That way you could avoid objects alltogether, just let the histogram reach from zero to 100 for the bars where you need it and from 0 to 0 where you don't need them. MT would scroll them with the chart and the user would not see tons of objects.

Hope that helps.


Markus
 
Or, on second thought, you may want to consider a different alternative. Instead of vertical bars, mark the points where your condition is met with symbols

   SetIndexStyle(0,DRAW_ARROW);
   SetIndexBuffer(0,Buffer1);
   SetIndexArrow(0,159);
   SetIndexEmptyValue(0, 0.0);




Then just where the condition fits, use

Buffer1[x]= Low[x] - 3*Point;

and where it doesn't fit say

Buffer1[x]= 0;



Markus

 

Re your last paragraph. Use TimeToStr(Time[x]) as the name for the object as that doesn't change. That way you will elegantly be able to always access the object that belongs to a bar. Even if the bar moved back into the past, it's Time[x] will be constant.


Markus,
Thanks for the input. I want to use the time as the unique name similar to the way you described above and I know how to delete an object calling that name, but what I don't know exactly how to do is to place that name into an array/variable to be called later or how to determine if that object is on the current bar or previous bar.

I want to use Object - Vertical Line, because I like the way it draws the line through everything on the window, i.e. price chart and all indicators.

This is what I'm trying to do...

If (all my parameters are met)  {
  Create a unique name based on current time and store it in a variable or array
  Delete previous line using ObjectDelete("UniqueName") [b]only if the previous line is on the current bar.[/b]
  Create a new line using CreateObject("UniqueName",OBJ_VLINE,...)
}



I know what I want, just don't know how to do it. I know how to use the Object fuctions, but I'm not sure how to store the names in an array and properly call them. Do I need to use a For loop when populating the array? Also, how do i determine if an object, or anything, is on the current bar or previous bar?

Thanks to anybody that may be able to help.

 
Keris,

I think you don't need an array. You can just go through all bars, do a
ObjectFind(TimeToString(Time[barcount])).

Essentially this says: Find the object that has a name matching this bar number (barcount).

If an object under that name exists, the bar has a line, if there's no such object there's no bar. And if barcount is greater than 0 it's a past bar.


Hope this helps



Markus
 

I think you don't need an array. You can just go through all bars, do a
ObjectFind(TimeToString(Time[barcount])).

Essentially this says: Find the object that has a name matching this bar number (barcount).

If an object under that name exists, the bar has a line, if there's no such object there's no bar. And if barcount is greater than 0 it's a past bar.


Markus,
Thank you for all of your help. It really got me thinking in the right direction. This is what I finally ended up with and it seems to work perfectly.

    for(int i=1; i<Bars; i++)
    {
     ObjectDelete(TimeToStr(Time[0]));
     ObjectCreate(TimeToStr(Time[0]),OBJ_VLINE,0,CurTime()-(Period()*60*0),0); //(CurTime()-(Period()*60*2))
	      ObjectSet(TimeToStr(Time[0]),OBJPROP_COLOR,Yellow);
		   ObjectSet(TimeToStr(Time[0]),OBJPROP_WIDTH,1); 
		   ObjectSet(TimeToStr(Time[0]),OBJPROP_STYLE,STYLE_SOLID);
		   ObjectSet(TimeToStr(Time[i]),OBJPROP_COLOR,Red);
   }



I was using TimeToStr(CurTime()) and every minute that a new bar would form it would have a new name. Couldn't keep track of them all. With Time[0], all Lines created on the same bar end up with the same name, TimeToStr(Time[0]), so they're easy to delete. When a new bar comes around, TimeToStr(Time[0]) gets updated and the one on the old bar doesn't get deleted since it's name is different.

I know you knew all that, but all the commentary in the previous paragraph was for anyone else reading the thread that might want to know exactly why it works.

Thanks again for all your help.

Keris

 
Glad to have been of help.

Good luck with the Shi-Channels.



Markus
Reason: