Back to General

Operations on Lists (Iterators & Amend) · General · Practice Question

Calculate Final PnL Using Over

Sign in to track your progress

Question

A trader's realized PnL for five trades is:

pnl:100 -20 50 -10 30

Calculate the trader's final PnL.

Expected result:

150

Solution

{x+y}/[0;pnl]

Alternative Solution

sum pnl

Explanation

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+30

but unlike scan, only the final result is returned:

150

This 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 150

Scan returns every intermediate result, whereas over returns only the final one.

The built-in function:

sum pnl

is equivalent and also returns:

150