Category Other  Published on 13/07/2019

Fractal Keltner channels

Description

Fractal Keltner channels are centered on one adaptative fractal moving average like FRAMA. A bar entering the green channel from above signals a short trend while a bar entering the blue channel from underneath signals a long trend. A bar fully outside its previous channel signals the end of the trend ..

This Indicator has been converted from MT5.  

javascript:void(0)

 


using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MA_FractalKeltnerChannels : Indicator
    {
    [Parameter()]
    public DataSeries source { get; set; }
    [Parameter(DefaultValue = 30)]
    public int e_period { get; set; }
    [Parameter(DefaultValue = 30)]
    public int normal_speed { get; set; }
    [Parameter(DefaultValue = 1.0)]
    public double alpha1 { get; set; }
    [Parameter(DefaultValue = 2.0)]
    public double alpha2 { get; set; }

    [Output("Main0",Color=Colors.Red)]
    public IndicatorDataSeries Result0 { get; set; }
    [Output("Main1",Color=Colors.Blue,PlotType= PlotType.Line,LineStyle=LineStyle.Lines )]
    public IndicatorDataSeries Result1 { get; set; }
    [Output("Main2",Color=Colors.Blue,PlotType= PlotType.Line,LineStyle=LineStyle.Lines )]
    public IndicatorDataSeries Result2 { get; set; }
    [Output("Main3",Color=Colors.Green,PlotType= PlotType.Line,LineStyle=LineStyle.Lines )]
    public IndicatorDataSeries Result3 { get; set; }
    [Output("Main4",Color=Colors.Green,PlotType= PlotType.Line,LineStyle=LineStyle.Lines )]
    public IndicatorDataSeries Result4 { get; set; }

    double LOG_2;
    int min_rates_total,g_period_minus_1;
    
    protected override void Initialize()
        {
    min_rates_total = Math.Max( e_period , normal_speed ) ;
    g_period_minus_1= ( e_period-1 ) ;
    LOG_2 = Math.Log ( 2.0 ) ;
        }

    public override void Calculate(int index)
        {
    double diff = 0 , priorDiff = 0 ;
    double length = 0 ;
    double priceMax = 0 , priceMin = 0 ;
    double fdi = 0 , trail_dim = 0 , beta = 0 , sum = 0 , deviation = 0 , frasma = 0 , hurst = 0 ;
    int    speed = 0 ;
    priceMax = source.Maximum ( e_period ) ;
    priceMin = source.Minimum ( e_period ) ;
    length = 0 ;
    priorDiff = 0 ;
    
    for(int kkk=0; kkk<=g_period_minus_1; kkk++)
        {
    if ( priceMax - priceMin > 0.0 )
           {
    diff = ( source [ index - kkk ] - priceMin ) / ( priceMax - priceMin ) ;
    if ( kkk > 0 )
              {
    length+=Math.Sqrt ( Math.Pow ( diff - priorDiff , 2.0 ) + ( 1.0 / Math.Pow ( e_period , 2.0 ) ) ) ;
              }
    priorDiff=diff;
           }
        }
    if ( length > 0.0 )
        {
    fdi = 1.0 + ( Math.Log ( length ) + LOG_2 ) / Math.Log ( 2 * g_period_minus_1 ) ;
        }
    else
        {
    fdi=0L;
        }
    hurst = 2 - fdi ; // The Hurst exponent
    trail_dim = 1 / hurst ; // This is the trail dimension, the inverse of the Hurst-Holder exponent 
    beta = trail_dim / 2 ;
    speed = ( int ) ( Math.Round ( normal_speed * beta ) ) ;
    sum = 0 ;
    for ( int iii = 0 ; iii < speed ; iii++ ) 
    sum += source [ Math.Max ( index-iii , 0 ) ] ;
    frasma = sum / speed ;          
    double Keltner = ( MarketSeries.High.Sum ( e_period ) - MarketSeries.Low.Sum ( e_period ) ) / e_period ;
    deviation=alpha1*Keltner;
    Result0 [ index ] = frasma ; 
    Result1 [ index ] = frasma+deviation*Math.Pow(alpha1,hurst);
    Result3 [ index ] = frasma-deviation*Math.Pow(alpha1,hurst);
    deviation=alpha2*Keltner;
    Result2 [ index ] = frasma+deviation*Math.Pow(alpha2,hurst);
    Result4 [ index ] = frasma-deviation*Math.Pow(alpha2,hurst);
        
        }
    }
}


GA
galafrin

Joined on 26.01.2013

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: MA_Fractal Keltner Channels.algo
  • Rating: 5
  • Installs: 3926
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
fermjy's avatar
fermjy · 7 years ago

really nice.. thank you