Creating new function from formula

phelp me, I want create function as VHF from this formula

SetBarsRequired(sbrAll,sbrAll);
VHFP = Param( "VHFPERIOD", 20, 2, 100, 1 );
HCP = HHV( C, VHFP );
LCP = LLV( C, VHFP );
NUMERATOR = HCP - LCP;
for ( i=BarCount-1;i> BarCount-VHFP-1;i--);
{
DIFF = abs(C[i] - C[i-1]);
}
DENOMINATOR = Sum( DIFF, VHFP );
VHF = NUMERATOR*100 / DENOMINATOR;
Plot( VHF, "VHF", colorWhite, styleLine );
_SECTION_END();

@maidanhthanh the VHF is coded in Trader's Tips of the Oct 2016 issue of Technical Analysis of Stocks and Commodities magazine.

http://www.amibroker.com/members/traders/10-2016.html

Other attempts are also easily found all over the internet.

1 Like

This one

is incorrect as it is not returning Diff array but number (Diff element of array). And BTW you have set semicolon at end of loop statement.

So what you did is the same as doing this one

for ( i=BarCount-1;i> BarCount-VHFP-1;i--) { } // end of loop

// after loop statement
DIFF = abs(C[i] - C[i-1]); // Diff not being array but number (element of array)

(BTW, also read here about not making assumptions about number of available bars if creating loops)

So instead use array processing (and as function -> "I want create function..")

function fVHF(period) { 
  local HCP, LCP, NUMERATOR, DIFF;
  local DENOMINATOR, VHF;  
  HCP = HHV( C, period );
  LCP = LLV( C, period );
  NUMERATOR = HCP - LCP;
  DIFF = abs(C - Ref(C, -1));
  DENOMINATOR = Sum( DIFF, period );
  VHF = NUMERATOR / DENOMINATOR*100;
  return VHF;
}

VHFP = Param( "VHFPERIOD", 20, 2, 100, 1 );
VHF = fVHF(VHFP);

Plot( VHF, "VHF", colorWhite, styleLine );
3 Likes