OnPositionClosed event firing multiple times
OnPositionClosed event firing multiple times
07 May 2024, 14:44
Hello all. I'm trying to log a position when it closes, writing the position details to a csv file. It seems the event is firing multiple times as the csv has multiple entries for the position, confirmed by the position.id.
Grateful for any advice.
private void OnPositionClosed(PositionClosedEventArgs args)
{
LogTrade(args.Position);
}
Replies
mattbarfoot
09 May 2024, 20:56
( Updated at: 10 May 2024, 05:27 )
RE: OnPositionClosed event firing multiple times
PanagiotisCharalampous said:
Hi there,
Can you please share with us the source code of your cBot and provide us with instructions on how to reproduce this problem?
Best regards,
Panagiotis
Hey Panagiotis, I am attempting to log each position that closes in either the OnPositionClosed event or a CloseTrade sub. Multiple cbots with multiple instances will be running. Positions are logged in a csv file. Typically multiple entries are created in the csv which have different instanceIDs but have the same Label, which is set to the InstanceId when opening.
scenario 1 (CloseTrade)
Position Opened: ExecuteMarketOrder(TradeType.Buy, SymbolName, adjustedContracts, InstanceId, null, null);
Position Closes on x number of bars being reached, which then calls CloseTrade
private void CloseTrade(Position position)
{
LogTrade(position);
ClosePosition(position);
}
scenario 2 (OnPositionClosed event)
Position Opened:
ExecuteMarketOrder(TradeType.Sell, SymbolName, adjustedContracts, InstanceId, StopLossPips, TargetPips);
result.Position.ModifyTrailingStop(true);
private void OnPositionClosed(PositionClosedEventArgs args)
{
LogTrade(args.Position);
}
private void LogTrade(Position _position)
{
string log = $"{strategyName},{DateTime.Now},{Chart.TimeFrame},{0},{_position.TradeType},{_position.EntryTime}, {DateTime.UtcNow},{_position.Symbol},{_position.Pips}, {_position.NetProfit},{_position.GrossProfit},{_position.Id},{_position.Label}";
string Path;
File.AppendAllText(Path,log + Environment.NewLine);
}
@mattbarfoot
PanagiotisCharalampous
10 May 2024, 05:38
RE: RE: OnPositionClosed event firing multiple times
mattbarfoot said:
PanagiotisCharalampous said:
Hi there,
Can you please share with us the source code of your cBot and provide us with instructions on how to reproduce this problem?
Best regards,
Panagiotis
Hey Panagiotis, I am attempting to log each position that closes in either the OnPositionClosed event or a CloseTrade sub. Multiple cbots with multiple instances will be running. Positions are logged in a csv file. Typically multiple entries are created in the csv which have different instanceIDs but have the same Label, which is set to the InstanceId when opening.
scenario 1 (CloseTrade)
Position Opened: ExecuteMarketOrder(TradeType.Buy, SymbolName, adjustedContracts, InstanceId, null, null);
Position Closes on x number of bars being reached, which then calls CloseTrade
private void CloseTrade(Position position)
{
LogTrade(position);
ClosePosition(position);
}
scenario 2 (OnPositionClosed event)
Position Opened:
ExecuteMarketOrder(TradeType.Sell, SymbolName, adjustedContracts, InstanceId, StopLossPips, TargetPips);
result.Position.ModifyTrailingStop(true);
private void OnPositionClosed(PositionClosedEventArgs args)
{
LogTrade(args.Position);}
private void LogTrade(Position _position)
{
string log = $"{strategyName},{DateTime.Now},{Chart.TimeFrame},{0},{_position.TradeType},{_position.EntryTime}, {DateTime.UtcNow},{_position.Symbol},{_position.Pips}, {_position.NetProfit},{_position.GrossProfit},{_position.Id},{_position.Label}";
string Path;
File.AppendAllText(Path,log + Environment.NewLine);}
Hi there,
If multiple instances are running then the event will be triggered multiple times, once per instance. You should check inside the event handler if the closed position has been created by the specific instance and only then log it.
Best regards,
Panagiotis
@PanagiotisCharalampous
mattbarfoot
10 May 2024, 06:43
RE: RE: RE: OnPositionClosed event firing multiple times
PanagiotisCharalampous said:
mattbarfoot said:
PanagiotisCharalampous said:
Hi there,
Can you please share with us the source code of your cBot and provide us with instructions on how to reproduce this problem?
Best regards,
Panagiotis
Hey Panagiotis, I am attempting to log each position that closes in either the OnPositionClosed event or a CloseTrade sub. Multiple cbots with multiple instances will be running. Positions are logged in a csv file. Typically multiple entries are created in the csv which have different instanceIDs but have the same Label, which is set to the InstanceId when opening.
scenario 1 (CloseTrade)
Position Opened: ExecuteMarketOrder(TradeType.Buy, SymbolName, adjustedContracts, InstanceId, null, null);
Position Closes on x number of bars being reached, which then calls CloseTrade
private void CloseTrade(Position position)
{
LogTrade(position);
ClosePosition(position);
}
scenario 2 (OnPositionClosed event)
Position Opened:
ExecuteMarketOrder(TradeType.Sell, SymbolName, adjustedContracts, InstanceId, StopLossPips, TargetPips);
result.Position.ModifyTrailingStop(true);
private void OnPositionClosed(PositionClosedEventArgs args)
{
LogTrade(args.Position);}
private void LogTrade(Position _position)
{
string log = $"{strategyName},{DateTime.Now},{Chart.TimeFrame},{0},{_position.TradeType},{_position.EntryTime}, {DateTime.UtcNow},{_position.Symbol},{_position.Pips}, {_position.NetProfit},{_position.GrossProfit},{_position.Id},{_position.Label}";
string Path;
File.AppendAllText(Path,log + Environment.NewLine);}
Hi there,
If multiple instances are running then the event will be triggered multiple times, once per instance. You should check inside the event handler if the closed position has been created by the specific instance and only then log it.
Best regards,
Panagiotis
Hey Panagiotis, So to confirm, a new instance is created every time a new position opens. So would the code below reference the correct instance:
ExecuteMarketOrder(TradeType.Sell, SymbolName, adjustedContracts, InstanceId, StopLossPips, TargetPips);
result.Position.ModifyTrailingStop(true);
and close event looks like:
private void OnPositionClosed(PositionClosedEventArgs args)
{
if(instanceID == args.Position.Label)
LogTrade(args.Position);
}
@mattbarfoot
PanagiotisCharalampous
11 May 2024, 06:46
RE: RE: RE: RE: OnPositionClosed event firing multiple times
mattbarfoot said:
PanagiotisCharalampous said:
mattbarfoot said:
PanagiotisCharalampous said:
Hi there,
Can you please share with us the source code of your cBot and provide us with instructions on how to reproduce this problem?
Best regards,
Panagiotis
Hey Panagiotis, I am attempting to log each position that closes in either the OnPositionClosed event or a CloseTrade sub. Multiple cbots with multiple instances will be running. Positions are logged in a csv file. Typically multiple entries are created in the csv which have different instanceIDs but have the same Label, which is set to the InstanceId when opening.
scenario 1 (CloseTrade)
Position Opened: ExecuteMarketOrder(TradeType.Buy, SymbolName, adjustedContracts, InstanceId, null, null);
Position Closes on x number of bars being reached, which then calls CloseTrade
private void CloseTrade(Position position)
{
LogTrade(position);
ClosePosition(position);
}
scenario 2 (OnPositionClosed event)
Position Opened:
ExecuteMarketOrder(TradeType.Sell, SymbolName, adjustedContracts, InstanceId, StopLossPips, TargetPips);
result.Position.ModifyTrailingStop(true);
private void OnPositionClosed(PositionClosedEventArgs args)
{
LogTrade(args.Position);}
private void LogTrade(Position _position)
{
string log = $"{strategyName},{DateTime.Now},{Chart.TimeFrame},{0},{_position.TradeType},{_position.EntryTime}, {DateTime.UtcNow},{_position.Symbol},{_position.Pips}, {_position.NetProfit},{_position.GrossProfit},{_position.Id},{_position.Label}";
string Path;
File.AppendAllText(Path,log + Environment.NewLine);}
Hi there,
If multiple instances are running then the event will be triggered multiple times, once per instance. You should check inside the event handler if the closed position has been created by the specific instance and only then log it.
Best regards,
Panagiotis
Hey Panagiotis, So to confirm, a new instance is created every time a new position opens. So would the code below reference the correct instance:
ExecuteMarketOrder(TradeType.Sell, SymbolName, adjustedContracts, InstanceId, StopLossPips, TargetPips);
result.Position.ModifyTrailingStop(true);
and close event looks like:
private void OnPositionClosed(PositionClosedEventArgs args)
{
if(instanceID == args.Position.Label)LogTrade(args.Position);
}
It looks correct but you should test it
@PanagiotisCharalampous
PanagiotisCharalampous
08 May 2024, 05:37
Hi there,
Can you please share with us the source code of your cBot and provide us with instructions on how to reproduce this problem?
Best regards,
Panagiotis
@PanagiotisCharalampous