Can someone help me w/ this code. It doesn't quite draw trend lines right. Or if anyone has a better way, please let me know.
Inserted Code
#property indicator_chart_window
#property indicator_buffers 2
extern int varExtDepth=12;
extern int varExtDeviation=5;
extern int varExtBackstep=3;
double ceiling[];
double floor[];
int init()
{
//---- indicators
IndicatorBuffers(2);
IndicatorDigits(Digits);
//---- drawing settings
//---- indicator buffers mapping
SetIndexBuffer(0,ceiling);
SetIndexBuffer(1,floor);
SetIndexLabel(0,"Upper band") ;
SetIndexLabel(1,"Lower band") ;
SetIndexEmptyValue(0,0.0);
SetIndexEmptyValue(1,0.0);
ArraySetAsSeries(ceiling,true);
ArraySetAsSeries(floor,true);
//----
return(0);
}
int deinit()
{
//----
ObjectDelete("Ceiling"); ObjectDelete("Floor");
//----
return(0);
}
int start()
{
// int counted_bars=IndicatorCounted();
//----
int shift;
bool varNewBar=funcIsNewBar(Period());
if (varNewBar)
{
ObjectDelete("Ceiling"); ObjectDelete("Floor");
procDrawTrends();
for(shift=Bars-1; shift>=0; shift--)
{
ceiling[shift]=ObjectGetValueByShift("Ceiling",shift);
floor[shift]=ObjectGetValueByShift("Floor",shift);
}
}
return(0);
}
//+------------------------------------------------------------------+
void procDrawTrends()
{
int ZigZagHighCount=0, ZigZagLowCount=0, ZigZagCount=0;
int ZigZagShift = 0;
double ZZHigh[2],ZZLow[2];
datetime ZZHTime[2],ZZLTime[2];
while(ZigZagHighCount < 3 && ZigZagLowCount < 3)
{
double ZigZag=iCustom(NULL,0,"ZigZag",varExtDepth,varExtDeviation,varExtBackstep,0,ZigZagShift);
if((ZigZag == High[ZigZagShift] || ZigZag == Low[ZigZagShift]) && ZigZagCount==0){ZigZagCount++; ZigZagShift++;}
else
{
if(ZigZag == High[ZigZagShift])
{
ZZHigh[ZigZagHighCount]=ZigZag;
ZZHTime[ZigZagHighCount]=iTime(NULL,0,ZigZagShift);
ZigZagHighCount++; ZigZagCount++;
}
//do reverse for short
if(ZigZag == Low[ZigZagShift])
{
ZZLow[ZigZagLowCount]=ZigZag;
ZZLTime[ZigZagHighCount]=iTime(NULL,0,ZigZagShift);
ZigZagLowCount++; ZigZagCount++;
}
}
ZigZagShift++;
}
ObjectCreate("Ceiling",OBJ_TREND,0,ZZHTime[1],ZZHigh[1],ZZHTime[0],ZZHigh[0]);
ObjectCreate("Floor",OBJ_TREND,0,ZZLTime[1],ZZLow[1],ZZLTime[0],ZZLow[0]);
ObjectSet("Ceiling",OBJPROP_STYLE,STYLE_SOLID);
ObjectSet("Floor",OBJPROP_STYLE,STYLE_SOLID);
ObjectSet("Ceiling",OBJPROP_WIDTH,2);
ObjectSet("Floor",OBJPROP_WIDTH,2);
ObjectSet("Ceiling",OBJPROP_COLOR,Blue);
ObjectSet("Floor",OBJPROP_COLOR,Red);
ObjectSet("Ceiling",OBJPROP_RAY,true);
ObjectSet("Floor",OBJPROP_RAY,true);
//----
string varMessage,n="\n";
varMessage=StringConcatenate("ZigZagCount: ",ZigZagCount,n,
"ZigZagHighCount: ",ZigZagHighCount,n,
"ZigZagLowCount: ",ZigZagLowCount,n,
"ZZHTime[0]: ",funcSetDateTime(ZZHTime[0]),n,
"ZZHigh[0]: ",ZZHigh[0],n,
"ZZHTime[1]: ",funcSetDateTime(ZZHTime[1]),n,
"ZZHigh[1]: ",ZZHigh[1],n,
"ZZLTime[0]: ",funcSetDateTime(ZZLTime[0]),n,
"ZZLow[0]: ",ZZLow[0],n,
"ZZLTime[1]: ",funcSetDateTime(ZZLTime[1]),n,
"ZZLow[1]: ",ZZLow[1]);
Comment(varMessage);
return(0);
}
string funcSetDateTime(datetime varTime)
{
datetime curtime=varTime;
int theday=TimeDay(curtime);
int themonth=TimeMonth(curtime);
int theyear=TimeYear(curtime);
int thehour=TimeHour(curtime);
int theminute=TimeMinute(curtime);
string thedate=StringConcatenate(themonth,"/",theday,"/",theyear," ",thehour,":",theminute);
return(thedate);
}
//+---------------------- support funcion(s) ------------------------+
bool funcIsNewBar(int timeFrame)
{
bool res=false;
// the array contains open time of the current (zero) bar
// for 7 (seven) timeframes
static datetime _sTime[7];
int i=6;
//Note: i below will be 6 or timeframe will be day.
switch (timeFrame)
{
case 1 : i=0; break;
case 5 : i=2; break;
case 15 : i=3; break;
case 30 : i=4; break;
case 60 : i=5; break;
case 240: break;
case 1440:break;
default: timeFrame = 1440;
}
//----
if (_sTime[i]==0 || _sTime[i]!=iTime(Symbol(),timeFrame,0))
{
_sTime[i] = iTime(Symbol(),timeFrame,0);
res=true;
}
//----
return(res);
}