Ma Cross in other timeframe

by anlv.hut in category Trend at 04/04/2018
Description

This indicator shows red dot when fast ma is lower than slow ma (for sell position) and vise versa.

Thanks

 

Time Frame MA: timeframe that 2 lines are calculated.

 

 

 

 

Download
715 downloads
How to install
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgocTrader
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class LvAMaCross : Indicator
    {
        [Parameter("MA Type", DefaultValue = MovingAverageType.TimeSeries)]
        public MovingAverageType maType { get; set; }

        [Parameter("Slow Period", DefaultValue = 30, MinValue = 1)]
        public int slowPeriod { get; set; }

        [Parameter("Fast Period", DefaultValue = 12, MinValue = 1)]
        public int fastPeriod { get; set; }

        [Parameter("Time Frame MA", DefaultValue = "Hour")]
        public TimeFrame timeFrame { get; set; }


        [Output("Sell Point", Color = Colors.Red, PlotType = PlotType.Points, Thickness = 7)]
        public IndicatorDataSeries SellSeries { get; set; }

        [Output("Buy Point", Color = Colors.Blue, PlotType = PlotType.Points, Thickness = 7)]
        public IndicatorDataSeries BuySeries { get; set; }


        public IndicatorDataSeries FastDataSeries { get; set; }

        public IndicatorDataSeries SlowDataSeries { get; set; }


        //market series cho timeframe cần tính
        public MarketSeries selectedSeries;

        public MovingAverage fastMA;
        public MovingAverage slowMA;

        protected override void Initialize()
        {
            FastDataSeries = CreateDataSeries();
            SlowDataSeries = CreateDataSeries();

            if (timeFrame == MarketSeries.TimeFrame)
            {
                selectedSeries = MarketSeries;
                fastMA = Indicators.MovingAverage(MarketSeries.Close, fastPeriod, maType);
                slowMA = Indicators.MovingAverage(MarketSeries.Close, slowPeriod, maType);
            }
            else
            {
                selectedSeries = MarketData.GetSeries(timeFrame);
                fastMA = Indicators.MovingAverage(selectedSeries.Close, fastPeriod, maType);
                slowMA = Indicators.MovingAverage(selectedSeries.Close, slowPeriod, maType);
            }

        }

        public override void Calculate(int index)
        {


            var selectedIndex = selectedSeries.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index]);

            if (selectedIndex == -1)
            {
                return;
            }

            FastDataSeries[index] = fastMA.Result[selectedIndex];
            SlowDataSeries[index] = slowMA.Result[selectedIndex];

            if (isCrossBelow())
            {
                // sell
                SellSeries[index] = MarketSeries.High[index];
            }
            else if (isCrossAbove())
            {
                BuySeries[index] = MarketSeries.Low[index];
            }
        }

        #region Predicate
        public bool isCrossAbove()
        {
            return FastDataSeries.HasCrossedAbove(SlowDataSeries, 0);
        }
        public bool isCrossBelow()
        {
            return FastDataSeries.HasCrossedBelow(SlowDataSeries, 0);
        }
        #endregion
    }
}
Comments
0