MarketSeries not working properly

Created at 17 Mar 2019, 16:59
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!
A.

a.fernandez.martinez

Joined 02.03.2019

MarketSeries not working properly
17 Mar 2019, 16:59


Hello, I made the following code to calculate the average pip variation between each close on all the graph of the selected custom timeframe (which may differ from the symbol one) but I'm having some weird bugs...

 

[Parameter("Timeframe to calculate graph's averages")]
        public TimeFrame mTimeframe { get; set; }


        // -------------------------------------------------- ON START --------------------------------------------------
        protected override void OnStart()
        {

            MarketSeries mathTimeframe = MarketData.GetSeries(mTimeframe);
            double graphVariationSum = 0;

            int count = 0;
            int marketSeriesCloseLength = MarketSeries.Close.Count;
            for (int i = marketSeriesCloseLength - 1; i >= 1; i--)
            {
                double currentClose = mathTimeframe.Close[i];
                double previousClose = mathTimeframe.Close[i - 1];

                if (currentClose > previousClose)
                    graphVariationSum += currentClose - previousClose;
                else if (currentClose < previousClose)
                    graphVariationSum += previousClose - currentClose;
                else
                    graphVariationSum += 0;

                count++;
            }

            double averageGraphVariationSum = graphVariationSum / count;


            Print("Last close = " + mathTimeframe.Close[marketSeriesCloseLength - 1]);
            Print("graph variation sum = " + To.Pips(graphVariationSum, Symbol.PipSize) + " average variation = " + To.Pips(averageGraphVariationSum, Symbol.PipSize));
        }

 

The first bug happens when I change the timeframe of the symbol, it makes the custom timeframe having a different result when it should always have the same result since it's one of the Parameters and does not change :

Symbol Timeframe = m1

Custom Timeframe = m1 (parameter name is mTimeframe)

Result of sum = 2830

 

Symbol Timeframe = h1

Custom Timeframe = m1 (parameter name is mTimeframe)

Result of sum = 45

As you can see the result changed but the custom timeframe didn't.

I can't understand why... It should stay the same because of the GetSeries : MarketData.GetSeries(mTimeframe).

My question is why does it change and how to make it calculate only on the custom selected timeframe ?

 

 

You can see the second bug on the first image.

The last close printed by the bot (mathTimeframe.Close[marketSeriesCloseLength - 1]) is not the same as the one shown on the graphic (1.13303 instead of 1.13063.).

 

 

The last bug happens when I set a custom Timeframe higher than the Timeframe of the symbol.

The value of the last close doesn't get displayed, instead I get a "Not numerical" message...

 

Thanks for your help.

 

 

 

 

 


@a.fernandez.martinez
Replies

a.fernandez.martinez
17 Mar 2019, 17:04 ( Updated at: 21 Dec 2023, 09:21 )

The first Image with better quality :

 

Data type is : Tick Data from the Server (accurate)


@a.fernandez.martinez

PanagiotisCharalampous
18 Mar 2019, 10:53

Hi a.fernandez.martinez,

Can you explain what are you trying to do in the following lines of code?

            int marketSeriesCloseLength = MarketSeries.Close.Count;
            for (int i = marketSeriesCloseLength - 1; i >= 1; i--)
            {

Since the length of each timeframe is different, you will get different results. It is also not clear to me what are you trying to achieve here therefore I cannot advise futher.

Best Regards,

Panagiotis


@PanagiotisCharalampous

a.fernandez.martinez
18 Mar 2019, 11:36

int marketSeriesCloseLength = MarketSeries.Close.Count;

I meant to do this : int marketSeriesCloseLength = mathTimeframe.Close.Count

*facepalm*

I made the change and it looks like it works, I'll double check this evening, thanks a lot (again) !

 

for (int i = marketSeriesCloseLength - 1; i >= 1; i--)
{

I'm looping through all the close prices for the selected timeframe beginning by the end.

However now that you point it out, I realise I can just loop from the beginning, I'll make the change.

 


@a.fernandez.martinez