private void ProcessTrade(TradeResult tr, Position pos, decimal pospl) { //Compare to prevpos bool initial = false; if (_prevPos == null) { _prevPos = pos; initial = true; } if (_tradecount.ContainsKey(tr.Source.Symbol)) { _tradecount[tr.Source.Symbol]++; } else { _tradecount.Add(tr.Source.Symbol, 1); } if (!_days.Contains(tr.Source.Xdate)) { _days.Add(tr.Source.Xdate); } int usizebefore = _prevPos.UnsignedSize; var miubefore = _prevPos.IsFlat ? 0 : _prevPos.UnsignedSize * _prevPos.AvgPrice; //Use new position from here bool isroundturn = (usizebefore > 0) && pospl != 0 && !initial && (pos.Direction != _prevPos.Direction); // get comissions Commissions += tr.Commission; // calculate MIU and store on array var miu = pos.IsFlat ? 0 : pos.AvgPrice * pos.UnsignedSize; if (miu != 0) { _miu.Add(miu); } // if we closed something, update return decimal grosspl = pospl; decimal netpl = grosspl - tr.Commission; // count return _grossreturn.Add(grosspl); _netreturns.Add(netpl); // get pct return for portfolio decimal pctret; if (miubefore == 0) { pctret = netpl / miu; } else { pctret = netpl / miubefore; } _pctrets.Add(pctret); //adjust initial capital based balance decimal portfolioreturn = netpl / Balance; _portfPctreturns.Add(portfolioreturn); // add to neg returns if negative if (portfolioreturn < 0) { _portfNegpctreturns.Add(portfolioreturn); } // adjust current balance Balance += netpl; // if it is below our zero, count it as negative return if (pctret < 0) { _negret.Add(pctret); } if (isroundturn) { RoundTurns++; if (pospl >= 0) { RoundWinners++; } else if (pospl < 0) { RoundLosers++; } } if (!Symbols.Contains(tr.Source.Symbol)) { Symbols += tr.Source.Symbol + ","; SymbolCount++; } Trades++; SharesTraded += Math.Abs(tr.Source.Xsize / tr.Source.Security.LotSize); GrossPL += tr.ClosedPl; if ((tr.ClosedPl > 0) && !_exitscounted.Contains(tr.Source.Id)) { if (tr.Source.Direction == Direction.Long) { SellWins++; SellPL += tr.ClosedPl; } else { BuyWins++; BuyPL += tr.ClosedPl; } if (tr.Source.Id != 0) { _exitscounted.Add(tr.Id); } Winners++; _consecWinners++; _consecLosers = 0; } else if ((tr.ClosedPl < 0) && !_exitscounted.Contains(tr.Source.Id)) { if (tr.Source.Direction == Direction.Long) { SellLosers++; SellPL += tr.ClosedPl; } else { BuyLosers++; BuyPL += tr.ClosedPl; } if (tr.Source.Id != 0) { _exitscounted.Add(tr.Id); } Losers++; _consecLosers++; _consecWinners = 0; } if (tr.ClosedPl > 0) { _winpnl += tr.ClosedPl; } else if (tr.ClosedPl < 0) { _losepnl += tr.ClosedPl; } if (_consecWinners > ConsecWin) { ConsecWin = _consecWinners; } if (_consecLosers > ConsecLose) { ConsecLose = _consecLosers; } if ((tr.OpenSize == 0) && (tr.ClosedPl == 0)) { Flats++; } if (tr.ClosedPl > MaxWin) { MaxWin = tr.ClosedPl; } if (tr.ClosedPl < MaxLoss) { MaxLoss = tr.ClosedPl; } if (tr.OpenPl > MaxOpenWin) { MaxOpenWin = tr.OpenPl; } if (tr.OpenPl < MaxOpenLoss) { MaxOpenLoss = tr.OpenPl; } //set prev position _prevPos = pos; }