Information

Username: sohoj
Member since: 02 Jan 2015
Last login: 02 Jan 2015
Status: Active

Activity

Where Created Comments
Algorithms 1 4
Forum Topics 0 0
Jobs 0 0

Last Algorithm Comments

SO
sohoj · 8 years ago

Credit -   cjdduarte

SO
sohoj · 8 years ago

 

//For SMA By Sohoj

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC)]
    public class SMAMTF : Indicator
    {
        [Parameter(DefaultValue = 14)]
        public int Periods { get; set; }

        [Parameter("SMA Timeframe1", DefaultValue = "Minute15")]
        public TimeFrame SMATimeframe1 { get; set; }

        [Parameter("SMA Timeframe2", DefaultValue = "Hour")]
        public TimeFrame SMATimeframe2 { get; set; }

        [Parameter("SMA Timeframe3", DefaultValue = "Hour4")]
        public TimeFrame SMATimeframe3 { get; set; }

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

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

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

        private MarketSeries series1;
        private MarketSeries series2;
        private MarketSeries series3;

        private SimpleMovingAverage Sma1;
        private SimpleMovingAverage Sma2;
        private SimpleMovingAverage Sma3;

        protected override void Initialize()
        {
            series1 = MarketData.GetSeries(SMATimeframe1);
            series2 = MarketData.GetSeries(SMATimeframe2);
            series3 = MarketData.GetSeries(SMATimeframe3);

            Sma1 = Indicators.SimpleMovingAverage(series1.Close, Periods);
            Sma2 = Indicators.SimpleMovingAverage(series2.Close, Periods);
            Sma3 = Indicators.SimpleMovingAverage(series3.Close, Periods);

        }

        public override void Calculate(int index)
        {

            var index1 = GetIndexByDate(series1, MarketSeries.OpenTime[index]);
            if (index1 != -1)
            {
                SMA1[index] = Sma1.Result[index1];
            }

            var index2 = GetIndexByDate(series2, MarketSeries.OpenTime[index]);
            if (index2 != -1)
            {
                SMA2[index] = Sma2.Result[index2];
            }

            var index3 = GetIndexByDate(series3, MarketSeries.OpenTime[index]);
            if (index3 != -1)
            {
                SMA3[index] = Sma3.Result[index3];
            }

        }


        private int GetIndexByDate(MarketSeries series, DateTime time)
        {
            for (int i = series.Close.Count - 1; i > 0; i--)
            {
                if (time == series.OpenTime[i])
                    return i;
            }
            return -1;
        }
    }
}

SO
sohoj · 9 years ago

Here is modified code.Coding seems easy in Calgo

Language: C#
Trading Platform: cAlgocTrader

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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = falseScalePrecision = 5, AccessRights = AccessRights.None)]
    public class BollingerBandsWidth : Indicator
    {
        [Parameter("Period", DefaultValue = 20)]
        public int Period 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 Source get; set; }

        private BollingerBands _bollingerBands;

        [Output("diff", PlotType = PlotType.Histogram)]
        public IndicatorDataSeries Diff get; set; }

        protected override void Initialize()
        {
            _bollingerBands = Indicators.BollingerBands(Source, Period, K, MaType);
        }

        public override void Calculate(int index)
        {
            Diff[index] = (_bollingerBands.Top[index] - _bollingerBands.Bottom[index]) / _bollingerBands.Main[index];
        }
    }
}

SO
sohoj · 9 years ago

Is it possible to plot the this indicator as histogram instead of lines.Please do this. 

 

Thanks in advance.