Using marketseries.close type stuff in custom class

Created at 09 Sep 2015, 00: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!
CL

cloesd

Joined 28.02.2015

Using marketseries.close type stuff in custom class
09 Sep 2015, 00:48


I want to create a custom class in my bot to neaten the code up a bit.

I can succeed in doing most of it however when I try to reference marketseries.close.last(2);
I get:
 

12/03/2015 00:00:01.060 | Crashed in OnStart with NullReferenceException: Object reference not set to an instance of an object.

 

public class barista : Robot
    {
        private double testvar;
        public double baristaInst()
        {
            testvar = MarketSeries.Close.Last(2);
            return testvar;
        }

    }

 I've also tried naming the class as barista : NewCbot

Still not working. (NewCbot being the name of the main robot).


@cloesd
Replies

ClickAlgo
09 Sep 2015, 08:04

Hello,

I think you are confused about Object Oriented Programming with Inheritance and just creating a sub class, which is a big subject to study:-

https://msdn.microsoft.com/en-us/library/ms173149.aspx

https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)

What you are attempting to do is just create a sub class to be used from a parent class, if you wish to use any cAlgo objects in the new class then you will need to pass them in, here is a basic example of what you are attempting to do.

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

        // declare private field for new class
        private Barrista barrista;

        protected override void OnStart()
        {
            // create an instance of the class
            barrista = new Barrista();
        }

        protected override void OnTick()
        {
            // call method of class and pass it the MarketSeries data object, returns a double
            double result = barrista.baristaInst(MarketSeries);
            Print(result.ToString());
        }
    }

    public class Barrista
    {
        // constructor
        public Barrista()
        {

        }

        // public method of class which receives the marketseries data object
        public double baristaInst(MarketSeries marketSeries)
        {
            double testvar = marketSeries.Close.Last(2);
            return testvar;
        }
    }
}

 


@ClickAlgo

cloesd
09 Sep 2015, 12:31

RE:

Paul_Hayes said:

Hello,

I think you are confused about Object Oriented Programming with Inheritance and just creating a sub class, which is a big subject to study:-

https://msdn.microsoft.com/en-us/library/ms173149.aspx

https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)

What you are attempting to do is just create a sub class to be used from a parent class, if you wish to use any cAlgo objects in the new class then you will need to pass them in, here is a basic example of what you are attempting to do.

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

        // declare private field for new class
        private Barrista barrista;

        protected override void OnStart()
        {
            // create an instance of the class
            barrista = new Barrista();
        }

        protected override void OnTick()
        {
            // call method of class and pass it the MarketSeries data object, returns a double
            double result = barrista.baristaInst(MarketSeries);
            Print(result.ToString());
        }
    }

    public class Barrista
    {
        // constructor
        public Barrista()
        {

        }

        // public method of class which receives the marketseries data object
        public double baristaInst(MarketSeries marketSeries)
        {
            double testvar = marketSeries.Close.Last(2);
            return testvar;
        }
    }
}

Ahh.. I see,
Thanks.

I understand the basics of OOP just been a while.

 


@cloesd

lec0456
23 Oct 2018, 06:07

I created a subclass to encapsulate all the indicator data that I send to a database. I tried using inheritance  with :Robot or <RobotName> and it complies but i get an exception error.

I passed in the Robot object and used this. to do it. It works for the Market series but it can not access the individual indicators. isthere a way to access the indicators of a Robot from within a subclass? I would prefer to not pass tham all in.


@lec0456

lec0456
24 Oct 2018, 08:55

RE:

lec0456 said:

I created a subclass to encapsulate all the indicator data that I send to a database. I tried using inheritance  with :Robot or <RobotName> and it complies but i get an exception error.

I passed in the Robot object and used this. to do it. It works for the Market series but it can not access the individual indicators. isthere a way to access the indicators of a Robot from within a subclass? I would prefer to not pass tham all in.

I solved my own problem.  All you hear is crickets from this forum. I passed in the Name of the cBot to the method in the subclass, no inheritance was needed. Got access to all indicators, marketseries, etc.


@lec0456

freemangreat
24 Oct 2018, 15:47

Thanks lec,

Code - is the best language of self-expression.


@freemangreat