From date and time (of the previous day) to date and time of the next day

Dear friends, I need to calculate the range from low to high during the period from 18:00 (when the new session [ETH] starts after a one-hour pause in NQ futures) until 16:55 of the following day (when the after market of the next session ends). I have managed to do it with the code I provide, but I have to change the date each time. (the time remains constant). Can this be done without needing parameters and be done automatically every time?

startDate =   ParamDate("Start Date","2025-06-24"); 
startTime =   ParamTime("Start Time","18:00:00",0); 
endDate =     ParamDate("End Date","2025-06-25"); 
endTime =     ParamTime("End Time","16:55:00",0); 

startPoint=IIf(TimeNum()>=startTime AND DateNum()==startDate,1,0); 
startPoint=(startPoint-Ref(startPoint,-1))==1; 
endPoint=IIf(TimeNum()>=endTime AND DateNum()==endDate,1,0); 
endPoint=(endPoint-Ref(endPoint,-1))==1; 

timeWindow=Flip(startPoint,endPoint); 
myHigh=ValueWhen(timeWindow,HighestSince(startPoint,High)); 
myLow=ValueWhen(timeWindow,LowestSince(startPoint,Low)); 

SetBarsRequired(1500,0);
SetChartOptions(0, chartShowDates); 
Plot(C,"",colorGrey40,64); 

Plot(startPoint,"",ColorRGB(0,0,200), styleArea|styleOwnScale,0,1,0,-1); 
Plot(endPoint,"",ColorRGB(200,0,0), styleArea|styleOwnScale,0,1,0,-1); 
Plot(myHigh,"\nmyHigh",colorAqua,styleLine); 
Plot(myLow,"\nmyLow",colorOrange,styleLine);
Title = EncodeColor(colorAqua)+ "myHigh = " + WriteVal(LastValue(myHigh),1.2)+ EncodeColor(colorOrange)+ "\nmyLow = " + WriteVal(LastValue(myLow),1.2) + EncodeColor(colorYellow) +"\nrange H-L = " + WriteVal(LastValue(myHigh-myLow),1.2);

Try incorporating something similar to this untested snippet into your code:

tn = TimeNum();
bi = BarIndex();

isEthStart = tn >= startTime AND
             Ref(tn,-1) < startTime;

isEthEnd = tn <= endTime AND
           Ref(tn,1) > endTime;

isETH = Flip(isEthStart, (isEthEnd OR bi == LastValue(bi));

ethHigh = ValueWhen(isETH, HighestSince(isEthStart, H);
ethLow = ValueWhen(isETH, LowestSince(isEthStart, L);

All of the variables above are arrays, so use them accordingly. You'll need to change your plot logic, and will probably want to use SelectedValue() instead of LastValue().

1 Like

Yes, dear @mradtke, I think it's all right, it just needed a few small corrections. I will see how it goes with today and I will update you. I want to thank you very much for your good spirit and your time. I also give the complete code.

tn = TimeNum();
bi = BarIndex();

startTime = 180000;
endTime = 165500;

isEthStart = tn >= startTime AND
             Ref(tn,-1) < startTime;

isEthEnd = tn <= endTime AND
           Ref(tn,1) > endTime;

isETH = Flip(isEthStart, (isEthEnd OR bi == LastValue(bi)));
ethHigh = ValueWhen(isETH, HighestSince(isEthStart, H));
ethLow = ValueWhen(isETH, LowestSince(isEthStart, L));

SetBarsRequired(1500,0);
SetChartOptions(0, chartShowDates); 
Plot(C,"",colorGrey40,64); 

Plot(ethHigh,"\nmyHigh",colorAqua,styleLine); 
Plot(ethLow,"\nmyLow",colorOrange,styleLine);

Title = EncodeColor(colorAqua)+ "myHigh = " + WriteVal(LastValue(ethHigh),1.2)+ EncodeColor(colorOrange)+ "\nmyLow = " + WriteVal(LastValue(ethLow),1.2) + EncodeColor(colorYellow) +"\nrange H-L = " + WriteVal(LastValue(ethHigh-ethLow),1.2);

1 Like

It was tested and with the change of the new period, and it is correct. Just one remark. It works correctly even without the bi.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.