I believe your example just needs one little addition. The market position should be tested in the OnPositionUpdate method so that we're only updating the balance when the position is flat, that is, when the update is a completed trade rather than entering a new position.
protected override void OnPositionUpdate(Cbi.Position position, double averagePrice, int quantity, Cbi.MarketPosition marketPosition) { // Check to make sure there is at least one trade in the collection if (SystemPerformance.AllTrades.Count > 0) { if (marketPosition == MarketPosition.Flat) { // Get the last completed real-time trade (at index 0) Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1]; // Calculate the PnL for the last completed real-time trade double lastProfit = lastTrade.ProfitCurrency ; xAccountSize= xAccountSize + lastProfit; Print(xAccountSize.ToString()); //Prints Account Size Print("The last trade's profit is " + lastProfit); } } }
Leave a comment: