I am working on a hedge-scheme for my strategy. To determine the correct hedge position I need the current $value of the open positions.
The documentation hints at
The documentation of Position.LastPrice says:
Unfortunately this does not work. The Position.LastPrice shows often rather bogus values.
To demonstrate I created this strategy:
It produces Log Viewer entries like these:
Often the entry and BarHistory.Close price are close, but LastPrice is off.
The documentation hints at
CODE:
double openPosValue = pos.Quantity * pos.LastPrice;
The documentation of Position.LastPrice says:
QUOTE:
Returns the latest applicable price for the Position. For an open Position, this is the latest closing price in Bars. For a closed Position, it is ExitPrice.
Unfortunately this does not work. The Position.LastPrice shows often rather bogus values.
To demonstrate I created this strategy:
CODE:
// LastPriceTest using WealthLab.Backtest; using WealthLab.Core; namespace WealthScript1 { public class MyStrategy : UserStrategyBase { public override void Execute(BarHistory bars, int idx) { if (!HasOpenPosition(bars, PositionType.Long)) { // Entry PlaceTrade(bars, TransactionType.Buy, OrderType.Market); } else { Position pos = LastOpenPosition; // Compare Position.LastPrice with BarHistory.Close price double posLastPrice = pos.LastPrice; double bhPrice = bars.Close[idx]; if (posLastPrice != bhPrice) // Mismatch!!! Write to Log Viewer { info($"{bars.DateTimes[idx]:yyyy-MM-dd} {pos.Symbol} entry={pos.EntryPrice:F2} " + $"pos.LastPrice={posLastPrice:F2} bh.Price={bhPrice:F2}"); } // Exit if(idx - pos.EntryBar > 10) ClosePosition(pos, OrderType.Market); } } private void info(string msg) { WLHost.Instance.AddLogItem("LastPriceTest", msg, WLColor.Blue); } } }
It produces Log Viewer entries like these:
QUOTE:
"18.11.2026 08:15:55:469" "LastPriceTest" "2026-11-13 GS entry=920,00 pos.LastPrice=877,52 bh.Price=916,56"
"18.11.2026 08:15:55:469" "LastPriceTest" "2026-11-13 JPM entry=346,41 pos.LastPrice=329,96 bh.Price=346,19"
"18.11.2026 08:15:55:469" "LastPriceTest" "2026-11-13 NVDA entry=170,00 pos.LastPrice=155,46 bh.Price=162,85"
"18.11.2026 08:15:55:469" "LastPriceTest" "2026-11-13 CAT entry=606,98 pos.LastPrice=570,79 bh.Price=600,20"
"18.11.2026 08:15:55:469" "LastPriceTest" "2026-11-16 UNH entry=408,00 pos.LastPrice=389,68 bh.Price=412,02"
"18.11.2026 08:15:55:469" "LastPriceTest" "2026-11-16 CRM entry=220,00 pos.LastPrice=194,52 bh.Price=214,83"
"18.11.2026 08:15:55:469" "LastPriceTest" "2026-11-16 IBM entry=246,75 pos.LastPrice=231,09 bh.Price=242,51"
"18.11.2026 08:15:55:470" "LastPriceTest" "2026-11-16 V entry=423,83 pos.LastPrice=416,77 bh.Price=426,46"
"18.11.2026 08:15:55:470" "LastPriceTest" "2026-11-16 SHW entry=350,38 pos.LastPrice=344,15 bh.Price=350,54"
Often the entry and BarHistory.Close price are close, but LastPrice is off.
Rename
Looking ...
Because it's designed this way, we'll update that description with the statement that the property is meant to be used for post-processing, in Perf. Visualizers, for example.
The current price of the position during script execution is simply bars.Close[idx], or, if you prefer, pos.Bars.Close[idx].
The current price of the position during script execution is simply bars.Close[idx], or, if you prefer, pos.Bars.Close[idx].
I think there are two ways to improve the situation:
1. Make it work
Fix the LastPrice getter of the Position class to work according to the current documentation. I think there is no technical reason against it and I consider it good design.
2. Stop it from returning bogus values
If you can't/don't want to adhere to solution #1 then please make the LastPrice getter return 0.0 or double.NaN as long as there is no valid/reliable value.
Something that works somtimes causes a lot of headache, debug time and so on...
1. Make it work
Fix the LastPrice getter of the Position class to work according to the current documentation. I think there is no technical reason against it and I consider it good design.
2. Stop it from returning bogus values
If you can't/don't want to adhere to solution #1 then please make the LastPrice getter return 0.0 or double.NaN as long as there is no valid/reliable value.
Something that works somtimes causes a lot of headache, debug time and so on...
There about a dozen Position properties that are designed to work in a similar way for post-processing (MFE, MAE, MAEPct, etc.). So don't get your hopes up. You can already get the price in the most direct way without relying on a property designed for a different purpose.
QUOTE:
Position properties that are designed to work in a similar way for post-processing (MFE, MAE, MAEPct, etc.).
The Position QuickRef docs for MAE, MAEPct, etc does state the "generic" Position properties (without the idx parameter) are mainly for Performance Visualizer calculations, but it doesn't state why, which is not good. Now I understand their storage is in a different context (Backtester context, not the UserStrategyBase context).
I would state that in the QuickRef and either make these generic values "protected" in the UserStrategyBase context, null, or Double.NaN. I may have been using them wrong without knowing it.
Your Response
Post
Edit Post
Login is required