Problem with creating a new class
Problem with creating a new class
24 Aug 2018, 11:36
Hello,
I need to create a new class, but I still have the same error message:
Crashed in OnStart with NullReferenceException: Object reference not set to an instance of an object.
Here is an example of what you want to do:
using System; using System.Linq; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class NewTest : Robot { [Parameter(DefaultValue = 0.0)] public double Parameter { get; set; } protected override void OnStart() { MaClass GH = new MaClass(2, 5); Print(GH.prix); Print(GH.test()); } protected override void OnTick() { // Put your core logic here } protected override void OnStop() { // Put your deinitialization logic here } } public class MaClass : Robot { public int a { get; set; } public int b { get; set; } public int c { get; set; } public int resultat { get; set; } public double prix { get; set; } public MaClass(int A, int B) { a = A; b = B; c = 12; resultat = 0; prix = Symbol.Ask; } public int test() { resultat = a + b + c; return resultat; } } }
The error comes from line 53, when I use: "Symbol.Ask", in my new class.
I do not understand why ?
Thanks in advance.
Sorry, if my english is not good ...
Replies
courquinpasisa
24 Aug 2018, 12:14
A big thank you for this quick response.
I tried, and it works.
Could you explain to me why I can not use the functions of cAlgo.API in my new class.
Here is another example that returns the same error message.
using System; using System.Linq; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class NewTest : Robot { [Parameter(DefaultValue = 0.0)] public double Parameter { get; set; } protected override void OnStart() { MaClass GH = new MaClass(2, 5, Symbol.Ask); Print(GH.prix); Print(GH.test()); } protected override void OnTick() { // Put your core logic here } protected override void OnStop() { // Put your deinitialization logic here } } public class MaClass : Robot { public int a { get; set; } public int b { get; set; } public int c { get; set; } public int resultat { get; set; } public double prix { get; set; } public bool val; public MaClass(int A, int B, double C) { a = A; b = B; c = 12; resultat = 0; prix = C; foreach (var position in Positions) { if (position.TradeType == TradeType.Sell) val = true; else val = false; } } public int test() { resultat = a + b + c; return resultat; } } }
Anything in parameter is a solution, but is there another way of doing things.
Maybe by declaring my new class in another way, in order to use the functions of cAlgo.API?
@courquinpasisa
PanagiotisCharalampous
24 Aug 2018, 12:20
Hi courquinpasisa,
Symbol and other properties are populated only after the cBot is attached to a chart. MaClass is not attached to a chart, NewTest is, therefore the properties are not populated and you need to pass them as parameters.
Best Regards,
Panagiotis
@PanagiotisCharalampous
courquinpasisa
24 Aug 2018, 12:27
Hi Panagiotis,
Thank you for this explanation, it is now much clearer to me.
Best regards.
@courquinpasisa
courquinpasisa
29 Aug 2018, 15:36
Hi Panagiotis,
The explanations you gave me allowed me to move forward in my project. But I have a problem again, I wish a method of my class sends an order on the market. Here is an example :
using System; using System.Linq; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class NewTest : Robot { [Parameter(DefaultValue = 0.0)] public double Parameter { get; set; } public string TextReturn; public Symbol eurUsd; protected override void OnStart() { eurUsd = MarketData.GetSymbol("EURUSD"); MaClass3 GH = new MaClass3(2, 5, Symbol.Ask); GH.ordre(eurUsd); } protected override void OnTick() { // Put your core logic here } protected override void OnStop() { // Put your deinitialization logic here } } public class MaClass3 : Robot { private int a { get; set; } private int b { get; set; } private int c { get; set; } private Symbol EurUsd; public MaClass3(int A, int B, double C) { a = A; b = B; c = C; } public void ordre(Symbol EU) { EurUsd = EU; ExecuteMarketOrder(TradeType.Buy, EurUsd, 1000); } } }
I'm trying to pass the "MarketData.GetSymbol" parameter but I have the same error message: Crashed in OnStart with NullReferenceException: The object reference is not set to an instance of an object.
Is it possible to use ExecuteMarketOrder in a method of my new class.
Thank you in advance.
@courquinpasisa
PanagiotisCharalampous
29 Aug 2018, 15:47
Hi courquinpasisa,
The exception happens for the same reason as before. Why do you need to handle these operations in a separate class?
Best Regards,
Panagiotis
@PanagiotisCharalampous
courquinpasisa
29 Aug 2018, 16:07
I have several robots that I test, and I want to create a new class whose role is to manage the orders executed by these robots and possibly to place a new order (hedging).
This new class can be used easily in my different robots.
Best regards,
@courquinpasisa
PanagiotisCharalampous
29 Aug 2018, 16:18
Hi courquinpasisa,
You could consider making this class a base class and program all the robots to inherit from it. See below
using System; using System.Linq; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class NewTest : BaseRobot { [Parameter(DefaultValue = 0.0)] public double Parameter { get; set; } public string TextReturn; public Symbol eurUsd; protected override void OnStart() { eurUsd = MarketData.GetSymbol("EURUSD"); order(eurUsd); } protected override void OnTick() { // Put your core logic here } protected override void OnStop() { // Put your deinitialization logic here } } public class BaseRobot : Robot { public BaseRobot() { } public void order(Symbol EU) { ExecuteMarketOrder(TradeType.Buy, EU, 1000); } } }
Best Regards,
Panagiotis
@PanagiotisCharalampous
courquinpasisa
29 Aug 2018, 16:50
I did not think about this possibility.
I think it's a very good idea. I will rethink my new class as a basic class.
Thank you for this solution and for your exelent work on the forum.
Best regards.
@courquinpasisa
PanagiotisCharalampous
24 Aug 2018, 11:41
Hi courquinpasisa,
Try to pass the Symbol.Ask value as a parameter to the new object rather than accessing it directly.
Best Regards,
Panagiotis
@PanagiotisCharalampous