no difference in "MarketSeries.OPEN and CLOSE" Prices in Back TESTING

Created at 15 Nov 2015, 21:00
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

no difference in "MarketSeries.OPEN and CLOSE" Prices in Back TESTING
15 Nov 2015, 21:00


Dear cALGO TEAM,

Below is the Screen shots of the result when Changing the dates and the common code.   Problem I am facing is that in "MarketSeries" function on TIME FRAME 15-minutes :

1. MarketSeries.Open [Count]

2. MarketSeries.Close [Count]

are giving similar values in both but if I use "[count - 1] or [count-2]", it gives differnet OPEN AND CLOSE PRICES which is correct.  BUT if I try to access them by [Count] only, they give same prices for both OPEN AND CLOSE.  Like if I want to get the Current Candle Body Size, I get "zero" due to same prices.

ATTACHED FILES :

Only on 8/June/15 to 12/June/15 it has given different prices (highlighted by yellow color), while all on other dates (eg. 15June15 to 19June15) it give same PRICE (red color underline on jpeg) for both OPEN and CLOSE PRICES on [COUNT].

 

Also, if the BACK-TESTING Start Date = 15June15 & END DATE = 19June15,

whereas, Count = 0, then

MarketSeries.OpenTime [ Count ] = 10-June-15 23:00,   instead of 15/June/15 00:00

Therefore, for "15-minutes" time Frame, I have to set the Count = 197 to access the BACK-TESTING Start Date.

why ?  shouldn't it start from the BACK-TESTING START DATE !

 

Thank you and waiting for your prompt reply.

///S.Khan

 

 

 

 

 

 

 

 

 

 


@GoldnOil750
Replies

Daedalusx086
15 Nov 2015, 22:42

Looks like that is being caused by the fact you're only updating the values for Current_Open_Price and Current_Close_Price at the start of each new bar.

 

That means that when you're getting the values for Current_Close_Price when only 1 tick has defined the current bar, which means the close price (the last tick) the system has for that bar also happens to be the starting (opening value) for that bar. Backtesting treats the tick flow as if it's in real-time for the bot, so won't let it see the "future" (namely the ticks yet to pass) from the bot's time reference.

 

You would need to adjust your code so it is updating the Current_Close_Price dynamically every few ticks if the Current_Close_Price is vital in your algo. 

 

Cheers.


@Daedalusx086

GoldnOil750
16 Nov 2015, 07:06

RE:

Daedalusx086 said:

Looks like that is being caused by the fact you're only updating the values for Current_Open_Price and Current_Close_Price at the start of each new bar.

 

That means that when you're getting the values for Current_Close_Price when only 1 tick has defined the current bar, which means the close price (the last tick) the system has for that bar also happens to be the starting (opening value) for that bar. Backtesting treats the tick flow as if it's in real-time for the bot, so won't let it see the "future" (namely the ticks yet to pass) from the bot's time reference.

 

You would need to adjust your code so it is updating the Current_Close_Price dynamically every few ticks if the Current_Close_Price is vital in your algo. 

 

Cheers.

 

Thank you for your comment.  What I understand is that "OnBAR" function gets triggers when a new BAR is found.  While I thought when a 'bar' has closed. 

Also, please see the first jpeg attachment. in that see the yellow underline and in that it is getting the "Close_Price" of the bar, every time perfectly.

 

also, why does cALGO throw the BAR data before the BACK-TESTING start date.  Like if the start date is 15June, but u will get data from 13/june or so and have to skip so many bars.


@GoldnOil750

Spotware
16 Nov 2015, 10:02

Dear Trader,

This is done on purpose to simulate the real time environment. When you start a cBot on a real time environment you have some candles from the past. In addition, Indicators which are calculated based on past values wouldn't work in if these candles were not existing.


@Spotware

GoldnOil750
16 Nov 2015, 10:11

RE:

Spotware said:

Dear Trader,

This is done on purpose to simulate the real time environment. When you start a cBot on a real time environment you have some candles from the past. In addition, Indicators which are calculated based on past values wouldn't work in if these candles were not existing.

 

THANK YOU !   BUT what about the OPEN AND CLOSE PRICE values.  why are they same ?  check the jpeg files.  on START DATE 8/June/15  it gives different values but if Start DATE is 15/June/15, it gives same values for both OPEN AND CLOSE in the Function MarketSeries.Open[count] or MarketSeries.Close[count].

same code, same everything just changing the START DATES on BACK TESTING !!!!

??????


@GoldnOil750

Spotware
16 Nov 2015, 12:27

Dear Trader,

When you use the count method to receive the number of the bars it includes the initial bar which has the index 0. So in other words the first bar has index 0, the second bar has index 1 and the n bar has index n-1. You can't use the count method to retrieve values in the way you do it. Since the code snippet you provide isn't in the OnBar() method and we don't know when your method is called we can't further assist you. Please also note that we don't provide coding assistance services. We more than glad to assist you with specific questions about cAlgo.API. You also contact one of our Partners or post a job in Development Jobs section for further coding assistance.


@Spotware

GoldnOil750
16 Nov 2015, 16:11

RE:

Spotware said:

Dear Trader,

When you use the count method to receive the number of the bars it includes the initial bar which has the index 0. So in other words the first bar has index 0, the second bar has index 1 and the n bar has index n-1. You can't use the count method to retrieve values in the way you do it. Since the code snippet you provide isn't in the OnBar() method and we don't know when your method is called we can't further assist you. Please also note that we don't provide coding assistance services. We more than glad to assist you with specific questions about cAlgo.API. You also contact one of our Partners or post a job in Development Jobs section for further coding assistance.

 

Dear SPOTWARE,

I have pasted the CODE HERE, BELOW to explain the frustration I am going through and will appreciate if you can explain that why do I GET  """TWO SETS OF DIFFERENT DATA"""  on OPEN AND CLOSE prices, when I CALL IT through ""OnBar()"   and when I call through "OnSTOP" function.  CAN YOU EXPLAIN THIS ?  and how do "WE" programmers know which is correct. 

Secondly, if you choose START DATE as 05-January-2015 to 09-Jan-2015,  you get 100 count of previous data, but if you chose a week in June or any other month you get around 197 count of previous data. SO HOW DO "WE" figure out what kind of previous data we will get on a particular time frame and on a particular date????

TIME FRAME = 15 minutes.  Currency = GBP/JPY

START DATE = 05-Jan-2015 to 09-Jan-15  (one week)

CODE (copy and paste and it will RUN and you can compare the two data.  One will be in the LOG FILE and the OTHER will be in a CSV file.  just compare and see for yourself.  Check from Count = 100 onwards :   THE OPEN & CLOSE PRICES in LOG file does not match with the one in CSV file.

THANK YOU !!!!

(waiting for your reply)

///S.Khan

 

 

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 SK_cBOT_BuySell_onSIGNAL : Robot
    {
        ////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////
        ////                        GLOBAL VARIABLES
        ////////////////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////////////////

        //private static string label_Trade = "B&S_SIGNAL-1";
        private int Bar_Count = 0;

        //END OF GLOBAL VARIABLES Declaration

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////                        ON START
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        protected override void OnStart()
        {
            //Print Date Time to Display Charts/Screen
            string tempText1 = string.Format("{0:ddd-d-MM-y,h:mm tt}", Server.Time);
            Print("cBOT Start Date & Time : " + tempText1);
        }
        //End of On_START  function

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////                        ON       BAR
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        protected override void OnBar()
        {
            int i = Bar_Count;

            Print(i, ",   ", MarketSeries.OpenTime[i], ",   ", MarketSeries.Open[i], ",   ", MarketSeries.Close[i], ",   ", MarketSeries.High[i], MarketSeries.Low[i],
            MarketSeries.TickVolume[i], MarketSeries.Median[i], MarketSeries.Typical[i], MarketSeries.WeightedClose[i]);

            Bar_Count += 1;
        }
        //End of On_TICK  function

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

            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            var folderPath = Path.Combine(desktopFolder, "trendbars");

            Directory.CreateDirectory(folderPath);

            var filePath = Path.Combine(folderPath, Symbol.Code + " SK " + TimeFrame + ".csv");

            using (var writer = File.CreateText(filePath))
            {
                for (var i = 0; i < MarketSeries.Close.Count + 1; i++)
                {
                    writer.WriteLine(ConcatWithComma(i, MarketSeries.OpenTime[i], MarketSeries.Open[i], MarketSeries.Close[i], MarketSeries.High[i], MarketSeries.Low[i], MarketSeries.TickVolume[i], MarketSeries.Median[i], MarketSeries.Typical[i], MarketSeries.WeightedClose[i]));
                }
                //END FOR
            }
            //END USING
        }
        //END FUNCTION : On_STOP

        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ////            FUNCTION  CONCATE WITH COMMA
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        private string ConcatWithComma(params object[] parameters)
        {
            return string.Join(",", parameters.Select(p => p.ToString()));
        }
        //END FUNCTION : Concate with Comma
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    }
    //END OF MAIN PUBLIC CLASS
    //////////////////////////////////////////////

}
//END OF MAIN cALGO ROBOT
/////////////////////////////////////////////////////////////////////////

 


@GoldnOil750

GoldnOil750
16 Nov 2015, 16:18

correction in above code

Set "Access Right"  to "Full Access" in the above code. 


@GoldnOil750

Spotware
17 Nov 2015, 07:50

Dear Trader,

We tried to reproduce your issue without success. We kindly ask you to clear your cache and Backtesting cache and try again. The cache and Backtesting cache are located in:

C:\Users\%USERNAME%\AppData\Roaming\%BROKER%-cAlgo\Cache

C:\Users\%USERNAME%\AppData\Roaming\%BROKER%-cAlgo\BacktestingCache

 


@Spotware