Exploration results do not match with specified conditions

Greetings everyone,

I'm seeking guidance regarding an issue I've encountered while comparing the results of my conditions using the printf() function and the columns in my filters. Unfortunately, the results do not match as expected. ( an example for an exploration for AAPL dated January 8, 2024)

image

Currently, I'm in the process of collecting specimens for my playbook through exploration. The conditions that must hold true when the time is equal to 11:00:00 are:

  1. inSequence
  2. inPattern

Below is the code segment in question:

Plot( Close, "Close", colorDefault, styleCandle );
tm = TimeNum();
tm2 = TimeNum()/10000;
//printf("time = %s\n", NumToStr(tm));

//SESSION BREAKS 
PMstart = Ref(tm,-1) > tm;
PMStartTime = ValueWhen(PMstart,tm);;  	//premarket time start
PMEndtime = 093000;		//premarket time end
AHStartTime  = 160000;// afterhours time start
AHEndtime = 200000;  // afterhours time end

isPreMarket = (tm >= PMStartTime AND tm < PMEndtime);  
isAfterHours = (tm > AHStartTime AND tm < AHEndtime);
PMend = tm == PMEndtime;

Plot( isPreMarket, "", ColorRGB(41, 37, 35), styleArea | styleOwnScale, 0, 1,0,-5 );  
Plot( isAfterHours, "", ColorRGB(24, 33, 55), styleArea | styleOwnScale, 0, 1,0,-5 );


StartTime 	= 093000; 
EndTime   	= 100000;
trade_start = 093000;
trade_end 	= 120000;
market_open = tm == StartTime;
tm_range = tm >= StartTime AND tm <= EndTime;
time_ended = tm > EndTime;
isTrading_time =  tm >= trade_start AND tm <= trade_end;
isTrade_time = tm >= trade_start AND tm <= trade_end;//for br_at_1

// ATR
tf_ATR = inDaily;
TimeFrameSet(tf_ATR);
pATR = ATR(14); // Calculate ATR based on the daily data
mATR = Ref(pATR, -1);
TimeFrameRestore(); // Restore the original timeframe
expandmode = expandLast;
pATR = TimeFrameExpand( pATR, tf_ATR, expandmode );// Reference the ATR value from the previous day
mATR =  TimeFrameExpand( mATR, tf_ATR, expandmode );

// ATR floor and ceiling
f_ATR = floor(mATR);
c_ATR = ceil(mATR);
ATR_option = Param("Applied ATR",1,1,3,1);//applied ATR
app_ATR = IIf(ATR_option == 1,f_ATR,
		  IIf(ATR_option == 2, mATR, c_ATR));
ATR_quotient = app_ATR/4;

//Established High
idHigh = ValueWhen(isTrading_time, HighestSince(market_open, H));
Plot_idHigh = IIf(isPremarket OR isAfterHours, Null,idHigh);

//Established Low
idLow = ValueWhen(tm_range, LowestSince(market_open, L));
Plot_idLow = IIf(isPremarket OR isAfterHours, Null,idLow);
tm_at_low = ValueWhen(idLow,tm);//time low is established

//DAY LOW TO HIGH
idLow25		= idLow + (.25*ATR_quotient);
idLow50		= idLow + (.5*ATR_quotient);
idLow75		= idLow + (.75*ATR_quotient);

level1 		= idLow + ATR_quotient;


//PREVIOUS DAY RANGE
tf_candle_range = inDaily;
TimeFrameSet(tf_candle_range);

// Calculate previous OHLC
pd_Open = Ref(O,0);
pd_High = Ref(H,0);
pd_Low = Ref(L,0);
pd_Close = Ref(C,0);

pdc_range = abs(pd_High - pd_low);

TimeFrameRestore();
pdc_Range = TimeFrameExpand(pdc_Range, tf_candle_range, expandmode);
pd_Open = TimeFrameExpand(pd_Open, tf_candle_range, expandmode);
pd_High = TimeFrameExpand(pd_High, tf_candle_range, expandmode);
pd_Low = TimeFrameExpand(pd_Low, tf_candle_range, expandmode);
pd_Close =TimeFrameExpand(pd_Close, tf_candle_range, expandmode);
pdcr_quotient = pdc_Range/4;

pdcr1 = pd_Low;
pdcr2 = pd_low + pdcr_quotient;
pdcr3 = pdcr2 + pdcr_quotient;
pdcr4 = pdcr3 +pdcr_quotient;
pdcr5 = pd_High;


// -=STUDY FORMULA=-

//Break of Level 1
level1_break = IIf(isTrade_time, Cross(Close, level1) OR (O < level1 AND C > level1),Null);
level1_break = ExRem(level1_break, tm==093000);
mshape_level1_break = IIf(level1_break, shapeHollowDownArrow + shapePositionAbove,shapeNone);
PlotShapes(mshape_level1_break,colorGreen, 0,L);
plot_level1 = IIf(isPremarket OR isAfterHours, Null,level1);
Plot(plot_level1,"Level 1", colorAqua, style = styleDashed,minvalue=0, maxvalue=0, XShift=0,Zorder=0,width=1);
//Highest as of Time stop
TimeStop = 0110000;
isNewDay = Day() != Ref(Day(), -1);
BSND = BarsSince(isNewDay); // bars since new day
BSBO1 = BarsSince(level1_break); // bars since break of level 1


timeRange =  tm >= trade_start AND tm <= TimeStop; // 9:30 to time stop
TOB = IIf(BSBO1 > BSND, TimeStop , ValueWhen(level1_break, tm)); // time of level 1 break
//printf("TOB = %s\n", NumToStr(TOB));

bsRange = tm >= TOB and tm < TimeStop;// time from break to time stop
TS_Highest = ValueWhen(bsRange, HighestSince(level1_break, H)); // Time stop highest
Plot( bsRange, "", ColorRGB(21, 21, 27), styleArea | styleOwnScale, 0, 1,0,-5 );  // AREA OF BS RANGE


// LOWEST LOW BETWEEN BREAK OF LEVEL 1 AND TS_HIGHEST
HH = H > Ref(H, -1) AND H > Ref(H, 1);
pH = ValueWhen(HH, H,0); // future 1
cH = ValueWhen(HH, H,1); // current
fH = ValueWhen(HH, H,2); // previous 1
HHs = cH > pH AND cH > fH;
HHs = ExRem(HHs, level1_break);
pk = Max(HH, HHs);
pk = ExRem(pk, level1_break);
pkValue = ValueWhen(pk, High);
TOP = ValueWhen(pk, tm, 0); // time of Peak
pkRange = tm > TOP and tm <= TimeStop;// time from peak to time stop
dip = ValueWhen(pkRange, LowestSince(tm==TOP, L)); // dip

Plot(TS_Highest,"Highest as of TS", colorGreen, style = styleLine, minvalue=0, maxvalue=0, XShift=0,Zorder=0,width=1);
Plot(dip,"Dip", colorRed, style = styleLine, minvalue=0, maxvalue=0, XShift=0,Zorder=0,width=1);
//Bar identification
isTS_Highest = TS_Highest > Ref(TS_Highest, -1);
isDip = dip < Ref(dip, -1);
bstsh = BarsSince(isTS_Highest); // bars since ts_highest
bsd = BarsSince(isDip); // bars since dip



//SPECIMEN CONDITION
//sequence in order
inSequence = tm == TimeStop AND 
		     TOP < TimeStop AND 
			 TOB <= TOP AND 
			 bstsh < bsd;

// correct pattern
inPattern = tm == TimeStop AND 
			dip > idLow AND 
			dip < pkValue AND 
			TS_Highest > pkValue;
			
specimen = inSequence AND inPattern;
			
for( i = 0; i < BarCount; i++ ) 
{
if( pk[i] ) 
	PlotText( "Peak" , i, H[ i ]*1.001, colorWhite);
if( inSequence[i] ) 
	PlotText( "In Sequence" , i, H[ i ]*1.003, colorAqua);
if( inPattern[i] ) 
	PlotText( "In pattern" , i, H[ i ]*1.004, colorAqua);
if( specimen[i] ) 
	PlotText( "SPECIMEN" , i, H[ i ]*1.005, colorWhite);

} 
printf("date = %s\n", (Date()));
printf("inSequence = %s\n", NumToStr(inSequence));
printf("inPattern = %s\n", NumToStr(inPattern));
printf("specimen = %s\n", NumToStr(specimen));		


//EXPLORATION

Filter = tm==110000;


AddColumn (inSequence, "inSequence");
AddColumn (inPattern, "inPattern");
AddColumn (specimen, "specimen");



The code basically sets the following in sequence

  1. A certain level is broken.
  2. A peak is reached after the break.
  3. A dip occurs after the peak.
  4. The highest point after the dip.
  5. All conditions must be met as of 11:00:00.

If all conditions are met in sequence, inSequence equals 1.

Regarding the pattern:

  1. The dip should be higher than the established low.
  2. The dip should be lower than the peak.
  3. The highest point as of 11:00:00 should be higher than the previous peak.
  4. All conditions must be met as of 11:00:00.

When both the sequence and pattern conditions are met, specimen equals 1.

Hope you can guide me. thanks in advance

1 Like

It seems to me that a logical place to start would be to look at a chart to confirm whether the Exploration or the Interpretation (printf) is correct. Once you know which one is broken, you can use other debugging techniques like the ones described here: How do I debug my formula?

2 Likes

As for the chart i don't see any problem as all conditions are plotted correctly. So I dug deeper using printf()

As you can see the prinf resulted as follows:
inSequence is true
inPattern is true
Specimen is true

But when used in column after exploration
inSequence is true
inPattern is false
Specimen is false

Here's the portion of that code:

printf("date = %s\n", (Date()));
printf("inSequence = %s\n", NumToStr(inSequence));
printf("inPattern = %s\n", NumToStr(inPattern));
printf("specimen = %s\n", NumToStr(specimen));		


//EXPLORATION

Filter = tm==110000;


AddColumn (inSequence, "inSequence");
AddColumn (inPattern, "inPattern");
AddColumn (specimen, "specimen");

I cant seem to figure out what's causing them to mismatch.

Thanks for the response :slightly_smiling_face:

1 Like

My point was that you should use ONLY the chart to determine if inPattern is true on the date in question. That will help you identify what needs fixing. You might also find the built-in debugger helpful.

1 Like

Thanks. Will do that and post some updates.

1 Like

Either Interval(2) or ATR_option setting or both do differ in chart and analysis.
So simply check them by outputting both in chart and analysis.

1 Like

Okay so I tried to debug using _TRACEF() and Exploration and here's the result:

(these are the last lines of my original code)

_TRACEF("inSequence = %g",inSequence);
_TRACEF("inPattern = %g",inPattern);
_TRACEF("specimen = %g",inSequence);

//EXPLORATION
Filter = tm==110000;

AddColumn (inSequence, "inSequence");
AddColumn (inPattern, "inPattern");
AddColumn (specimen, "specimen");

The log shows the following:


at 11:00 on January 8, 2024
inSequence = 1
inPattern = 1
specimen = 1

Comparing it with my exploration
image

it can be observed that
sequence = 1
pattern = 0
specimen = 0

Also the chart seems fine:

I really can't figure out why the log does not match with the exploration:
Am I missing something here?

Well here's the reason why. Chart and Exploration Mismatch.

Took me a while to realise. Now I know which part to debug and what tool to use.

Thanks for leading me to the answer. :grin:

Hello sir, I'd like to hire you for a coding job but am unable to send PM's. Could you please send me a message? Thank you!

@traderzz,

You joined in 2019 yet you still don't have "Verified Badge".

Only users with "Verified Badge" are allowed to post on this forum.

Search "Verified Badge" for more information on how to get verified.

Also it is against forum rules to use multiple User Profiles.

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