Box Trading indicator

Indicators coded by community members
Message
Author
Pr0verbs
Posts: 1
Joined: Sun Sep 23, 2007 1:39 am

Box Trading indicator

#1 Postby Pr0verbs » Sun Sep 23, 2007 1:44 am

Id like to request an indicator that draws a box that measures the high and low for a specific time frame that is user specified.

Example:
start 00:00 GMT
end 6:00GMT
draw a horizonal line on the high and low during 0-6GMT.

Is this posible?

Thanks for your time.

Here is the code I am using in metatrader 4.

extern int FromHour=3;
extern int FromMinute=45;
extern int ToHour=11;
extern int ToMinute=45;
extern int BarsFrom=50;
extern int BarsTo=0;
extern bool Live=true; // ! Not Tested
extern bool ShowHLofDay=true; // Show Highest/Lowest price of the day after box end time
extern bool DrawBoxLines=true;
extern bool ShowBoxDescriptions=true;
extern bool ShowHLDescriptions=true;
extern color BoxColor=DarkSlateGray;
extern color BoxLinesColor=DarkSlateGray;
extern color HLlinesColor=Red;

datetime BoxStartTime,BoxEndTime,HLstartTime,HLendTime;
int BoxIndex, HLday,HourDif,precision;
bool BoxStartIsOK,BoxEndIsOK,HLstartTimeOK,BoxLinesCreated,HLcreatedH,HLcreatedL;
double TopOfBox,BottomOfBox,HighOfDay,LowOfDay;

#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//
int HourNow,BarsCount;
datetime Now;
if(StringFind(Symbol(),"JPY",0)!=(-1)) precision=2; else precision=4;
BoxStartIsOK=false;
BoxEndIsOK=false;
BoxIndex=0;
for(int i=ObjectsTotal();i>=0;i--)
{
if(StringSubstr(ObjectName(i),0,8)=="srdc3box") ObjectDelete(ObjectName(i));
}
if(Period()>60) return(0);
//######################################################
for(i=BarsFrom;i>BarsTo;i--)
{
Now=Time[i];
/*if(TimeHour(Now)==FromHour&&TimeMinute(Now)==FromMinute&&!BoxStartIsOK)
{
BoxStartIsOK=true;
BoxEndIsOK=false;
}*/
if(TimeHour(Now)==ToHour&&TimeMinute(Now)==ToMinute)//&&!BoxEndIsOK&&BoxStartIsOK)
{
BoxEndTime=Now;
BarsCount=0;
if(FromHour<=ToHour) HourDif=ToHour-FromHour; else HourDif=24-(FromHour-ToHour);

for(int j=(i+(HourDif*(60/Period())));j>i;j--)
{
if(TimeDayOfWeek(Time[j])<=TimeDayOfWeek(Now)) BarsCount++;
}
BoxStartTime=Time[i+BarsCount];

TopOfBox=iHigh(Symbol(),Period(),iHighest(Symbol(),Period(),MODE_HIGH,BarsCount,i+1));
BottomOfBox=iLow(Symbol(),Period(),iLowest(Symbol(),Period(),MODE_LOW,BarsCount,i+1));
ObjectCreate("srdc3box"+BoxIndex,OBJ_RECTANGLE,0,BoxStartTime,TopOfBox,BoxEndTime,BottomOfBox);
ObjectSet("srdc3box"+BoxIndex,OBJPROP_COLOR,BoxColor);
if(ShowBoxDescriptions) ObjectSetText("srdc3box"+BoxIndex,"H"+DoubleToStr(TopOfBox,precision)+" L"+DoubleToStr(BottomOfBox,precision)+" "+DoubleToStr((TopOfBox-BottomOfBox)/Point,0)+"pips",10,"Times New Roman",Red);
BoxStartIsOK=false;
BoxEndIsOK=false;
BoxIndex++;
HLstartTime=Now;
HLday=TimeDay(Now);
HLstartTimeOK=true;
HLcreatedH=false;
HLcreatedL=false;
BoxLinesCreated=false;
}
//######################################################
if(ShowHLofDay&&HLstartTimeOK)
{
BarsCount=0;
for(j=i;j>0;j--)
{
if(TimeDayOfWeek(Time[j])<TimeDayOfWeek(Now)||TimeHour(Time[j])==FromHour) break;
BarsCount++;
}
BarsCount--;
HLcreatedH=false;
HLcreatedL=false;
//HLstartTimeOK=false;
HLendTime=Time[i-BarsCount];
HourNow=TimeHour(Time[i+1])+1;
HighOfDay=iHigh(Symbol(),Period(),iHighest(Symbol(),Period(),MODE_HIGH,BarsCount,i-BarsCount));
LowOfDay=iLow(Symbol(),Period(),iLowest(Symbol(),Period(),MODE_LOW,BarsCount,i-BarsCount));
if(HighOfDay>TopOfBox&&!HLcreatedH)
{
ObjectCreate("srdc3boxHoD"+BoxIndex,OBJ_TREND,0,HLstartTime,HighOfDay,HLendTime,HighOfDay);
ObjectSet("srdc3boxHoD"+BoxIndex,OBJPROP_COLOR,HLlinesColor);
if(ShowHLDescriptions) ObjectSetText("srdc3boxHoD"+BoxIndex,DoubleToStr(HighOfDay,precision)+" "+DoubleToStr((HighOfDay-TopOfBox)/Point,0)+"pips",10,"Times New Roman",Red);
ObjectSet("srdc3boxHoD"+BoxIndex,OBJPROP_RAY,0);
//HLstartTimeOK=false;
HLcreatedH=true;
}
if(LowOfDay<BottomOfBox&&!HLcreatedL)
{
ObjectCreate("srdc3boxLoD"+BoxIndex,OBJ_TREND,0,HLstartTime,LowOfDay,HLendTime,LowOfDay);
ObjectSet("srdc3boxLoD"+BoxIndex,OBJPROP_COLOR,HLlinesColor);
if(ShowHLDescriptions) ObjectSetText("srdc3boxLoD"+BoxIndex,DoubleToStr(LowOfDay,precision)+" "+DoubleToStr((BottomOfBox-LowOfDay)/Point,0)+"pips",10,"Times New Roman",Red);
ObjectSet("srdc3boxLoD"+BoxIndex,OBJPROP_RAY,0);
//HLstartTimeOK=false;
HLcreatedL=true;
}

}
//######################################################
if(DrawBoxLines&&HLstartTimeOK)
{
BarsCount=0;
for(j=i;j>0;j--)
{
if(TimeDayOfWeek(Time[j])<TimeDayOfWeek(Now)||TimeHour(Time[j])==FromHour) break;
BarsCount++;
}
BarsCount--;
HLendTime=Time[i-BarsCount];
ObjectCreate("srdc3boxH"+BoxIndex,OBJ_TREND,0,HLstartTime,TopOfBox,HLendTime,TopOfBox);
ObjectSet("srdc3boxH"+BoxIndex,OBJPROP_COLOR,BoxLinesColor);
ObjectSet("srdc3boxH"+BoxIndex,OBJPROP_RAY,0);
ObjectCreate("srdc3boxL"+BoxIndex,OBJ_TREND,0,HLstartTime,BottomOfBox,HLendTime,BottomOfBox);
ObjectSet("srdc3boxL"+BoxIndex,OBJPROP_COLOR,BoxLinesColor);
ObjectSet("srdc3boxL"+BoxIndex,OBJPROP_RAY,0);
BoxLinesCreated=true;
}
else
{
BoxLinesCreated=false;
}
//######################################################
HLstartTimeOK=false;


}
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
for(int i=ObjectsTotal();i>=0;i--)
{
if(StringSubstr(ObjectName(i),0,8)=="srdc3box") ObjectDelete(ObjectName(i));
}
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
datetime Now;
int BarsCount;
//----
if(Period()>60) return(0);
if(Live==false) return(0);
Now=Time[BarsTo];


if(TimeHour(Now)==ToHour&&TimeMinute(Now)==ToMinute)//&&!BoxEndIsOK&&BoxStartIsOK)
{
BoxEndTime=Now;
BarsCount=0;
if(FromHour<=ToHour) HourDif=ToHour-FromHour; else HourDif=24-(FromHour-ToHour);

for(int j=(BarsTo+(HourDif*(60/Period())));j>BarsTo;j--)
{
if(TimeDayOfWeek(Time[j])<=TimeDayOfWeek(Now)) BarsCount++;
}
BoxStartTime=Time[BarsTo+BarsCount];
if(FromHour<=ToHour) HourDif=ToHour-FromHour; else HourDif=24-(FromHour-ToHour);
TopOfBox=iHigh(Symbol(),Period(),iHighest(Symbol(),Period(),MODE_HIGH,BarsCount,1));
BottomOfBox=iLow(Symbol(),Period(),iLowest(Symbol(),Period(),MODE_LOW,BarsCount,1));
ObjectCreate("srdc3box"+BoxIndex,OBJ_RECTANGLE,0,BoxStartTime,TopOfBox,BoxEndTime,BottomOfBox);
ObjectSet("srdc3box"+BoxIndex,OBJPROP_COLOR,BoxColor);
if(ShowBoxDescriptions) ObjectSetText("srdc3box"+BoxIndex,"H"+DoubleToStr(TopOfBox,precision)+" L"+DoubleToStr(BottomOfBox,precision)+" "+DoubleToStr((TopOfBox-BottomOfBox)/Point,0)+"pips",10,"Times New Roman",Red);
BoxStartIsOK=0;
BoxEndIsOK=0;
BoxEndTime=TimeCurrent();
BoxIndex++;
HLstartTime=Now;
HLday=TimeDay(Now);
HLstartTimeOK=true;
}
//######################################################
if(DrawBoxLines&&HLstartTimeOK)
{
if(TimeDayOfWeek(BoxEndTime)>TimeDayOfWeek(Now)||TimeHour(Now)==FromHour)
//if(HLday!=TimeDay(Now)||BoxStartIsOK)
{
HLstartTimeOK=false;
BoxLinesCreated=false;
}
if(HLstartTimeOK)
{
if(!BoxLinesCreated)
{
ObjectCreate("srdc3boxH"+BoxIndex,OBJ_TREND,0,HLstartTime,TopOfBox,Now,TopOfBox);
ObjectSet("srdc3boxH"+BoxIndex,OBJPROP_COLOR,BoxLinesColor);
ObjectSet("srdc3boxH"+BoxIndex,OBJPROP_RAY,0);
ObjectCreate("srdc3boxL"+BoxIndex,OBJ_TREND,0,HLstartTime,BottomOfBox,Now,BottomOfBox);
ObjectSet("srdc3boxL"+BoxIndex,OBJPROP_COLOR,BoxLinesColor);
ObjectSet("srdc3boxL"+BoxIndex,OBJPROP_RAY,0);
BoxLinesCreated=true;
}
else
{
ObjectSet("srdc3boxH"+BoxIndex,OBJPROP_TIME2,Now);
ObjectSet("srdc3boxL"+BoxIndex,OBJPROP_TIME2,Now);
}
}
}
//######################################################
if(ShowHLofDay&&HLstartTimeOK)
{
if(TimeDayOfWeek(BoxEndTime)>TimeDayOfWeek(Now)||TimeHour(Now)==FromHour)
//if(HLday!=TimeDay(Now)||BoxStartIsOK)
{
HLstartTimeOK=false;
HLcreatedH=false;
HLcreatedL=false;
}
if(HLstartTimeOK)
{
HighOfDay=iHigh(Symbol(),Period(),iHighest(Symbol(),Period(),MODE_HIGH,MathAbs(FromHour-ToHour)*(60/Period()),1));
LowOfDay=iLow(Symbol(),Period(),iLowest(Symbol(),Period(),MODE_LOW,MathAbs(FromHour-ToHour)*(60/Period()),1));
if(!HLcreatedH)
{
if(HighOfDay>TopOfBox)
{
ObjectCreate("srdc3boxHoD"+BoxIndex,OBJ_TREND,0,HLstartTime,HighOfDay,Now,HighOfDay);
ObjectSet("srdc3boxHoD"+BoxIndex,OBJPROP_COLOR,HLlinesColor);
if(ShowHLDescriptions) ObjectSetText("srdc3boxHoD"+BoxIndex,DoubleToStr(HighOfDay,precision)+" "+DoubleToStr((HighOfDay-TopOfBox)/Point,0)+"pips",10,"Times New Roman",Red);
ObjectSet("srdc3boxHoD"+BoxIndex,OBJPROP_RAY,0);
HLcreatedH=true;
}
}
else
{
if(HighOfDay>ObjectGet("srdc3boxHoD"+BoxIndex,OBJPROP_PRICE1))
{
if(ShowHLDescriptions) ObjectSetText("srdc3boxHoD"+BoxIndex,DoubleToStr(HighOfDay,precision)+" "+DoubleToStr((HighOfDay-TopOfBox)/Point,0)+"pips",10,"Times New Roman",Red);
ObjectSet("srdc3boxHoD"+BoxIndex,OBJPROP_PRICE1,HighOfDay);
ObjectSet("srdc3boxHoD"+BoxIndex,OBJPROP_TIME2,Now);
ObjectSet("srdc3boxHoD"+BoxIndex,OBJPROP_PRICE2,HighOfDay);
}
}
if(!HLcreatedL)
{
if(LowOfDay<BottomOfBox)
{
ObjectCreate("srdc3boxLoD"+BoxIndex,OBJ_TREND,0,HLstartTime,LowOfDay,Now,LowOfDay);
ObjectSet("srdc3boxLoD"+BoxIndex,OBJPROP_COLOR,HLlinesColor);
if(ShowHLDescriptions) ObjectSetText("srdc3boxLoD"+BoxIndex,DoubleToStr(LowOfDay,precision)+" "+DoubleToStr((BottomOfBox-LowOfDay)/Point,0)+"pips",10,"Times New Roman",Red);
ObjectSet("srdc3boxLoD"+BoxIndex,OBJPROP_RAY,0);
HLcreatedL=true;
}
}
else
{
if(LowOfDay<ObjectGet("srdc3boxLoD"+BoxIndex,OBJPROP_PRICE1))
{
if(ShowHLDescriptions) ObjectSetText("srdc3boxLoD"+BoxIndex,DoubleToStr(LowOfDay,precision)+" "+DoubleToStr((BottomOfBox-LowOfDay)/Point,0)+"pips",10,"Times New Roman",Red);
ObjectSet("srdc3boxLoD"+BoxIndex,OBJPROP_PRICE1,LowOfDay);
ObjectSet("srdc3boxLoD"+BoxIndex,OBJPROP_TIME2,Now);
ObjectSet("srdc3boxLoD"+BoxIndex,OBJPROP_PRICE1,LowOfDay);
}
}
}
}
//----
return(0);
}
//+------------------------------------------------------------------+

tenshi
Posts: 6
Joined: Mon Sep 01, 2008 1:57 am

#2 Postby tenshi » Tue Sep 30, 2008 1:59 am

It would be great if this were made? For example, a box signifying London opening hour would be really useful in back testing.


Return to “Indicators”

Who is online

Users browsing this forum: No registered users and 19 guests