Kagi indicator AFL programming

I want to have Kagi indicator on my Amibroker. I have search for some code on internet but all of the have 2 errors.

_SECTION_BEGIN("KAGI CHART");
//KAGI Chart (or Swing) for Amibroker Indicator window. 
//Reverse swing is 3%.

SetBarsRequired(100000,100000);

Close = IIf( C<10, round(C*10)/10, IIf( C>=10 AND C<50, int(C) + round(round(frac(C)*10)/5)*5/10, int(C)));

Reverse = LastValue(IIf(C<50,0.05,IIf(C>=50 AND C<100,0.04,0.03)));


EnableScript("jscript");
<%

Close = VBArray( AFL( "Close" ) ).toArray();

KC = new Array();

// initialize first element
j = 0;
KC[j] = Close[0];

down = 1;    // By default the first bar is a down bar.
up = 0;
swap = 0;

// perform the loop that produces Kagi Chart
for( i = 1; i < Close.length; i++ )
{
 Reverse =AFL("Reverse");                        // percent reversal requirement

 if( Close[i] <= KC[j] && down)         //continue down
 {
  KC[j] = Close[i];
 }
 else
 {
  if( Close[i] >= KC[j]*(1 + Reverse) && down)  //Change direction to up
  {
   j++;
   swap = 1;

   KC[j] = Close[i];
  }
 }
 if( Close[i] >= KC[j] && up)         //Continue up
 { 
  KC[j] = Close[i];
 }
 else
 {
  if( Close[i] <= KC[j]*(1 - Reverse) && up)   //Change direction to down
  {
   j++;

   KC[j] = Close[i];
   swap = 1;
  }
 }
 if( swap )
 {
  swap = 0;
  if( up )
  {
   up = 0;
   down = 1;
  }
  else
  {
   up = 1;
   down = 0;
  }
 }
}
delta = Close.length - j-1;      //to get all of chart

AFL.Var("KC") = KC;
AFL.Var("delta") = delta;
AFL.Var("Reverse") = Reverse;
 
%>

KC = Ref( KC, -delta );
C = KC;
L = LLV(C,2);
H = HHV(C,2);

MyColor = 
IIf(C>Ref(H,-2),colorBrightGreen, 
IIf(C<Ref(L,-2),colorRed,
colorBlack));
Graph0BarColor = MyColor;
Graph0Style = 512+4096;
GraphXSpace = 5;
Graph0 = C;
Graph0Name = "Kagi Chart";
Graph1BarColor = 1;
Graph1Style = 16+2048+4096;
Graph1 = Reverse*100;
Graph1Name = "Reverse %";
_SECTION_END();
_SECTION_END();

Line 10 Reverse = LastValue(IIf(C<50,0.05,IIf(C>=50 AND C<100,0.04,0.03)));

and

Line 100 Graph1 = Reverse*100;

are always error. Please help me. Thank you.

@christopher987 it is probably because that code was written ~17 years ago. The user created variable in this afl "reverse" needs to be called something else since more recent versions of AmiBroker have a built in function called "Reverse" (so rename your variable).
https://www.amibroker.com/guide/afl/reverse.html

I think you should give credit to the original author of that code from the User Library (though other web sites have copied it without giving proper attribution),
http://www.amibroker.com/members/library/detail.php?id=236

And another effort to code Kagi Swing Charts also needs some updating of variable names,
http://www.amibroker.com/members/library/detail.php?id=794

1 Like

@christopher987,

See