Execution Simulator
Problem Description
Implement TWAP (Time-Weighted Average Price) and VWAP (Volume-Weighted Average Price) execution algorithms. Given a parent order and a series of OHLCV market data bars, slice the order across bars and simulate execution for both algorithms.
Compute arrival price, average execution prices, implementation shortfall, total execution cost, and the number of bars used for each algorithm. Output 9 aggregate metrics.
Tier System & Performance Targets
| LANGUAGE | GOLD | SILVER | BRONZE |
|---|---|---|---|
| C++ / Rust | < 500ms | < 2000ms | < 8000ms |
| Java | < 1000ms | < 4000ms | < 15000ms |
| Python | < 5000ms | < 15000ms | < 60000ms |
Evaluation Criteria
Correctness: 9 output values are compared against a reference. Lines 1-7 (floats) use tolerance 0.0001. Lines 8-9 (integers) must match exactly. All 9 must be correct to pass.
Performance: Wall-clock time to process all bars determines tier placement.
Input Format
Read from stdin. The first line contains the parent order info: SIDE TOTAL_QTY NUM_BARS. The next NUM_BARS lines each contain a comma-separated OHLCV bar:
SIDE TOTAL_QTY NUM_BARS
timestamp,open,high,low,close,volume
timestamp,open,high,low,close,volume
...
Where:
SIDE: BUY or SELLTOTAL_QTY: Total quantity to execute across all barsNUM_BARS: Number of OHLCV bars to followtimestamp: Bar timestamp (integer)open, high, low, close: Bar prices (float)volume: Bar volume (integer)
Output Format
Output exactly 9 lines to stdout:
ARRIVAL_PRICE
TWAP_AVG_PRICE
VWAP_AVG_PRICE
TWAP_SHORTFALL
VWAP_SHORTFALL
TWAP_TOTAL_COST
VWAP_TOTAL_COST
TWAP_BARS_EXECUTED
VWAP_BARS_EXECUTED
Where:
ARRIVAL_PRICE: Price at time of order arrival (float, 6 decimals)TWAP/VWAP_AVG_PRICE: Average execution price for each algo (float, 6 decimals)TWAP/VWAP_SHORTFALL: Implementation shortfall vs arrival price (float, 6 decimals)TWAP/VWAP_TOTAL_COST: Total execution cost (float, 6 decimals)TWAP/VWAP_BARS_EXECUTED: Number of bars with fills (integer)
Example
Input
BUY 1000 5
1000,100.00,101.50,99.50,101.00,50000
1001,101.00,102.00,100.50,101.50,60000
1002,101.50,103.00,101.00,102.50,45000
1003,102.50,103.50,102.00,103.00,55000
1004,103.00,104.00,102.50,103.50,70000
Output
100.000000
102.200000
102.050000
2.200000
2.050000
2200.000000
2050.000000
5
5
Line 1: Arrival price (first bar open). Lines 2-3: TWAP and VWAP average fill prices. Lines 4-5: Shortfall (avg price - arrival). Lines 6-7: Total cost. Lines 8-9: Bars with execution (integers).
Submission Rules
- Your program receives input via stdin
- Output 9 lines to stdout (7 floats with 6 decimal places, 2 integers)
- Program must complete within 120 seconds
- Memory usage must not exceed 2GB
- Single-file solutions only
- Java class must be named
Solution
Starter Templates
C++
#include <cstdio>
#include <cmath>
#include <vector>
struct Bar {
long long timestamp;
double open, high, low, close;
long long volume;
};
int main() {
char side[8];
long long total_qty;
int num_bars;
scanf("%s %lld %d", side, &total_qty, &num_bars);
std::vector<Bar> bars(num_bars);
for (int i = 0; i < num_bars; i++) {
scanf("%lld,%lf,%lf,%lf,%lf,%lld",
&bars[i].timestamp, &bars[i].open, &bars[i].high,
&bars[i].low, &bars[i].close, &bars[i].volume);
}
double arrival = bars[0].open;
// Implement TWAP: distribute qty equally across bars
// Implement VWAP: distribute qty proportional to volume
// Compute avg price, shortfall, total cost for each
double twap_avg = 0, vwap_avg = 0;
double twap_short = 0, vwap_short = 0;
double twap_cost = 0, vwap_cost = 0;
int twap_bars = 0, vwap_bars = 0;
printf("%.6f\n", arrival);
printf("%.6f\n", twap_avg);
printf("%.6f\n", vwap_avg);
printf("%.6f\n", twap_short);
printf("%.6f\n", vwap_short);
printf("%.6f\n", twap_cost);
printf("%.6f\n", vwap_cost);
printf("%d\n", twap_bars);
printf("%d\n", vwap_bars);
return 0;
}
Python
import sys
header = input().split()
side = header[0]
total_qty = int(header[1])
num_bars = int(header[2])
bars = []
for _ in range(num_bars):
parts = input().split(',')
bars.append({
'ts': int(parts[0]),
'open': float(parts[1]),
'high': float(parts[2]),
'low': float(parts[3]),
'close': float(parts[4]),
'volume': int(parts[5])
})
arrival = bars[0]['open']
# Implement TWAP: distribute qty equally across bars
# Implement VWAP: distribute qty proportional to volume
# Compute avg price, shortfall, total cost for each
twap_avg = vwap_avg = 0.0
twap_short = vwap_short = 0.0
twap_cost = vwap_cost = 0.0
twap_bars = vwap_bars = 0
print(f"{arrival:.6f}")
print(f"{twap_avg:.6f}")
print(f"{vwap_avg:.6f}")
print(f"{twap_short:.6f}")
print(f"{vwap_short:.6f}")
print(f"{twap_cost:.6f}")
print(f"{vwap_cost:.6f}")
print(twap_bars)
print(vwap_bars)
Rust
use std::io::{self, BufRead, Write, BufWriter};
fn main() {
let stdin = io::stdin();
let stdout = io::stdout();
let mut out = BufWriter::new(stdout.lock());
let mut lines = stdin.lock().lines();
let header: Vec<String> = lines.next().unwrap().unwrap().trim()
.split_whitespace().map(String::from).collect();
let _side = &header[0];
let total_qty: i64 = header[1].parse().unwrap();
let num_bars: usize = header[2].parse().unwrap();
let mut opens = Vec::with_capacity(num_bars);
let mut volumes = Vec::with_capacity(num_bars);
for _ in 0..num_bars {
let line = lines.next().unwrap().unwrap();
let p: Vec<&str> = line.trim().split(',').collect();
opens.push(p[1].parse::<f64>().unwrap());
volumes.push(p[5].parse::<i64>().unwrap());
}
let arrival = opens[0];
// Implement TWAP and VWAP execution
let (twap_avg, vwap_avg) = (0.0f64, 0.0f64);
let (twap_short, vwap_short) = (0.0f64, 0.0f64);
let (twap_cost, vwap_cost) = (0.0f64, 0.0f64);
let (twap_bars, vwap_bars) = (0i32, 0i32);
writeln!(out, "{:.6}", arrival).unwrap();
writeln!(out, "{:.6}", twap_avg).unwrap();
writeln!(out, "{:.6}", vwap_avg).unwrap();
writeln!(out, "{:.6}", twap_short).unwrap();
writeln!(out, "{:.6}", vwap_short).unwrap();
writeln!(out, "{:.6}", twap_cost).unwrap();
writeln!(out, "{:.6}", vwap_cost).unwrap();
writeln!(out, "{}", twap_bars).unwrap();
writeln!(out, "{}", vwap_bars).unwrap();
}
Java
import java.util.*;
import java.io.*;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] header = br.readLine().trim().split("\\s+");
String side = header[0];
long totalQty = Long.parseLong(header[1]);
int numBars = Integer.parseInt(header[2]);
double[] opens = new double[numBars];
long[] volumes = new long[numBars];
for (int i = 0; i < numBars; i++) {
String[] parts = br.readLine().trim().split(",");
opens[i] = Double.parseDouble(parts[1]);
volumes[i] = Long.parseLong(parts[5]);
}
double arrival = opens[0];
// Implement TWAP and VWAP execution
double twapAvg = 0, vwapAvg = 0;
double twapShort = 0, vwapShort = 0;
double twapCost = 0, vwapCost = 0;
int twapBars = 0, vwapBars = 0;
System.out.printf("%.6f%n", arrival);
System.out.printf("%.6f%n", twapAvg);
System.out.printf("%.6f%n", vwapAvg);
System.out.printf("%.6f%n", twapShort);
System.out.printf("%.6f%n", vwapShort);
System.out.printf("%.6f%n", twapCost);
System.out.printf("%.6f%n", vwapCost);
System.out.printf("%d%n", twapBars);
System.out.printf("%d%n", vwapBars);
}
}