22#ifndef INCLUDE_TRADE_HPP_
33#define INCLUDE_TRADE_HPP_
44
5- #include < array>
65#include < csignal>
76#include < cstddef>
8- #include < cstdio>
97#include < deque>
10- #include < iostream>
11- #include < map>
128#include < memory>
139#include < utility>
1410#include < vector>
1915#include < stock_emu_lib/util/RingQueue.hpp>
2016
2117#include " TradeHistory.hpp"
18+ #include " stock_emu_lib/trade/TradeRequestBoard.hpp"
2219
2320struct TradeBoard ;
2421struct Trader ;
2522class StockMarket ;
2623using StockMarketRef = std::shared_ptr<StockMarket>;
2724using Tick_t = int ;
2825
29- struct Trader {
30- StockId id;
31- Money money;
32-
33- struct StockData_t : public std ::map<StockId, StockHoldingCount> {
34- static StockData_t create (const std::vector<std::pair<StockId, StockCount_data_t>>& data);
35- };
36-
37- StockData_t stock;
38-
39- void printStat () {
40- // std::printf("user %d\n money: %d\n stock: %d\n", id, money.getValue(), stock[1]);
41- std::cout << std::format (" id:{}\n money: {}\n stock:\n " , id, money);
42- for (const auto & i : stock) {
43- std::cout << std::format (" {}: {}\n " , i.first , i.second );
44- }
45- }
46-
47- Trader (StockId id_, Money&& money_, StockData_t&& stock_ = {})
48- : id(id_), money(std::move(money_)), stock(std::move(stock_)) {}
49-
50- void sell (StockPrice price, StockCount amount, StockId id, StockMarketRef ref);
51- void buy (StockPrice price, StockCount amount, StockId id, StockMarketRef ref);
52- };
53-
54- template <class T >
55- concept TradeRequest = requires (T& t) { t.amount == 0 ; };
56-
57- struct SellTradeRequest {
58- StockHoldingCount amount;
59- Trader& trader;
60- StockId id;
61- SellTradeRequest** refer = nullptr ;
62-
63- SellTradeRequest (StockHoldingCount&& amount_, Trader& trader_, StockId id_)
64- : amount(std::move(amount_)), trader(trader_), id(id_) {}
65-
66- void reject () {
67- amount.move (amount.to_StockCount ()).to (trader.stock [id]);
68- // *refer = nullptr;
69- }
70- };
71-
72- struct BuyTradeRequest {
73- StockCount amount;
74- Trader& trader;
75- Money money;
76- BuyTradeRequest** refer = nullptr ;
77-
78- BuyTradeRequest (StockCount amount_, Trader& trader_, Money&& money_)
79- : amount(amount_), trader(trader_), money(std::move(money_)) {}
80-
81- void reject () {
82- money.move (money.as_Price ()).to (trader.money );
83- this ->amount = 0 ;
84- // *refer = nullptr;
85- }
86-
87- BuyTradeRequest () = delete ;
88- };
89-
9026struct TradeBoard {
9127public:
9228 StockId id;
@@ -102,53 +38,7 @@ struct TradeBoard {
10238 void MarketOrder_sell (StockCount count, Trader&);
10339 void MarketOrder_Buy (StockCount count, Trader&);
10440
105- void printAll () {
106- constexpr int show_limit = 10 ;
107-
108- std::printf (" %10s|price|\n " , " " );
109-
110- struct count_price {
111- StockPrice price;
112- StockCount count;
113- };
114-
115- std::array<count_price, show_limit> prices;
116- size_t price_current_count = 0 ;
117-
118- for (auto i = sell.order_list .begin (); i != sell.order_list .end (); ++i) {
119- const auto & price = i->first ;
120- StockCount amount_sum = 0 ;
121- for (const auto & j : i->second ) {
122- amount_sum += j->amount .to_StockCount ();
123- }
124- if (amount_sum == 0 ) continue ;
125- prices[price_current_count] = {price, amount_sum};
126- price_current_count++;
127- if (price_current_count == show_limit) break ;
128- // std::cout << std::format("{: >10} {: ^5} \n", amount_sum, price.getValue());
129- }
130- for (auto i = prices.rbegin (); i != prices.rend (); i++) {
131- if (i->count .value == 0 ) {
132- continue ;
133- }
134- std::cout << std::format (" {: >10} {: ^5} \n " , i->count , i->price .getValue ());
135- }
136-
137- int count = 0 ;
138- for (auto i = buy.order_list .rbegin (); i != buy.order_list .rend (); ++i, ++count) {
139- if (count > show_limit) {
140- break ;
141- }
142-
143- const auto & price = i->first ;
144- StockCount amount_sum = 0 ;
145- for (const auto & j : i->second ) {
146- amount_sum += j->amount ;
147- }
148- if (amount_sum == 0 ) continue ;
149- std::cout << std::format (" {:10} {: ^5} {: >10}\n " , " " , price.getValue (), amount_sum);
150- }
151- }
41+ void printAll ();
15242
15343 constexpr const auto & getBuyBoard () const {
15444 return buy;
@@ -179,60 +69,16 @@ struct TradeBoard {
17969 }
18070
18171private:
182- template <TradeRequest Request_t, class limit_destructor_t >
183- struct TradeRequestBoard {
184- class RequestList_Wrapper : public std ::deque<Request_t*> {
185- public:
186- bool is_empty () const {
187- for (const auto & i : *this ) {
188- if (i != nullptr ) {
189- if (i->amount != 0 ) {
190- return false ;
191- }
192- }
193- }
194- return true ;
195- }
196- };
197-
198- std::map<StockPrice, RequestList_Wrapper> order_list = {};
199- util::RingQueue<std::deque<Request_t>, 24 , limit_destructor_t > limit_queue;
200-
201- template <class ... Args>
202- void add (size_t expire, StockPrice price, Args&&... arg) {
203- limit_queue.get (expire).emplace_back (std::forward<Args>(arg)...);
204- auto * elem = &limit_queue.get (expire).back ();
205-
206- order_list[price].emplace_back (elem);
207- auto * ref_ptr = &order_list[price].back ();
208- elem->refer = ref_ptr;
209- }
210-
211- bool OrderExists (StockPrice price) const {
212- if (const auto find = order_list.find (price); find == order_list.end ()) {
213- return false ;
214- }
215- if (order_list.at (price).empty ()) {
216- return false ;
217- }
218- if (order_list.at (price).is_empty ()) {
219- printf (" ???\n " );
220- return false ;
221- }
222- return true ;
223- }
224- };
225-
22672 struct buy_limit_destruct {
227- static void run (std::deque<BuyTradeRequest>&);
73+ static void run (std::deque<Trade:: BuyTradeRequest>&);
22874 };
22975
23076 struct sell_limit_destruct {
231- static void run (std::deque<SellTradeRequest>&);
77+ static void run (std::deque<Trade:: SellTradeRequest>&);
23278 };
23379
234- TradeRequestBoard<BuyTradeRequest, buy_limit_destruct> buy;
235- TradeRequestBoard<SellTradeRequest, sell_limit_destruct> sell;
80+ Trade:: TradeRequestBoard<Trade:: BuyTradeRequest, buy_limit_destruct> buy;
81+ Trade:: TradeRequestBoard<Trade:: SellTradeRequest, sell_limit_destruct> sell;
23682
23783 StockMarket& market_ref;
23884 StockPrice stock_value = {300 };
0 commit comments