You are given a series of trades. Each trade has a key, a value, a quantity, and a sequence number. For each trade, print the
You are given a series of trades. Each trade has a key, a value, a quantity, and a sequence number. For each trade, print the weighted moving average of all the trades for that particular key. Round your average to two decimal places. If the sequence number for any trade TT is less than the sequence number for any previous trade, throw out TT. Do not print anything for TT or factor it into your WMA.
M_{x+1} = {(M_x * Q_x)+(v_{x+1} * q_{x+1}) over Q_{x+1}}M x+1 = Q x+1 (M x *Q x )+(v x+1 *q x+1 )
Where M = weighted moving average, Q = total quantity, q = current quantity, and v = current value.
Input:
The input will be a semicolon-delimited string of trades, with each trade being comma-delimited. The parameters for each trade will be in order:
Key-Value Quantity Sequence numberWhere key is a string, value is a rational floating-point number, quantity is a positive integer, and the sequence number is a positive integer.
Example:
1,2000,5,1;1,2050,5,2;2,3000,10,3Output:
For each trade you do a calculation for, print on a new line {key}: {weighted moving average for key}
Example, from the example input above:
1: 2000 # key: CurrentAverage(1) = ((0*0)+(2000*5))/5 1: 2025 # key: CurrentAverage(1) = ((2000*5)+(2050*5))/10 2: 3000 # key: CurrentAverage(2) = ((0*0)+(3000*10))/10Test 1
Test Input
Download Test Input
1,2000,5,1;1,2030,15,2;1,2000,10,1;2,2050,15,5;1,2067,8,6;2,2050,5,7Expected Output
Download Test Output
1: 2000 1: 2022.5 2: 2050 1: 2035.21 2: 2050Test 2
Test Input
Download Test Input
1,2000,5,3;2,2040,5,2Expected Output
Download Test Output
1: 2000Test 3
Test Input
Download Test Input
1,2000,5,2;1,2050,5,4Expected Output
Download Test Output
1: 2000 1: 2025Test 4
Test Input
Download Test Input
1,2000,5,1;2,2050,15,2Expected Output
Download Test Output
1: 2000 2: 2050Step by Step Solution
3.58 Rating (148 Votes )
There are 3 Steps involved in it
Step: 1
Solution Calculate And Prints Weighted Moving Average def wmatradelnfo Split Trade Info String trade...See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started