Hi Everyone,
I am new to AFL programming and currently working on extracting the closing price from the futures market. The market has two sessions:
My formula requires:
Daily ATR
Closing price of the morning session for each day (at 16:55)
I attempted the following AFL code but ended up retrieving the closing price at midnight (00:00 AM) instead of the expected morning session close.
My Current Code:
TimeFrameSet(inDaily);
PVD_Cls = Ref(Close,-1); // Close price of previous day
ATR(20)
pv_xp = TimeFrameExpand(PVD_Cl , inDaily,mode=expandFirst);
TimeFrameRestore();
Issue:
The above code returns the closing price at midnight (00:00 AM), but I need the closing price at 16:55 (end of the morning session).
Question:
How can I modify my code to retrieve the daily closing price specifically at 16:55 instead of midnight?
Thanks in advance for your help!
nsm51
April 18, 2025, 9:02am
2
cond = TimeNum() == 165500; // or the start time of this bar
pc = IIf( cond, C, Null );
closeArray = ValueWhen( NOT IsNull( pc ), pc ); // fill Null with prev C
printf("\n%g", SelectedValue( closeArray ) );
@NSM51
Thank you very much for the code above.
It works perfectly!
However, the standard daily ATR still recalculates at midnight. Is there a way to modify it so that ATR updates at 16:55 instead?
I’d really appreciate any guidance.
Thanks in advance for your help!
nsm51
April 20, 2025, 9:59am
4
you should post your code to avoid guessing game.
What i posted is to get your custom Close. You need to use the same thing for H and L.
Then, compress those arrays to Daily, and calculate your own ATR because the built-in function does not accept custom arrays. I am not sure if there is another way.
https://www.amibroker.com/guide/afl/atr.html
AFL Function Reference - ATR
ATR
average true range Indicators
(AFL 1.3)
SYNTAX
atr( period )
RETURNS
ARRAY
FUNCTION
Calculates Average True Range indicator
EXAMPLE
atr(7)
SEE ALSO
Comments:
Bob Jagow
bjagow@charterr.net
2003-02-06 23:37:50 For other MAs, use ATR(1) to get the True Range.
E.g., MA(ATR(1),period), WMA(ATR(1),period), etc.
Tomasz Janeczko
tj --at-- amibroker.com
2005-02-03 06:46:39 Note that original formul…