This is actually a port from jVest, a trading system I developed in 2016.
Normally, your broker displays your open positions. But, most of that is useless for measuring your performance, especially if you are an options trader that rolls, or a stock trader that scales in and out.
To me, a position is when I first enter an underlying to when I am flat in it. In between, I can scale, roll, and combine stocks and options (e..g, covered calls). What I care about is how I’m doing overall in that underlying, not how an individual leg is performing, or the p/l of one leg since open.
Under this definition, a position can have both open and closed trades. A position can even consist entirely of closed trades. That just makes it a closed position. But, when I query the past, I can clearly see how I performed because of these historical groups.
Hopefully, I’ll have this working in the next few days in uVest. In the meantime, to understand the goal of this code port, here is the text output of my jVest positions report for NUGT from 2016:
Check out uVest: