Need some help with my indicator

Created at 27 Jul 2013, 00:05
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
ER

ErikD

Joined 22.04.2013

Need some help with my indicator
27 Jul 2013, 00:05


Hi!

I am new to programming, trying to get my indicator to work

As I am new to this I have really come to an end and dont know what I should do from here

Any help is appreciated



      
     

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC)]
    public class Test : Indicator {
    
    
 
    #region Variables
        
       
        public  double alpha1;
        public  double HP;
        public double a1;
        public double b1;
        public double c1;
        public double c2; 
        public double c3;  
        public double Filt;
       
       
      
       public IndicatorDataSeries _filt;
       public IndicatorDataSeries _hp;
       
       
       #endregion
       


        #region  Parameters
        
        
        
        [Output("Roofing", Color = Colors.Purple)]
        public IndicatorDataSeries Roofing { get; set; }
        
        [Parameter("Max", DefaultValue = 48)]
        public int Period { get; set; }
        
         [Parameter("Min", DefaultValue = 10)]
        public int Period2 { get; set; }
        
        [Parameter]
        public DataSeries Source { get; set; }

        
        #endregion   
       
        protected override void Initialize()
        {
            _filt = CreateDataSeries();
            _hp = CreateDataSeries();
           
           alpha1 = (Math.Cos(.707*360 / Period) + Math.Sin (.707*360 / Period) - 1) / Math.Cos(.707*360 / Period); 
        
        
        
        
        a1 = Math.Exp(-1.414*3.14159 / Period2);
        b1 = 2*a1*Math.Cos(1.414*180 / Period2);
        c2 = b1;
        c3 = -a1*a1;
        c1 = 1 - c2 - c3;
            
            
            }
             
            
              public override void Calculate(int index)
        
         {
       
      
     _hp[1]=0;
_hp[2]=0;
_hp[index]= (1 - alpha1 / 2)*(1 - alpha1 / 2)*((Source[index] - 2*Source[index-1] + Source[index-2]) + 2*(1 - alpha1)*_hp[index-2] - (1 - alpha1)*(1 - alpha1)*_hp[index-3]); ; _filt[index] = c1*(_hp[index]*2) / 2 + c2*_filt[index-1] + c3*_filt[index-2]; Roofing[index]=_filt[index]; return; } } }




@ErikD
Replies

ErikD
27 Jul 2013, 11:54

I think I got it to work now

 


@ErikD

ErikD
27 Jul 2013, 12:32

Or maybe not

What I did was 

             _hp[1] = 0;
            _hp[2] = 0;
            _filt[1] = 0;
            _filt[2] = 0;

 

 

But as soon as period is over 40 I get a NaN


@ErikD

atrader
29 Jul 2013, 10:40

Consider these changes. The period over 10 for both fields, does not produce any results (goes to infinity). You probably need to confirm the formula. Or maybe post the formula here. 

What is this indicator supposed to show?

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, ScalePrecision = 5)]
    public class Forum_1305 : Indicator
    {
        #region Variables

        private IndicatorDataSeries _filt;
        private IndicatorDataSeries _hp;
        private double a1;
        private double alpha1;
        private double b1;
        private double c1;
        private double c2;
        private double c3;

        

        #endregion

        #region  Parameters

        [Output("Roofing", Color = Colors.Purple)]
        public IndicatorDataSeries Roofing { get; set; }

        [Parameter("Max", DefaultValue = 8)]
        public int Period { get; set; }

        [Parameter("Min", DefaultValue = 4)]
        public int Period2 { get; set; }

        [Parameter]
        public DataSeries Source { get; set; }

        #endregion

        protected override void Initialize()
        {
            _filt = CreateDataSeries();
            _hp = CreateDataSeries();

            // consider these changes: 180 = pi, 360 = 2*pi, 1.414 = Math.Sqrt(2),  .707 = Math.Sqrt(2)/2            

//            alpha1 = (Math.Cos(.707*360/Period) + Math.Sin(.707*360/Period) - 1)/Math.Cos(.707*360/Period);
            alpha1 = (Math.Cos(Math.Sqrt(2) * Math.PI / Period) + Math.Sin(Math.Sqrt(2) * Math.PI / Period) - 1) / Math.Cos(Math.Sqrt(2) * Math.PI / Period);


            a1 = Math.Exp(-Math.Sqrt(2)*Math.PI/Period2);
//            b1 = 2*a1*Math.Cos(1.414*180/Period2);
            b1 = 2 * a1 * Math.Cos(Math.Sqrt(2) * Math.PI / Period2);
            c2 = b1;
            c3 = -a1*a1;
            c1 = 1 - c2 - c3;

            _hp[0] = 0;
            _hp[1] = 0;
            _hp[2] = 0;
            _filt[0] = 0;
            _filt[1] = 0;
            _filt[2] = 0;

        }


        public override void Calculate(int index)
        {
            if(index < 3) return;

            _hp[index] = (1 - alpha1/2)*(1 - alpha1/2)*
                         ((Source[index] - 2*Source[index - 1] + Source[index - 2]) + 2*(1 - alpha1)*_hp[index - 2] -
                          (1 - alpha1)*(1 - alpha1)*_hp[index - 3]);
            
            _filt[index] = c1*(_hp[index]*2)/2 + c2*_filt[index - 1] + c3*_filt[index - 2];

            // Use print to check the results
            //Print("{0},{1},{2}", index, _hp[index], _filt[index]);
            
            Roofing[index] = _filt[index];

        }
    }
}




@atrader

ErikD
30 Jul 2013, 21:57

Thanks for the reply Atrader

The indicator is a convertaion from one of Ehlers papers

Its basicly a HighPass filter that is supposed to filter out signals over a higher noumber (default should be 48) that is then smootherd by what ehlers call a super smoother.

The indicator can be used as a standalone indicator but I have found that it is best used as a scourse for other osciliators (I have TS aswell)

The code looks good and I can tell from the indicator that it produces the result that I want, only that the period is to slow which make it that messy.

I'll see if I can find the paper and link it, the formula is in there but its basicly what I wrote in the code.

Could be with the way EasyLaunge and C prioritize code.

The alternative is that I use a libary with signal codes such as SignalLab but I rahter not :P


@ErikD

atrader
31 Jul 2013, 10:11

I corrected a couple of things according to the paper.

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, ScalePrecision = 5)]
    public class Forum_1305 : Indicator
    {
        #region Variables

        private IndicatorDataSeries _filt;
        private IndicatorDataSeries _hp;
        private double a1;
        private double alpha1;
        private double b1;
        private double c1;
        private double c2;
        private double c3;

        

        #endregion

        #region  Parameters

        [Output("Roofing", Color = Colors.Purple)]
        public IndicatorDataSeries Roofing { get; set; }

        [Parameter("Max", DefaultValue = 48)]
        public int Period { get; set; }

        [Parameter("Min", DefaultValue = 10)]
        public int Period2 { get; set; }

        [Parameter]
        public DataSeries Source { get; set; }

        #endregion

        protected override void Initialize()
        {
            _filt = CreateDataSeries();
            _hp = CreateDataSeries();

            alpha1 = (Math.Cos(Math.Sqrt(2) * Math.PI / Period) + Math.Sin(Math.Sqrt(2) * Math.PI / Period) - 1) / Math.Cos(Math.Sqrt(2) * Math.PI / Period);

            a1 = Math.Exp(-Math.Sqrt(2)*Math.PI/Period2);
            b1 = 2 * a1 * Math.Cos(Math.Sqrt(2) * Math.PI / Period2);
            c2 = b1;
            c3 = -a1*a1;
            c1 = 1 - c2 - c3;

            _hp[0] = 0;
            _hp[1] = 0;

            _filt[0] = 0;
            _filt[1] = 0;

        }

        public override void Calculate(int index)
        {
            
            if (index < 2) return;

            _hp[index] = (1 - alpha1/2)*(1 - alpha1/2)*
                         ((Source[index] - 2*Source[index - 1] + Source[index - 2]) + 2*(1 - alpha1)*_hp[index - 1] -
                          (1 - alpha1)*(1 - alpha1)*_hp[index - 2]);
            
            _filt[index] = c1 * (_hp[index] + _hp[index-1]) / 2 + c2 * _filt[index - 1] + c3 * _filt[index - 2];
           
            Roofing[index] = _filt[index];

        }
    }
}




@atrader