Looking for confirmation: static variables accessible across all bot instances?

Created at 04 Oct 2019, 10:31
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!
FI

firemyst

Joined 26.03.2019

Looking for confirmation: static variables accessible across all bot instances?
04 Oct 2019, 10:31


Hello Team Spotware:

Just wanted to confirm support that static variables declared as public in cbots are visible/accessible under every instance for a particular cBot?

So I have a bot with a DataSeries declared as:

        private DataSeries _closeLongSeries;

Instead of having this variable sucking up memory for every instance, I'd like to declare it as:

        public static DataSeries _closeLongSeries;

so there's only 1 instance of it across the 21 bot instances instead of 1 for each bot instance since it will be the same.

Is this supported in cAlgo with bots?

This question was also posed in this thread ( #8 ), but never answered:

https://ctrader.com/forum/cbot-support/11907

 

Thank you.


@firemyst
Replies

PanagiotisCharalampous
04 Oct 2019, 10:43

Hi FireMyst,

Yes this should be the case. You can check it with the example below

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 NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        public static int Counter;
        protected override void OnStart()
        {
            // Put your initialization logic here
        }

        protected override void OnTick()
        {
            Counter++;
            Print(Counter);
        }

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

Best Regards,

Panagiotis


@PanagiotisCharalampous

firemyst
04 Oct 2019, 10:51

Awesome!

Thank you for the confirmation @Panagiotis!

 


@firemyst