” Electronic Tradin程序 辅导、 辅导STL、Java,c++程序 写作Programming Assignment Four: Electronic TradingOut: July 13, 2020; Due: July 31, 2020.I. Motivation1. Get familiar with the various data structures provided by STL: priorityqueues (std::priority_queue), hash tables (std::unordered_map,std::unordered_set, std::unordered_mutlimap,std::unordered_multiset), and binary search trees (std::map, std::set,std::multimap, std::multiset).2. Get experience in choosing proper data structures.3. Become more proficient at testing and debugging code.II. Programming OverviewIn this project, you are asked to write a program to help facilitate the trading of equities on anelectronic exchange market. The market offers a variety of equities. Any market client can placea buy or sell order on equity to request that a transaction be executed when matching sellers orbuyers become available. Your program should take in buy and sell orders for a variety ofequities as they arrive and match buyers with sellers to execute trades as quickly as possible.III. Input FormatThe input Will arrive from standard input (cin), not from an ifstream. The input contains aseries of orders that will be presented such that orders with lower timestamps always appear first.Orders will be formatted as follows:TIMESTAMP CLIENT_NAME BUY_OR_SELL EQUITY_SYMBOL $PRICE #QUANTITY DURATIONon a single line, with all fields separated by one or more spaces/tabs. For example:0 Jack BUY GOOG $100 #50 2The definition of each field is:1. TIMESTAMP – A non-negative integer value corresponding to the time. Its unit is second.2. CLIENT_NAME – The buyer or sellers name. This will be a string that contains onlyalphanumeric characters and _.3. BUY_OR_SELL – The string BUY or the string SELL, corresponding to the type oforder.4. EQUITY_SYMBOL – The shorthand name of the equity. This will be a string that contains1-5 characters that are alphanumeric character, _, or . (examples: C, F, GM, KO, TGT,WMT, AAPL, PZZA, BRK.A, BRK.B)Programming作业 辅导、 辅导Electronic Trading作业5. PRICE – This is a positive integer. If it is a buy order, this is the highest price the buyeris willing to pay for the equity. If it is a sell order, this is the lowest price the seller iswilling to sell the equity for. Buyers may pay less than their limit price, sellers might getmore money than the minimum they ask for. The $ sign will appear in the input beforethis value.6. QUANTITY – A positive integer representing the number of shares the client wants tobuy/sell. The # sign will appear in the input before this value.7. DURATION – An integer value indicating how long the order will stay in the market.There are 3 possible cases. For DURATION = -1, the order will never expire and stayforever in the market until it is matched for transaction completely. For DURATION = 0,the order is an Immediate Or Cancel (IOC) order which needs to be matched fortransaction immediately, partially or completely. Any remaining quantity is canceled. ForDURATION 0, the Order will expire and exit the market right beforetimestamp+DURATION.All valid input will be arranged by timestamp (lowest timestamp first). As you read in orders,you should assign all orders a unique ID number, such that the first order you read gets an ID of0, the second an ID of 1, and so on. These ID numbers ensure that there is only one possiblematching of buyers and sellers based on arrival timestamps. They are also useful for tracking anddebugging. You do not need to check for invalid input.IV. SpecificationsMarket LogicA variable CURRENT_TIMESTAMP is maintained throughout the run of the program. It alwaysstarts at 0.Your program must perform as follows:1. Read the next order from input.2. If the orders TIMESTAMP != CURRENT_TIMESTAMP then:a. If the –median option is specified, print the median price of all equities thathave been traded on at least once by this point in the lexicographical order byEQUITY_SYMBOL (see below).b. If the –midpoint option is specified, print the midpoint price for all equitiesthat have had at least one order placed by this point in the simulation in thelexicographical order by EQUITY_SYMBOL (see below).c. Set CURRENT_TIMESTAMP equal to the orders TIMESTAMP .d. If any orders Have expired, remove them from your data structure.3. Match the order you just read with the unexpired orders that are stored in your datastructure. If the order is not fully filled and it is not an IOC order, you should add theorder into your data structure to match with future orders. Once the order is fully filled,no further match should be carried out.4. Repeat previous steps until the end of the day, defined as when there is no more input(and thus no more trades to be performed).5. Treat the end of day like the timestamp has moved again, and output median andmidpoint information as necessary.6. Print all end of day output.Orders and TradesAll orders on the market are considered limit orders. A buy limit order expresses the intent tobuy at most N shares of a given stock at no more than D dollars per share, where D is called thelimit. A sell limit order expresses the intent to sell at most N shares of a given stock at no lessthan D dollars per share. To facilitate trade execution, an order can be split and matched withseveral other orders at different execution prices.Order books and trade executionFor each equity, You should keep track of all current buy and sell orders in a dedicated containerdata structure called an order book. A trade can be executed for a given equity when the buyorder with the highest limit price (and in the event of ties, lowest ID number) has a buy limitprice greater than Or equal to the sell limit price of the sell order with the lowest limit price (andin the event of ties, lowest ID number). If this happens, then a trade is executed between the buyorder with the highest limit price (and in the event of ties, lowest ID number) and the sell orderwith the lowest limit price (and in the event of ties, lowest ID number). For a given order book,the order of trade execution is fully determined. You must support this order correctly and ensurehigh speed of trade execution by optimizing the data structures.For example, given the following orders as input:0 SELLER_1 SELL GOOG $125 #10 -10 SELLER_2 SELL GOOG $100 #30 -10 SELLER_3 SELL GOOG $100 #15 -10 BUYER_1 BUY GOOG $200 #4 -10 BUYER_2 BUY GOOG $250 #50 -10 SELLER_4 SELL GOOG $60 #20 -1The first trade to be executed would be BUYER_1 buying 4 of SELLER_2s shares. This isbecause at this point, the program has not read BUYER_2s order yet (see market logic section)and SELLER_2 has the lowest sell price. While SELLER_2 and SELLER_3 are both selling atthe same price, SELLER_2s order arrived first, and will therefore have a lower ID number.When that first trade is executed, SELLER_2s order must be revised to offer only 26 shares(because 4 had already been traded). The revised order keeps the ID of the original order.Whenever a trade is executed, the match price of the trade is the limit price of the order (buy orsell) with the lower ID number. In this case, BUYER_1 offered to buy for $200 and SELLER_2offered to sell for $100; because SELLER_2 has a lower ID number, the trade will be executedat a match Price of $100 per share.For another example, suppose the input is as follows:0 BUYER_1 BUY GOOG $100 #10 -10 BUYER_2 BUY GOOG $125 #30 -10 BUYER_3 BUY GOOG $125 #15 -10 SELLER_1 SELL GOOG $120 #4 -10 SELLER_2 SELL GOOG $110 #4 -1The first trade to be executed would be BUYER_2 buying 4 of SELLER_1s shares with a matchprice of $125. This is because at this point, the program has not read SELLER_2s order yet (seemarket logic section) and BUYER_2 has the highest buy price. While BUYER_2 and BUYER_3are both buying at the same price, BUYER_2s order arrived first, and will therefore have a lowerID number. Since BUYER_2 has a lower ID number than SELLER_1, the trade will be executedat a match price of $125 per share.Order expiration and IOC ordersEach order comes into the order book with a DURATION at which to stay in the order book. IfDURATION = -1, then the order stays indefinitely. If DURATION 0 then the order shouldbe removed from the order Book right before the CURRENT_TIMESTAMP = TIMESTAMP +DURATION. If we had the following sequence of orders:0 SELLER_1 SELL GOOG $125 #10 21 BUYER_1 BUY GOOG $200 #5 -12 BUYER_2 BUY GOOG $150 #5 -1Then first BUYER_1 would buy 5 shares of GOOG from SELLER_1 for $125, but beforeBUYER_2 can buy the remaining shares, SELLER_1s order will expire, and so only one tradewill happen for this sequence of 3 orders.If DURATION = 0, then the order is an IOC order which means whatever part of the order cantransact should transact, but any remaining quantity should be canceled and not placed in theorder book. If we had the following sequence of trades:0 SELLER_1 SELL GOOG $125 #5 -10 BUYER_1 BUY GOOG $200 #10 00 SELLER_2 SELL GOOG $150 #5 -1BUYER_1 will buy 5 shares of GOOG from SELLER_1 for $125 with its IOC order, but theremaining 5 shares will expire before SELLER_2 will be able to sell its shares. If BUYER_1sorder DURATION were changed from 0 to 1, the order sequence looked like:0 SELLER_1 SELL GOOG $125 #5 -10 BUYER_1 BUY GOOG $200 #10 10 SELLER_2 SELL GOOG $150 #5 -1and all of the orders will transact.Commission feesFor providing this matching service, the market (and thus your program) also takes a commissionfee of 1% from every completed trade from both the buyer and the seller. Suppose in a trade, abuyer purchased 4 shares of equity for $100 per share from a seller. The commission fee is:(1004)/100 = $4 from both the buyer and seller. So the buyer will pay (1004)+4 =$404 and the seller Will receive (1004)4 = $396, and the market will earn a commissionof (24) = $8. For these calculations, all values should be integers and therefore all decimalswill be truncated. Commissions must be computed exactly as above when executing the trade. Tobe clear, do not calculate the combined commissions of the buyer and seller in a single arithmeticexpression (such as total_commission = 2 match_price num_shares/100 ), as this may yielddifferent results when truncating. Instead, first calculate the commission as shown and thenmultiply the result by 2.V. Program ArgumentsYour program should be named as main. It should take the following case-sensitive commandlineoptions:1. -v, –verbose: An optional flag that indicates the program should print additionaloutput information while trades are being executed (see output section for more details).2. -m, –median: An optional flag that indicates the program should print the currentmedian match price for each equity at the times specified in the Market Logic sectionabove (see output section for more details).3. -p, –midpoint: An optional flag that indicates the program should print the currentmidpoint price for each equity that has had at least one order placed for it at timesspecified in the Market Logic section above (see output section for more details).4. -t, –transfers: An optional flag that indicates the program should printadditional output information at the end of the day to show the net amount of fundstransferred by all clients (see output section for more details).5. -g, –ttt EQUITY_SYMBOL: An optional flag that may appear more than oncewith different equity Symbols as arguments. This option requests that at the end of theday the program determines what was the best time to buy (once) and then subsequentlysell (once) a particular equity during the day to maximize profit. More information is inthe output section.If multiple options are specified that produce output at the end of the program, the outputshould be printed in the order that they are listed here (e.g., –transfers before–ttt).Examples of legal command lines: ./main infile.txt outfile.txt ./main –verbose –transfers outfile.txt ./main -v -t outfile.txt ./main –verbose –median outfile.txt ./main –transfers –verbose –ttt GOOG –ttt IBMWe will not be specifically error-checking your command-line handling, but we expect that yourprogram conforms with the default behavior of getopt_long (see https://www.gnu.org/software/libc/manual/html_node/Getopt.html#Getopt). Incorrect command-linehandling may lead to a variety of difficult-to-diagnose problems.VI. Output FormatAll the outputs are printed through cout.DefaultAt the end of the day, after all inputs have been read and all possible trades completed, thefollowing output should always be printed before any optional end of day output:—End of Day—Commission Earnings: $COMMISION_EARNINGSTotal Amount of Money Transferred: $MONEY_TRANSFERREDNumber of Completed Trades: NUMBER_OF_COMPLETED_TRADESNumber of Shares Traded: NUMBER_OF_SHARES_TRADEDwith the corresponding values being in the output text. The dollar signs should be printed in theoutput where indicated. The MONEY_TRANSFERRED value should not include commissions.For example, suppose By the end of the day, only two trades have happened: BUYER_1purchased 4 shares of equity EQ_1 from SELLER_1 for $100 per share and BUYER_2purchased 8 shares of equity EQ_2 from SELLER_2 for $40 per share. Then the total amountof money transferred is 4100+840 = 720. The number of completed trades is 2 and the numberof shares traded is 4+8=12.Verbose OptionIf and only if the –verbose option is specified on the command line, whenever a trade iscompleted you should print:BUYER_NAME purchased NUMBER_OF_SHARES shares of EQUITY_SYMBOLfrom SELLER_NAME for $PRICE/shareon a single line. In the following example:0 SELLER_1 SELL GOOG $125 #10 -10 SELLER_2 SELL GOOG $100 #10 -10 SELLER_3 SELL GOOG $100 #10 -10 SELLER_3 SELL GOOG $80 #10 00 BUYER_1 BUY GOOG $200 #4 -1you should print:BUYER_1 purchased 4 shares of GOOG from SELLER_2 for $100/shareNo trades can be executed on an equity for a given CURRENT_TIMESTAMP if there is no buyerwilling to pay the lowest asking price of any seller. An example of such a scenario is:0 BUYER_1 BUY GOOG $100 #50 -10 SELLER_1 SELL GOOG $200 #10 -10 BUYER_2 BUY GOOG $150 #3 -10 SELLER_2 SELL GOOG $175 #30 -1Median OptionIf and only if the –median option is specified on the command line, at the times described inthe Market Logic section (above), your program should print the current median match price ofall completed trades for each equity that were executed in the time interval[0, CURRENT_TIMESTAMP]. To be clear, this is the median of the match prices of the tradesthemselves. This does not consider the quantity traded in each trade. Equities withlexicographically smaller EQUITY_SYMBOLs must be printed first. If no matches have beenmade on a particular Equity, do not print a median for it. If there are an even number of trades,take the average of the middle two to compute the median. The output format is:Median match price of EQUITY_SYMBOL at time CURRENT_TIMESTAMP is$MEDIAN_PRICEMidpoint OptionIf and only if the –midpoint option is specified on the command line, at the times describedin the Market Logic section (above), your program should print the current price midpoint foreach equity that has had at least one order placed in the time interval[0, CURRENT_TIMESTAMP] in lexicographical order. The midpoint of an equity is aninteger average between the highest price of buy orders, and the lowest price of sell orders thatare still active for the given equity, which is calculated exactly by(HighestBuy + LowestSell)/2 for the same equity. The division here is the integerdivision in C++. The output format is:Midpoint of EQUITY_SYMBOL at time CURRENT_TIMESTAMP is$MIDPOINT_PRICEIf an equity currently has zero buy or zero sell orders, then output a line like the following:Midpoint of EQUITY_SYMBOL at time CURRENT_TIMESTAMP is undefinedAs an example, if the following sequence of orders were placed:0 PlanetExpress SELL CAR $120 #1 -10 BluthCorp SELL CAR $110 #1 -10 KrustyKrab BUY CAR $80 #1 -10 BluthCorp BUY CAR $105 #1 -11 PlanetExpress SELL CAR $80 #2 -12 BluthCorp BUY CAR $70 #1 -1The midpoint output would be the following:Midpoint of CAR at time 0 is $107Midpoint of CAR at time 1 is undefinedMidpoint of CAR at time 2 is $90After time 0, the highest buy order is from BluthCorp for $105, and the lowest sell order isalso from BluthCorp for $110. The midpoint is (110 + 105)/2 = 107 (with integermath). At time 1 PlanetExpress sells to both buy orders, so there are no buy orders at theend of time 1 and therefore the quote is undefined. At time 2, BluthCrop places a new buyorder for $70. Now the midpoint is (70 + 110)/2= 90.Transfers OptionIf and only if the –transfers option is specified on the command line, you should print thefollowing information at the end of the day for each client who placed an order during the run ofthe program:CLIENT_NAME bought NUMBER_OF_STOCKS_BOUGHT and soldNUMBER_OF_STOCKS_SOLD for a net transfer of $NET_VALUE_TRADEDThis should be printed such that clients with lexicographically smaller client names are printedfirst. The NET_VALUE_TRADED does not include commissions.Time-Travel Trading OptionIf and only if the –ttt option (Time-Travel Trading) is specified on the command line, youshould do the following.If the –ttt option is specified more than once, you should print the results for eachEQUITY_SYMBOL that is given in the same order that they were given in the command line. Inother words, if the command line had first –ttt MSFT and then –ttt IBM, you wouldprint MSFTs information before IBMs. We will not specify the same equity twice.In time travel trading, you are a time traveler that wants to find the ideal times that you couldhave bought an equity and then later could have sold that equity to maximize profit (or if it isnot possible to make A profit, to do this while minimizing losses). Your program will print aTIMESTAMP1 and a TIMESTAMP2 corresponding to the times you could have placed ordersto do this.What this means is that TIMESTAMP1 will be the same as some actual sell order that came induring the day, and that TIMESTAMP2 will be the same as some actual buy order that came inafter the sell order (i.e., with a higher ID number). The assumption is that the time travelerwould have placed those orders immediately after the actual orders.When calculating the results for time travel trading, the only factors are the time and price oforders that happened throughout the day. Quantity is not considered. One way to think about thisis to imagine (only for the purpose of time travel trading) that all orders are for unlimitedquantity.If there would be more than one answer that yields the optimal result, you should prefer theanswer with the lowest ID. If during the day there is not at least one actual sell order followed byat least one actual buy order, then TIMESTAMP1 and TIMESTAMP2 should both be printed as-1.The output format is as follows:Time travelers would buy EQUITY_SYMBOL at time: TIMESTAMP1 andsell it at time: TIMESTAMP2As an example, if the following sequence of orders were placed:0 SELLER_1 SELL GOOG $10 #5 -11 BUYER_1 BUY GOOG $20 #8 -12 SELLER_1 SELL GOOG $12 #10 -13 BUYER_1 BUY GOOG $16 #3 -14 SELLER_1 SELL GOOG $8 #10 -15 BUYER_1 BUY GOOG $16 #2 -16 SELLER_1 SELL GOOG $9 #7 -17 BUYER_1 BUY GOOG $19 #4 -1Then the time-travel trading for GOOG should output:Time travelers would buy GOOG at time: 4 and sell it at time: 7A Complete ExampleInput:0 PlanetExpress SELL AMD $120 #32 10 BadWolfCorp BUY GE $200 #20 80 BluthCorp BUY AMD $100 #50 101 KrustyKrab BUY AMD $130 #10 71 PlanetExpress SELL GE $150 #50 61 PlanetExpress BUY NFLX $80 #15 63 BluthCorp SELL AMZN $50 #22 -14 BadWolfCorp SELL GE $50 #15 -14 BadWolfCorp SELL AMZN $100 #30 104 KrustyKrab BUY AMZN $130 #12 04 BadWolfCorp BUY AMZN $50 #30 55 BadWolfCorp SELL AMZN $50 #5 05 BluthCorp BUY AMD $150 #25 06 PlanetExpress SELL AMD $80 #100 -16 BadWolfCorp BUY AMD $120 #10 16 KrustyKrab BUY GE $110 #10 3Output when run with –verbose, –median, –midpoint, –transfers,and –ttt AMZN:Midpoint of AMD at time 0 is $110Midpoint of GE at time 0 is undefinedBadWolfCorp purchased 20 shares of GE from PlanetExpress for$200/shareMedian match price of GE at time 1 is $200Midpoint of AMD at time 1 is undefinedMidpoint of GE At time 1 is undefinedMidpoint of NFLX at time 1 is undefinedMedian match price of GE at time 3 is $200Midpoint of AMD at time 3 is undefinedMidpoint of AMZN at time 3 is undefinedMidpoint of GE at time 3 is undefinedMidpoint of NFLX At time 3 is undefinedKrustyKrab purchased 12 shares of AMZN from BluthCorp for$50/shareBadWolfCorp purchased 10 shares of AMZN from BluthCorp for$50/shareMedian match price of AMZN at time 4 is $50Median match price of GE at time 4 is $200Midpoint of AMD at time 4 is undefinedMidpoint of AMZN at time 4 is $75Midpoint of GE at time 4 is undefinedMidpoint of NFLX at time 4 is undefinedBadWolfCorp purchased 5 shares of AMZN from BadWolfCorp for$50/shareMedian match price of AMZN at time 5 is $50Median match price of GE at time 5 is $200Midpoint of AMD at time 5 is undefinedMidpoint of AMZN at time 5 is $75Midpoint of GE at time 5 is undefinedMidpoint of NFLX at time 5 is undefinedKrustyKrab purchased 10 shares of AMD from PlanetExpress for$130/shareBluthCorp purchased 50 shares of AMD from PlanetExpress for$100/shareBadWolfCorp purchased 10 shares of AMD from PlanetExpress for$80/shareKrustyKrab purchased 10 shares of GE from BadWolfCorp for$50/shareMedian match price of AMD at time 6 is $100Median match price of AMZN at time 6 is $50Median match price of GE at time 6 is $125Midpoint of AMD AT time 6 is undefinedMidpoint of AMZN at time 6 is $75Midpoint of GE at time 6 is undefinedMidpoint of NFLX at time 6 is undefined—End of Day—Commission Earnings: $258Total Amount of Money Transferred: $12950Number of Completed Trades: 8Number of Shares Traded: 127BadWolfCorp bought 45 and sold 15 for a net transfer of $-4800BluthCorp bought 50 and sold 22 for a net transfer of $-3900KrustyKrab bought 32 and sold 0 for a net transfer of $-2400PlanetExpress bought 0 and sold 90 for a net transfer of $11100Time travelers would buy AMZN at time: 3 and sell it at time: 4VII. Hints and Implementation Requirements1. We highly encourage the use of the STL (priority queues (std::priority_queue),hash tables (std::unordered_map, std::unordered_set,std::unordered_mutlimap, std::unordered_multiset), and binary searchtrees (std::map, std::set, std::multimap, std::multiset)) for thisproject, with the exception of two prohibited features: The C++11 regular expressionslibrary and the thread/atomics libraries (which spoil runtime measurements). Do not useother libraries (e.g., boost, pthreads, etc).2. To improve the performance of your program, we recommend you to add -O2 option whencompiling your programs.3. For large Test cases, I/O could be the runtime bottleneck. To reduce the I/O time, werecommend you to put the following two statements at the beginning of your main function:std::ios::sync_with_stdio(false);std::cin.tie(0);Please ignore the potential memory issues caused by the above statements (i.e., when youcall valgrind to check for memory issues).VIII. Source Code FilesYou have the freedom to define all of your source code files.IX. CompilingSince std::unordered_map is a new feature of C++ 11, you should add the option-std=c++11 to the g++ compiling command. Make sure the version of your g++ is highenough. Otherwise it may not support that option.X. TestingWe have supplied an input file called test.txt for you to test your market program. It can befound in the Programming-Assignment-Four-Related-Files.zip. To do this test, type thefollowing into the Linux terminal once your program has been compiled:./main –verbose –median –midpoint –transfers –ttt AMZN test.txt result.txtThe correct output can be found in the file out.txt. This is the minimal amount of tests youshould run to check your program. Those programs that do not pass this test are not likely toreceive much credit. You should also write other different test cases yourself to test yourprogram extensively.XI. Submitting and Due DateYou should submit all the source code files and a Makefile via the online judgment system.The Makefile compiles a program named main. Please also include a pdf file containing allyour source code. See announcement from the TAs for details about how to submit these files.The due time is 11:59 pm on July 31, 2020.XII. GradingYour program will be graded along four criteria:1. Functional Correctness2. Implementation Constraints (Violation of constraints will cause a deduction of 50%)3. General Style4. PerformanceFunctional Correctness is determined by running a variety of test cases against your program,checking your Solution using our automatic testing program. We will grade ImplementationConstraints to see if you have met all of the implementation requirements and restrictions.General Style refers to the ease with which TAs can read and understand your program, and thecleanliness and elegance of your code. Part of your grade will also be determined based on theperformance of your algorithm. We will test your program with some large test cases. If yourprogram is not able to finish within a reasonable amount of time, you will lose the performancescore for those test cases.如有需要,请加QQ:99515681 或邮箱:99515681@qq.com
“
添加老师微信回复‘’官网 辅导‘’获取专业老师帮助,或点击联系老师1对1在线指导。