Creating other classes and Object reference exception

Created at 02 Aug 2016, 20:51
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!
EG

egi.messito

Joined 02.08.2016

Creating other classes and Object reference exception
02 Aug 2016, 20:51


HI All

I am using cAlgo and Visual Studio. 

I want to incorporate logic and other objects in other classes. 

I have a number of issues:

 

1) when i try to access MarketSeries.High[i] or .Low[i] i get a nullpointer exception as if the static method is not recognized. The compilation has no wornings or errors. In cAlgo i get object reference not set to an object.

It happens also for the method Print("");

Is there something i have to add in the other classes i use to make sure this doesnt happen? 

2)From cAlgo, although doesnt give any error and compiles fine, i dont get the code complition help like for the rest of the API. So a method from my own class would not autocomplete. I dont midn too much as i work on the classes from Visual Studio. It is only annoying.

The class is like this:

I am going mad trying to understand what do i need to add to avoid MarketSeries to give me the nullpointerexception thanks

class Candy:Robot
    {
        private double High, Low, Open, Close, Size, Center, QuarterUp, QuarterDwn;

        List<CandyType> candyQualities;
        
        public Candy(int i) {
            candyQualities = new List<CandyType>();
            //
            try { 
                Print("candle index "+i+" initialized");
                AnalyzeCandle(i);
            }
            catch (Exception e)
            {
                Print("{0} ", e.StackTrace);

            }
        }

        public void AnalyzeCandle(int i){
            High = MarketSeries.High[i];
            Low = MarketSeries.Low[i];
            Open = MarketSeries.Open[i];
            Close = MarketSeries.Close[i];
            Size = Math.Abs(High - Low);
            Center = High - (High-Low)/2;
            QuarterUp = High - (High - Center) / 2;
            QuarterDwn = High - Size*3/4;
}
}

 

 


@egi.messito
Replies

egi.messito
02 Aug 2016, 21:24

RE:

At a more basic level 

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; }

        public Testami t;

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

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

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

    public class Testami : Robot
    {
        public Testami()
        {
            var index = MarketSeries.Close.Count - 1;

            double high = MarketSeries.High[index - 3];

            Print("test");
        }
    }
}

this would crash - why i cannot use Print and MarketSeries in the custom class?


@egi.messito

egi.messito
03 Aug 2016, 15:49

anybody?


@egi.messito

egi.messito
03 Aug 2016, 16:51

Solved by passing 

StrategyTest st = new StrategyTest(MarketData.GetSeries(TimeFrame));

But i can still not Print(""); from another class

 

what do i need to initiate in the other class to be able to Print?


@egi.messito

egi.messito
03 Aug 2016, 17:08

Solved:

Need to pass (this) in the custome class to get the Printing function into the log. 

this is the easier way other then using reflection...

public class CustomClass
{
    private MyRobot _robot;
  
    public CustomClass(MyRobot robot)
    {
        _robot = robot;
    }
  
    public void Print(string message)
    {
         _robot.PrintToLog(message)
    }
}

So in my code:

StrategyTest st = new StrategyTest(MarketData.GetSeries(TimeFrame), this);
            st.doSomething();

 


@egi.messito