i try this code below for testing to know the right future bar time over weekends, but there is bugs in my code . i have tried these
some days to look for where the problem is, but still can not find the cause of error . Please help if you have time ...
Thank you very much in advance.
//+------------------------------------------------------------------+//| |//| trying to check for future bar time that over weekends !!! |//+------------------------------------------------------------------+#property strict#property indicator_chart_window//-----------------------------------------------------------------------inputstring info = "better to test this on big TF (ex: H1,H4,D1)";
inputstring info2 = "please see 'Experts' tab ...";
inputint lookBackBars = 200;
inputint futureBarsCount = 40;
datetime curTime,futureTime,calcFutureTime;
int i,j,k,limit,cnt;
int ctf,curBar,futureBar;
string symb;
string sFutureTime,sCalcFutureTime;
//+------------------------------------------------------------------+//| Custom indicator initialization function |//+------------------------------------------------------------------+intOnInit() {
symb = Symbol();
ctf = Period();
//-----------------------------------if (lookBackBars>Bars) {
Print(__FUNCTION__, " ERROR : lookBackBars(",lookBackBars,") must be smaller than : ",Bars,".");
Alert("ERROR : lookBackBars(",lookBackBars,") must be smaller than : ",Bars,".");
return(INIT_FAILED);
} // if//---return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+//| Custom indicator iteration function |//+------------------------------------------------------------------+intOnCalculate(constint rates_total,
constint prev_calculated,
constdatetime &time[],
constdouble &open[],
constdouble &high[],
constdouble &low[],
constdouble &close[],
constlong &tick_volume[],
constlong &volume[],
constint &spread[]) {
limit = (int) MathMin(lookBackBars,Bars);
for (i=limit; i>0; i--) {
curTime = iTime(symb,ctf,i);
futureBar = i-futureBarsCount;
if (futureBar>=0) { // still history bar, for testing only ...
futureTime = iTime(symb,ctf,futureBar);
calcFutureTime = FuncValidateFutureTime(curTime,ctf,futureBarsCount);
//--- show errorif (futureTime != calcFutureTime) {
sFutureTime = TimeToStr(futureTime);
sCalcFutureTime = TimeToStr(calcFutureTime);
Print("Error at pair: ",symb," TF:",FuncPeriodToStr(ctf),", futureBarsCount:",futureBarsCount," ,barNr:",i," ,time:",curTime," ,futureTime=",sFutureTime," ,calcFutureTime=",sCalcFutureTime);
} // if
} // if
} // for//---return(rates_total);
}
//+--------------------------------------------------------------------------------------------------------+datetime FuncValidateFutureTime(datetime theBeginTime,int theTF,int futureBarAmounts) {
datetime retValue = 0;
double weeks = MathFloor((futureBarAmounts*theTF*60)/(5*24*3600));
datetime targetTime = theBeginTime+(futureBarAmounts*theTF*60);
int weekAmount = (int) weeks;
int targetDay = TimeDayOfWeek(targetTime);
//---if (weekAmount == 0) {
if (targetDay==0) { // sunday
retValue = targetTime + 24*60*60;
} // ifelseif (targetDay==6) { // saturday
retValue = targetTime + 2*24*60*60;
} // else ifelse { // workdays
retValue = targetTime;
} // else
} // if \else { // over weekendsif (targetDay==0) { // sunday
retValue = targetTime + (24*60*60) + (weekAmount*2*24*3600);
} // ifelseif (targetDay==6) { // saturday
retValue = targetTime + (2*24*60*60) + (weekAmount*2*24*3600);
} // else ifelse { // workdays
retValue = targetTime + (weekAmount*2*24*3600);
} // else
} // else//--- Print("weeks=",weeks," ,weekAmounts = ",weekAmount," ,targetDay=",targetDay," ,retVal=",retValue);
//--- return(retValue);
}
//+-----------------------------------------------------------------------------------------------------+ string FuncPeriodToStr(int thePeriod) {
string retVal="";
switch(thePeriod) {
case1: retVal = "M1"; break;
case5: retVal = "M5"; break;
case15: retVal = "M15"; break;
case30: retVal = "M30"; break;
case60: retVal = "H1"; break;
case240: retVal = "H4"; break;
case1440: retVal = "D1"; break;
case10080: retVal = "W1"; break;
case43200: retVal = "MN1"; break;
}
return(retVal);
}
//+------------------------------------------------------------------+
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Hi,
i try this code below for testing to know the right future bar time over weekends, but there is bugs in my code . i have tried these some days to look for where the problem is, but still can not find the cause of error . Please help if you have time ...
Thank you very much in advance.