Control Main Robot Class from other Class

Created at 25 Jul 2017, 03:48
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!
GA

GammaQuant

Joined 14.06.2017

Control Main Robot Class from other Class
25 Jul 2017, 03:48


Hello i am buinding a personal framework that will enable me to quickly build robots that have adanced functionaly and trade management in the framework. But i am trying to make the main robot class do soem thing in a seprate class and then the seprate class do some thing back in the main class. going from the main class to the seprate class is easy and i can ger the main robot cAlgo functrions by passing "this" to the seprate class. but when i try to do some thing back in the main classs fomr the seprate i get a NullReferenceException........ i get the error both if i call the method via inheritence of if i instansiate the main class in a new object like i have below.

this is the code i have:

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


        private PrintSub _demo;

        protected override void OnStart()
        {
            // Put your initialization logic here
            _demo = new PrintSub();
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

        protected override void OnBar()
        {
            // Put your core logic here
            _demo.POnBar(this);
        }

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

        public void MainPrinter()
        {
            int index  = MarketSeries.Close.Count - 2;
            Print("Hello World Main - Last Bar Close Price {0}", MarketSeries.High[index]);
        }
    }

    public class PrintSub : Printer
    {

        public void POnBar(Algo algo)
        {
            var price = algo.MarketSeries.Open.LastValue;
            algo.Print("Hello World - Bar Open Price {0}", price);
            PrintInMainRobot();
        }

        public void PrintInMainRobot()
        {
            Printer go = new Printer();
            go.MainPrinter();
        }
    }

Does anyone have anysudgestions how to overcome this problem?

Thank you


@GammaQuant
Replies

GammaQuant
25 Jul 2017, 03:49

sorry for the bad spelling


@GammaQuant

GammaQuant
25 Jul 2017, 03:55

RE:

Hello i am building a personal framework that will enable me to quickly
build robots that have advanced functionality and trade management in the
framework. But i am trying to make the main robot class do some thing in
a separate class and then the separate class do some thing back in the
main class. going from the main class to the separate class is easy and i
can ger the main robot cAlgo functions by passing "this" to the
separate class. but when i try to do some thing back in the main classs
from the separate i get a NullReferenceException........ i get the error
both if i call the method via inheritance of if i instantiate the main
class in a new object like i have below.


@GammaQuant

Spotware
25 Jul 2017, 10:20

Hi GammaQuant,

There isn't any obvious issue in your code, so maybe you could try debugging your cBot in VS and add a try/catch around your POnBar() initialization code, like below, and check what is going wrong

            try
            {
                var price = algo.MarketSeries.Open.LastValue;
                algo.Print("Hello World - Bar Open Price {0}", price);
                PrintInMainRobot();
            }
            catch
            {

            }

Let us know if the above helps

Best Regards,

cTrader Team


@Spotware

GammaQuant
25 Jul 2017, 12:26

Hello Thank you for the reply. I just tried this on my home computer with the try catch.

and im still getting the error it not stoppin gthe application but its giving the error each time it tries to acces the methods in the main class.

I dont have Visual Studio at home i have it in the office but i use 2015 CE and debugging does not work on it.........

i will have another go at debugging when i get into the office.

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


        private PrintSub _demo;

        protected override void OnStart()
        {
            // Put your initialization logic here
            _demo = new PrintSub();
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

        protected override void OnBar()
        {
            // Put your core logic here
            _demo.POnBar(this);
        }

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

        public void MainPrinter()
        {
            int index = MarketSeries.Close.Count - 2;
            Print("Hello World Main - Last Bar Close Price {0}", MarketSeries.High[index]);
        }
    }

    public class PrintSub : Printer
    {

        public void POnBar(Algo algo)
        {
            try
            {
                var price = algo.MarketSeries.Open.LastValue;
                algo.Print("Hello World - Bar Open Price {0}", price);
                PrintInMainRobot();
            } catch (NullReferenceException ex)
            {
                algo.Print("Null Reference Exception yo! {0}", ex.Message);
            }

        }

        public void PrintInMainRobot()
        {
            Printer go = new Printer();
            go.MainPrinter();
        }
    }
}

13/07/2017 00:00:00.000 | Backtesting started
13/07/2017 01:00:00.000 | Hello World - Bar Open Price 1.14295
13/07/2017 01:00:00.000 | Null Reference Exception yo! Object reference not set to an instance of an object.
13/07/2017 02:00:00.000 | Hello World - Bar Open Price 1.14343
13/07/2017 02:00:00.000 | Null Reference Exception yo! Object reference not set to an instance of an object.
13/07/2017 03:00:00.000 | Hello World - Bar Open Price 1.1437
13/07/2017 03:00:00.000 | Null Reference Exception yo! Object reference not set to an instance of an object.
13/07/2017 04:00:00.000 | Hello World - Bar Open Price 1.14342
13/07/2017 04:00:00.000 | Null Reference Exception yo! Object reference not set to an instance of an object.
13/07/2017 05:00:00.000 | Hello World - Bar Open Price 1.14372
13/07/2017 05:00:00.000 | Null Reference Exception yo! Object reference not set to an instance of an object.
13/07/2017 06:00:00.000 | Hello World - Bar Open Price 1.14472
13/07/2017 06:00:00.000 | Null Reference Exception yo! Object reference not set to an instance of an object.
13/07/2017 07:00:00.000 | Hello World - Bar Open Price 1.14469
13/07/2017 07:00:00.000 | Null Reference Exception yo! Object reference not set to an instance of an object.
13/07/2017 08:00:00.000 | Hello World - Bar Open Price 1.14215
13/07/2017 08:00:00.000 | Null Reference Exception yo! Object reference not set to an instance of an object.
13/07/2017 09:00:00.000 | Hello World - Bar Open Price 1.14037
13/07/2017 09:00:00.000 | Null Reference Exception yo! Object reference not set to an instance of an object.
13/07/2017 10:00:00.000 | Hello World - Bar Open Price 1.13961
13/07/2017 10:00:00.000 | Null Reference Exception yo! Object reference not set to an instance of an object.
13/07/2017 11:00:00.000 | Hello World - Bar Open Price 1.13883
13/07/2017 11:00:00.000 | Null Reference Exception yo! Object reference not set to an instance of an object.
13/07/2017 12:00:00.000 | Hello World - Bar Open Price 1.14105
13/07/2017 12:00:00.000 | Null Reference Exception yo! Object reference not set to an instance of an object.
13/07/2017 13:00:00.000 | Hello World - Bar Open Price 1.14158
13/07/2017 13:00:00.000 | Null Reference Exception yo! Object reference not set to an instance of an object.
13/07/2017 14:00:00.000 | Hello World - Bar Open Price 1.13988
13/07/2017 14:00:00.000 | Null Reference Exception yo! Object reference not set to an instance of an object.
13/07/2017 15:00:00.000 | Hello World - Bar Open Price 1.1404
13/07/2017 15:00:00.000 | Null Reference Exception yo! Object reference not set to an instance of an object.
13/07/2017 16:00:00.000 | Hello World - Bar Open Price 1.13842
13/07/2017 16:00:00.000 | Null Reference Exception yo! Object reference not set to an instance of an object.
13/07/2017 17:00:00.000 | Hello World - Bar Open Price 1.13908
13/07/2017 17:00:00.000 | Null Reference Exception yo! Object reference not set to an instance of an object.
13/07/2017 18:00:00.000 | Hello World - Bar Open Price 1.13976
13/07/2017 18:00:00.000 | Null Reference Exception yo! Object reference not set to an instance of an object.
13/07/2017 19:00:00.000 | Hello World - Bar Open Price 1.1404
13/07/2017 19:00:00.000 | Null Reference Exception yo! Object reference not set to an instance of an object.
13/07/2017 20:00:00.000 | Hello World - Bar Open Price 1.14067
13/07/2017 20:00:00.000 | Null Reference Exception yo! Object reference not set to an instance of an object.
13/07/2017 21:01:00.000 | Hello World - Bar Open Price 1.13975
13/07/2017 21:01:00.000 | Null Reference Exception yo! Object reference not set to an instance of an object.
13/07/2017 22:00:00.000 | Hello World - Bar Open Price 1.13998
13/07/2017 22:00:00.000 | Null Reference Exception yo! Object reference not set to an instance of an object.
13/07/2017 23:00:00.000 | Hello World - Bar Open Price 1.13956
13/07/2017 23:00:00.000 | Null Reference Exception yo! Object reference not set to an instance of an object.
13/07/2017 23:59:00.000 | Backtesting finished

 


@GammaQuant

GammaQuant
25 Jul 2017, 16:20

Hi Spotware.

So after some tinkering In order to make this work i had to pass the algo object back into the Main class and call Marketserise and the Print method as methods of the algo object. This also means if i need to execute a new order from another class in the main class i need to pass around the robot object and any other object that is part of cAlgo api to access that method or property like Positions etc..:

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


        private PrintSub _demo;

        protected override void OnStart()
        {
            // Put your initialization logic here
            _demo = new PrintSub();
            
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

        protected override void OnBar()
        {
            // Put your core logic here
            _demo.POnBar(this, this, Positions);
        }

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

        public void MainPrinter(Algo algo, Robot robot, Positions Positions)
        {
            int index = algo.MarketSeries.Close.Count - 2;

            algo.Print("Hello World Main - Last Bar High Price {0}", algo.MarketSeries.High[index]);
            robot.ExecuteMarketOrder(TradeType.Buy, algo.Symbol, 100000, "Printer", 5, 5);
        }
    }

    public class PrintSub : Printer
    {

        public Printer Go;
        public void POnBar(Algo algo, Robot robot, Positions Positions)
        {
            try
            {
                var price = algo.MarketSeries.Open.LastValue;
                algo.Print("Hello World - Bar Open Price {0}", price);
                PrintInMainRobot(algo, robot, Positions);
            }
            catch (Exception ex)
            {
                algo.Print("Null Reference Exception yo! {0}", ex.Message);
            }
        }

        public void PrintInMainRobot(Algo algo, Robot robot, Positions Positions)
        {
            Go = new Printer();
            Go.MainPrinter(algo, robot, Positions);
        }
    }
}

So once you leave the main class any other class (including the main robot class) does not have access to cAlgo Api. Is there a way to make it available always in the main clas?. As many of my methods can be invoked as normal from within the Robot Class and also from outside classes where i will need to pass the calgo api. this means i may have to put two methods or some extra conditional statement now one for use within the Main Robot class and one for seperate classes. How can the cAlgo Api be made availble in the Main Class without me having to pass it around?


@GammaQuant

Spotware
25 Jul 2017, 16:39

Hi GammaQuant,

In your first sample you should modify your PrintInMainRobot() as follows

 public void PrintInMainRobot(Printer algo)
 {
        algo.MainPrinter();
 }

Hope this helps.

Best Regards,

cTrader Team


@Spotware

GammaQuant
25 Jul 2017, 17:38

Thank you very much for that Wow that obvious isn't it...... :). Sorry ive only be programing c# for 1.5 months. but i exspanded on your solution so that i only have to pass it once in the onstart and then i use a static method every where i invoke calgo api methods out side of the main class and any methods within the main class from any other of the classes that ive created for my framework. Thank you

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


        private PrintSub _demo;

        protected override void OnStart()
        {
            // Put your initialization logic here
            Po.Load(this);
            _demo = new PrintSub();
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

        protected override void OnBar()
        {
            // Put your core logic here
            _demo.POnBar();
        }

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

        public void MainPrinter()
        {
            int index = MarketSeries.Close.Count - 2;
            Print("Hello World Main - Last Bar Close Price {0}", MarketSeries.High[index]);
            ExecuteMarketOrder(TradeType.Buy, Symbol, 100000, "Printer", 5, 5);
        }
    }

    public class PrintSub : Printer
    {

        public void POnBar()
        {
            var price = Po.Go().MarketSeries.Open.LastValue;
            Po.Go().Print("Hello World - Bar Open Price {0}", price);
            PrintInMainRobot();
        }

        public void PrintInMainRobot()
        {
            Po.Go().MainPrinter();
        }
    }

    public static class Po
    {
        public static Printer MainClass;

        public static void Load(Printer mainClass)
        {
            MainClass = mainClass;
        }

        public static Printer Go()
        {
            return MainClass;
        }
    }
}

 


@GammaQuant