QUOTE:
How do i access the last N (say 3 or 4) positions in the IList of Positions?
By looping over the
Positions list and querying the current position's
NetProfitAsOfBar / NetProfitAsOfBarPercent properties. I leave this boring stuff up to you, there's plenty of examples in the QuickRef/forum. ;)
Instead, I'll concentrate on a Linq version which also looks more elegant and terse. To run my example you'll need just one prerequisite. Until the Editor gets upgraded in WL 6.? to support C#4.0 out of the box, you need to add a reference to System.Core v4.0. In your Strategy's "References" dialog, switch to "Other assemblies to reference...", click "Add a reference", find the file called
System.Core.dll, highlight it and confirm. The file's placement depends on your OS and WLP "bitness". For 64-bit WLP, choose
c:\windows\microsoft.net\framework64\v4.0.30319, for 32-bit WLP it's
c:\windows\microsoft.net\framework\v4.0.30319The example creates 6 positions and filters them using Linq, taking the last 3, then averages the PnL for each bar using the LINQ's built-in Average method:
CODE:
Please log in to see this code.
Things to keep in mind:
1. These properties are always based on 1 share while the Strategy is executing. They return actual values only in PosSizers and Visualizers (which are processed after the Strategy has finished executing).
2. The list is not portfolio-wide: it represents positions of the current symbol due to Wealth-Lab's internal architecture. Workarounds:
WealthScript Techniques | Portfolio-wide conditionsAccessing the number of positions in a portfolioAs to random numbers, there's no shortage of C# RNG examples on the internet if you search it.