cTrader用インジケータ マルチタイムフレーム移動平均線表示

5分足のチャートを見ながら4時間足や日足の移動平均線を表示させたかったので書いた

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
{
    [Indicator(AutoRescale = false, IsOverlay = true, AccessRights = AccessRights.None)]
    public class InMultiTimeFrame : Indicator
    {
        [Parameter("Ma1 TimeFrame", Group = "Ma1", DefaultValue = "Minute5")]
        public TimeFrame Ma1TimeFrame { get; set; }

        [Parameter("Ma1 Periods", Group = "Ma1", DefaultValue = 2, MaxValue = 100, MinValue = 1)]
        public int Ma1Periods { get; set; }

        [Output("Ma1", LineColor = "Yellow")]
        public IndicatorDataSeries Ma1 { get; set; }

        [Parameter("Ma2 TimeFrame", Group = "Ma2", DefaultValue = "Minute5")]
        public TimeFrame Ma2TimeFrame { get; set; }

        [Parameter("Ma2 Periods", Group = "Ma2", DefaultValue = 16, MaxValue = 100, MinValue = 1)]
        public int Ma2Periods { get; set; }

        [Output("Ma2", LineColor = "Orange")]
        public IndicatorDataSeries Ma2 { get; set; }

        [Parameter("Ma3 TimeFrame", Group = "Ma3", DefaultValue = "Minute15")]
        public TimeFrame Ma3TimeFrame { get; set; }

        [Parameter("Ma3 Periods", Group = "Ma3", DefaultValue = 16, MaxValue = 100, MinValue = 1)]
        public int Ma3Periods { get; set; }

        [Output("Ma3", LineColor = "Red")]
        public IndicatorDataSeries Ma3 { get; set; }

        //[Parameter("Ma4 TimeFrame", Group = "Ma4", DefaultValue = "Minute30")]
        //public TimeFrame Ma4TimeFrame { get; set; }

        //[Parameter("Ma4 Periods", Group = "Ma4", DefaultValue = 16, MaxValue = 100, MinValue = 1)]
        //public int Ma4Periods { get; set; }

        //[Output("Ma4", LineColor = "Blue")]
        //public IndicatorDataSeries Ma4 { get; set; }

        private Hage[] Debu;

        protected override void Initialize()
        {
            Debu = new Hage[] {
                MakeHage(Ma1TimeFrame, Ma1Periods, Ma1),
                MakeHage(Ma2TimeFrame, Ma2Periods, Ma2),
                MakeHage(Ma3TimeFrame, Ma3Periods, Ma3),
                //MakeHage(Ma4TimeFrame, Ma4Periods, Ma4),
            };
        }

        private Hage MakeHage(TimeFrame tf, int Periods, IndicatorDataSeries ds)
        {
            Hage o = new Hage();
            Print($"Info:InMultiTimeFrame {SymbolName}{tf}のBarsの読み込み開始");
            o.Bars = MarketData.GetBars(tf, SymbolName);
            Print($"Info:InMultiTimeFrame {SymbolName}{tf}のBarsの読み込み終了");
            o.Ma = Indicators.MovingAverage(o.Bars.ClosePrices, Periods, MovingAverageType.Simple);
            o.Ds = ds;
            return o;
        }

        public override void Calculate(int index)
        {
            foreach (Hage o in Debu)
            {
                o.Calculate(this, index, Bars.OpenTimes[index]);
            }
        }
    }

    public class Hage
    {
        public Bars Bars;
        public MovingAverage Ma;
        public IndicatorDataSeries Ds;

        public void Calculate(InMultiTimeFrame robo, int DsIndex, DateTime time)
        {
            int index = Bars.OpenTimes.GetIndexByTime(time);

            if (index == -1)
            {
                robo.Print($"Info:InMultiTimeFrame {Bars.SymbolName}のBarsには{time}に該当するデータが含まれていないため追加読み込み開始");

                int count = 999;
                while (0 < count && index == -1)
                {
                    count = Bars.LoadMoreHistory();
                    index = Bars.OpenTimes.GetIndexByTime(time);
                }

                if (index == -1)
                {
                    robo.Print($"Error:InMultiTimeFrame {Bars.SymbolName}のBarsから{time}に該当するデータの追加読み込み失敗");
                    return;
                }

                robo.Print($"Info:InMultiTimeFrame {Bars.SymbolName}のBarsから{time}に該当するデータの追加読み込み完了");
            }

            Ds[DsIndex] = Ma.Result[index];
        }
    }
}


この記事が気に入ったらサポートをしてみませんか?