Skip to content

Commit 2867ecb

Browse files
committed
wip
1 parent 056e147 commit 2867ecb

File tree

9 files changed

+137
-105
lines changed

9 files changed

+137
-105
lines changed

stock_emu_lib/StockMeta.hpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
#pragma once
22

33
#ifndef INCLUDE_STOCKMETA_HPP_
4-
#include "stock_emu_lib/trade/Trade.hpp"
4+
#include <stock_emu_lib/Stock.hpp>
5+
#include <stock_emu_lib/util/MultiVector.hpp>
56

6-
class StockMeta {
7-
TradeBoard board;
7+
struct StockMeta {
8+
std::tuple<double, double> price_standard_base;
9+
};
10+
11+
class StockMetaList {
12+
public:
13+
const auto& getRate() const noexcept {
14+
return price_standard_rate;
15+
}
16+
17+
void add(StockMeta&& add_meta) {
18+
meta.push_back(std::move(add_meta));
19+
price_standard_rate.push(0, 0);
20+
}
21+
22+
private:
23+
std::vector<StockMeta> meta;
24+
util::MultiVector<double, double> price_standard_rate;
825
};
926

1027
#endif // INCLUDE_STOCKMETA_HPP_

stock_emu_lib/bot/trader/Trader.cpp

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -93,51 +93,9 @@ void Fundamental::Trade(Trader &trader, StockMarketRef market) {
9393
// board->getCurrentPrice().latest.getValue()
9494
// << "\n";
9595
//
96-
97-
if (board->getCurrentPrice().higer) {
98-
// StockValue よりも安く買える
99-
if (board->getCurrentPrice().higer.value() < board->StockValue()) {
100-
StockPrice price = getMarketPriceHigher(board, board->StockValue(), 1);
101-
trader.buy(price, {getTradeAmount()}, board->id, market);
102-
return;
103-
} else {
104-
// StockValueより高い
105-
106-
// 買い手がいない
107-
if (!board->getCurrentPrice().lower) {
108-
StockPrice price = getMarketPriceLower(board, board->StockValue(), 1);
109-
if (price <= board->StockValue()) {
110-
return;
111-
}
112-
auto count_tmp = getTradeAmount();
113-
auto count =
114-
StockCount{trader.stock[stock] >= count_tmp ? count_tmp : trader.stock[stock].to_StockCount()};
115-
trader.sell(price, count, board->id, market);
116-
return;
117-
}
118-
}
119-
}
120-
if (board->getCurrentPrice().lower) {
121-
if (board->getCurrentPrice().lower.value() > board->StockValue()) {
122-
// std::cout << "high\n";
123-
StockPrice price = getMarketPriceLower(board, board->StockValue(), 1);
124-
auto count_tmp = getTradeAmount();
125-
auto count = StockCount{trader.stock[stock] >= count_tmp ? count_tmp : trader.stock[stock].to_StockCount()};
126-
trader.sell(price, count, board->id, market);
127-
return;
128-
} else {
129-
// 売り手がいない
130-
if (!board->getCurrentPrice().higer) {
131-
StockPrice price = getMarketPriceHigher(board, board->StockValue(), 1);
132-
if (price >= board->StockValue()) {
133-
return;
134-
}
135-
trader.buy(price, {getTradeAmount()}, board->id, market);
136-
return;
137-
}
138-
}
139-
}
140-
// std::cout << "nothing\n";
96+
const auto &datalist = market->getDatalist();
97+
auto data = datalist.getRate().getRow(stock);
98+
auto value_data1 = std::get<0>(data);
14199
}
142100

143101
} // namespace bot::trader

stock_emu_lib/trade/Trade.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,7 @@ void TradeBoard::tick() noexcept {
135135
}
136136
}
137137

138-
void TradeBoard::updateStockValue(StockPrice price, StockMarketRef market) {
139-
this->stock_value = price;
140-
market->updatePricePerValue(*this);
141-
}
142-
143-
void StockMarket::updatePricePerValue(const TradeBoard& ref) {
144-
this->value_dondake_hanareteru.at(ref.id) =
145-
std::abs(ref.getHistory().getCurrentPrice().latest.getValue() - ref.StockValue().getValue());
146-
}
138+
void StockMarket::updatePricePerValue(const TradeBoard& ref) {}
147139

148140
void TradeBoard::sell_limit_destruct::run(std::deque<Trade::SellTradeRequest>& q) {
149141
for (auto& i : q) {

stock_emu_lib/trade/Trade.hpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include "stock_emu_lib/StockMeta.hpp"
23
#ifndef INCLUDE_TRADE_HPP_
34
#define INCLUDE_TRADE_HPP_
45

@@ -29,8 +30,7 @@ struct TradeBoard {
2930

3031
constexpr static int expire_time = 4;
3132

32-
TradeBoard(StockId id_, StockMarket& market_, StockPrice value = {300})
33-
: id(id_), market_ref(market_), stock_value(value) {}
33+
TradeBoard(StockId id_, StockMarket& market_) : id(id_), market_ref(market_) {}
3434

3535
bool LimitOrder_Sell(StockPrice, StockCount, Trader&);
3636
bool LimitOrder_Buy(StockPrice, StockCount, Trader&);
@@ -48,12 +48,6 @@ struct TradeBoard {
4848
return sell;
4949
}
5050

51-
constexpr const auto& StockValue() const {
52-
return stock_value;
53-
}
54-
55-
void updateStockValue(StockPrice price, StockMarketRef market);
56-
5751
void tick() noexcept;
5852

5953
constexpr const auto& getHistory() const noexcept {
@@ -81,7 +75,6 @@ struct TradeBoard {
8175
Trade::TradeRequestBoard<Trade::SellTradeRequest, sell_limit_destruct> sell;
8276

8377
StockMarket& market_ref;
84-
StockPrice stock_value = {300};
8578

8679
TradeBoardHistory history;
8780
PriceLimit limit;
@@ -97,10 +90,10 @@ class StockMarket {
9790
return get(id);
9891
}
9992

100-
StockId add(StockPrice value = {300}) {
93+
StockId add(StockMeta&& meta) {
10194
auto new_id = static_cast<StockId>(boards.size());
102-
value_dondake_hanareteru.emplace_back();
103-
boards.emplace_back(new TradeBoard{new_id, *this, value});
95+
data_list.add(std::move(meta));
96+
boards.emplace_back(new TradeBoard{new_id, *this});
10497
return new_id;
10598
}
10699

@@ -115,7 +108,11 @@ class StockMarket {
115108
* 気が向いたらいい名前を考える
116109
*/
117110
const auto& get_value_dondake_hanareteru() const {
118-
return value_dondake_hanareteru;
111+
return data_list.getRate().getColumn<0>();
112+
}
113+
114+
auto& getDatalist() const noexcept {
115+
return data_list;
119116
}
120117

121118
void tick() {
@@ -126,7 +123,7 @@ class StockMarket {
126123

127124
private:
128125
std::vector<std::shared_ptr<TradeBoard>> boards;
129-
std::vector<double> value_dondake_hanareteru;
126+
StockMetaList data_list;
130127
};
131128

132129
inline Trader::StockData_t Trader::StockData_t::create(const std::vector<std::pair<StockId, StockCount_data_t>>& data) {

stock_emu_lib/trade/TradeHistory.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,21 @@ void TradeBoardHistory::update_history(StockPrice price, StockCount count) {
8585
CurrentStockPrice.latest = price;
8686
this->history.push_back({count, price});
8787

88-
auto& current_history = tick_history.back();
89-
if (current_history.start.getValue() == TickHistory::nothing) {
90-
current_history.start = price;
88+
if (currentTick.start.getValue() == TickHistory::nothing) {
89+
currentTick.start = price;
9190
}
9291

93-
current_history.end = price;
92+
currentTick.end = price;
9493

95-
if (current_history.high < price) {
96-
current_history.high = price;
97-
}
98-
if (current_history.low > price) {
99-
current_history.low = price;
94+
if (currentTick.high < price) {
95+
currentTick.high = price;
10096
}
97+
if (price < currentTick.low) currentTick.low = price;
10198
}
10299

103-
const TickHistory& TradeBoardHistory::tick() {
104-
const auto& result = tick_history.back();
105-
tick_history.emplace_back();
100+
const TickHistory TradeBoardHistory::tick() {
101+
const auto result = currentTick;
102+
tick_history.push(result.start, result.end, result.high, result.low);
103+
currentTick = TickHistory{};
106104
return result;
107105
}

stock_emu_lib/trade/TradeHistory.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include "stock_emu_lib/util/MultiVector.hpp"
23
#ifndef INCLUDE_TRADE_TRADEHISTORY_HPP
34
#define INCLUDE_TRADE_TRADEHISTORY_HPP
45

@@ -20,6 +21,7 @@ struct TickHistory {
2021

2122
constexpr static Money_data_t nothing = std::numeric_limits<Money_data_t>::min();
2223
};
24+
2325
struct TradeBoard;
2426

2527
/**
@@ -46,7 +48,7 @@ class TradeBoardHistory {
4648

4749
void update_history(StockPrice price, StockCount count);
4850

49-
const TickHistory& tick();
51+
const TickHistory tick();
5052

5153
private:
5254
struct {
@@ -55,7 +57,9 @@ class TradeBoardHistory {
5557
StockPrice latest = {0};
5658
} CurrentStockPrice;
5759

60+
TickHistory currentTick;
61+
5862
std::deque<TradeHistory> history{{{300}, {100}}};
59-
std::deque<TickHistory> tick_history = {{}};
63+
util::MultiVector<StockPrice, StockPrice, StockPrice, StockPrice> tick_history;
6064
};
6165
#endif // INCLUDE_TRADE_TRADEHISTORY_HPP

stock_emu_lib/util/MultiVector.hpp

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,76 @@
44
#define INCLUDE_UTIL_MULTIVECTOR_HPP_
55
#include <cstddef>
66
#include <tuple>
7-
#include <type_traits>
87
#include <utility>
98
#include <vector>
109

1110
namespace util {
11+
#if __cplusplus >= 202002L
12+
template<class OutT, class... DataT>
13+
concept OutT_check = requires(DataT... args) { OutT{args...}; };
14+
#else
15+
template<class OutT, class... args>
16+
class OutT_check_class {
17+
template<class OutT_, class... args_, typename = decltype(OutT_{std::declval<args_>()...})>
18+
static std::true_type chk();
1219

20+
template<typename, typename...>
21+
static std::false_type chk();
22+
23+
public:
24+
static constexpr bool value = decltype(chk<OutT, args...>())::value;
25+
};
26+
template<class OutT, class... args>
27+
constexpr bool OutT_check = OutT_check_class<OutT, args...>::value;
28+
29+
#endif
1330
template<class... T>
14-
class MlutiVector {
31+
class MultiVector {
1532
std::tuple<std::vector<T>...> data;
33+
size_t count = 0;
1634

17-
constexpr static size_t Data_Colum_count = std::tuple_size<decltype(data)>::value;
35+
constexpr static size_t Data_Colum_count = sizeof...(T);
1836

1937
public:
38+
size_t getRowCount() const noexcept {
39+
return count;
40+
}
41+
2042
void push(const T&... args) {
43+
count++;
2144
push_helper<0>(args...);
2245
}
2346

47+
void push(const std::tuple<T...>& arg) {
48+
count++;
49+
push_helper(arg, std::make_index_sequence<Data_Colum_count>());
50+
}
51+
2452
template<size_t Index>
25-
const auto& getColumn() {
53+
const auto& getColumn() const {
2654
return std::get<Index>(data);
2755
}
2856

29-
void erase(size_t index) {
30-
erase_helper(index, std::make_index_sequence<Data_Colum_count>());
57+
std::tuple<T...> getRow(size_t index) const {
58+
return get_As_helper<std::tuple<T...>>(index, std::make_index_sequence<Data_Colum_count>());
3159
}
3260

3361
template<class Out_t>
3462
Out_t get_As(size_t index) {
35-
static_assert(std::is_aggregate_v<Out_t>, "get_As need construct by {T...}");
36-
return get_As_helper<Out_t>(index, std::make_index_sequence<Data_Colum_count>());
63+
constexpr auto is_valid = OutT_check<Out_t, T&...>;
64+
static_assert(is_valid, "get_As parameter type must need construct by {T&...}");
65+
if constexpr (is_valid) {
66+
return get_As_helper<Out_t>(index, std::make_index_sequence<Data_Colum_count>());
67+
}
3768
}
3869

3970
template<class Out_t>
4071
const Out_t get_As(size_t index) const {
41-
static_assert(std::is_aggregate_v<Out_t>, "get_As need construct by {T...}");
42-
return get_As_helper<Out_t>(index, std::make_index_sequence<Data_Colum_count>());
72+
constexpr auto is_valid = OutT_check<Out_t, const T&...>;
73+
static_assert(is_valid, "get_As parameter type must need construct by {const T&...}.");
74+
if constexpr (is_valid) {
75+
return get_As_helper<Out_t>(index, std::make_index_sequence<Data_Colum_count>());
76+
}
4377
}
4478

4579
private:
@@ -48,14 +82,9 @@ class MlutiVector {
4882
return {std::get<Seq>(data)[index]...};
4983
}
5084

51-
template<class VectorT>
52-
static void eraseVector(std::vector<VectorT>& vec, size_t index) {
53-
vec.erase(vec.begin() + index);
54-
}
55-
56-
template<class IndexT, IndexT... Seq>
57-
void erase_helper(size_t index, std::integer_sequence<IndexT, Seq...>) {
58-
(..., eraseVector(std::get<Seq>(data), index));
85+
template<class Out_t, class IndexT, IndexT... Seq>
86+
Out_t get_As_helper(size_t index, std::integer_sequence<IndexT, Seq...>) const {
87+
return {std::get<Seq>(data)[index]...};
5988
}
6089

6190
template<size_t index, class Front>
@@ -68,6 +97,11 @@ class MlutiVector {
6897
std::get<index>(data).push_back(front);
6998
push_helper<index + 1>(tail...);
7099
}
100+
101+
template<class IndexT, IndexT... Seq>
102+
void push_helper(std::tuple<T...> arg, std::integer_sequence<IndexT, Seq...>) {
103+
(..., std::get<Seq>(data).push_back(std::get<Seq>(arg)));
104+
}
71105
};
72106
}
73107

test/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ FetchContent_MakeAvailable(googletest)
1313
enable_testing()
1414
add_executable(stock_emu_test
1515
./main.cpp
16-
./trade/trade.cpp
16+
#./trade/trade.cpp
17+
./Fuzzy.cpp
1718
)
1819

1920
target_include_directories(stock_emu_test PRIVATE ../)
2021

2122
target_link_libraries(stock_emu_test PRIVATE
22-
stock_emu_lib
23+
# stock_emu_lib
2324
GTest::gtest_main
2425
)
2526
target_compile_options(stock_emu_test PRIVATE

0 commit comments

Comments
 (0)