Stock Portfolio Organizer

The ultimate porfolio management solution.

Shares, Margin, CFD's, Futures and Forex
EOD and Realtime
Dividends and Trust Distributions
And Much More ....
For Portfolio Manager Click Here

WiseTrader Toolbox

#1 Selling Amibroker Plugin featuring:

Advanced Adaptive Indicators
Advanced Pattern Exploration
Neural Networks
And Much More ....
Find Out More Here

Averages Functions Include for Amibroker (AFL)
demon
almost 14 years ago
Amibroker (AFL)

Rating:
3 / 5 (Votes 2)
Tags:
amibroker, moving average, function

This is a number of functions for various Moving Averages that is not native to glorious Amibroker to be used as include file for yor purposes. If you have improvements just post and we ask administration to update script with new MAs.
This is done to collect extensive set of libraries for easier programming.

Credited to various authors among the net (appreciate you work, even if don’t bother of your names) and me myself.

Actually I’m looking for adding more complicated smoothers as Kalman Filters, Laguerre and other.

Similar Indicators / Formulas

Hull Moving Average (HMA)
Submitted by kaiji almost 15 years ago
Smoothed MA (SSMA)
Submitted by kelvinhand over 11 years ago
Guppy GMMA with "derived" Indicator
Submitted by dalmas over 14 years ago
Candlestick above SMA 20
Submitted by tjhailioe over 14 years ago
Trend Scalping System
Submitted by esnataraj over 14 years ago
SAIEMA
Submitted by saiprakash over 11 years ago

Indicator / Formula

Copy & Paste Friendly
/* Elastic Volume Weighted Moving Average by Christian B. Fries */
function eVWMA(array, N)
{
  result[0] = array[0];
  for (i = 1; i < BarCount; i++) 
  {
    result[i] = ((N - Volume[i]) * result[i - 1] + Volume[i] * array[i]) / N;
  }
  return result;
}

/* Hull Moving Average */
function HMA(array, period)
{
  return WMA(2 * WMA(array, int(period / 2)) - WMA(array, period), int(sqrt(period)));
}

/* Kaufmann Adaptive Moving Average */
function KAMA(array, period)
{
  Direction = array - Ref(array, -period);
  Volatility = Sum(abs(array - Ref(array, -1)), period);
  Volatility = IIf(Volatility > 0, Volatility, 0.00001);
  ER = abs(Direction / Volatility);
  FastSC = 2 / (2 + 1);
  SlowSC = 2 / (30 + 1);
  SSC = ER * (FastSC - SlowSC) + SlowSC;
  Constant = SSC^2;
  return AMA(array, Constant);
}

/* Tilson's T3 */
function T3(array, period)
{
  s = 0.84;
  e1 = EMA(array, period);
  e2 = EMA(e1, period);
  e3 = EMA(e2, period);
  e4 = EMA(e3, period);
  e5 = EMA(e4, period);
  e6 = EMA(e5, period);
  c1 = -s^3;
  c2 = 3*s^2+3*s^3;
  c3 = -6*s^2 - 3*s - 3*s^3;
  c4 = 1 + 3*s + s^3 + 3*s^2;
  return c1 * e6 + c2 * e5 + c3 * e4 + c4*e3;
}

/* SMMA - Smoothed Moving Average */
function SMMA(array, period)
{
  SetBarsRequired(1000, 0);
  
  fsum = 0;
  for (i = 0; i < period; i++)
  {
    result[i] = Null;
    fsum += array[i];
  }
  result[period] = fsum / period;

  for (i = period + 1; i < BarCount; i++) 
  {
    result[i] = (result[i - 1] * period - result[i - 1] + array[i]) / period;
  }
  return result;
}

/* Sine Weighted Moving Average */
function SWMA(array, period)
{
  SetBarsRequired(1000, 0);
    
  PI = 4 * atan(1);
  k = PI / (period + 1);

  // denominator
  den = 0;
  for (i = 1; i <= period; i++) den += sin(i * k);
  
  for (i = 0; i < period - 1; i++) result[i] = Null;
  for (i = period - 1; i < BarCount; i++) 
  { 
    nom = 0;
    for (j = 1; j <= period; j++) nom += sin(j * k) * array[i - period + j];
    
    result[i] = nom / den;
  }

  return result;
}

// Triangular Moving Average
function TMA(array, period)
{
  if ((period % 2) > 0)
  { 
    // odd
    Coef1 = (period + 1) /2;
    Coef2 = Coef1;
  }
  else
  {
    // even
    Coef1 = period / 2 + 1;
    Coef2 = period / 2;
  }
  
  return MA(MA(array, Coef1), Coef2);
}

/* Volume Weighted Moving Average */
function VWMA(array, period)
{
  return MA(Volume * array, period) / MA(Volume, period);
}

2 comments

1. empottasch

nice thanks

2. thangbiz
// I add more code to PRINT the MA
// Downloaded From https://www.WiseStockTrader.com
// https://www.wisestocktrader.com/indicators/1398-averages-functions-include-for-amibroker-afl

/* Kaufmann Adaptive Moving Average */
function KAMA(array, period)
{
  Direction = array - Ref(array, -period);
  Volatility = Sum(abs(array - Ref(array, -1)), period);
  Volatility = IIf(Volatility > 0, Volatility, 0.00001);
  ER = abs(Direction / Volatility);
  FastSC = 2 / (2 + 1);
  SlowSC = 2 / (30 + 1);
  SSC = ER * (FastSC - SlowSC) + SlowSC;
  Constant = SSC^2;
  return AMA(array, Constant);
}

/* Tilson's T3 */
function T3(array, period)
{
  s = 0.84;
  e1 = EMA(array, period);
  e2 = EMA(e1, period);
  e3 = EMA(e2, period);
  e4 = EMA(e3, period);
  e5 = EMA(e4, period);
  e6 = EMA(e5, period);
  c1 = -s^3;
  c2 = 3*s^2+3*s^3;
  c3 = -6*s^2 - 3*s - 3*s^3;
  c4 = 1 + 3*s + s^3 + 3*s^2;
  return c1 * e6 + c2 * e5 + c3 * e4 + c4*e3;
}

/* SMMA - Smoothed Moving Average */
function SMMA(array, period)
{
  SetBarsRequired(1000, 0);
  
  fsum = 0;
  for (i = 0; i < period; i++)
  {
    result[i] = Null;
    fsum += array[i];
  }
  result[period] = fsum / period;

  for (i = period + 1; i < BarCount; i++) 
  {
    result[i] = (result[i - 1] * period - result[i - 1] + array[i]) / period;
  }
  return result;
}

/* Sine Weighted Moving Average */
function SWMA(array, period)
{
  SetBarsRequired(1000, 0);
    
  PI = 4 * atan(1);
  k = PI / (period + 1);

  // denominator
  den = 0;
  for (i = 1; i <= period; i++) den += sin(i * k);
  
  for (i = 0; i < period - 1; i++) result[i] = Null;
  for (i = period - 1; i < BarCount; i++) 
  { 
    nom = 0;
    for (j = 1; j <= period; j++) nom += sin(j * k) * array[i - period + j];
    
    result[i] = nom / den;
  }

  return result;
}

// Triangular Moving Average
function TMA(array, period)
{
  if ((period % 2) > 0)
  { 
    // odd
    Coef1 = (period + 1) /2;
    Coef2 = Coef1;
  }
  else
  {
    // even
    Coef1 = period / 2 + 1;
    Coef2 = period / 2;
  }
  
  return MA(MA(array, Coef1), Coef2);
}

/* Volume Weighted Moving Average */
function VWMA(array, period)
{
  return MA(Volume * array, period) / MA(Volume, period);
}


Type = ParamList("Type", "Hull Moving Average,Kaufmann Adaptive Moving Average,Tilson's T3,SMMA - Smoothed Moving Average,Sine Weighted Moving Average,Triangular Moving Average,Volume Weighted Moving Average");

Periods = Param("Periods", 14, 2, 300 );

if( Type == "Hull Moving Average" ) 						m = HMA( Close, Periods );
if( Type == "Kaufmann Adaptive Moving Average" ) 			m = KAMA( Close, Periods );
if( Type == "Tilson's T3" ) 								m = T3( Close, Periods );
if( Type == "SMMA - Smoothed Moving Average" ) 				m = SMMA( Close, Periods );
if( Type == "Sine Weighted Moving Average" ) 				m = SWMA( Close, Periods );
if( Type == "Triangular Moving Average" ) 					m = TMA( Close, Periods );
if( Type == "Volume Weighted Moving Average" ) 				m = VWMA( Close, Periods );


Plot(m,type,colorYellow,styleLine,Null,Null,0,0,2);
_SECTION_END();

Leave Comment

Please login here to leave a comment.

Back