Positions.Opened
Positions.Opened
16 Jun 2022, 18:59
Hi Ahmad
using Positions.Opened += Positions_Opened;
If I place a order from QuickTradeButtons, ie obj.postion.comment is empty get crashed with error "Crashed in BeginInvoke with NullReferenceException: Object reference not set to an instance of an object."
If the conditions are met there is no error and the list gets filled/Added. Tried without BeginInvoke also its the same
private void Positions_Opened(PositionOpenedEventArgs obj)
{
BeginInvokeOnMainThread(() =>
{
var psn = obj.Position;
Print(GlobalSymbolName + "::Event Positions Opened....");
if (psn.SymbolName.Equals(GlobalSymbolName, StringComparison.Ordinal) && psn.TradeType == TradeType.Buy && psn.Comment.Contains("AO", StringComparison.CurrentCultureIgnoreCase)
&& !string.IsNullOrEmpty(psn.Comment) )
{
Print(GlobalSymbolName + "Buy Opened Position opened id====" + psn.Id);
// GlobalBuyPositionsIds.Add(obj.Position.Id);
// GlobalBuyPositionsIds.TrimExcess();
// Print(SymbolName + "GlobalBuyPositionsidList=" + GlobalBuyPositionsIds[0]);
}
if (psn.SymbolName.Equals(GlobalSymbolName, StringComparison.Ordinal) && psn.TradeType == TradeType.Sell && psn.Comment.Contains("AO")
&& !string.IsNullOrEmpty(psn.Comment))
{
Print(GlobalSymbolName + "Sell Opened Position opened id====" + psn.Id);
// GlobalSellPositionsIds.Add(obj.Position.Id);
// GlobalSellPositionsIds.TrimExcess();
// Print(SymbolName + "GlobalSellPositionsidList=" + GlobalSellPositionsIds[0]);
}
}
);
}
Replies
amusleh
17 Jun 2022, 10:47
Hi,
A position comment or label can be null, so you have to check them before dereferencing them otherwise you will get null reference exception.
You should your code to:
private void Positions_Opened(PositionOpenedEventArgs obj)
{
BeginInvokeOnMainThread(() =>
{
var psn = obj.Position;
Print(GlobalSymbolName + "::Event Positions Opened....");
if (psn.SymbolName.Equals(GlobalSymbolName, StringComparison.Ordinal) && psn.TradeType == TradeType.Buy && !string.IsNullOrWhiteSpace(psn.Comment) && psn.Comment.Contains("AO", StringComparison.Ordinal))
{
Print(GlobalSymbolName + "Buy Opened Position opened id====" + psn.Id);
// GlobalBuyPositionsIds.Add(obj.Position.Id);
// GlobalBuyPositionsIds.TrimExcess();
// Print(SymbolName + "GlobalBuyPositionsidList=" + GlobalBuyPositionsIds[0]);
}
if (psn.SymbolName.Equals(GlobalSymbolName, StringComparison.Ordinal) && psn.TradeType == TradeType.Sell && !string.IsNullOrWhiteSpace(psn.Comment) && psn.Comment.Contains("AO", StringComparison.Ordinal))
{
Print(GlobalSymbolName + "Sell Opened Position opened id====" + psn.Id);
// GlobalSellPositionsIds.Add(obj.Position.Id);
// GlobalSellPositionsIds.TrimExcess();
// Print(SymbolName + "GlobalSellPositionsidList=" + GlobalSellPositionsIds[0]);
}
}
);
}
@amusleh
m4trader4
17 Jun 2022, 08:45
RE:
By adding null its working, dint understand why?
if (psn != null && psn.SymbolName.Equals(GlobalSymbolName, StringComparison.Ordinal) && psn.TradeType == TradeType.Sell && psn.Comment.Contains("AO") && !string.IsNullOrEmpty(psn.Comment))
@m4trader4