Information

Username: lordyy
Member since: 24 Mar 2015
Last login: 21 Apr 2024
Status: Active

Activity

Where Created Comments
Algorithms 1 1
Forum Topics 10 11
Jobs 0 0

Last Algorithm Comments

LO
lordyy · 6 months ago

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo
{
    [Levels(-200, 0, 200)]
    [Indicator(AccessRights = AccessRights.None)]
    public class BollengerSqueeze : Indicator
    {
        [Parameter(DefaultValue = "Hello Ctrader!")]
        public string Message { get; set; }

        private CommodityChannelIndex _commodityChannelIndex;
        private StandardDeviation _standardDeviation;
        private AverageTrueRange _averageTrueRange;



        [Parameter("Periods", Group = "CCI", DefaultValue = 50)]
        public int CciPeriods { get; set; }

        [Parameter("Source", Group = "STD")]
        public DataSeries StdSource { get; set; }

        [Parameter("Periods", Group = "STD", DefaultValue = 14)]
        public int STDPeriods { get; set; }

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

        [Parameter("Source", Group = "MovingAverage")]
        public DataSeries MvpSource { get; set; }

        [Parameter("Periods", Group = "MovingAverage", DefaultValue = 20)]
        public int MvPeriods { get; set; }


        [Parameter("Periods", Group = "ATR", DefaultValue = 14)]
        public int ATRPeriods { get; set; }

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




        [Output("Histogramp1", LineColor = "LawnGreen", PlotType = PlotType.Histogram, Thickness = 5)]
        public IndicatorDataSeries ResultHistogramPositive1 { get; set; }

        [Output("Histogramp2", LineColor = "Green", PlotType = PlotType.Histogram, Thickness = 5)]
        public IndicatorDataSeries ResultHistogramPositive2 { get; set; }

        [Output("HistogramN1", LineColor = "Pink", PlotType = PlotType.Histogram, Thickness = 5)]
        public IndicatorDataSeries ResultHistogramNegative1 { get; set; }

        [Output("HistogramN2", LineColor = "Red", PlotType = PlotType.Histogram, Thickness = 5)]
        public IndicatorDataSeries ResultHistogramNegative2 { get; set; }

        [Output("HistogramSq", LineColor = "Blue", PlotType = PlotType.Histogram, Thickness = 5)]
        public IndicatorDataSeries ResultHistogramSq { get; set; }


        [Output("Line", LineColor = "MidnightBlue")]
        public IndicatorDataSeries ResultLine { get; set; }

        protected override void Initialize()
        {

            Print(Message);
            _commodityChannelIndex = Indicators.CommodityChannelIndex(CciPeriods);
            _standardDeviation = Indicators.StandardDeviation(StdSource, STDPeriods, STDMAType);
            _averageTrueRange = Indicators.AverageTrueRange(ATRPeriods, ATRMAType);
        }
        public override void Calculate(int index)
        {
            // Calculate value at specified index
            // Result[index] = 00


            ResultLine[index] = _commodityChannelIndex.Result[index];
            
            
            if (_standardDeviation.Result[index ] / _averageTrueRange.Result[index ] <= 0.75)
                ResultHistogramSq[index] = _commodityChannelIndex.Result[index];
            else

            if (_standardDeviation.Result[index ] / _averageTrueRange.Result[index ] >= 1.5)//是否>=1.5 深色
            {
                if (_commodityChannelIndex.Result[index] >= 0)
                    ResultHistogramPositive2[index] = _commodityChannelIndex.Result[index];
                else
                    ResultHistogramNegative2[index] = _commodityChannelIndex.Result[index];
            }
            else//<1.5浅色
            {
                if (_commodityChannelIndex.Result[index] >= 0)
                    ResultHistogramPositive1[index] = _commodityChannelIndex.Result[index];
                else
                    ResultHistogramNegative1[index] = _commodityChannelIndex.Result[index];
            }
            

        }
    }
}