'nullable object must have a value' error - need help!

Created at 17 Jul 2016, 22:29
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!
GO

goldspanner

Joined 12.07.2016

'nullable object must have a value' error - need help!
17 Jul 2016, 22:29


Can anyone help with this error? The below code is giving me a 'nullable object must have a value' error:

 

protected override void OnTick()
        {
            var long_position = Positions.Find(label, Symbol, TradeType.Buy);
            var short_position = Positions.Find(label, Symbol, TradeType.Sell);

            if ((long_position != null && long_position_ok((double)long_position.StopLoss) == false))
            {
                close_long_position(long_position);
            }

            if ((short_position != null && short_position_ok((double)short_position.StopLoss) == false))
            {
                close_short_position(short_position);
            }
        }

 

What I don't understand is that there are example bots that do this exact same thing i.e. Positions.Find and then check if the position is null, and they work fine. Any help would be appreciated!

 

Thanks


@goldspanner
Replies

moneybiz
17 Jul 2016, 23:47

RE:
(double)long_position.StopLoss

StopLoss is nullable double which means it can be null. Correct your logic to handle "double?" data type.


@moneybiz

goldspanner
18 Jul 2016, 01:12

RE: RE:

moneybiz said:

(double)long_position.StopLoss

StopLoss is nullable double which means it can be null. Correct your logic to handle "double?" data type.

Thanks! That fixed that one, although I'm still getting the same error from here as well - any ideas?:

 

protected override void OnBar()
        {
            var long_position = Positions.Find(label, Symbol, TradeType.Buy);
            var short_position = Positions.Find(label, Symbol, TradeType.Sell);

            if (long_position != null)
                ModifyPosition(long_position, long_ATR_stop(long_position.StopLoss), null);
            else if (long_position == null && long_crossover() == true && long_bias() == true && above_avg_vol() == true && ADX_long_ok() == true)
            {
                open_long_position();
            }

            if (short_position != null)
                ModifyPosition(short_position, short_ATR_stop(short_position.StopLoss), null);
            else if (short_position == null && short_crossover() == true && short_bias() == true && above_avg_vol() == true && ADX_short_ok() == true)
            {
                open_short_position();
            }
        }

Thanks

 


@goldspanner