'nullable object must have a value' error - need help!
'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
Replies
goldspanner
18 Jul 2016, 01:12
RE: RE:
moneybiz said:
(double)long_position.StopLossStopLoss 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
moneybiz
17 Jul 2016, 23:47
RE:
StopLoss is nullable double which means it can be null. Correct your logic to handle "double?" data type.
@moneybiz