Topics
Replies
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.
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);
}
}
}
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
simone.faina95
05 Oct 2022, 23:06
RE:
Hi there, thank you for your suggestion, now it's works well.
Ty
@simone.faina95