Question: ay trading involves buying and subsequently selling financial instruments (Links to an external site.)Links to an external site. (e.g. stocks (Links to an external site.)Links
ay trading involves buying and subsequently selling financial instruments (Links to an external site.)Links to an external site. (e.g. stocks (Links to an external site.)Links to an external site., options (Links to an external site.)Links to an external site., futures (Links to an external site.)Links to an external site., derivatives (Links to an external site.)Links to an external site., currencies (Links to an external site.)Links to an external site.) within the same trading day (Links to an external site.)Links to an external site., such that all positions will usually be closed before the market close of the trading day. Depending on one's trading strategy, it may range from several to hundreds of orders a day. The following is a typical instrument chart showing the daily pricing of gold (per oz) back in 2012. The Y axis represents the value gold in USD ($) and the X axis, time, on 3 particular days (9/14. 9/16, and 9/17):
You are a software engineer working for PCC, Inc. specializing in advising day traders with predicative triggers telling them when it's best time to buy/sell in a given day based on an instrument's historic data. This is a complex predicative system you are working on. All of the pricing data for this instrument are stored chronologically in a vector.
Write the following function taking in an integer vector (vector
int getMaxProfit(vector&prices)
Examples:
input: {1,2,4}
output: 3
note on output: you'd buy $1, sell $4 => $3 profit
input: {4,2,1}
output: 0
note on output: you will not be able to buy low, sell high in this pricing order => no buy, no sell => $0 profit
input: {1}
output: 0
note on output: this may be the time slice at the end of the trading day; if you buy in at $1 you lose money since you won't be able to sell it before market closes
input: {1,2,5,1,6}
output: 5
note on output: you'd buy $1, sell $6 => profit $5
input: {3,1,5,2,4}
output: 4
note on output: you'd buy $1, sell $5 => profit $4
Constraints / Assumptions:
For this problem, there is AT MOST one buy and one sell (i.e., max of two orders, a "buy" order and a "sell" order)
Prices are in USD ($) and are > 0
Input is passed in by reference
Max Profit (output) is defined as the max price difference between your buy and sell prices ($profit = ($sell - $buy))
Input cannot be empty and it has at least one price data
Output is your max profit based on the input (vector
You should submit a fully working console program however your main() function isn't graded; only the above referenced function is
It is your responsibility to compile your code in C++11/C++14 (no pre-C++11!). Failure to do that means your code isn't compilable and you will receive 0 point!
Failure to follow the same exact function signature receives -1 styling point deduction
Food for thought: can you get your solution to within O(n)?
Logic: 9 points (-1 point for each test case failure); 1 point for function signature
Styling/Documentation: 1 point
USD/oz 1785.0 24 Hour Spot Gold (Bid) Septenber 17, 2012 17:49 Sep 14 NY close 1770.50 - Sep 16 Sunday -Sep 17 Last 1762.50 www.kitco.com 1788.0 1775.0 1770.8 1765.0 1768.0 1755.0 Hong Kon on on ondon New York Globex New York Globex 1758.0 NY Time 80:00 GMT 04 88 04:00 08:00 12 00 16:00 20:00 23:59
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
