Star Patterns Recognition

by afhacker in category Other at 24/05/2016
Description

This indicator spot morning and evening start patterns, It will draw a dot above or below the middle candle of particular pattern:

Download
1435 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, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MorningEveningStar : Indicator
    {
        [Parameter("Alarm", DefaultValue = true)]
        public bool Alarm { get; set; }

        [Parameter("Alarm", DefaultValue = "C:\\Windows\\Media\\notify.wav")]
        public string alarmFile { get; set; }

        [Parameter("Divide Range To", DefaultValue = 1)]
        public double divideTo { get; set; }

        [Parameter("Time Filter", DefaultValue = false)]
        public bool timeFilter { get; set; }

        [Parameter("Start Hour", DefaultValue = 7, MinValue = 0, MaxValue = 23)]
        public int startHour { get; set; }

        [Parameter("End Hour", DefaultValue = 13, MinValue = 0, MaxValue = 23)]
        public int endHour { get; set; }

        [Parameter("Avoid Monday?", DefaultValue = false)]
        public bool avoidMonday { get; set; }

        [Parameter("Avoid Friday?", DefaultValue = false)]
        public bool avoidFriday { get; set; }

        [Parameter("Avoid December?", DefaultValue = false)]
        public bool avoidDecember { get; set; }

        [Parameter("Avoid Holidays?", DefaultValue = false)]
        public bool avoidHolidays { get; set; }

        [Parameter("Dots Space(pip)", DefaultValue = 1)]
        public int dotsSpace { get; set; }

        [Output("Up Point", Color = Colors.Orange, PlotType = PlotType.Points, Thickness = 5)]
        public IndicatorDataSeries UpPoint { get; set; }

        [Output("Down Point", Color = Colors.Orange, PlotType = PlotType.Points, Thickness = 5)]
        public IndicatorDataSeries DownPoint { get; set; }


        private int alarmIndex;


        // Holidays: Add holidays here
        int[,] holidays = 
        {
            {
                1,
                6,
                18
            },
            {
                15,
                0,
                0
            },
            {
                25,
                28,
                0
            },
            {
                25,
                0,
                0
            },
            {
                5,
                16,
                30
            },
            {
                2,
                29,
                0
            },
            {
                4,
                14,
                0
            },
            {
                15,
                0,
                0
            },
            {
                5,
                0,
                0
            },
            {
                3,
                10,
                0
            },
            {
                1,
                11,
                24
            },
            {
                8,
                26,
                0
            }
        };


        protected override void Initialize()
        {
            alarmIndex = 0;
        }

        public override void Calculate(int index)
        {
            var firstCandleHigh = MarketSeries.High.Last(3);
            var firstCandleLow = MarketSeries.Low.Last(3);
            var firstCandleOpen = MarketSeries.Open.Last(3);
            var firstCandleClose = MarketSeries.Close.Last(3);
            var firstCandleRange = Math.Round((firstCandleHigh - firstCandleLow), Symbol.Digits);

            var secondCandleHigh = MarketSeries.High.Last(2);
            var secondCandleLow = MarketSeries.Low.Last(2);
            var secondCandleOpen = MarketSeries.Open.Last(2);
            var secondCandleClose = MarketSeries.Close.Last(2);
            var secondCandleRange = Math.Round((secondCandleHigh - secondCandleLow), Symbol.Digits);

            var thirdCandleHigh = MarketSeries.High.Last(1);
            var thirdCandleLow = MarketSeries.Low.Last(1);
            var thirdCandleOpen = MarketSeries.Open.Last(1);
            var thirdCandleClose = MarketSeries.Close.Last(1);
            var thirdCandleRange = Math.Round((thirdCandleHigh - thirdCandleLow), Symbol.Digits);




            bool bullishPattern = false;
            bool bearishPattern = false;
            if (firstCandleOpen > firstCandleClose && secondCandleOpen > secondCandleClose && thirdCandleOpen < thirdCandleClose)
                bullishPattern = true;
            else if (firstCandleOpen < firstCandleClose && secondCandleOpen < secondCandleClose && thirdCandleOpen > thirdCandleClose)
                bearishPattern = true;
            else
                return;

            bool secondCandleOk = false;
            if (secondCandleRange <= firstCandleRange / divideTo)
            {
                if (bullishPattern && secondCandleLow < firstCandleLow)
                    secondCandleOk = true;
                else if (bearishPattern && secondCandleHigh > firstCandleHigh)
                    secondCandleOk = true;
            }


            bool thirdCandleOk = false;
            double firstCandleRangeHalf = firstCandleRange / 2;
            if (bullishPattern && thirdCandleClose >= firstCandleLow + firstCandleRangeHalf)
                thirdCandleOk = true;
            else if (bearishPattern && thirdCandleClose <= firstCandleHigh - firstCandleRangeHalf)
                thirdCandleOk = true;


            // Time Filter
            bool isTimeCorrect = false;
            if (timeFilter || avoidDecember || avoidFriday || avoidMonday || avoidHolidays)
                isTimeCorrect = timeFilterCheck();
            else
                isTimeCorrect = true;

            if ((bullishPattern || bearishPattern) && secondCandleOk && thirdCandleOk && isTimeCorrect)
            {
                if (bullishPattern)
                    DrawPoint(index, 0);
                else if (bearishPattern)
                    DrawPoint(index, 1);
            }
        }

        private void DrawPoint(int index, int candleType)
        {
            if (Alarm && alarmIndex != index)
                Notifications.PlaySound(alarmFile);
            alarmIndex = index;
            if (candleType == 0)
                DownPoint[index - 2] = MarketSeries.Low[index - 2] - (Symbol.PipSize * dotsSpace);
            if (candleType == 1)
                UpPoint[index - 2] = MarketSeries.High[index - 2] + (Symbol.PipSize * dotsSpace);

        }



        // Checking the opening time of candle
        private bool timeFilterCheck()
        {
            bool timeOk = false;
            if (timeFilter && MarketSeries.OpenTime.Last(1).Hour >= startHour && MarketSeries.OpenTime.Last(1).Hour <= endHour)
                timeOk = true;
            else if (!timeFilter)
                timeOk = true;
            bool decemberOk = false;
            if (avoidDecember && MarketSeries.OpenTime.Last(1).Month != 12)
                decemberOk = true;
            else if (!avoidDecember)
                decemberOk = true;
            bool mondayOk = false;
            if (avoidMonday && MarketSeries.OpenTime.Last(1).DayOfWeek != DayOfWeek.Monday)
                mondayOk = true;
            else if (!avoidMonday)
                mondayOk = true;
            bool fridayOk = false;
            if (avoidFriday && MarketSeries.OpenTime.Last(1).DayOfWeek != DayOfWeek.Friday)
                fridayOk = true;
            else if (!avoidFriday)
                fridayOk = true;
            bool holidaysOk = false;
            if (avoidHolidays)
            {
                for (int i = 0; i < holidays.GetLength(1); i++)
                {
                    if (MarketSeries.OpenTime.Last(1).Day == holidays[MarketSeries.OpenTime.Last(1).Month - 1, i])
                    {
                        holidaysOk = false;
                        break;
                    }
                    else
                        holidaysOk = true;

                }
            }
            else
                holidaysOk = true;

            if (timeOk && decemberOk && mondayOk && fridayOk && holidaysOk)
                return true;
            else
                return false;
        }

    }
}
Comments
0