Positions.Opened event and NullReferenceException

Created at 03 Feb 2024, 21:54
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!
CH

ChrisSpire

Joined 12.12.2022

Positions.Opened event and NullReferenceException
03 Feb 2024, 21:54


Hello.

Trying to follow this example, but with class library:

MyClassLibrary lib = new MyClassLibrary(this);

Positions.Opened += OnPositionOpened;        

public void OnPositionOpened(PositionOpenedEventArgs args)     
{         
	lib.Opened(arg.Position);	
}

in class library:

[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
public class MyClassLibrary : Robot
{
	public MyClassLibrary(Robot robot)
    {
        cBot = robot;
    }
	
	public void Opened(Position pos)     
	{         
		Print(pos.Id);
	}
}

gives me: Crashed in Positions.Opened event with NullReferenceException: Object reference not set to an instance of an object.


@ChrisSpire
Replies

PanagiotisCharalampous
05 Feb 2024, 07:01

Hi there,

Please share with us the complete cBot code and steps how to reproduce this exception.

Best regards,

Panagiotis


@PanagiotisCharalampous

ChrisSpire
09 Feb 2024, 17:19

RE: Positions.Opened event and NullReferenceException

I have updated the original post (awaiting moderation).

PanagiotisCharalampous said: 

Hi there,

Please share with us the complete cBot code and steps how to reproduce this exception.

Best regards,

Panagiotis

 


@ChrisSpire

PanagiotisCharalampous
10 Feb 2024, 08:23

RE: RE: Positions.Opened event and NullReferenceException

Lanikeha said: 

I have updated the original post (awaiting moderation).

PanagiotisCharalampous said: 

Hi there,

Please share with us the complete cBot code and steps how to reproduce this exception.

Best regards,

Panagiotis

 

Hi there,

You cannot use the Print method inside the ClassLibrary since it does not have access to the log. You should rewrite your code so that the message printing takes place inside the main cBot.

Best regards,

Panagiotis


@PanagiotisCharalampous

ChrisSpire
10 Feb 2024, 12:06

RE: RE: RE: Positions.Opened event and NullReferenceException

PanagiotisCharalampous said: 

Hi there,

You cannot use the Print method inside the ClassLibrary since it does not have access to the log. You should rewrite your code so that the message printing takes place inside the main cBot.

Best regards,

Panagiotis

Thanks! Works well!


@ChrisSpire

ctrader.guru
11 Feb 2024, 06:41 ( Updated at: 11 Feb 2024, 07:06 )

Try this approach https://github.com/cTrader-Guru/CallBack-Example/blob/master/CallBack%20Example/CallBack%20Example.cs

 

/*  CTRADER GURU

    Homepage    : https://ctrader.guru/
    Telegram    : https://t.me/ctraderguru
    Twitter     : https://twitter.com/cTraderGURU/
    Facebook    : https://www.facebook.com/ctrader.guru/
    YouTube     : https://www.youtube.com/cTraderGuru
    GitHub      : https://github.com/ctrader-guru
    LinkedIn    : https://www.linkedin.com/company/ctrader-guru/
    Discord     : https://discord.gg/yE5XE3z5Nz

*/
using System;
using cAlgo.API;
using cTraderGuru.CallbackExample;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class CallBackExample : Robot
    {
        
        protected override void OnStart()
        {
            


        }

        protected override void OnTick()
        {

            HelloWorld.Show(Print, Stop) ;

        }

        protected override void OnStop()
        {



        }

    }

}

namespace cTraderGuru.CallbackExample {

    public static class HelloWorld {

        public static void Show(Action<string> MyPrint, Action MyStop) {

            MyPrint( "Hello World cTrader Guru!" );
            MyStop();
        
        }
    
    }

}

@ctrader.guru

ChrisSpire
12 Feb 2024, 21:19 ( Updated at: 13 Feb 2024, 06:51 )

RE: Positions.Opened event and NullReferenceException

Thank you!

ctrader.guru said: 

Try this approach https://github.com/cTrader-Guru/CallBack-Example/blob/master/CallBack%20Example/CallBack%20Example.cs

 

/*  CTRADER GURU    Homepage    : https://ctrader.guru/    Telegram    : https://t.me/ctraderguru    Twitter     : https://twitter.com/cTraderGURU/    Facebook    : https://www.facebook.com/ctrader.guru/    YouTube     : https://www.youtube.com/cTraderGuru    GitHub      : https://github.com/ctrader-guru    LinkedIn    : https://www.linkedin.com/company/ctrader-guru/    Discord     : https://discord.gg/yE5XE3z5Nz*/using System;using cAlgo.API;using cTraderGuru.CallbackExample;namespace cAlgo.Robots{    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]    public class CallBackExample : Robot    {                protected override void OnStart()        {                    }        protected override void OnTick()        {            HelloWorld.Show(Print, Stop) ;        }        protected override void OnStop()        {        }    }}namespace cTraderGuru.CallbackExample {    public static class HelloWorld {        public static void Show(Action<string> MyPrint, Action MyStop) {            MyPrint( "Hello World cTrader Guru!" );            MyStop();                }        }}

 


@ChrisSpire