question for onPositionClosed in cBots
question for onPositionClosed in cBots
10 Dec 2020, 09:29
private void OnPositionsClosed(PositionClosedEventArgs args)
{
if (args.Reason == PositionCloseReason.StopLoss)
{
SendMessage(_serverName + " / " + _brokerName + " / " + Account.Number + "\n" + SymbolName + " closed by StopLoss " + TimeInUtc);
}
}
Dear Panagiotis,
I have this method in my cBot to report closind of a position in case of a triggered stoploss. It has worked great while testing with a single instance. Now I'm facing the problem, that, when running more instances on different currencies, and one is triggered, I get messages for each and all currency pairs, multiple messages.
Normally I check "if (_pos.SymbolName == Symbol.Name)" but this don't work here.
Cant you please be so kind and give me a hint, how to solve this problem? How can I achive, that position closing event is only triggered for the currency the cBot is running for?
KIndest regards,
Replies
xabbu
10 Dec 2020, 12:13
( Updated at: 21 Dec 2023, 09:22 )
Hey Panagiotis,
the code works, it sends a message if a position is closed.
The problem is, I have 35 instances running on that code, and when an position is closed due to a stoploss in ONE instance, EVERY instance sends this message, even if there are no open or closed positions...
@xabbu
xabbu
10 Dec 2020, 13:44
Dear Panagiotis,
I have found (?) this solution to my problem:
if (args.Reason == PositionCloseReason.StopLoss)
{
foreach (var _hist in History)
{
if (_hist.SymbolName == SymbolName && _hist.ClosingTime >= Time.Date)
{
SendMessage(_serverName + " / " + _brokerName + " / " + Account.Number + "\n" + SymbolName + " closed by StopLoss " + _hist.ClosingTime);
}
}
}
What do you think, could this be the right solution? Or do you recommend another and more elegant way...?
Kindest regards
@xabbu
PanagiotisCharalampous
10 Dec 2020, 14:35
Hi xabbu,
I still do not understand why you do not check the symbol of the closed position. You posted the solution yourself in the first post.
Best Regards,
Panagiotis
@PanagiotisCharalampous
xabbu
11 Dec 2020, 11:20
Sorry Panagiotis,
I don't get it. What should I do / modify? Even if I use
I will get the message for ALL positions open, not only for the pair where the TP has occured. I have no idea anymore - what do I'm doing wrong here?
private void OnPositionsClosed(PositionClosedEventArgs args)
{
if (args.Reason == PositionCloseReason.TakeProfit)
{
foreach (var _pos in Positions)
{
if (_pos.SymbolName == Symbol.Name)
{
//ModifyPositionAsync(_pos, _pos.EntryPrice, _pos.TakeProfit);
ModifyPosition(_pos, _pos.EntryPrice, _pos.TakeProfit);
SendMessage(_serverName + " / " + _brokerName + " / " + Account.Number + "\n" + SymbolName + " closed by TakeProfit " + TimeInUtc);
}
}
}
}
@xabbu
PanagiotisCharalampous
11 Dec 2020, 12:18
Hi xabbu,
You need to check if the position that triggered this event has the same symbol as the one the instance is running on. Like below
private void OnPositionsClosed(PositionClosedEventArgs args)
{
if (args.Position.SymbolName == Symbol.Name)
{
// Run your code
}
}
Best Regards,
Panagiotis
@PanagiotisCharalampous
PanagiotisCharalampous
10 Dec 2020, 10:33
Hi xabbu,
I am not sure why do you think that the check will not work. It should work. Did you check it and it didn't? What is the actual code you used? What happened?
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous