WRONG values of ATR in BACK TESTING

Created at 24 Dec 2015, 20:22
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!
GoldnOil750's avatar

GoldnOil750

Joined 07.04.2015

WRONG values of ATR in BACK TESTING
24 Dec 2015, 20:22


Hi,

I am trying to get ATR VALUES on BACK TESTING and plan to write them into a CSV file.  But when I compare the values I am getting in BACK TESTING and the Values on the ATR Graph of the same date and period, it is TOTALLY DIFFERENT.  HAve copied the whole executable code over here so you can run and see the values for yourself.  And then manually cross-check these values on the Graph.  

a little help is required. Thank you.

 


using System.Globalization;
using System.IO;
using System.Threading;
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;


namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]

    public class testingSymbolATRValue : Robot
    {
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////                        USER INPUT                                                                            ///////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        [Parameter("<--------------> FRIDAY <-------------->")]
        public string temp50 { get; set; }


        [Parameter("47. Live (No),   BackTesting (Yes)", DefaultValue = false)]
        public bool p_Flag_BackTesting { get; set; }

        // END OF USER INPUT          /////

        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////                        GLOBAL VARIABLES                                                                      ///////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        private int dummy_Count = 0;
        private int Day_of_the_Week_1 = 0;
        private int Day_of_the_Week_2 = 0;
        // ATR INDICATOR INSTANCE /////////////////////
        private AverageTrueRange ATR_Indicator_1;
        private AverageTrueRange ATR_Indicator_2;
        private AverageTrueRange ATR_Indicator_3;
        private AverageTrueRange ATR_Indicator_4;
        private AverageTrueRange ATR_Indicator_5;
        private AverageTrueRange ATR_Indicator_6;
        private AverageTrueRange ATR_Indicator_7;
        private AverageTrueRange ATR_Indicator_8;
        private AverageTrueRange ATR_Indicator_9;
        //INIDIVIDUAL ATR VALUE OF ALL PAIRS WITH DIFFERENT ATR SETTINGS
        private double Crt_ATR_Val_1;
        private double Crt_ATR_Val_2;
        private double Crt_ATR_Val_3;
        private double Crt_ATR_Val_4;
        private double Crt_ATR_Val_5;
        private double Crt_ATR_Val_6;
        private double Crt_ATR_Val_7;
        private double Crt_ATR_Val_8;
        private double Crt_ATR_Val_9;
        ////////////////////////////////////////////////////

        //  END OF GLOBAL VARIABLES   /////

        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////                        ON START                                                                              ///////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        protected override void OnStart()
        {
            //DISPLAY START DATE AND TIME
            string t_Date = string.Format("{0:ddd-d-MMMM-y,h:mm tt}", Server.Time);
            Print("");
            Print("cBOT Start Date & Time : " + t_Date);

        }
        //End FUNCTION On_Start

        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////                        ON       B A R                                                                        ///////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        protected override void OnBar()
        {

            //SET THE DAY OF WEEK VALUE  MON=1, TUE=2, ........FRI=5
            Day_of_the_Week_1 = (int)Server.Time.DayOfWeek;

            //SKIP THE FEW STARTING BARS   TIMEFRAME = 1 MINUTE
            if (dummy_Count >= 300 && dummy_Count < 301)
                Get_ATR_Values();

            dummy_Count += 1;

            //IF DAY HAS CHANGED, THEN RESET VALUES ON DAY START
            if (Day_of_the_Week_2 != Day_of_the_Week_1)
            {
                Print("");
                Print("Change of DAY triggered : inside OnBar Method");
                //RESET THE COUNT OF OnBar
                dummy_Count = 0;
            }
            //END IF

            //MAKE A COPY OF THE DAY TO BE COMPARED, ON THE NEXT BAR
            Day_of_the_Week_2 = Day_of_the_Week_1;


        }
        //END METHOD On_Bar

        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////                        ON       T I C K                                                                      ///////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        protected override void OnTick()
        {
            // Put your core logic here
        }
        //End METHOD On_TICK

        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////                                                Get ATR Values                                                ///////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        private void Get_ATR_Values()
        {
            int t_Count_1 = 0, t_Count_2 = 0, t_Count_3 = 0;
            double t_1 = 0;
            t_1 = Symbol.PipSize;


            //GET A TEMP VARIABLE FOR MARKETDATA FOR THE SYMBOL AND TIME FRAME SPECIFIED BY USER

            //////////////////////////////////////////////////////////////////////////////////////////////
            // WEEKLY                    
            var temp_1A = MarketData.GetSeries(TimeFrame.Weekly);
            ATR_Indicator_1 = Indicators.AverageTrueRange(temp_1A, 1, MovingAverageType.Simple);

            var temp_1B = MarketData.GetSeries(TimeFrame.Weekly);
            ATR_Indicator_2 = Indicators.AverageTrueRange(temp_1B, 3, MovingAverageType.Simple);

            var temp_1C = MarketData.GetSeries(TimeFrame.Weekly);
            ATR_Indicator_3 = Indicators.AverageTrueRange(temp_1C, 9, MovingAverageType.Simple);

            t_Count_1 = ATR_Indicator_1.Result.Count;

            Crt_ATR_Val_1 = Math.Round(ATR_Indicator_1.Result.LastValue / t_1, 0);
            Crt_ATR_Val_2 = Math.Round(ATR_Indicator_2.Result.LastValue / t_1, 0);
            Crt_ATR_Val_3 = Math.Round(ATR_Indicator_3.Result.LastValue / t_1, 0);
            //////////////////////////////////////////////////////////////////////////////////////////////
            // DAILY                    
            var temp_2A = MarketData.GetSeries(TimeFrame.Daily);
            ATR_Indicator_4 = Indicators.AverageTrueRange(temp_2A, 3, MovingAverageType.Simple);

            var temp_2B = MarketData.GetSeries(TimeFrame.Daily);
            ATR_Indicator_5 = Indicators.AverageTrueRange(temp_2B, 9, MovingAverageType.Simple);

            var temp_2C = MarketData.GetSeries(TimeFrame.Daily);
            ATR_Indicator_6 = Indicators.AverageTrueRange(temp_2C, 27, MovingAverageType.Simple);

            t_Count_2 = ATR_Indicator_4.Result.Count;

            Crt_ATR_Val_4 = Math.Round(ATR_Indicator_4.Result.LastValue / t_1, 0);
            Crt_ATR_Val_5 = Math.Round(ATR_Indicator_5.Result.LastValue / t_1, 0);
            Crt_ATR_Val_6 = Math.Round(ATR_Indicator_6.Result.LastValue / t_1, 0);
            //////////////////////////////////////////////////////////////////////////////////////////////
            // 15-MIN
            var temp_3A = MarketData.GetSeries(TimeFrame.Minute15);
            ATR_Indicator_7 = Indicators.AverageTrueRange(temp_3A, 5, MovingAverageType.Simple);

            var temp_3B = MarketData.GetSeries(TimeFrame.Minute15);
            ATR_Indicator_8 = Indicators.AverageTrueRange(temp_3B, 15, MovingAverageType.Simple);

            var temp_3C = MarketData.GetSeries(TimeFrame.Minute15);
            ATR_Indicator_9 = Indicators.AverageTrueRange(temp_3C, 45, MovingAverageType.Simple);

            t_Count_3 = ATR_Indicator_7.Result.Count;

            Crt_ATR_Val_7 = Math.Round(ATR_Indicator_7.Result.LastValue / t_1, 0);
            Crt_ATR_Val_8 = Math.Round(ATR_Indicator_8.Result.LastValue / t_1, 0);
            Crt_ATR_Val_9 = Math.Round(ATR_Indicator_9.Result.LastValue / t_1, 0);
            //////////////////////////////////////////////////////////////////////////////////////////////

            //Print("WEEKLY : ATR VALUE = " + Crt_ATR_Val_1 + ", " + Crt_ATR_Val_2 + ", " + Crt_ATR_Val_3);
            //Print("WEEKLY : Count = " + t_Count_1);
            //Print("");
            Print("DAILY : ATR VALUE  = " + Crt_ATR_Val_4 + ", " + Crt_ATR_Val_5 + ", " + Crt_ATR_Val_6);
            Print("DAILY : Count = " + t_Count_2);
            //Print("");
            //Print("15-MIN : ATR VALUE  = " + Crt_ATR_Val_7 + ", " + Crt_ATR_Val_8 + ", " + Crt_ATR_Val_9);
            //Print("15-MIN Count = " + t_Count_3);

        }
        //END FUNCTION Get_ATR_Values


        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////                        ON STOP                                                                               ///////////////////////////////
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        protected override void OnStop()
        {
            //BLANK LINE
            Print("");

            string temp_Text = string.Format("{0:ddd-d-MMM-y,h:mm tt}", Server.Time);
            Print("cBOT ''onStop'' Stop Date & time : " + temp_Text);

        }
        //END METHOD On_STOP

    }
    //END OF MAIN PUBLIC CLASS
}
//END OF MAIN cALGO ROBOT

 

 

 


@GoldnOil750
Replies

GoldnOil750
24 Dec 2015, 20:26

The Back-TESTING values are :

1. Tick DATA from Server

2. EUR-USD

3. Time Frame = 1 minute

4. Dated from : 23-Nov-2915    to     27-Nov-2015

 

Manually cross-checking the ATR values on EUR-USD DAILY CHART

1. ATTACHED :  Three ATR, (Simple) with  Period = 3, 9, 27 


@GoldnOil750

Spotware
24 Dec 2015, 20:37

Dear Trader,

We would like to inform you that we do not provide coding assistance services. We more than glad to assist you with specific questions about cAlgo.API. You also can contact one of our Partners or post a job in Development Jobs section for further coding assistance.

If you believe that there is an issue with one of our methods/indicators, we kindly ask you to send us the simplest example showing it.


@Spotware

GoldnOil750
24 Dec 2015, 22:38

RE:

Spotware said:

Dear Trader,

We would like to inform you that we do not provide coding assistance services. We more than glad to assist you with specific questions about cAlgo.API. You also can contact one of our Partners or post a job in Development Jobs section for further coding assistance.

If you believe that there is an issue with one of our methods/indicators, we kindly ask you to send us the simplest example showing it.

I have minimized the code.   Please see it for yourself the ATR values are wrong in the BACKTESTING when compared to the Actual Values taken from the Graph.   Run it on a  1-min chart and then compare the values of ATR with the ATR values on the CHART.   

 

//////////////////////
/////////// USE 1-MIN CHART
//////////////////////

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


namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]

    public class testingSymbolATRValue : Robot
    {
        ////  USER INPUT       //////
        [Parameter("<--------------> FRIDAY <-------------->")]
        public string temp50 { get; set; }

        //// GLOBAL VARIABLES ///////
        private int dummy_Count = 0;
        private int Day_of_the_Week = 0;
        private int Copy_of_DayoftheWeek = 0;

        // ATR INDICATOR INSTANCE 
        private AverageTrueRange ATR_Indicator_1;
        private AverageTrueRange ATR_Indicator_2;

        //INIDIVIDUAL ATR VALUE OF ALL PAIRS WITH DIFFERENT ATR SETTINGS
        private double Crt_ATR_Val_1;
        private double Crt_ATR_Val_2;

        /////////////////////////////////////////////////////////////////////
        ////                        ON START                           //////
        /////////////////////////////////////////////////////////////////////
        protected override void OnStart()
        {
            Print("cBOT Started");
        }
        //End METHOD On_Start

        /////////////////////////////////////////////////////////////////////
        ////                        ON_BAR                             //////      
        /////////////////////////////////////////////////////////////////////
        protected override void OnBar()
        {
            //SET THE DAY OF WEEK VALUE  MON=1, TUE=2, ........FRI=5
            Day_of_the_Week = (int)Server.Time.DayOfWeek;

            //SKIP THE FEW STARTING BARS   TIMEFRAME = 1 MINUTE
            if (dummy_Count >= 300 && dummy_Count < 301)
                Get_ATR_Values();

            dummy_Count += 1;

            //IF DAY HAS CHANGED, THEN RESET VALUES ON DAY START
            if (Copy_of_DayoftheWeek != Day_of_the_Week)
            {
                Print("");
                Print("Change of DAY triggered");
                dummy_Count = 0;
            }
            //END IF

            //MAKE A COPY OF THE DAY TO BE COMPARED, ON THE NEXT BAR
            Copy_of_DayoftheWeek = Day_of_the_Week;
        }
        //END METHOD On_Bar

        /////////////////////////////////////////////////////////////////////
        ////                        Get_ATR_Values                     //////      
        /////////////////////////////////////////////////////////////////////
        private void Get_ATR_Values()
        {
            double t_1 = 0;
            t_1 = Symbol.PipSize;

            // DAILY                    
            var temp_1 = MarketData.GetSeries(TimeFrame.Daily);
            ATR_Indicator_1 = Indicators.AverageTrueRange(temp_1, 9, MovingAverageType.Simple);
            ATR_Indicator_2 = Indicators.AverageTrueRange(temp_1, 27, MovingAverageType.Simple);

            Crt_ATR_Val_1 = Math.Round(ATR_Indicator_1.Result.LastValue / t_1, 0);
            Crt_ATR_Val_2 = Math.Round(ATR_Indicator_2.Result.LastValue / t_1, 0);

            /////////////////////////////////////////////////////////////

            Print("DAILY : ATR VALUE  = " + Crt_ATR_Val_1 + ", " + Crt_ATR_Val_2);
        }
        //END METHOD Get_ATR_Values
    }
    //END OF MAIN PUBLIC CLASS
}
//END OF MAIN cALGO ROBOT

 


@GoldnOil750