How to know if the High of the day or Low of the day formed first

Dear Seniors,

I would like to scan for the day high / low of each stock and classify them into two groups based on their formation..

for instance, lets say the high of a stock is 1000 and low of the same stock for the day is 900 with Open price as 950 and closing price as 980. I want to classify or know its trend for the day based on the price movement from High to Low or Low to high in a day.

if not, Can we know timestamp for the day high or day low and classify them based on whichever( Day High or Low) formed first?

Kindly share your thoughts or any reference...on the above..

Regards,
Hems

With EOD data is not possble. Use intraday data to flag which one ocurr first

Hello,

I guess I couldn't make my point clear enough. I would like to give another scenario here..Please check once..

  1. Count the 5 mins candles from left to right of the past 3 days.the total 5 mins candles would be 72 for a 6 hours trading day * 3 ( 9:!5 am to 3:15 pm)
  2. Get the candle number of Lowest low and Highest High of past 3 days.
  3. If the low made candle number is greater than high made candle number then it means low came first.
  4. If the high made candle number is greater than the low made candle number then it means High came first .

Lets say, Lowest low made on 24th candle and highest high made on 59th candle.so its an uptrend from lowest low to highest high with in these 3 days.

Can anyone help on this?

Hello Seniors,

As mentioned above... this has been my work so far.. I am able to plot N Day High low and the candle count of current day.

  1. How can I make the candle count from N days?
  2. How can I know the candle number of Higher High and Lower Low?

Help is very much appreciated... Thanks in advance..

_SECTION_BEGIN( "Price" );

Plot(C, "Close", colorDefault, styleCandle|styleNoTitle);
SetChartOptions(0,chartShowArrows|chartShowDates);
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.2f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );

_SECTION_END();

N = Param("Look Back Period", 3, 1, 60, 1);

_SECTION_BEGIN("N-Day High Low");

TimeFrameSet(inDaily);

NDayHi = H[BarCount - 1 - N];
NDayLo = L[BarCount - 1 - N];
XH = XL = BarCount - 1 - N;

for(i = BarCount - 1 - N; i < BarCount - 1; i++)
{
if(H[i] > NDayHi)
{
NDayHi = H[i];
XH = i;
}
if(L[i] < NDayLo)
{
NDayLo = L[i];
XL = i;
}
}

HLine = LineArray(BarCount - 1 - N, NDayHi, BarCount - 2, NDayHi);
LLine = LineArray(BarCount - 1 - N, NDayLo, BarCount - 2, NDayLo);

TimeFrameRestore();

Plot(TimeFrameExpand(Hline,inDaily,expandLast),  " Swing High", colorYellow , styleDashed,styleThick);
Plot(TimeFrameExpand(LLine,inDaily,expandLast), " Swing Low", colorBlue, styleDashed,styleThick);


/////////// Plotting candle stick numbers ////////////////

iBars = BarsSince( Day() != Ref( Day(), -1 ) );

for ( i = BarCount - ( LastValue( iBars ) + 1 ); i < BarCount; i++ )
{
    j[i] = abs( ( LastValue( BarCount ) - 2 * i - ( LastValue( iBars - i ) ) - 2 ) );
    PlotText( NumToStr( j[i], 1 ), i, L[i] - L[i]*0.05, colorYellow );
}


Filter = HLine OR LLine;

AddColumn(HLine,"3D-Hi",1.2);
AddColumn(LLine,"3D-Lo",1.2);

_SECTION_END();

@beppe - Respected sir... my sincere apologies for tagging you as it was purely intended to seek some help from you and nothing else.. I would request you to guide me in the above scenario as I have tried many ways to work it out and yet unable to conclude on the results..

Thanks in advance

Image%201

This is how it looks once plotted. All i wanted is to know the bar number of candle that made Low and bar number of candle that made High in the past N bars period.

Once the candle numbers are known, I would like to compare it and decide on what came first .. Hope i have given clear explanation on the requirement..

You do not need any loop. Just LowestSinceBars() and Ref().

/// @link https://tinyurl.com/rlktqpa
daysback = 3;
SetBarsRequired(daysback*inDaily/Max(1,Interval()));

Plot( C, "Price", colorDefault, styleBar );

dn = DateNum();
newday = dn != Ref( dn, -1);

ll = LowestSince(newday, L, daysback);
bars_ll = LowestSinceBars(newday, L, daysback);
Plot( ll, "LL", colorRed, styleStaircase | styleDashed );

dt = DateTime();
bi = BarIndex();
qfdb = Status("quickaflfirstdatabar");
dt_at_byd_low = SelectedValue(Ref(dt, -bars_ll));
bar_at_low = SelectedValue(Ref(bi, -bars_ll));
bars_since_ll = bi-bar_at_low;
printf("Datetime of Low before yesterday: %s\n", DateTimeToStr(dt_at_byd_low));
printf("Bar of Low before yesterday: %g\n", bar_at_low);
printf("QuickAFL bar of Low before yesterday: %g\n", qfdb+bar_at_low);
printf("Bars since Low before yesterday: %g", bars_since_ll);

12


PS: Please do not write essays into thread subject. Thank you!

2 Likes

Dear @fxshrat..

Thanks a lot for the response.. and point taken with regards to the lengthy Header in the subject line..

I have elaborated the code further to get my desired output...but facing issues.

My requirement is as follows:

  1. Identify whether Low or High formed first within N - Days
  2. Once Identified, Plot Levels based on Low or High formation in first place... If suppose, Low formed first with in the N day period, I would want the Fibonacci retracement levels to be formed from Low to High ( with in those N days) .

For this I have considered the following two logics as suggested above:

LowFirst = bars_since_ll > bars_since_hh;  

// OR //  

 LowFirst = DateTimeDiff(DT_At_Byd_Low,DT_At_Byd_High) > 0 ;


HighFirst = bars_since_hh > bars_since_ll; 

// OR //   

 HighFirst = DateTimeDiff(DT_At_Byd_High,DT_At_Byd_Low) > 0;

I am having problems in case of a HIGH and LOW formed on a same day.. I am providing the entire code so far.. Request your suggestion to handle this issue..

// Fucked Up Fibonacci Trading Strategy being Automated Since 5 Days !!

_SECTION_BEGIN( "Price" );

Plot(C, "Close", colorDefault, styleCandle|styleNoTitle);
SetChartOptions(0,chartShowArrows|chartShowDates);
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.2f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );

_SECTION_END();

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


_SECTION_BEGIN("N-Day High Low");


N = Param("Look Back Period", 2, 1, 60, 1);


TimeFrameSet(inDaily);

NDayHi = H[BarCount - 1 - N];
NDayLo = L[BarCount - 1 - N];
XH = XL = BarCount - 1 - N;

for(i = BarCount - 1 - N; i < BarCount - 1; i++)
{
if(H[i] > NDayHi)
{
NDayHi = H[i];
XH = i;
}
if(L[i] < NDayLo)
{
NDayLo = L[i];
XL = i;
}
}

HLine = LineArray(BarCount - 1 - N, NDayHi, BarCount - 2, NDayHi);
LLine = LineArray(BarCount - 1 - N, NDayLo, BarCount - 2, NDayLo);

Range = HLine - LLine;

TimeFrameRestore();

 
Plot(TimeFrameExpand(Hline,inDaily,expandFirst),  " Swing High", colorYellow , styleDashed,styleThick,styleOwnScale);
Plot(TimeFrameExpand(LLine,inDaily,expandFirst), " Swing Low", colorBlue, styleDashed,styleThick,styleOwnScale);

_SECTION_END();

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

_SECTION_BEGIN(" Identifying Hi / Lo FIrst ");

Daysback = N;
SetBarsRequired(Daysback*inDaily/Max(1,Interval()));

DN = DateNum();
Newday = DN != Ref( DN, -1);

LL = LowestSince(Newday, L, Daysback);
Bars_LL = LowestSinceBars(Newday, L, Daysback);

HH = HighestSince(Newday, H, Daysback);
bars_HH = HighestSinceBars(Newday, H, Daysback);


DT = DateTime();  BI = BarIndex();  


DT_At_Byd_Low = SelectedValue(Ref(DT, -Bars_LL));
Bar_At_Low = SelectedValue(Ref(Bi, -Bars_LL));
Bars_Since_LL = BI-Bar_At_Low;

DT_At_Byd_High = SelectedValue(Ref(DT, -Bars_HH));
Bar_At_High = SelectedValue(Ref(BI, - Bars_HH));
Bars_Since_HH = BI-Bar_At_High;


printf("Datime of Hi b4 yest: %s\n", DateTimeToStr(dt_at_byd_High));
printf("Datime of Lo b4 yest: %s\n", DateTimeToStr(dt_at_byd_low));

printf("Bar of Lo b4e yest: %g\n", bar_at_low);
printf("Bar of Hi b4e yest: %g\n", bar_at_High);

printf("Bars since Lo b4 yest: %g\n", bars_since_ll);
printf("Bars since hi b4 yest: %g\n", bars_since_hh);

printf("lowfirst: %g\n", lowfirst);
printf("highfirst: %g\n", HighFirst);




LowFirst = bars_since_ll > bars_since_hh;  // OR //   LowFirst = DateTimeDiff(DT_At_Byd_Low,DT_At_Byd_High) > 0 ;

HighFirst = bars_since_hh > bars_since_ll; // OR //    HighFirst = DateTimeDiff(DT_At_Byd_High,DT_At_Byd_Low) > 0;



Color = IIf( bars_since_ll > bars_since_hh, colorBrightGreen, ColorRed);
TextColor = IIf( bars_since_ll > bars_since_hh, colorBlack, colorWhite); 


if (colorBrightGreen ) // OR If ( LowFIrst == 0 )
{

F161 = LLine + (Range *1.618);
F127 = LLine + (Range *1.272);
F100 = LLine + (Range *1);
F78 = LLine + (Range *0.786);
F61 = LLine + (Range *0.618);
F50 = LLine + (Range *0.50);
F38 = LLine + (Range *0.382);
F23 = LLine + (Range *0.236);
F0 = LLine + (Range *0.0);

}

else 

{

F161 = HLine - (Range *1.618);
F127 = HLine - (Range *1.272);
F100 = HLine - (Range *1);
F78 = HLine - (Range *0.786);
F61 = HLine - (Range *0.618);
F50 = HLine - (Range *0.50);
F38 = HLine - (Range *0.382);
F23 = HLine - (Range *0.236);
F0 = HLine - (Range *0.0);


};


Filter = HLine OR LLine; 
AddColumn(HLine,"    3D-Hi  ",1.2);
AddColumn(LLine,"     3D-Lo  ",1.2);
AddColumn( IIf( bars_since_ll > bars_since_hh, 76, 72), " Lo / Hi - First ", formatChar, TextColor, Color, 100);
AddColumn(F61,"  0.618 - Level ",1.2);

_SECTION_END();

Please provide a way forward as i am completely stuck at this very point. Thanks in advance..

Reference%20Picture

This is one of the cases where it resulted Low first though it was Highfirst with those N-Days period.. This is creating reverse plotting of levels that I wish to draw on the chart.. Kindly help.. Thanks

The LowestSince* code I posted is correct.

You are just comparing apples to oranges!

The (subpar) loop you have there excludes last day.
On the other hand LowestSince* of my posted code includes "current" day.

unnamed

To exclude current day we have to look one bar backwards from start of "current" day and then apply Lowest/HighesSince to look for lowest/highest since days back.

BTW, please don't remove links from code you could not solve yourself.

/// @link https://tinyurl.com/uglfnsp
daysback = 2;
SetBarsRequired(daysback*inDaily/Max(1,Interval()));

Plot( C, "Price", colorDefault, styleBar );

dn = DateNum();
newday = dn != Ref( dn, -1);
bars_today = BarsSince(newday)+1;

ll = Valuewhen(newday, Ref(LowestSince(newday, L, daysback), -1));
bars_ll = Valuewhen(newday, Ref(LowestSinceBars(newday, L, daysback), -1))+bars_today;
Plot( ll, "LL", colorRed, styleStaircase | styleDashed );

hh = Valuewhen(newday, Ref(HighestSince(newday, H, daysback), -1));
bars_hh = Valuewhen(newday, Ref(HighestSinceBars(newday, H, daysback), -1))+bars_today;
Plot( hh, "HH", colorGreen, styleStaircase | styleDashed );

dt = DateTime();
bi = BarIndex();
qfdb = Status("quickaflfirstdatabar");

dt_at_low = Ref(dt, -bars_ll);
bar_at_low = Ref(bi, -bars_ll);
bars_since_ll = bi-bar_at_low;

dt_at_high = Ref(dt, -bars_hh);
bar_at_high = Ref(bi, -bars_hh);
bars_since_hh = bi-bar_at_high;

printf("Datetime of High before yesterday: %s\n", DateTimeToStr(SelectedValue(dt_at_high)));
printf("Datetime of Low before yesterday: %s\n", DateTimeToStr(SelectedValue(dt_at_low)));
printf("Bars since High before yesterday: %g\n", bars_since_hh);
printf("Bars since Low before yesterday: %g\n", bars_since_ll);
//printf("Bar of Low before yesterday: %g\n", bar_at_low);
//printf("QuickAFL bar of Low before yesterday: %g\n", qfdb+bar_at_low);

Dear @fxshrat Thanks a lot for the help. I have used the suggested code to extend it further towards my desired output and found the following issues:

  1. If any stock has High & low formation on a very same day with in the look-back period, results are shown inversely. Instead of High analysis is displaying Low. Reference image of - Titan Chart is attached explaining this case..

Titan%20chart%20for%20ref

2.In an another case, Though High formed first, the formula used to calculate desired level using If Function is giving wrong information.. Reference image 2 is attached explaining this case

reference%20image%202

Example is TVS Motors chart..

TVS%20Motors%20chart%20for%20ref

and the code is as follows:




/// @link https://tinyurl.com/uglfnsp
daysback = 3;
SetBarsRequired(daysback*inDaily/Max(1,Interval()));

Plot( C, "Price", colorDefault, styleBar );

dn = DateNum();
newday = dn != Ref( dn, -1);
bars_today = BarsSince(newday)+1;

ll = Valuewhen(newday, Ref(LowestSince(newday, L, daysback), -1));
bars_ll = Valuewhen(newday, Ref(LowestSinceBars(newday, L, daysback), -1))+bars_today;
Plot( ll, "LL", colorRed, styleStaircase | styleDashed );

hh = Valuewhen(newday, Ref(HighestSince(newday, H, daysback), -1));
bars_hh = Valuewhen(newday, Ref(HighestSinceBars(newday, H, daysback), -1))+bars_today;
Plot( hh, "HH", colorGreen, styleStaircase | styleDashed );

dt = DateTime();
bi = BarIndex();
qfdb = Status("quickaflfirstdatabar");

dt_at_low = Ref(dt, -bars_ll);
bar_at_low = Ref(bi, -bars_ll);
bars_since_ll = bi-bar_at_low;

dt_at_high = Ref(dt, -bars_hh);
bar_at_high = Ref(bi, -bars_hh);
bars_since_hh = bi-bar_at_high;

printf("Datetime of High before yesterday: %s\n", DateTimeToStr(SelectedValue(dt_at_high)));
printf("Datetime of Low before yesterday: %s\n", DateTimeToStr(SelectedValue(dt_at_low)));
printf("Bars since High before yesterday: %g\n", bars_since_hh);
printf("Bars since Low before yesterday: %g\n", bars_since_ll);
//printf("Bar of Low before yesterday: %g\n", bar_at_low);
//printf("QuickAFL bar of Low before yesterday: %g\n", qfdb+bar_at_low);


///// Extended Code ////////////////////////////////////

N = Param("Look Back Period", 3, 1, 60, 1);

_SECTION_BEGIN("N-Day High Low");

TimeFrameSet(inDaily);

NDayHi = H[BarCount - 1 - N];
NDayLo = L[BarCount - 1 - N];
XH = XL = BarCount - 1 - N;

for(i = BarCount - 1 - N; i < BarCount - 1; i++)
{
if(H[i] > NDayHi)
{
NDayHi = H[i];
XH = i;
}
if(L[i] < NDayLo)
{
NDayLo = L[i];
XL = i;
}
}

HLine = LineArray(BarCount - 1 - N, NDayHi, BarCount - 2, NDayHi);
LLine = LineArray(BarCount - 1 - N, NDayLo, BarCount - 2, NDayLo);

TimeFrameRestore();

H1 = ValueWhen (H,HLine);
L1 = ValueWhen (L,LLINE);

//Plot(TimeFrameExpand(Hline,inDaily,expandFirst),  " Swing High", colorYellow , styleDashed,styleThick,styleOwnScale);
//Plot(TimeFrameExpand(LLine,inDaily,expandFirst), " Swing Low", colorBlue, styleDashed,styleThick,styleOwnScale);

_SECTION_END();

LowFirst = bars_since_ll > bars_since_hh ;
HighFirst = bars_since_hh > bars_since_ll;

Color = IIf( LowFirst, colorBrightGreen, colorRed);
TextColor = IIf( LowFIrst, colorBlack, colorWhite); 

Range = H1 - L1;

if (colorBrightGreen  )

{

F61 = L1 + (Range *0.618);

}

else 

{

F61 = H1 - (Range *0.618);

};

Filter = colorBrightGreen OR colorRed;

AddColumn(H1,"    3D-Hi  ",1.2);
AddColumn(L1,"     3D-Lo  ",1.2);
AddColumn( IIf( bars_since_ll > bars_since_hh, 76, 72), " Lo / Hi - First ", formatChar, TextColor, Color, 100); 
AddColumn(F61,"  FL0.618 - Level ",1.2);

My Understanding is that, within If function calculation of formula is applied to all the stocks irrespective of the given condition and Else is being nullified completely.

To reiterate my whole requirement,

  1. I have tried to identify whether the high / low came first with in a given look back period and
  2. Once identified, plot 0.618 level with specific calculation depending the High / low formation.

Kindly suggest where I have gone wrong.

Thanks a lot for your precious time and efforts. It really helps a lot ...!

Dear @fxshrat ..

Request you to kindly look into the above issue and suggest a way forward please..

Dear Seniors @awilson, @portfoliobuilder, @beppe,@panos..

Can someone kindly help me in the above mentioned issue? I am unable to take it forward hence looking for your guidance... Request you to help me on this..

Thanks n advance..

Your incorrect output in analysis comes from the fact that you are applying EOD interval in Analysis. In your analysis you should set the same interval as of chart. And you should add Interval column then you know which interval you have set there in analysis.

AddTextColumn(Interval(2), "Interval", 1);

11


Then this one is flawed

if (colorBrightGreen  )
{
   F61 = L1 + (Range *0.618);
}
else 
{
   F61 = H1 - (Range *0.618);
};

It should be

Range = (H1 - L1) * 0.618;
F61 = IIf(LowFirst, L1 + Range, H1 - Range);

This one makes no sense too

Filter = colorBrightGreen OR colorRed;

Colors are not true/false arrays. They are type number.
So instead you should use Filter such as this

Filter = newday;


Here is (almost) completely rewritten code (also removing your loop).

/// @link https://tinyurl.com/uglfnsp
daysback = 3;
SetBarsRequired(daysback*inDaily/Max(1,Interval()));

Plot( C, "Price", colorDefault, styleBar );

dn = DateNum();
newday = dn != Ref( dn, -1);
bars_today = BarsSince(newday)+1;

ll = Valuewhen(newday, Ref(LowestSince(newday, L, daysback), -1));
bars_ll = Valuewhen(newday, Ref(LowestSinceBars(newday, L, daysback), -1))+bars_today;
Plot( ll, "LL", colorRed, styleStaircase | styleDashed );

hh = Valuewhen(newday, Ref(HighestSince(newday, H, daysback), -1));
bars_hh = Valuewhen(newday, Ref(HighestSinceBars(newday, H, daysback), -1))+bars_today;
Plot( hh, "HH", colorGreen, styleStaircase | styleDashed );

dt = DateTime();
bi = BarIndex();
qfdb = Status("quickaflfirstdatabar");

dt_at_low = Ref(dt, -bars_ll);
bar_at_low = Ref(bi, -bars_ll);
bars_since_ll = bi-bar_at_low;

dt_at_high = Ref(dt, -bars_hh);
bar_at_high = Ref(bi, -bars_hh);
bars_since_hh = bi-bar_at_high;
printf("Datetime of High before yesterday: %s\n", DateTimeToStr(SelectedValue(dt_at_high)));
printf("Datetime of Low before yesterday: %s\n", DateTimeToStr(SelectedValue(dt_at_low)));
printf("Bars since High before yesterday: %g\n", bars_since_hh);
printf("Bars since Low before yesterday: %g\n", bars_since_ll);
//printf("Bar of Low before yesterday: %g\n", bar_at_low);
//printf("QuickAFL bar of Low before yesterday: %g\n", qfdb+bar_at_low);

///// Extended Code ////////////////////////////////////
_SECTION_BEGIN("N-Day High Low");
function fxLookupArray(dt, array) {	
	// e.g. getting LLV/HHV, Lowest/HighestSince backwards
	// by fxshrat@gmail.com
	local i, n, bi, dt_list, flag, nullcnt, result;
	bi = BarIndex();
	result = Null;
	flag = dt != Ref(dt,-1);
	dt_list = SparseCompress(flag, dt);
	nullcnt = NullCount(dt_list);
	for ( n = nullcnt; n < BarCount; n++ ) {
		i = Lookup(bi, dt_list[n]);
		result[i] = array[i];
	}
	return ValueWhen(array == result, array);
}

L1 = fxLookupArray(dt_at_low, L);
H1 = fxLookupArray(dt_at_high, H);

Plot(ValueWhen(dt == SelectedValue(dt_at_high), H), "Swing High", colorYellow , styleDashed | styleStaircase);
Plot(ValueWhen(dt == SelectedValue(dt_at_low), L), "Swing Low", colorBlue, styleDashed | styleStaircase);
PlotShapes((L == L1) * shapesmallCircle, colorBlue, 0, L1, 0 );
PlotShapes((H == H1) * shapesmallCircle, colorYellow, 0, H1, 0 );
_SECTION_END();

LowFirst = bars_since_ll > bars_since_hh;
HighFirst = bars_since_hh > bars_since_ll;
Color = IIf(LowFirst, colorBrightGreen, colorRed);
TextColor = IIf(LowFIrst, colorBlack, colorWhite); 
Range = (H1 - L1) * 0.618;
F61 = IIf(LowFirst, L1 + Range, H1 - Range);

Filter = newday;

AddTextColumn(Interval(2), "Interval", 1);
AddColumn(H1,"    3D-Hi  ",1.4);
AddColumn(L1,"     3D-Lo  ",1.4);
AddColumn(IIf(LowFirst, 'L', 'H'), " Lo / Hi - First ", formatChar, TextColor, Color, 100); 
AddColumn(F61,"  FL0.618 - Level ",1.2);

6

6 Likes

Thanks a ton Mr. @fxshrat... It helped me a lot. Thanks for your time and efforts.. Highly appreciate it..

1 Like