In your OnTick method, replace the standard ClosePosition(position) call with this dedicated method. It queries the closed position from the History pool to guarantee it logs the true server-acknowledged fill price.
Code: Select all
private void ExecuteMarketAbort(Position position, string abortReason)
{
// 1. Snapshot the expected price before the network call
double expectedPrice = position.TradeType == TradeType.Buy ? Symbol.Bid : Symbol.Ask;
// 2. Execute the close synchronously
TradeResult result = ClosePosition(position);
if (result.IsSuccessful)
{
// 3. Fetch the actual execution record from history
var closedTrade = History.FindLast(position.Label, position.SymbolName);
if (closedTrade != null)
{
double actualPrice = closedTrade.ClosingPrice;
// Calculate slippage in Pips
double slippagePips = position.TradeType == TradeType.Buy
? (expectedPrice - actualPrice) / Symbol.PipSize
: (actualPrice - expectedPrice) / Symbol.PipSize;
Print($"[ABORT: {abortReason}] Expected: {expectedPrice:F5} | Filled: {actualPrice:F5} | Slippage: {slippagePips:F1} pips");
}
}
else
{
Print($"[ABORT FAILED] {abortReason} - Error: {result.Error}");
}
}