OnPositionClosed event is triggered multiple time while backtesting a cBot
            OnPositionClosed event is triggered multiple time while backtesting a cBot
            
                 04 Sep 2020, 12:51
            
                    
Dear Panagiotis,
I need some help, if possible:
while programming a cBot, I use the following part of code:
private void OnPositionsClosed(PositionClosedEventArgs args)
        {
            if (args.Reason == PositionCloseReason.TakeProfit)
            {
                Print("Take Profit eingetreten");
                foreach (var _pos in Positions)
                {
                    ModifyPosition(_pos, _pos.EntryPrice, _pos.TakeProfit);
                }
            }
            if (args.Reason == PositionCloseReason.StopLoss)
            {
                Print("StopLoss eingetreten");
            }
        }
But if an event like TakeProfit or StopLoss has occurred, the statement is printed or executed multiple time, as show in the screenshot:
 
This happens while I'm on visual backtesting...
Kindest regards,
Replies
                     xabbu
                     04 Sep 2020, 13:02
                                    
RE:
here it is, and many thanks again for your always ultrafast responses:
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Threading;
namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class QuestionToPanagiotis : Robot
    {
        public double _initialTradeVolume { get; set; }
        
        private double getATRPips()
        {
            return _atr.Result.LastValue / Symbol.PipSize;
        }
    }
}
@xabbu
                     xabbu
                     07 Sep 2020, 10:56
                                    
Dear Panagiotis,
I don't want to be to pushy, did you have any idea for me resolving this issue / behavior so that I can move on with my cBot? Because the event is triggered multiple time, any statement I would like to be executed is also executed multiple time...
Kindest regards and many thanks for your support
@xabbu
                     PanagiotisCharalampous
                     07 Sep 2020, 11:11
                                    
Hi xabbu,
Your problem is here
        protected override void OnBar()
        {
            Positions.Closed += OnPositionsClosed;
On each bar you add a new event handler on the event. This is why the method is executed multiple times. You should do this only once in the OnStart() method
Best Regards,
Panagiotis
@PanagiotisCharalampous

PanagiotisCharalampous
04 Sep 2020, 12:56
Hi xabbu,
Please provide us with the complete cBot code.
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous