Alarm Trend Line Hit

by goldclay in category Other at 22/09/2017
Description

This indicator , I adopt from https://ctdn.com/algos/indicators/show/203 ( This alram Horizontal line)

I change a llite bit to alarm Trendline hit 

Input data

1. input Point1 - datetime and price

2. input Point2 - datetime and price

3. setting in chart 

Indicator will draw line as Point 1 to point 2 and extend line to current bar.

It will show distance pips between line and bid(or ask) when it hit . Notification will alarm.

Goldclay

PS:

this link for Alarm Trendline Pro  https://youtu.be/-cRK5gAhdhc 

* alarm by sound

* alarm by email with attach file of screenshot chart.

#sound #email #windowalert #screenshot #trendline

Point1

Point2

Input parameter

If we add more indicator for more trend line

Download
201 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
//This indy has adopt from https://ctdn.com/algos/indicators/show/203
//Initial source code make for detect Horizontal line 
//But this I will check trendline Hit will alarm.


using System;
using System.IO;
using System.Collections.Generic;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Indicator(TimeZone = TimeZones.UTC, IsOverlay = true, AutoRescale = false, AccessRights = AccessRights.FullAccess)]
    public class TrendLineAlarm : Indicator
    {
        const Colors AskColor = Colors.DeepSkyBlue;
        const Colors BidColor = Colors.Red;


        [Parameter("Spot price (Bid: 1, Ask: 2)", DefaultValue = 1, MinValue = 1, MaxValue = 2)]
        public double BidOrAsk { get; set; }

        [Output("Bid Target", Color = BidColor, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries BidTarget { get; set; }

        [Output("Ask Target", Color = AskColor, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries AskTarget { get; set; }
        [Parameter("Alarm Y/N?", DefaultValue = false)]
        public bool AlarmW { get; set; }

        [Output("Played Notification", Color = Colors.Gray, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries PlayedNotificationLine { get; set; }
        [Parameter("Sound Filename ", DefaultValue = "tada.wav")]
        public string wavefile { get; set; }
//Horizontal line
        //[Parameter()]
        //public double Price { get; set; }
//Trend line
        [Parameter("Date1(YYYYMMDDhhmm", DefaultValue = "201709151300")]
        public string startDate { get; set; }
        [Parameter("Point1", DefaultValue = 0.95642)]
        public double startValue { get; set; }
        [Parameter("Date2(YYYYMMDDhhmm", DefaultValue = "201709200900")]
        public string endDate { get; set; }
        [Parameter("Point2", DefaultValue = 0.9595)]
        public double endValue { get; set; }


        bool spotPriceWasAbove;
        int tickCount;
        bool triggered;

        static HashSet<Notification> PlayedNotifications = new HashSet<Notification>();

        protected override void Initialize()
        {
            if (endValue == 0)
                MarketData.GetMarketDepth(Symbol).Updated += AnimateWarning;

            if (BidOrAsk == 1)
                spotPriceWasAbove = Symbol.Bid > endValue;
            else
                spotPriceWasAbove = Symbol.Ask > endValue;

            if (NotificationWasPlayed())
            {
                triggered = true;
            }
        }

        private void AnimateWarning()
        {
            if (tickCount++ % 2 == 0)
                ChartObjects.DrawText("warning", "Please specify the Price", StaticPosition.Center, Colors.Red);
            else
                ChartObjects.DrawText("warning", "Please specify the Price", StaticPosition.Center, Colors.White);
        }

        public override void Calculate(int index)
        {


            if (IsRealTime)
            {
                var color = BidOrAsk == 1 ? BidColor : AskColor;
                var spotPrice = BidOrAsk == 1 ? Symbol.Bid : Symbol.Ask;
                string lineName = "TL";

                DateTime d1 = new DateTime(Convert.ToInt32(startDate.Substring(0, 4)), Convert.ToInt32(startDate.Substring(4, 2)), Convert.ToInt32(startDate.Substring(6, 2)), Convert.ToInt32(startDate.Substring(8, 2)), Convert.ToInt32(startDate.Substring(10, 2)), 0);
                int startIndex = MarketSeries.OpenTime.GetIndexByExactTime(d1);
                DateTime d2 = new DateTime(Convert.ToInt32(endDate.Substring(0, 4)), Convert.ToInt32(endDate.Substring(4, 2)), Convert.ToInt32(endDate.Substring(6, 2)), Convert.ToInt32(endDate.Substring(8, 2)), Convert.ToInt32(endDate.Substring(10, 2)), 0);
                int endIndex = MarketSeries.OpenTime.GetIndexByExactTime(d2);
                double slopeline = (endValue - startValue) / (endIndex - startIndex);
                double b = startValue - (slopeline * startIndex);
                double eValue = ((slopeline * index) + b);
                var distanceTL = Math.Round(Math.Abs(eValue - spotPrice) / Symbol.PipSize, 1);
                double Price = eValue;
                ChartObjects.DrawLine(lineName, startIndex, startValue, index, eValue, Colors.Red);
                ChartObjects.DrawText("distance", "" + distanceTL + " pips left", index + 5, Price, VerticalAlignment.Center, HorizontalAlignment.Right, color);
                if (spotPriceWasAbove && spotPrice <= Price || !spotPriceWasAbove && spotPrice >= Price)
                {
                    if (AlarmW)
                    {
                        var windowsFolder = Environment.GetFolderPath(Environment.SpecialFolder.Windows);
                        Notifications.PlaySound(Path.Combine(windowsFolder, "Media", wavefile));
                    }
                    triggered = true;

                    ChartObjects.RemoveObject("distance");

                    PlayedNotifications.Add(CreateNotification());

                    Print("Alarm : " + Symbol.Code + "  Hit Trend line !!! ");
                }
                if (triggered)
                {
                    PlayedNotificationLine[index] = eValue;
                    return;
                }

                if (BidOrAsk == 1)
                    BidTarget[index] = eValue;
                else
                    AskTarget[index] = eValue;

            }
        }

        private bool NotificationWasPlayed()
        {
            return PlayedNotifications.Contains(CreateNotification());
        }

        private Notification CreateNotification()
        {
            return new Notification 
            {
                Symbol = Symbol.Code,
                Price = endValue,
                BidIsSpotPrice = BidOrAsk == 1
            };
        }
    }

    struct Notification
    {
        public string Symbol;
        public double Price;
        public bool BidIsSpotPrice;
    }
}
Comments
0