How to calculate the range of AM and PM and plot it?

Hi all,

I want to calculate the range (High –Close) of AM (9:30-13:00) and want to plot the result at the last bar of AM in 5-min chart, and then want to calculate the range (High –Close) of PM (13:01-16:00) and want to plot the result at the last bar of AM in 5-min chart.

I tried to code it:

tn1 = TimeNum();
startTime = 93000; 
endTime = 130000; 
timeOK1 = tn1 >= startTime AND tn1 <= endTime;

HHV1 = (C, tn1);
LLV1 = (C, tn1);
Range1 = HHV1 - LLV1;
Range1bar = lastvalue (timeOK1, True);

tn2 = TimeNum();
startTime = 130100; 
endTime = 160000; 
timeOK2 = tn2 >= startTime AND tn2 <= endTime;

HHV2 = (C, tn2);
LLV2 = (C, tn2);
Range2 = HHV2 - LLV2;
Range2bar = lastvalue (timeOK2, True);

dist = 1.5*ATR(10); 

for( i = 0; i < BarCount; i++ ) 
{ 
if( Range1bar [i] ) PlotText( "Range1@" + C[ i ], i, Range1bar[ i ]+dist[i], colorGreen ); 
}

dist = 1.5*ATR(10); 

for( i = 0; i < BarCount; i++ ) 
{ 
if( Range2bar [i] ) PlotText( "Range2@" + C[ i ], i, Range2bar[ i ]+dist[i], colorGreen ); 
}

However, it didn’t work, anyone know to how to code it? Please tell me, thank you.

It's my dream too. At noon I see the range of the futur afternoon. My search is in progress :blush:

Can you elaborate what you are looking for ?

1 Like

you could do something like this:

tn = TimeNum();
startTime = 93000;
endTime = 130000;
timeOK1 = tn >= startTime AND tn <= endTime;
firstBar = tn >= startTime ;
firstBar = ( firstBar - Ref( firstBar, -1 ) ) == 1;

HighOK1 = ValueWhen( timeOK1, HighestSince( firstBar, High ) );
LowOK1 = ValueWhen( timeOK1, LowestSince( firstBar, Low ) );

DH1 = TimeFrameCompress( HighOK1, inDaily, compressLast );
DL1 = TimeFrameCompress( LowOK1, inDaily, compressLast );
DH1 = TimeFrameExpand( DH1, inDaily, expandFirst );
DL1 = TimeFrameExpand( DL1, inDaily, expandFirst );

startTime = 130000;
endTime = 160000;
timeOK2 = tn >= startTime AND tn <= endTime;
firstBar = tn >= startTime ;
firstBar = ( firstBar - Ref( firstBar, -1 ) ) == 1;

HighOK2 = ValueWhen( timeOK2, HighestSince( firstBar, High ) );
LowOK2 = ValueWhen( timeOK2, LowestSince( firstBar, Low ) );

DH2 = TimeFrameCompress( HighOK2, inDaily, compressLast );
DL2 = TimeFrameCompress( LowOK2, inDaily, compressLast );
DH2 = TimeFrameExpand( DH2, inDaily, expandFirst );
DL2 = TimeFrameExpand( DL2, inDaily, expandFirst );

GraphXSpace = 5;
SetChartBkColor( colorBlack );
SetChartOptions( 1, chartShowDates, chartGridMiddle, 0, 0, 0 );
SetBarFillColor( IIf( C > O, ColorRGB( 0, 75, 0 ), IIf( C <= O, ColorRGB( 75, 0, 0 ), colorLightGrey ) ) );
Plot( C, "", IIf( C > O, ColorRGB( 0, 255, 0 ), IIf( C <= O, ColorRGB( 255, 0, 0 ), colorLightGrey ) ), 64, Null, Null, 0, 0, 1 );

Plot( IIf( timeOK1, DH1, Null ), "", colorRed, 1 );
Plot( IIf( timeOK1, DL1, Null ), "", colorGreen, 1 );
Plot( timeOK1, "", ColorRGB( 20, 20, 0 ), styleArea | styleOwnScale, 0, 1, 0, -1, 1 );

Plot( IIf( timeOK2, DH2, Null ), "", colorRed, 1 );
Plot( IIf( timeOK2, DL2, Null ), "", colorGreen, 1 );
Plot( timeOK2, "", ColorRGB( 0, 20, 20 ), styleArea | styleOwnScale, 0, 1, 0, -1, 1 );
3 Likes

Are you looking for this graph, before to display anything else ?
Tested on a future.

// Range upto Time marks
SetChartBkGradientFill( colortan, colortan ) ;

_SECTION_BEGIN( "Price" );
SetChartOptions( 0, chartShowArrows | chartShowDates );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} \nOp %g, \nHi %g, \nLo %g, \nCl %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Plot( C, "Close", ParamColor( "Color", colorWhite ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );
_SECTION_END();

tn1 = TimeNum();
startTime = 120000;
endTime = 150000;
timeOK1 = tn1 >= startTime AND tn1 <= endTime;
//Plot(timeOK1,"\ntimeOK1",colorBlack,1+styleOwnScale);
HHV1 = HHV( H, timeOK1 );
//Plot(HHV1,"\nHHV1",colorYellow,1);

LLV1 = LLV( C, timeOK1 );
//Plot(LLV1,"\nLLV1",colorBlue,1);

Range1 = (HHV1 - LLV1) AND timeOK1 ;
//Plot(Range1,"\nRange1",colorRose,1+styleOwnScale);
MaxRangeInWindow = HHV(HHV1-LLV1, timeOK1);
Plot(IIf(timeOK1,MaxRangeInWindow,Null),"\nMaxRangeInWindow",colorBlue,styleHistogram+styleOwnScale+styleLeftAxisScale);

Hi empottasch,

Thank so much, it really help me a lot.

Hi reinsley,

Thank for your sharing, but i am afraid my thinking is slightly different with you. What i am looking for:

  1. Marking different period (e.g. AM, PM), that's empottasch have helped me a lot.
  2. Finding the HHV and LLV of WHOLE PERIOD (i.e. HHV and LLV of AM)
  3. Calculating the range (HHV of AM - LLV of AM) and plot it out in chart.

For easier understanding, please see my uploaded pic