Operations on Lists (Iterators & Amend) · General · Practice Question
Calculate Final PnL Using Over
Question
A trader's realized PnL for five trades is:
pnl:100 -20 50 -10 30Calculate the trader's final PnL.
Expected result:
150Solution
{x+y}/[0;pnl]Alternative Solution
sum pnlExplanation
The over iterator:
/repeatedly applies a function, passing the result of each iteration into the next.
The expression:
{x+y}/[0;pnl]performs:
0+100
100-20
80+50
130-10
120+30but unlike scan, only the final result is returned:
150This makes over ideal when only the final aggregate value is required.
Typical uses include:
- Final PnL
- Total traded volume
- Total commissions
- Portfolio exposure
Compare this with scan:
{x+y}\[0;pnl]which returns:
100 80 130 120 150Scan returns every intermediate result, whereas over returns only the final one.
The built-in function:
sum pnlis equivalent and also returns:
150