# Need help to understand Function

Experts,

what would be the value of P and how it will be used?
what is the meaning of TD_Supply(H) & TD_Demand(L)?

``````function TD_Supply(P)
{
return ( P > Ref(P, 1) AND P > Ref(P, -1) AND P > Ref(C, -2));
}
function TD_Demand(P)
{
return ( P < Ref(P, 1) AND P < Ref(P, -1) AND P < Ref(C, -2));
}

////////////////////////////////////////////////////////////////////////
ShowTDP 		= ParamToggle("Show TD Pionts", "No|Yes", 1);
str = "";

if(ShowTDP)
{
PlotShapes(TD_Supply(H)*shapeSmallCircle, colorRed, 0, H, H*.001);
PlotShapes(TD_Demand(L)*shapeSmallCircle, colorGreen, 0, L, -L*.001);
}

//Plotting Area
Plot(C, "", IIf(C>O,colorGreen, colorRed), styleCandle);
``````

Hi

The key is to understand the REF function:
https://www.amibroker.com/guide/afl/ref.html

function TD_Supply(P) is called like this (with parameter HIGH):
TD_Supply(H)

Therefore its body will become:
HIGH > Ref(HIGH, 1) AND HIGH > Ref(HIGH, -1) AND HIGH > Ref(C, -2)

a) HIGH > Ref(HIGH, 1) means current bar HIGH is higher than the NEXT bar HIGH (looks into the future).

b) HIGH > Ref(HIGH, -1) means current bar HIGH is higher than the PREVIOUS bar HIGH.

c) HIGH > Ref(C, -2) means current bar HIGH is higher than the CLOSE from 2 bars ago.

If conditions a, b and c are ALL TRUE, then function TD_Supply(P) will return TRUE.

Apply similar logic to function TD_Demand(P).

1 Like

Thanks Bob for clear explanation!
Now i understand that P is just Variable that will be used to call H/L/C/O.

You are welcome.

Actually we call P a "parameter" (see https://en.wikipedia.org/wiki/Parameter_(computer_programming) ).

It does behave as a variable and you are right, its purpose is to take whatever value we give it.

So if we set

``````xx=(2*H+L)/MA(C, 15);
TD_Demand(xx);
``````

Then P will take the values of xx, ie (2*H+L)/MA(C, 15);

1 Like

Sorry Bob, I have poor understanding on the Programming side & I am trying to learn it.

The Example that you have given about "xx" gave me more clarity how better we can use this and get the values that we desire.

Thanks Bob!

Hi

You do need moderate programming skils to use AFL efficiently.