Amibroker AFL needed

Hi all,

Need equivalent code in AFL for below Pine SCript code

haopen = na(haopen) ? (O + C) / 2 : (haopen + haclose) / 2

Please let me know if any further details required...Thanks in advance.



Well-Known Member
Before explaining what is happening here, let me explain a general C programming syntax.
Condition = y ? ValueWhenTrue : ValueWhenFalse
where y is a function which has returned a boolean value (1 or 0, in other words, true or false) from a called function or is a comparison between two different entities (array elements, variables, matrix, whatever) resulting 1 if the comparison is true or 0 if false.
Here the meaning of the syntax is:
if( y is true )
      Condition = ValueWhenTrue
else //when y is false
      Condition = ValueWhenFalse
Let us now understand the PineScript's na() function:
na() checks whether an expression or a variable returns a number or not.
For example:
y = na( "abc" ) //Output: y = 0 becase abc is not a number
y = na( 15 ) //Output: y = 1 becase 15 is a number
y = na( Null ) //Output: y = 0 becase Null is not a number
Coming to your PineScript code:
haopen = na(haopen) ? (O + C) / 2 : (haopen + haclose) / 2
It means:
if( Current element of "haopen" array is NOT A NUMBER ) //Could be a Null for being empty
    Current "haopen" element = ( Current "Open" + Current "Close" ) / 2
else //Current element of "haopen" array is A NUMBER
    Current "haopen" element = ( Current "haopen" + Current "haclose" ) / 2
In order to calculate Heiken-Ashi candles, below formulas are used to find its Open, High, Low, Close:
haClose = ( O + H + L + C ) / 4
haOpen = ( ( haOpen of Previous Bar ) + ( haClose of Previous Bar ) ) / 2
haHigh = Maximum of ( H, haOpen, haClose )
haLow = Minimum of ( L, haOpen, haClose )
Out of this if you understand the problem of calculating haOpen in charting platforms, you will be able to comprehend the PineScript code that you have posted and the below AmiBroker code as requested:
_SECTION_BEGIN( "Heiken-Ashi" );
     //Heiken-Ashi Candle Calculations
     HaClose = ( O + H + L + C ) / 4;
     HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
     HaHigh = Max( H, Max( HaClose, HaOpen ) );
     HaLow = Min( L, Min( HaClose, HaOpen ) );    
     PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "", colorDefault, styleCandle | styleNoLabel, Null, Null, 0, 0 );
Instead of elongating this post longer and without getting further into the reason why AMA is used in AmiBroker instead of a simple Na (not a number) like function, this is to ascertain that AmiBroker's solution is highly accurate than PineScript's method to this egg-first or chicken-first problem.


Well-Known Member
Hey..thanks for prompt reply..I want to incorporate this either in this Heiken-Ashi or simple Candlestick chart. Anything would do. Please help
@sanyogsingh can you confirm whose PineScript you have attached? EddieOz, drowkraken, pekipek, scarf, theky2784, luundi??? Or can you share the TradingView webpage from where you copied the PineScript?

Its just that I hate to code anything without understanding every minute fragment of the concept that I am coding. It is hard for me to read someone else's code. If you confirm the Id of the person, I might contact using TradingView forum and seek some more information on PPO Divergence.
Last edited:


Well-Known Member
You did not give the man his credit by at-least mentioning his Id or the link! Learning and plagiarism cannot go hand-in-hand. And I cannot be your partner in crime on this at the cost of the author's generosity.

In the long run, you will learn nothing and keep asking. If you can search on Google and find out that PPO or its divergence or Heiken-Ashi is fine means of Technical Analysis, why not push further and learn how to code AFL? It's all available in the internet for free. But that's hard work, no??? I can understand! Why not someone else do it for me??? I can just rest, and let others do the hard work for me!!!! No????

I will happily help you only if you show some efforts to decipher the code.
Whatever you said makes sense and FYI I asked scarf in tradingview mail itself first for PPO Divergence AFL but couldn’t get desired result, maybe he is not well versed with AFL coding and as I saw your meaningful and detailed explanation on one of my queries, thought to ask you about this as well. I never deny any credits and always appreciate in open forum for the good work, maybe it’s you, scarf or someone else. Believe me these systems when used appropriately, can give very good results and as Amibroker could be integrated with almost all RT data, I wanted to get this on open forum to be useful for all. Hope you understand this and help me in this and my future queries on AFL.


Similar threads

Broker Special Offers

Intraday Higher Leverage

Save up to 90% in brokerage and get higher leverage for intraday trades.

Are you a day trader?