How to initialise indicator to a field, located in other code file

Created at 22 Feb 2022, 09:24
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!
MA

mark.lite

Joined 03.07.2019

How to initialise indicator to a field, located in other code file
22 Feb 2022, 09:24


I have cBot split in to multiple code-files / classes due to the size of it, and for easy management. Everything works, however I can't seem to get access to:

  • Indicators (cAlgo.API.Indicators)
  • Bars (cAlgo.API.Internals)
  • Account (cAlgo.API)

from outside of the main Robot's public class Mycbot : Robot (cAlgo.Robots)

 

Here is the example method located in main class:

//// that is within namespace cAlgo.Robots

public void InitiateIndicators()
        {
                // Initialize Indicators with settings
                if (Profile.UseRSIOpen_bool)
                {
                    TraderIndicators.RSIOpen = Indicators.RelativeStrengthIndex(Bars.ClosePrices, Profile.RsiPeriodsClose_int);
                    TraderIndicators.SMARSIOp = Indicators.SimpleMovingAverage(TraderIndicators.RSIOpen.Result, 3);
                }
                if (Profile.UseRSIClose_bool)
                {
                    TraderIndicators.RSIClose = Indicators.RelativeStrengthIndex(Bars.ClosePrices, Profile.RsiPeriodsClose_int);
                    TraderIndicators.SMARSICl = Indicators.SimpleMovingAverage(TraderIndicators.RSIClose.Result, 3);
                }
        }

 

where TraderIndicators is a public class located in another code file.

problem: If I try to initialize indicators or access Account info in another class, no matter what I specify in namespaces, or which prefixes I use... I always get errors CS0120 or CS1955

I'm using the same namespace in another code file, also have these namespaces indicated:

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


question: How to access Indicators, Bars, Account from other class rather then from the main : Robot class? 


@mark.lite
Replies

amusleh
22 Feb 2022, 10:46

Hi,

Those are properties of Algo class which is the base class of Robot and Indicator.

You can't access them outside the scope of Indicator/Robot classes, you can access them if you pass the Robot/Indicator instance to other class that you want to use, here is an example:

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Test : Robot
    {
        private MyClass _myClass;

        protected override void OnStart()
        {
            _myClass = new MyClass(this);
        }
    }

    // This class can be on another code file or same code file
    public class MyClass
    {
        private readonly Algo _algo;

        // Algo is located at cAlgo.API.Internals
        // You have to add it's namespace to use it
        // You can also use Robot/Indicator instead of Algo
        // In case you need more specific functions of them
        // Algo only has the common methods and properties
        // between Indicator and Robot
        public MyClass(Algo algo)
        {
            _algo = algo;
        }

        private SimpleMovingAverage _sma;

        public void DoSomething()
        {
            var bars = _algo.Bars;

            // You can use Algo Chart property for drawing and other chart related stuff
            var chart = _algo.Chart;

            _sma = _algo.Indicators.SimpleMovingAverage(bars.ClosePrices, 20);
        }
    }
}

 


@amusleh

mark.lite
22 Feb 2022, 19:38

RE:

ok thx,

And how can I access methods located in Algo?

for example how to Print("") message from another class? 

_algo.Print() is not accessable in this case..


@mark.lite

amusleh
23 Feb 2022, 08:38

RE: RE:

mark.lite said:

ok thx,

And how can I access methods located in Algo?

for example how to Print("") message from another class? 

_algo.Print() is not accessable in this case..

Hi,

You can call the Print method, it's accessible:

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Test : Robot
    {
        private MyClass _myClass;

        protected override void OnStart()
        {
            _myClass = new MyClass(this);

            // Using print via property
            _myClass.Print("Test");
        }
    }

    // This class can be on another code file or same code file
    public class MyClass
    {
        private readonly Algo _algo;

        // Algo is located at cAlgo.API.Internals
        // You have to add it's namespace to use it
        // You can also use Robot/Indicator instead of Algo
        // In case you need more specific functions of them
        // Algo only has the common methods and properties
        // between Indicator and Robot
        public MyClass(Algo algo)
        {
            _algo = algo;
        }

        private SimpleMovingAverage _sma;

        // You can also use this property if you are using
        // this class instance from another class
        public Action<string> Print
        {
            get
            {
                return _algo.Print;
            }
        }

        public void DoSomething()
        {
            var bars = _algo.Bars;

            // You can use Algo Chart property for drawing and other chart related stuff
            var chart = _algo.Chart;

            _sma = _algo.Indicators.SimpleMovingAverage(bars.ClosePrices, 20);

            // Calling Print
            _algo.Print("Something");
        }
    }
}

 


@amusleh