Hard to explain, easy to show...

Created at 24 Apr 2019, 10:06
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!
JO

Jonkey

Joined 09.11.2015

Hard to explain, easy to show...
24 Apr 2019, 10:06


(1hr chart) - Tescode below. It relates to how a cbot initializes past data for an instance of an indicator used in the bor.

1. Log output (with Period paramter = 50): 

#####################################
01/11/2018 11:00:00.000 | Backtesting started
01/11/2018 12:00:00.000 | sma: prev: 1.1346988 last: 1.1346042 isRising? False | isFalling? True
...

NO NaNs - all good up to a period of 125

...
07/11/2018 23:00:00.000 | sma: prev: 1.1420504 last: 1.1422636 isRising? True | isFalling? False
10/11/2018 08:58:00.000 | Backtesting finished
#####################################

 

2. Log output (with Period paramter = 200):

#####################################
01/11/2018 11:00:00.000 | Backtesting started
01/11/2018 12:00:00.000 | sma: prev: NaN last: NaN isRising? False | isFalling? False
... 75 NaNs
06/11/2018 14:00:00.000 | sma: prev: NaN last: NaN isRising? False | isFalling? False
06/11/2018 15:00:00.000 | sma: prev: NaN last: 1.1379837 isRising? False | isFalling? False

06/11/2018 16:00:00.000 | sma: prev: 1.1379808 last: 1.13798455 isRising? True | isFalling? False
...
07/11/2018 23:00:00.000 | sma: prev: 1.13863155 last: 1.13869405 isRising? True | isFalling? False
10/11/2018 08:58:00.000 | Backtesting finished
#####################################

Is the best answer to solve this is to just run the bot on a higher time frame as it only populates 125 Periods of past data when the cbot starts?

Thank you.

The test code just for reference:

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Testpad : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Parameter("SMA's Period", DefaultValue = 4800, MaxValue = 9600, MinValue = 24, Step = 24)]
        public int smaPeriod { get; set; }

        [Parameter()]
        public DataSeries Source { get; set; }

        private SimpleMovingAverage sma;

        protected override void OnStart()
        {
            // Put your initialization logic here

            sma = Indicators.SimpleMovingAverage(Source, smaPeriod);

        }

        protected override void OnBar()
        {

            Print("sma: prev: {0} last: {1} isRising? {2} | isFalling? {3}", sma.Result.Last(1), sma.Result.LastValue, sma.Result.IsRising(), sma.Result.IsFalling());
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

 

 


@Jonkey
Replies

Jonkey
24 Apr 2019, 23:54

A more specific question I should have asked is:

Is there anyway I can see if the 200 Day SMA is rising or falling inside a cbot from the moment I start it?

Cheers.


@Jonkey

PanagiotisCharalampous
25 Apr 2019, 11:04

Hi jonkey,

Try this approach and let me know if it works for you

            var source = MarketData.GetSeries(TimeFrame.Daily);
            sma = Indicators.SimpleMovingAverage(source.Close, smaPeriod);

Best Regards,

Panagiotis


@PanagiotisCharalampous

Jonkey
28 Apr 2019, 12:39

RE:

Panagiotis Charalampous said:

Hi jonkey,

Try this approach and let me know if it works for you

            var source = MarketData.GetSeries(TimeFrame.Daily);
            sma = Indicators.SimpleMovingAverage(source.Close, smaPeriod);

Best Regards,

Panagiotis

Thanks for the response Panagiotis, but still no luck... It made sense when I read your code but the dataseries must initialise in the same way...

So I think the problem is the dataseries needs to be populated with more past data to correctly calculate for sma.Result.Last(1) and sma.Result.LastValue with a large period... kind of like turbo lag on a car that takes time to spool up...

Worst case could this be achieved with a custom SMA indicatior or would they initialise the same way?

All the different time frames have access to different amounts of data also when initialising... for example H1 has access to the past 125 entries and D1 has access to past 75 entries... that is just from my testing anyway.

Thanks heaps for your help with this.


@Jonkey