Problem/Difficulties in Finding Price Level & Bar Index in other Timeframe

Hi all, :pray:
this is my first post, i hope i can do this right...

I would really appreciate if you guys can help me on my problem...

Background:
a. i work with 1 minute data and on M1 chart
b. i need to plot some lines in M1Chart with calculation/logic from M5 Chart
c. the calculation/logic is to find PriceLevel and its BarIndex

Codes:
Below are some of the code logic that works in any timeframe (for example lets say M1 chart)

cond_bar1 = (bCond OR sCond);
cond_bar2 = ValueWhen(cond_bar1, BarIndex());
final_CondBar = cond_bar1 AND (cond_bar2 < BarCount-1);

for(i = 1; i <= 3; i++)
{
	Bar_BI = LastValue(ValueWhen(final_CondBar, BarIndex(), i));
	Bar_Price = LastValue(ValueWhen(final_CondBar, C, i));

	Cond_Line= LineArray(Bar_BI[i], Bar_Price[i], BarCount-1, Bar_Price[i]);

	Plot(Cond_Line,"Cond"+i, colorGold, styleLine|styleNoRescale|styleNoTitle);
	PlotText("I" + i + "//  " + Bar_Price[i], BarCount+2, Bar_Price[i],colorGold);
}

Problem:
Now my problem is to plot the same logic; Taking True condition from M5 data, and plot it into the M1 chart.
I'm not really sure if this is indexing problem in my logic or my code just can't find the True condition of final_CondBar in Compressed M5,... Somehow the M5 price level and barindex (compressed & expanded) plot in M1 chart, is not same with those in M5 Chart (normal uncompressed).

I already tried 2 approaches from Amibroker references:

a. TimeFrameSet() and TimeFrameRestore()

TimeFrameSet(in5Minute);
cond_bar1 = (bCond OR sCond);
cond_bar2 = ValueWhen(cond_bar1, BarIndex());
final_CondBar = cond_bar1 AND (cond_bar2 < BarCount-1);

for(i = 1; i <= 3; i++)
{
	Bar_BI = LastValue(ValueWhen(final_CondBar, BarIndex(), i));
	Bar_Price = LastValue(ValueWhen(final_CondBar, C, i));

	Cond_Line= LineArray(Bar_BI[i], Bar_Price[i], BarCount-1, Bar_Price[i]);
	
	TimeFrameRestore();

	Plot(TimeFrameExpand(Cond_Line,in5Minute),"Cond"+i, colorGold, styleLine|styleNoRescale|styleNoTitle);
	PlotText("I" + i + "//  " + Bar_Price[i], BarCount+2, Bar_Price[i],colorGold);
}

b. TimeFrameCompress() & TimeFrameExpand()

cond_bar1 = (bCond OR sCond);
cond_bar2 = ValueWhen(cond_bar1, BarIndex());
final_CondBar = cond_bar1 AND (cond_bar2 < BarCount-1);

final_CondBar5 = TimeFrameCompress(final_CondBar, in5Minute);

for(i = 1; i <= 3; i++)
{
	Bar_BI = LastValue(ValueWhen(final_CondBar5, BarIndex(), i));
	Bar_Price = LastValue(ValueWhen(final_CondBar5, C, i));

	Cond_Line= LineArray(Bar_BI[i], Bar_Price[i], BarCount-1, Bar_Price[i]);
	
	TimeFrameRestore();

	Plot(TimeFrameExpand(Cond_Line,in5Minute),"Cond"+i, colorGold, styleLine|styleNoRescale|styleNoTitle);
	PlotText("I" + i + "//  " + Bar_Price[i], BarCount+2, Bar_Price[i],colorGold);
}

These 2 approaches are not showing correct pricelevel & barindex.
Please help.. or guide me to the right direction in working with this problem.
Thanks in advance. :pray:

im not really sure if this problem is too obvious (easy),
or my question is not clear enough.. so no one can answer... :face_with_head_bandage: :pray:

Please anyone... help me... anything... maybe just some guide or direction or explanation :pray:

thank you

no one?
it's been 5 days... and still no reply....

To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?

You should not be performing a TimeFrameRestore inside a loop. Not sure that it will actually break anything, but it's just a bad idea. You should be calling TimeFrameRestore once for each time that you call TimeFrameSet.

Most importantly, take the advice of @Tomasz and learn how to debug your own code. Those skills will serve you well on your AmiBroker journey.

Thanks Tomasz for your reply...

yep, i already did your advice, before i posted here..
i already put so many trace() and printf() in many places to see and analyze the results...
Unfortunately,.. i still can't get the correct result, like in normal timeframe...

Currently it's been 6 weeks since i started to code with this timeframe function..
starting from simple plain approach, and up into ridiculous/nonsense implementation.... already tried all with no luck

The code above is just one of many approaches that i already tried, which hopefully can give someone the whole concept of what im trying to achieve...

it will be highly appreciated if you or someone else can give me more insight regarding my problem...

You need to re-read http://www.amibroker.com/guide/h_timeframe.html with special attention to text inside orange boxes.
You are making obvious mistakes that @mradtke pointed out: calling TimeFrameRestore when it is not needed.
If you can't figure it out yourself, you can hire 3rd party coder: Third party services, blogs, courses, books, add-ons

Following one will do plot same lines start in 5-minute and shorter intervals such as 1-minute.

  1. you do not need subscripts []
    LastValue is element of array see manual
    Understanding how AFL works
  2. All timeframe code can be outside loop (before the loop)
  3. I have used expandPoint to set true at last intra-bar
  4. Removed Plot and LineArray and replaced by Gfx line draw
/// original at
/// @link https://forum.amibroker.com/t/problem-difficulties-in-finding-price-level-bar-index-in-other-timeframe/20321
/// "fix" at
/// https://forum.amibroker.com/t/problem-difficulties-in-finding-price-level-bar-index-in-other-timeframe/20321/9
TimeFrameSet(tmfrm = in5Minute);
	bCond = Cross(C, MA(C, 20));
	sCond = Cross(MA(C, 20), C);
	cond_bar1 = bCond OR sCond;
TimeFrameRestore();
cond_bar1 = Nz(TimeFrameExpand(cond_bar1, tmfrm, expandPoint));

bi = BarIndex();
cond_bar2 = ValueWhen(cond_bar1, bi);
final_CondBar = cond_bar1 AND (cond_bar2 < BarCount-1);

Plot( C, "Price", colorDefault, styleBar );
Plot(final_CondBar, "final_CondBar", colorRed, styleHistogram | styleOwnScale);

GfxSetCoordsMode(1);
GfxSelectPen(colorGold, 1, 0);

// three lines plot
for(i = 1; i <= 3; i++)
{
	Bar_BI = LastValue(ValueWhen(final_CondBar, bi, i));
	Bar_Price = LastValue(ValueWhen(final_CondBar, C, i));

	GfxMoveTo( Bar_BI, Bar_Price );
	GfxLineTo( BarCount-1, Bar_Price);	
	PlotText("I" + i + "//  " + Bar_Price, BarCount+2, Bar_Price,colorGold);
}

1 Like

thank you @mradtke. You helped me pointing out the mistake. :pray:

and yeah, i do wish i had started this AFL journey earlier. This is actually a steep learning curve for me, coming from non programming background - there are still lot of things for me to learn :crazy_face:

thanks @Tomasz , yes i do agree, Amibroker has very comprehensive documentation and references. There are still lots that i need to read and re-read more carefully. - Please bear with me. im trying to absorb it all :muscle::grin:

Thank you also for references to 3rd party coder. I might need it someday - (but hopefully not) :pray::grinning:

Oh my God @fxshrat ... your solution almost brought tears to my eyes.. (no, im not joking :joy:)
Yep.. 6 weeks of craziness trial n error finding solution in TimeFrame implementation. you've just made my day and now i can move on to next backlog in my trading system development.

Besides that, you also help me grabbing more in-depth concept of LastValue and the unnecessary usage of subscripts - (yes, there are still plenty of concepts that i need to learn in AFL along the way :pray:) Not yet mentioning, you also optimize my code with GFX usage (this is already in my backlog, was plan to change all LineArray with GFX later)

Thank you so much. Please let me know how i can reward you with something. You deserve it. :beers:

Oh.. if you don't mind, maybe there is one last thing that i want to ask, for helping me further understanding the concept:

usage of expandPoint;

i already tried to read the reference:
expandPoint - the resulting array gets not empty values only for the last bar within given period (all remaining bars are Null (empty))

May i know how is this make so much difference (instead of using expandLast / expandFirst? and when is expandPoint best usage? - im still trying to understand the "not empty values"

thank you :pray:

Below picture explains difference between expandPoint, expandLast, expandFirst.

cond_bar1 of posted code is true/false array returning zero or one.
The red vertical lines are results of cond_bar1. So if it plots vertical line in all three panes then it is returned 1 (TRUE) by cond_bar1.

With expandPoint and without using Nz() function con_bar1 would return non-empty (non-NULL) only at last (1-min) intra-bar of longer interval (5-min). See first pane of below picture.
By using Nz() function all empty (Null) results of con_bar1 are converted to zero. So it will return 1 if longer TF con_bar1 condition is TRUE at last intra bar else it will return zero (FALSE) for all remaining bars of array.

Now, with expandLast and exandFirst it would be returned TRUE for all intra-bars within longer interval's bar (-> 5-min) where 5-min interval's variable cond_bar1 returns true. And as you can see with expandFirst it may be a look forward intra-bar wise -> e.g. signal being returned at longer interval's last intra-bar Close -> it is known since first intra-bar already -> So problematic if used in backtest.

6 Likes

You rocks!! I don't know what to say, you nailed it :+1: :+1:
This is very clear explanation.

Now i know exactly when to use with for each of them.
I believe your explanation can also further helping other members in this forum to develop their need with timeframe functions.

thank you very much @fxshrat for your time & help :pray: