Join our facebook group for Amibroker afl code discussion https://www.facebook.com/groups/603980776889348

Schaff Trend Cycle Indicator afl

Schaff Trend Cycle Indicator afl
Schaff Trend Cycle Indicator afl

//www.aflcode.com
MA1=23;
MA2=50;
TCLen=10;
MA1=Param("ShortMACDLen",23,5,36);
MA2=Param("LOngMACDLen",50,10,100);
TCLen=Param("TCLen(StochPeriod)",10,5,20);
Factor=.5;
//Calculate a MACD Line
XMac = MACD(MA1,MA2) ; // MACD in Amibroker always uses Close for MACD calculation

//1st Stochastic: Calculate Stochastic of a MACD
Value1 = LLV(XMac, TCLen);
Value2 = HHV(XMac, TCLen) - Value1;

//Frac1=1; // prime Frac1 to a default of 1
//Frac1 = IIf(Value2 > 0, ((XMac - Value1) / Value2) * 100, Ref(FRAC1,-1));
// have to "prime" first value so that reference to "i-1" does not result in subscript out of range
// since MACD for both periods is not defined until MA2 period, 0 seems to be mathematically correct priming value
frac1=0;
for (i = 1; i < BarCount; i++) {
if (Value2[i] > 0) {
 frac1[i] = ((XMac[i] - Value1[i])/Value2[i])*100;
 }
else {
 frac1[i]= frac1[i-1];
 }
}

//Smoothed calculation for %FastD of MACD

PF[0]=frac1[0]; 
PF[1]=frac1[1];
for (i = 2; i < BarCount; i++) {
 PF[i]=PF[i-1]+(Factor*(frac1[i]-PF[i-1]));
}


//2nd Stochastic: Calculate Stochastic of Smoothed Percent FastD, above.
Value3 = LLV(PF, TCLen);
Value4 = HHV(PF, TCLen) - Value3;

//%FastK of PF
/*
Frac2=1;
Frac2 = IIf(Value4 > 0, ((PF - Value3) / Value4) * 100, Ref(FRAC2,-1));
*/

frac2[0]=0;
for (i = 1; i < BarCount; i++) {
 if (Value4[i] > 0 ) {
  frac2[i]=((PF[i] - Value3[i])/Value4[i])*100;
 }
 else {
  frac2[i]=frac2[i-1];
 }
}

//Smoothed calculation for %FastD of PF
PFF[0]=frac2[0];
PFF[1]=frac2[1];
for (i = 2; i < BarCount; i++) {
 PFF[i]=PFF[i-1]+(Factor*(frac2[i]-PFF[i-1]));
}

Plot(pff,"STLC",colorRed,styleLine);
Plot(75,"",colorBlue,styleLine|styleDashed);
Plot(25,"",colorYellow,styleLine|styleDashed);
Previous
Next Post »