Indicator not plotting correct line as per calc

Created at 15 Dec 2016, 13:26
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
GO GoldnOil750's avatar

GoldnOil750

Joined 07.04.2015

Indicator not plotting correct line as per calc
15 Dec 2016, 13:26


Hi,

I am having a slight problem with the indicator I am trying to create.  The output lines are percentage of Daily Total Pips(from open) to Daily ATR. (time frame and ATR period can be changed).     So it should be going in positive or negative region. Have drawn the centre "zero line".  (1) But some how the pair is showing continously positive or negative. (2) When Changing the Chart Time Frame, these lines simply go heywire....  have no idea why ?  need to develop something that is not Chart time frame dependent.

 

I have inserted the CODE here if someone can look into it.  This indicator would be help to lot Traders who can look at a 15-min Chart or Daily Time frame and see how the 

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using System.Text;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = false, ScalePrecision = 0, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    [Levels(-150, 0, 150)]
    public class Total_Pips_Movement : Indicator
    {
        [Parameter("Timeframe", DefaultValue = "Daily")]
        public TimeFrame Time_Frame_01 { get; set; }

        [Parameter("ATR Period", DefaultValue = 30)]
        public int ATR_Periods_01 { get; set; }

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

        [Output("1. EUR-USD", Color = Colors.YellowGreen)]
        public IndicatorDataSeries DataSeries_Plot_01 { get; set; }

        [Output("2. EUR-JPY", Color = Colors.MediumVioletRed)]
        public IndicatorDataSeries DataSeries_Plot_02 { get; set; }

        [Output("3. EUR-GBP", Color = Colors.Lime)]
        public IndicatorDataSeries DataSeries_Plot_03 { get; set; }

        [Output("4. EUR-AUD", Color = Colors.Beige)]
        public IndicatorDataSeries DataSeries_Plot_04 { get; set; }

        [Output("5. EUR-NZD", Color = Colors.Aqua)]
        public IndicatorDataSeries DataSeries_Plot_05 { get; set; }

        [Output("6. EUR-CAD", Color = Colors.DodgerBlue)]
        public IndicatorDataSeries DataSeries_Plot_06 { get; set; }

        [Output("7. EUR-CHF", Color = Colors.Red)]
        public IndicatorDataSeries DataSeries_Plot_07 { get; set; }

        //////////-----------------------------------------------------
        //////////----------      VARIABLES           -----------------
        //////////-----------------------------------------------------

        // string str_Display;

        private MarketSeries Current_Market_Series;

        private MarketSeries temp_01;
        private MarketSeries temp_02;
        private MarketSeries temp_03;
        private MarketSeries temp_04;
        private MarketSeries temp_05;
        private MarketSeries temp_06;
        private MarketSeries temp_07;

        private AverageTrueRange ATR_Indi_01;
        private AverageTrueRange ATR_Indi_02;
        private AverageTrueRange ATR_Indi_03;
        private AverageTrueRange ATR_Indi_04;
        private AverageTrueRange ATR_Indi_05;
        private AverageTrueRange ATR_Indi_06;
        private AverageTrueRange ATR_Indi_07;
        ////-------------------------------------
        Symbol Pair_01;
        Symbol Pair_02;
        Symbol Pair_03;
        Symbol Pair_04;
        Symbol Pair_05;
        Symbol Pair_06;
        Symbol Pair_07;
        ////-------------------------------------
        string str_01 = "";
        string str_02 = "";
        string str_03 = "";
        string str_04 = "";
        string str_05 = "";
        string str_06 = "";
        string str_07 = "";
        ////-------------------------------------
        private double PipSize_01 = 0;
        private double PipSize_02 = 0;
        private double PipSize_03 = 0;
        private double PipSize_04 = 0;
        private double PipSize_05 = 0;
        private double PipSize_06 = 0;
        private double PipSize_07 = 0;
        ////-------------------------------------
        double Prices_Open_Pair_01 = 0;
        double Prices_Close_Pair_01 = 0;

        double Prices_Open_Pair_02 = 0;
        double Prices_Close_Pair_02 = 0;

        double Prices_Open_Pair_03 = 0;
        double Prices_Close_Pair_03 = 0;

        double Prices_Open_Pair_04 = 0;
        double Prices_Close_Pair_04 = 0;

        double Prices_Open_Pair_05 = 0;
        double Prices_Close_Pair_05 = 0;

        double Prices_Open_Pair_06 = 0;
        double Prices_Close_Pair_06 = 0;

        double Prices_Open_Pair_07 = 0;
        double Prices_Close_Pair_07 = 0;
        ////-------------------------------------
        double Total_Pips_Pair_01 = 0;
        double Total_Pips_Pair_02 = 0;
        double Total_Pips_Pair_03 = 0;
        double Total_Pips_Pair_04 = 0;
        double Total_Pips_Pair_05 = 0;
        double Total_Pips_Pair_06 = 0;
        double Total_Pips_Pair_07 = 0;
        ////-------------------------------------
        double ATR_Pips_Pair_01 = 0;
        double ATR_Pips_Pair_02 = 0;
        double ATR_Pips_Pair_03 = 0;
        double ATR_Pips_Pair_04 = 0;
        double ATR_Pips_Pair_05 = 0;
        double ATR_Pips_Pair_06 = 0;
        double ATR_Pips_Pair_07 = 0;
        ////-------------------------------------
        int Percentage_Pips_Pair_01 = 0;
        int Percentage_Pips_Pair_02 = 0;
        int Percentage_Pips_Pair_03 = 0;
        int Percentage_Pips_Pair_04 = 0;
        int Percentage_Pips_Pair_05 = 0;
        int Percentage_Pips_Pair_06 = 0;
        int Percentage_Pips_Pair_07 = 0;
        ////-------------------------------------

        // int index_01 = 0;
        // int Total_ATR = 0;



        //////////-----------------------------------------------------

        protected override void Initialize()
        {
            // GET SYMBOL
            Pair_01 = MarketData.GetSymbol("EURUSD");
            Pair_02 = MarketData.GetSymbol("EURJPY");
            Pair_03 = MarketData.GetSymbol("EURGBP");
            Pair_04 = MarketData.GetSymbol("EURAUD");
            Pair_05 = MarketData.GetSymbol("EURNZD");
            Pair_06 = MarketData.GetSymbol("EURCAD");
            Pair_07 = MarketData.GetSymbol("EURCHF");

            // INITIALIZE PAIR CODE
            str_01 = Pair_01.Code;
            str_02 = Pair_02.Code;
            str_03 = Pair_03.Code;
            str_04 = Pair_04.Code;
            str_05 = Pair_05.Code;
            str_06 = Pair_06.Code;
            str_07 = Pair_07.Code;

            // GET PIP SIZE
            PipSize_01 = Pair_01.PipSize;
            PipSize_02 = Pair_02.PipSize;
            PipSize_03 = Pair_03.PipSize;
            PipSize_04 = Pair_04.PipSize;
            PipSize_05 = Pair_05.PipSize;
            PipSize_06 = Pair_06.PipSize;
            PipSize_07 = Pair_07.PipSize;

            Print("01. Pip Size = " + PipSize_01);
            Print("02. Pip Size = " + PipSize_02);
            Print("03. Pip Size = " + PipSize_03);
            Print("04. Pip Size = " + PipSize_04);
            Print("05. Pip Size = " + PipSize_05);
            Print("06. Pip Size = " + PipSize_06);
            Print("07. Pip Size = " + PipSize_07);

            // MARKET DATA AS PER SYMBOL AND TIME FRAME
            temp_01 = MarketData.GetSeries(Pair_01, Time_Frame_01);
            temp_02 = MarketData.GetSeries(Pair_02, Time_Frame_01);
            temp_03 = MarketData.GetSeries(Pair_03, Time_Frame_01);
            temp_04 = MarketData.GetSeries(Pair_04, Time_Frame_01);
            temp_05 = MarketData.GetSeries(Pair_05, Time_Frame_01);
            temp_06 = MarketData.GetSeries(Pair_06, Time_Frame_01);
            temp_07 = MarketData.GetSeries(Pair_07, Time_Frame_01);

            Current_Market_Series = MarketData.GetSeries(Time_Frame_01);


        }
        //////////-----------------------------------------------------

        public override void Calculate(int index)
        {
            /// GET INDEX VALUE
            var index_01 = GetIndexByDate(temp_01, MarketSeries.OpenTime[index]);
            var index_02 = GetIndexByDate(temp_02, MarketSeries.OpenTime[index]);
            var index_03 = GetIndexByDate(temp_03, MarketSeries.OpenTime[index]);
            var index_04 = GetIndexByDate(temp_04, MarketSeries.OpenTime[index]);
            var index_05 = GetIndexByDate(temp_05, MarketSeries.OpenTime[index]);
            var index_06 = GetIndexByDate(temp_06, MarketSeries.OpenTime[index]);
            var index_07 = GetIndexByDate(temp_07, MarketSeries.OpenTime[index]);

            
            //Total_ATR = 0;
            // str_Display = "";


            // ATR INDICATOR AS PER SYMBOL
            ATR_Indi_01 = Indicators.AverageTrueRange(temp_01, ATR_Periods_01, MovingAverageType.Simple);
            ATR_Indi_02 = Indicators.AverageTrueRange(temp_02, ATR_Periods_01, MovingAverageType.Simple);
            ATR_Indi_03 = Indicators.AverageTrueRange(temp_03, ATR_Periods_01, MovingAverageType.Simple);
            ATR_Indi_04 = Indicators.AverageTrueRange(temp_04, ATR_Periods_01, MovingAverageType.Simple);
            ATR_Indi_05 = Indicators.AverageTrueRange(temp_05, ATR_Periods_01, MovingAverageType.Simple);
            ATR_Indi_06 = Indicators.AverageTrueRange(temp_06, ATR_Periods_01, MovingAverageType.Simple);
            ATR_Indi_07 = Indicators.AverageTrueRange(temp_07, ATR_Periods_01, MovingAverageType.Simple);

            // PAIR - 01
            Prices_Open_Pair_01 = temp_01.Open.LastValue;
            Prices_Close_Pair_01 = temp_01.Close.LastValue;
            Total_Pips_Pair_01 = (Prices_Close_Pair_01 - Prices_Open_Pair_01) / PipSize_01;

            ATR_Pips_Pair_01 = ATR_Indi_01.Result[index_01] / PipSize_01;
            Percentage_Pips_Pair_01 = (int)((Total_Pips_Pair_01 / ATR_Pips_Pair_01) * 100);
            // Total_ATR += ATR_Pips_Pair_01

            // PAIR - 02
            Prices_Open_Pair_02 = temp_02.Open.LastValue;
            Prices_Close_Pair_02 = temp_02.Close.LastValue;
            Total_Pips_Pair_02 = (Prices_Close_Pair_02 - Prices_Open_Pair_02) / PipSize_02;

            ATR_Pips_Pair_02 = ATR_Indi_02.Result[index_02] / PipSize_02;
            Percentage_Pips_Pair_02 = (int)((Total_Pips_Pair_02 / ATR_Pips_Pair_02) * 100);

            // PAIR - 03
            Prices_Open_Pair_03 = temp_03.Open.LastValue;
            Prices_Close_Pair_03 = temp_03.Close.LastValue;
            Total_Pips_Pair_03 = (Prices_Close_Pair_03 - Prices_Open_Pair_03) / PipSize_03;

            ATR_Pips_Pair_03 = ATR_Indi_03.Result[index_03] / PipSize_03;
            Percentage_Pips_Pair_03 = (int)((Total_Pips_Pair_03 / ATR_Pips_Pair_03) * 100);

            // PAIR - 04
            Prices_Open_Pair_04 = temp_04.Open.LastValue;
            Prices_Close_Pair_04 = temp_04.Close.LastValue;
            Total_Pips_Pair_04 = (Prices_Close_Pair_04 - Prices_Open_Pair_04) / PipSize_04;

            ATR_Pips_Pair_04 = ATR_Indi_04.Result[index_04] / PipSize_04;
            Percentage_Pips_Pair_04 = (int)((Total_Pips_Pair_04 / ATR_Pips_Pair_04) * 100);

            // PAIR - 05
            Prices_Open_Pair_05 = temp_05.Open.LastValue;
            Prices_Close_Pair_05 = temp_05.Close.LastValue;
            Total_Pips_Pair_05 = (Prices_Close_Pair_05 - Prices_Open_Pair_05) / PipSize_05;

            ATR_Pips_Pair_05 = ATR_Indi_05.Result[index_05] / PipSize_05;
            Percentage_Pips_Pair_05 = (int)((Total_Pips_Pair_05 / ATR_Pips_Pair_05) * 100);

            // PAIR - 06
            Prices_Open_Pair_06 = temp_06.Open.LastValue;
            Prices_Close_Pair_06 = temp_06.Close.LastValue;
            Total_Pips_Pair_06 = (Prices_Close_Pair_06 - Prices_Open_Pair_06) / PipSize_06;

            ATR_Pips_Pair_06 = ATR_Indi_06.Result[index_06] / PipSize_06;
            Percentage_Pips_Pair_06 = (int)((Total_Pips_Pair_06 / ATR_Pips_Pair_06) * 100);

            // PAIR - 07
            Prices_Open_Pair_07 = temp_07.Open.LastValue;
            Prices_Close_Pair_07 = temp_07.Close.LastValue;
            Total_Pips_Pair_07 = (Prices_Close_Pair_07 - Prices_Open_Pair_07) / PipSize_07;

            ATR_Pips_Pair_07 = ATR_Indi_07.Result[index_07] / PipSize_07;
            Percentage_Pips_Pair_07 = (int)((Total_Pips_Pair_07 / ATR_Pips_Pair_07) * 100);


            DataSeries_Plot_01[index] = Percentage_Pips_Pair_01;
            DataSeries_Plot_02[index] = Percentage_Pips_Pair_02;
            DataSeries_Plot_03[index] = Percentage_Pips_Pair_03;
            DataSeries_Plot_04[index] = Percentage_Pips_Pair_04;
            DataSeries_Plot_05[index] = Percentage_Pips_Pair_05;
            DataSeries_Plot_06[index] = Percentage_Pips_Pair_06;
            DataSeries_Plot_07[index] = Percentage_Pips_Pair_07;

            // ChartObjects.DrawText("SMA001", str_01, (index + 1), t_1, VerticalAlignment.Top, HorizontalAlignment.Center, Colors.YellowGreen);
            // ChartObjects.DrawText("SMA002", str_02, (index + 2), t_2, VerticalAlignment.Top, HorizontalAlignment.Center, Colors.Pink);
            // ChartObjects.DrawText("SMA003", str_03, (index + 3), t_3, VerticalAlignment.Top, HorizontalAlignment.Center, Colors.Lime);
            // ChartObjects.DrawText("SMA004", str_04, (index + 1), t_4, VerticalAlignment.Top, HorizontalAlignment.Center, Colors.Beige);
            // ChartObjects.DrawText("SMA005", str_05, (index + 2), t_5, VerticalAlignment.Top, HorizontalAlignment.Center, Colors.Aqua);
            // ChartObjects.DrawText("SMA006", str_06, (index + 3), t_6, VerticalAlignment.Top, HorizontalAlignment.Center, Colors.DodgerBlue);
            // ChartObjects.DrawText("SMA007", str_07, (index + 1), t_7, VerticalAlignment.Top, HorizontalAlignment.Center, Colors.Red);



            // CHECK IF LAST BAR OR NOT
            if (!IsLastBar)
            {
                return;
            }
            else
            {
                Display_On_Screen();
            }


        }
        //END METHOD Calculate -----------------------------------------------------


        public void Display_On_Screen()
        {
            // StringBuilder S_1 = new StringBuilder();

            // S_1.Append(str_01);
            // S_1.AppendLine();
            // S_1.AppendFormat("{0:N2}", );
            // S_1.Append(" %");

            ChartObjects.DrawText("TF001", Time_Frame_01.ToString(), StaticPosition.TopLeft, Colors.MediumVioletRed);

            ChartObjects.DrawText("SMA001", str_01 + my_Tabs(13), StaticPosition.TopRight, Colors.YellowGreen);
            ChartObjects.DrawText("SMA02a", my_NL(1) + Percentage_Pips_Pair_01.ToString("00") + " % (" + Total_Pips_Pair_01.ToString("00") + ")" + my_Tabs(13), StaticPosition.TopRight, Colors.YellowGreen);
            ChartObjects.DrawText("SMA02b", my_NL(2) + ATR_Pips_Pair_01.ToString("00") + my_Tabs(14), StaticPosition.TopRight, Colors.YellowGreen);

            ChartObjects.DrawText("SMA003", str_02 + my_Tabs(12), StaticPosition.TopRight, Colors.MediumVioletRed);
            ChartObjects.DrawText("SMA04a", my_NL(1) + Percentage_Pips_Pair_02.ToString("00") + " % (" + Total_Pips_Pair_02.ToString("00") + ")" + my_Tabs(11), StaticPosition.TopRight, Colors.MediumVioletRed);
            ChartObjects.DrawText("SMA04b", my_NL(2) + ATR_Pips_Pair_02.ToString("00") + my_Tabs(12), StaticPosition.TopRight, Colors.MediumVioletRed);

            ChartObjects.DrawText("SMA005", str_03 + my_Tabs(9), StaticPosition.TopRight, Colors.Lime);
            ChartObjects.DrawText("SMA06a", my_NL(1) + Percentage_Pips_Pair_03.ToString("00") + " % (" + Total_Pips_Pair_03.ToString("00") + ")" + my_Tabs(9), StaticPosition.TopRight, Colors.Lime);
            ChartObjects.DrawText("SMA06b", my_NL(2) + ATR_Pips_Pair_03.ToString("00") + my_Tabs(10), StaticPosition.TopRight, Colors.Lime);

            ChartObjects.DrawText("SMA007", str_04 + my_Tabs(7), StaticPosition.TopRight, Colors.Beige);
            ChartObjects.DrawText("SMA07a", my_NL(1) + Percentage_Pips_Pair_04.ToString("00") + " % (" + Total_Pips_Pair_04.ToString("00") + ")" + my_Tabs(7), StaticPosition.TopRight, Colors.Beige);
            ChartObjects.DrawText("SMA07b", my_NL(2) + ATR_Pips_Pair_04.ToString("00") + my_Tabs(8), StaticPosition.TopRight, Colors.Beige);

            ChartObjects.DrawText("SMA009", str_05 + my_Tabs(5), StaticPosition.TopRight, Colors.Aqua);
            ChartObjects.DrawText("SMA10a", my_NL(1) + Percentage_Pips_Pair_05.ToString("00") + " % (" + Total_Pips_Pair_05.ToString("00") + ")" + my_Tabs(5), StaticPosition.TopRight, Colors.Aqua);
            ChartObjects.DrawText("SMA10b", my_NL(2) + ATR_Pips_Pair_05.ToString("00") + my_Tabs(6), StaticPosition.TopRight, Colors.Aqua);

            ChartObjects.DrawText("SMA011", str_06 + my_Tabs(3), StaticPosition.TopRight, Colors.DodgerBlue);
            ChartObjects.DrawText("SMA12a", my_NL(1) + Percentage_Pips_Pair_06.ToString("00") + " % (" + Total_Pips_Pair_06.ToString("00") + ")" + my_Tabs(3), StaticPosition.TopRight, Colors.DodgerBlue);
            ChartObjects.DrawText("SMA12b", my_NL(2) + ATR_Pips_Pair_06.ToString("00") + my_Tabs(4), StaticPosition.TopRight, Colors.DodgerBlue);

            ChartObjects.DrawText("SMA013", str_07 + my_Tabs(1), StaticPosition.TopRight, Colors.Red);
            ChartObjects.DrawText("SMA14a", my_NL(1) + Percentage_Pips_Pair_07.ToString("00") + " %  (" + Total_Pips_Pair_07.ToString("00") + ")" + my_Tabs(1), StaticPosition.TopRight, Colors.Red);
            ChartObjects.DrawText("SMA14b", my_NL(2) + ATR_Pips_Pair_07.ToString("00") + my_Tabs(2), StaticPosition.TopRight, Colors.Red);
        }
        //END METHOD Display_On_Screen


        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        static string my_Tabs(int n)
        {
            return new String('\t', n);
        }
        //END METHOD my_Tabs

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        static string my_NL(int n)
        {
            return new String('\n', n);
        }
        //END METHOD my_NL 

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        private int GetIndexByDate(MarketSeries t_Series, DateTime time)
        {
            for (int i = t_Series.Close.Count - 1; i > 0; i--)
            {
                if (t_Series.OpenTime[i] == time)
                    return i;
            }
            return -1;
        }
        //////////-----------------------------------------------------
    }
}

 

 

 


@GoldnOil750
Replies

GoldnOil750
15 Dec 2016, 13:28 ( Updated at: 21 Dec 2023, 09:20 )


@GoldnOil750

GoldnOil750
15 Dec 2016, 13:39 ( Updated at: 21 Dec 2023, 09:20 )

caught the mistake....

 


@GoldnOil750