Custom Library for cBots

Created at 04 Oct 2022, 00:44
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!
SI

simone.faina95

Joined 04.08.2022

Custom Library for cBots
04 Oct 2022, 00:44


Hello guys, I'm new ti cTrader, coming from MQL5. I would like to organize my custom class and methods in separate .DLL. In my custom class I need to use some class and methods coming from cAlgo.API like Bars, Chart, Print, etc. I just referenced cAlgo.API in my solution but not all these methods and class are available. How can I fix that? 

Ty


@simone.faina95
Replies

PanagiotisChar
04 Oct 2022, 08:05

Hi there,

It would be easier for us to help you if you shared some code with what you are doing. For example all the methods you mentioned are methods of the Robot class. You can only call them using a Robot object.

Aieden Technologies

Need Help? Join us on Telegram

 


@PanagiotisChar

simone.faina95
04 Oct 2022, 18:42 ( Updated at: 21 Dec 2023, 09:22 )

RE:

PanagiotisChar said:

Hi there,

It would be easier for us to help you if you shared some code with what you are doing. For example all the methods you mentioned are methods of the Robot class. You can only call them using a Robot object.

Aieden Technologies

Need Help? Join us on Telegram

 

Ty for your reply PanagiotisChar. I will try to be clearer, the use case is too create a custom library that serve more than one Robot class. 

So my custom class should look like this:

using cAlgo.API;

namespace customNamespace
{
    public class Utility
    {
        public Utility() { }
        public void customMethod()
        {
            double high = Bars[Bars.Count - 1].High;
            double low = Bars[Bars.Count - 1].Low;
            Print("High: " + high + " low: " + low);
            Chart.DrawHorizontalLine("max", high, Color.Red);
            Chart.DrawHorizontalLine("min", low, Color.Blue);
        }
    }
}

image.gifimage.gif

So in this solution I referenced the cAlgo.dll but class like Bars, Print, Chart are not usable.

They become usable if extend Robot.

using cAlgo.API;

namespace customNamespace
{
    public class Utility : Robot
    {
        public Utility() { }
        public void customMethod()
        {
            double high = Bars[Bars.Count - 1].High;
            double low = Bars[Bars.Count - 1].Low;
            Print("High: " + high + " low: " + low);
            Chart.DrawHorizontalLine("max", high, Color.Red);
            Chart.DrawHorizontalLine("min", low, Color.Blue);
        }
    }
}

Now I compile my lib and reference that in my cBot.
 

using cAlgo.API;
using customNamespace;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class TestingcBot : Robot
    {
        [Parameter(DefaultValue = "Initialize TestingcBot")]
        public string Message { get; set; }

        private Utility utility = new();

        protected override void OnStart()
        {
            Print(Message);
        }
        protected override void OnBar()
        {
            utility.customMethod();
            
        }
    }
}

No error in code but when I run the bot in backtest, I get this error:

Crashed in OnBar with NullReferenceException: Object reference not set to an instance of an object.

CBot instance [Testing cBot, EURUSD, h1] crashed with error "Crashed in OnBar with NullReferenceException: Object reference not set to an instance of an object."

I hope I have been clearer. any suggestions?


 

@simone.faina95

PanagiotisChar
05 Oct 2022, 08:19

Hi there,

public class Utility : Robot
    {
        public Utility() { }
        public void customMethod()
        {
            double high = Bars[Bars.Count - 1].High;
            double low = Bars[Bars.Count - 1].Low;
            Print("High: " + high + " low: " + low);
            Chart.DrawHorizontalLine("max", high, Color.Red);
            Chart.DrawHorizontalLine("min", low, Color.Blue);
        }
    }

You can't do this, since the Robot is instantiated by cTrader itself. A solution would be to pass the robot as a reference to the Utility class, something like this

public class Utility
    {
        public Utility() { }
        public void customMethod(Robot robot)
        {
            double high = robot.Bars[Bars.Count - 1].High;
            double low = robot.Bars[Bars.Count - 1].Low;
            robot.Print("High: " + high + " low: " + low);
            robot.Chart.DrawHorizontalLine("max", high, Color.Red);
            robot.Chart.DrawHorizontalLine("min", low, Color.Blue);
        }
    }

Aieden Technologies

Need Help? Join us on Telegram


@PanagiotisChar

simone.faina95
05 Oct 2022, 23:06

RE:

Hi there, thank you for your suggestion, now it's works well.

 

Ty


@simone.faina95