Accessing last N positions
Author: kbellare
Creation Date: 12/22/2013 5:00 PM
profile picture

kbellare

#1
How do i access the last N (say 3 or 4) positions in the IList of Positions? I'd like to average their profits and if they're negative, resist opening a position by using a biased random number generator .. e.g. code ..

Please provide sample code for Avg<Last 3 positions.NetProfit> and for the biased random number generator - appreciate it.

CODE:
Please log in to see this code.


thanks
Kiran
profile picture

Eugene

#2
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.30319

The 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 conditions
Accessing the number of positions in a portfolio

As to random numbers, there's no shortage of C# RNG examples on the internet if you search it.
profile picture

Cone

#3
Usually in a script running Portfolio Simulation mode you'd work with NetProfitAsOfBarPercent instead of NetProfitAsOfBar, but NetProfitAsOfBar would be valid if using the SetShareSize() Override for sizing within the script. NetProfitAsOfBarPercent is more than adequate for determining a positive or negative bias of position profit, however.



profile picture

kbellare

#4
Thanks,

I couldn't find sample code to access the last 3 positions in the IList or a complete document of all the properties of Positions object.
Is an IList the same as an array? Can i say

CODE:
Please log in to see this code.



The code in the Quick Reference only shows a foreach statement, so not sure how to retrieve the last 3 positions from an IList
CODE:
Please log in to see this code.
profile picture

Cone

#5
The first snippet should work if you decremented the index, i.e., i-- (not i++).

Let us stress again, working with NetProfit in a Strategy Script will give you the results you're looking for if and only if:
1. you're running Raw Profit mode, or,
2. you using the SetShareSize override option in Portfolio Simulation mode, or,
3. all your Position sizes happen to be '1' share/contract.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).