FastPrepDaily Settlement for Long and Short Positions

Daily Settlement for Long and Short Positions

Belvedere Trading logoBelvedere Trading● MediumFULLTIMEOA
Learn

Problem statement

A futures desk starts before day 1 with position startPosition and the previous day's settlement price startSettle. A positive position is long and a negative position is short.

For each day i:

  1. The desk holds its current position through that day's settlement. Its cash change is position * (settle[i] - previousSettle).
  2. The signed trade trade[i] takes effect at that close, after the day's cash change is calculated. A positive trade buys futures and a negative trade sells futures.
  3. The current settlement price becomes settle[i].

Return the running total settlement cash after every day.

Function

variationMargin(startPosition: int, startSettle: int, trade: int[], settle: int[]) → long[]

Examples

Example 1

startPosition = -2startSettle = 100trade = [0,5,0]settle = [110,104,95]return = [-20,-8,-35]

The initial short position earns -2 * 10 = -20 on day 1. It earns -2 * -6 = 12 on day 2 before the buy changes the position to 3, so the running total becomes -8. Day 3 contributes 3 * -9 = -27, producing -35.

Example 2

startPosition = 3startSettle = 50trade = [-1,0]settle = [55,53]return = [15,11]

Day 1 contributes 3 * 5 = 15, then the sell changes the position to 2. Day 2 contributes 2 * -2 = -4, leaving a running total of 11.

Constraints

  • 1 <= trade.length <= 10^5.
  • trade.length = settle.length.
  • -10^9 <= startPosition, trade[i] <= 10^9.
  • 1 <= startSettle, settle[i] <= 10^9.
  • Every position and returned running total fits in a signed 64-bit integer.
  • Every daily settlement cash change and every intermediate running cash total fits in a signed 64-bit integer.

More Belvedere Trading problems

See Belvedere Trading hiring insights
public long[] variationMargin(int startPosition, int startSettle, int[] trade, int[] settle) {
  // write your code here
}
startPosition-2
startSettle100
trade[0,5,0]
settle[110,104,95]
expected[-20,-8,-35]
Checking account…