1/*
2 * Legal Notice
3 *
4 * This document and associated source code (the "Work") is a part of a
5 * benchmark specification maintained by the TPC.
6 *
7 * The TPC reserves all right, title, and interest to the Work as provided
8 * under U.S. and international laws, including without limitation all patent
9 * and trademark rights therein.
10 *
11 * No Warranty
12 *
13 * 1.1 TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THE INFORMATION
14 * CONTAINED HEREIN IS PROVIDED "AS IS" AND WITH ALL FAULTS, AND THE
15 * AUTHORS AND DEVELOPERS OF THE WORK HEREBY DISCLAIM ALL OTHER
16 * WARRANTIES AND CONDITIONS, EITHER EXPRESS, IMPLIED OR STATUTORY,
17 * INCLUDING, BUT NOT LIMITED TO, ANY (IF ANY) IMPLIED WARRANTIES,
18 * DUTIES OR CONDITIONS OF MERCHANTABILITY, OF FITNESS FOR A PARTICULAR
19 * PURPOSE, OF ACCURACY OR COMPLETENESS OF RESPONSES, OF RESULTS, OF
20 * WORKMANLIKE EFFORT, OF LACK OF VIRUSES, AND OF LACK OF NEGLIGENCE.
21 * ALSO, THERE IS NO WARRANTY OR CONDITION OF TITLE, QUIET ENJOYMENT,
22 * QUIET POSSESSION, CORRESPONDENCE TO DESCRIPTION OR NON-INFRINGEMENT
23 * WITH REGARD TO THE WORK.
24 * 1.2 IN NO EVENT WILL ANY AUTHOR OR DEVELOPER OF THE WORK BE LIABLE TO
25 * ANY OTHER PARTY FOR ANY DAMAGES, INCLUDING BUT NOT LIMITED TO THE
26 * COST OF PROCURING SUBSTITUTE GOODS OR SERVICES, LOST PROFITS, LOSS
27 * OF USE, LOSS OF DATA, OR ANY INCIDENTAL, CONSEQUENTIAL, DIRECT,
28 * INDIRECT, OR SPECIAL DAMAGES WHETHER UNDER CONTRACT, TORT, WARRANTY,
29 * OR OTHERWISE, ARISING IN ANY WAY OUT OF THIS OR ANY OTHER AGREEMENT
30 * RELATING TO THE WORK, WHETHER OR NOT SUCH AUTHOR OR DEVELOPER HAD
31 * ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * Contributors
34 * - Doug Johnson
35 */
36
37/******************************************************************************
38 * Description: Implemenation of the MEETradingFloor class.
39 * See MEETradingFloor.h for a description.
40 ******************************************************************************/
41
42#include "main/MEETradingFloor.h"
43
44using namespace TPCE;
45
46RNGSEED CMEETradingFloor::GetRNGSeed(void) {
47 return (m_rnd.GetSeed());
48}
49
50void CMEETradingFloor::SetRNGSeed(RNGSEED RNGSeed) {
51 m_rnd.SetSeed(RNGSeed);
52}
53
54// Constructor - use default RNG seed
55CMEETradingFloor::CMEETradingFloor(CMEESUTInterface *pSUT, CMEEPriceBoard *pPriceBoard, CMEETickerTape *pTickerTape,
56 CDateTime *pBaseTime, CDateTime *pCurrentTime)
57 : m_pSUT(pSUT), m_pPriceBoard(pPriceBoard), m_pTickerTape(pTickerTape), m_pBaseTime(pBaseTime),
58 m_pCurrentTime(pCurrentTime), m_rnd(RNGSeedBaseMEETradingFloor), m_OrderProcessingDelayMean(1.0) {
59}
60
61// Constructor - RNG seed provided
62CMEETradingFloor::CMEETradingFloor(CMEESUTInterface *pSUT, CMEEPriceBoard *pPriceBoard, CMEETickerTape *pTickerTape,
63 CDateTime *pBaseTime, CDateTime *pCurrentTime, RNGSEED RNGSeed)
64 : m_pSUT(pSUT), m_pPriceBoard(pPriceBoard), m_pTickerTape(pTickerTape), m_pBaseTime(pBaseTime),
65 m_pCurrentTime(pCurrentTime), m_rnd(RNGSeed), m_OrderProcessingDelayMean(1.0) {
66}
67
68CMEETradingFloor::~CMEETradingFloor(void) {
69}
70
71inline double CMEETradingFloor::GenProcessingDelay(double Mean) {
72 double Result = RoundToNearestNsec(m_rnd.RndDoubleNegExp(Mean));
73
74 if (Result > m_MaxOrderProcessingDelay) {
75 return (m_MaxOrderProcessingDelay);
76 } else {
77 return (Result);
78 }
79}
80
81INT32 CMEETradingFloor::SubmitTradeRequest(PTradeRequest pTradeRequest) {
82 switch (pTradeRequest->eAction) {
83 case eMEEProcessOrder: { // Use {...} to keep compiler from complaining that
84 // other cases/default skip initialization of
85 // pNewOrder.
86 // This is either a market order or a limit order that has been
87 // triggered, so it gets traded right away. Make a copy in storage under
88 // our control.
89 PTradeRequest pNewOrder = new TTradeRequest;
90 *pNewOrder = *pTradeRequest;
91 return (m_OrderTimers.StartTimer(GenProcessingDelay(m_OrderProcessingDelayMean), this,
92 &CMEETradingFloor::SendTradeResult, pNewOrder));
93 } // Use {...} to keep compiler from complaining that other cases/default
94 // skip initialization of pNewOrder.
95 case eMEESetLimitOrderTrigger:
96 // This is a limit order
97 m_pTickerTape->PostLimitOrder(pTradeRequest);
98 return (m_OrderTimers.ProcessExpiredTimers());
99 default:
100 // Throw and exception - SHOULD NEVER GET HERE!
101 return (m_OrderTimers.ProcessExpiredTimers());
102 }
103}
104
105INT32 CMEETradingFloor::GenerateTradeResult(void) {
106 return (m_OrderTimers.ProcessExpiredTimers());
107}
108
109void CMEETradingFloor::SendTradeResult(PTradeRequest pTradeRequest) {
110 eTradeTypeID eTradeType;
111 TTradeResultTxnInput TxnInput;
112 TTickerEntry TickerEntry;
113 double CurrentPrice = -1.0;
114
115 eTradeType = m_pTickerTape->ConvertTradeTypeIdToEnum(pTradeRequest->trade_type_id);
116 CurrentPrice = m_pPriceBoard->GetCurrentPrice(pTradeRequest->symbol).DollarAmount();
117
118 // Populate Trade-Result inputs, and send to SUT
119 TxnInput.trade_id = pTradeRequest->trade_id;
120
121 // Make sure the Trade-Result has the right price based on the type of
122 // trade.
123 if ((eTradeType == eLimitBuy && pTradeRequest->price_quote < CurrentPrice) ||
124 (eTradeType == eLimitSell && pTradeRequest->price_quote > CurrentPrice)) {
125 TxnInput.trade_price = pTradeRequest->price_quote;
126 } else {
127 TxnInput.trade_price = CurrentPrice;
128 }
129
130 m_pSUT->TradeResult(&TxnInput);
131
132 // Populate Ticker Entry information
133 strncpy(TickerEntry.symbol, pTradeRequest->symbol, sizeof(TickerEntry.symbol));
134 TickerEntry.trade_qty = pTradeRequest->trade_qty;
135
136 // Note that the Trade-Result sent out above does not always use
137 // the current price. We're about to "lie" by setting the ticker entry
138 // price to the current price regardless of how the Trade-Result
139 // price was actually set. We do this to preserve the continuity
140 // of the price curve. This is important because the ticker prices
141 // are used by transactions to make decisions. It is possible for
142 // the Trade-Result price to be out of sync with the price curve,
143 // and putting this price into the ticker stream will alter the
144 // behavior of other transactions. If you're having a guilt-attack
145 // about this lie (or are just plain curious), comment out the
146 // assignment using CurrentPrice and uncomment the assignment
147 // using TxnInput.trade_price.
148 //
149 TickerEntry.price_quote = CurrentPrice;
150 // TickerEntry.price_quote = TxnInput.trade_price;
151
152 m_pTickerTape->AddEntry(&TickerEntry);
153
154 delete pTradeRequest;
155}
156