Signal and Momentum

Created at 23 Apr 2013, 01:03
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!
LG

Lgon

Joined 22.04.2013

Signal and Momentum
23 Apr 2013, 01:03


I'm trying to plot momentum oscillator (histogram) together with a signal (as points in the same graph), which is 1 when the bollinger upper band is lower than the keltner upper band, and the boll low is higher than the keltner low; otherwise the signal is 0.

However, nothing is plotted...

Could someone help?

(code below)

Tks.

 

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = false)]
    public classSignal1 : Indicator
    {
        private int Period = 20;
        private IndicatorDataSeries atr;
        private MovingAverage expo;
        private TrueRange tri;
        private MovingAverage _movingAverage;
        private StandardDeviation _standardDeviation;
        private MomentumOscillator _momentum;

        [Parameter(DefaultValue = 20)]
        public int KeltnerPeriod { get; set; }

        [Parameter(DefaultValue = 1.5)]
        public double BandDistance { get; set; }

        [Parameter("MAType")]
        public MovingAverageType matype { get; set; }


         [Parameter("BB Period", DefaultValue = 20)]
        public int Period2 { get; set; }

        [Parameter("SD Weight Coef", DefaultValue = 2)]
        public int K { get; set; }

        [Parameter("MA Type", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType MaType { get; set; }

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


//        [Output("Main")]
//        public IndicatorDataSeries KeltnerMain { get; set; }

//        [Output("ChannelUp", Color = Colors.Red)]
//        public IndicatorDataSeries ChannelUp { get; set; }

//        [Output("ChannelLow", Color = Colors.Blue)]
//        public IndicatorDataSeries ChannelLow { get; set; }
        
        
//        [Output("Main BB", Color = Colors.Blue)]
//        public IndicatorDataSeries Main { get; set; }

//        [Output("Upper", Color = Colors.Red)]
//        public IndicatorDataSeries Upper { get; set; }

//        [Output("Lower")]
//        public IndicatorDataSeries Lower { get; set; }
        
        [Output("Momentum", Color = Colors.Blue, PlotType = PlotType.Histogram, Thickness = 5)]
        public IndicatorDataSeries Result { get; set; }
        
        [Output("Signal", Color = Colors.Red, PlotType = PlotType.Points, Thickness = 5)]
        public IndicatorDataSeries Result2 { get; set; }

        protected override void Initialize()
        {
            atr = CreateDataSeries();
            tri = Indicators.TrueRange();
            expo = Indicators.MovingAverage(MarketSeries.Typical, KeltnerPeriod, matype);
            
            _momentum = Indicators.MomentumOscillator(MarketSeries.Close, 14);
            
            _movingAverage = Indicators.MovingAverage(Price, Period2, MaType);
            _standardDeviation = Indicators.StandardDeviation(Price, Period2, K, MaType);
        }

        public override void Calculate(int index)
        {
            if (index < Period + 1)
            {
                atr[index] = tri.Result[index];
            }
            if (index >= Period)
            {
                atr[index] = (atr[index - 1] * (Period - 1) + tri.Result[index]) / Period;
            }

//            KeltnerMain[index] = expo.Result[index];
//            ChannelUp[index] = expo.Result[index] + BandDistance * atr[index];
//            ChannelLow[index] = expo.Result[index] - BandDistance * atr[index];
            
//            Main[index] = _movingAverage.Result[index];
//            Upper[index] = _movingAverage.Result[index] + K * _standardDeviation.Result[index];
//            Lower[index] = _movingAverage.Result[index] - K * _standardDeviation.Result[index];
            
            
            double momentum = _momentum.Result[index];
            
            if  (_movingAverage.Result[index] + K * _standardDeviation.Result[index] - expo.Result[index] + BandDistance * atr[index] <= 0)
            {
                if (_movingAverage.Result[index] - K * _standardDeviation.Result[index] - expo.Result[index] - BandDistance * atr[index] > 0)
                {
                    Result2[index] = 10;
                }
                else 
                {
                    Result2[index] = 20;
                }    
            }
            
            
        }
    }
}


@Lgon
Replies

cAlgo_Fanatic
23 Apr 2013, 11:34

You can reference the Keltner Channels Indicator (need to download and build) and the Bollinger Bands (build in) Indicator. The Momentum Oscillator oscillates to 100, so you would have to scale it down if you want your signal output to oscillate between 1 and 0. Also you may want to oscillate between 1 and 0.99 instead of zero so that you can see the momentum histogram properly.

//#reference: KeltnerChannels.algo
using cAlgo.API;
using cAlgo.API.Indicators;
//...

        ///////////////////////////////////////////
        // Bollinger Bands
        private BollingerBands bollingerBands;

        [Parameter(DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType BBMaType { get; set; }
        [Parameter(DefaultValue = 2)]
        public double BBStdev { get; set; }
        [Parameter(DefaultValue = 14)]
        public int BBPeriod { get; set; }
        [Parameter]
        public DataSeries BBSource { get; set; }
        ///////////////////////////////////////////
        // Keltner Channels
        private KeltnerChannels keltnerChannels;

        [Parameter(DefaultValue = 1.5)]
        public double BandDistance { get; set; }
        [Parameter(DefaultValue = 20)]
        public int KeltnerPeriod { get; set; }

        [Parameter(DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType KeltnerMaType { get; set; }
        
        ///////////////////////////////////////////

//...

        protected override void Initialize()
        {            
            _momentum = Indicators.MomentumOscillator(MarketSeries.Close, 14);
            bollingerBands = Indicators.BollingerBands(BBSource, BBPeriod, BBStdev, BBMaType);
            keltnerChannels = Indicators.GetIndicator<KeltnerChannels>(KeltnerPeriod, BandDistance, KeltnerMaType);            
        }
        public override void Calculate(int index)
        {

            double momentum = _momentum.Result[index];
            Result[index] = momentum/100; // scale it down

            // which is 1 when the bollinger upper band is lower than the keltner upper band, 
            // and the boll low is higher than the keltner low; otherwise the signal is 0.
            if( bollingerBands.Top[index] < keltnerChannels.ChannelUp[index] 
                && bollingerBands.Bottom[index] > keltnerChannels.ChannelLow[index])
            {
                Result2[index] = 1;
            }
            else
            {
                Result2[index] = 0.98;
            }

        }
    }
}




@cAlgo_Fanatic

Lgon
23 Apr 2013, 12:05

Awesome! Thanks.

Is it possible to hide de signal plot when, for instance, it's 0? 


@Lgon

cAlgo_Fanatic
23 Apr 2013, 14:14

You can set it to double.NaN for each value you want to hide.  


@cAlgo_Fanatic

Lgon
23 Apr 2013, 17:19

Could you give me some guidance with it?


@Lgon

cAlgo_Fanatic
24 Apr 2013, 09:06

Result2[index] = double.NaN;




@cAlgo_Fanatic