Subscription to Positions.Closed event sometimes misses the event
Subscription to Positions.Closed event sometimes misses the event
25 Aug 2022, 14:43
Dear Spotware,
I subscribed a simple logic to Positions.Closed event to track account balance inside the cBot.
protected override void OnStart(){ Positions.Closed += TRD.Positions_Closed;}
public void Positions_Closed(PositionClosedEventArgs e)
{
ClosePosition(e.Position, e.Reason);
}
public void ClosePosition(Position p, PositionCloseReason reason)
{
blnce += p.NetProfit;
if (blnce < mnb)
{
mnb = blnce;
//Нужно проверять относительно начального баланса
double ddCsh = mxb - mnb;
double _dd = ddCsh / initBalance;
if (_dd > dd) dd = _dd;
}
else if (blnce > mxb)
{
mxb = blnce;
mnb = blnce;
}
}
And then I check if balance of backtester is different from the balance that I keep in cBot.
protected override void OnTick()
{
if (check && Math.Round(Account.Balance, 1) != Math.Round(TRD.blnce, 1))
{
Print("Account {0}. TRD {1}. Dif {2}", Math.Round(Account.Balance, 1), Math.Round(TRD.blnce, 1), Math.Round(Account.Balance, 1) - Math.Round(TRD.blnce, 1));
Stop();
}
//allow 1 tick to pass to make sure that events and Account.Balance to be updated
check = Math.Round(Account.Balance, 1) != Math.Round(TRD.blnce, 1);
}
Every new start of cBot gives the balance difference in different place. This is not due to lagging of Account.Balance update or event subscription to occur. If I remove Stop(); then cumulative difference is big.
Is it a bug of Positions.Closed event subscription, or Account.Balance is calculated wrongly in backtester ?
I tried on both 4.1 and 4.2.22 versions.
PanagiotisCharalampous
25 Aug 2022, 14:46
Hi there,
Please provide the complete cBot code and backtesting parameters so that we can reproduce the issue.
Best Regards,
Panagiotis
Join us on Telegram and Facebook
@PanagiotisCharalampous