Skip to content

Commit 20f5dd5

Browse files
author
open-trade
committed
add derived data support and start.py
1 parent 4d6799c commit 20f5dd5

File tree

8 files changed

+50
-4
lines changed

8 files changed

+50
-4
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ ticks
1717
*.txt
1818
*.con
1919
opentrade-dbg
20+
nohup.out

Diff for: src/opentrade/account.h

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ struct User : public AccountBase {
7272
SubAccountMapPtr sub_accounts_ = std::make_shared<SubAccountMap>();
7373
};
7474

75+
inline const User kEmptyUser;
76+
7577
class AccountManager : public Singleton<AccountManager> {
7678
public:
7779
static void Initialize();

Diff for: src/opentrade/algo.cc

+14-1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,19 @@ void AlgoManager::Run(int nthreads) {
177177
runners_[i].tid_ = threads_[i].get_id();
178178
}
179179
#endif
180+
181+
for (auto& pair : adapters()) {
182+
if (pair.first.at(0) != '_') continue;
183+
auto user_name = pair.second->config("user");
184+
auto user = AccountManager::Instance().GetUser(user_name);
185+
auto algo = Spawn(std::make_shared<Algo::ParamMap>(), pair.first,
186+
user ? *user : kEmptyUser, "{}", "");
187+
if (algo) {
188+
LOG_INFO("Started " << pair.first << " , id=" << algo->id());
189+
} else {
190+
LOG_ERROR("Failed to start" << pair.first);
191+
}
192+
}
180193
}
181194

182195
void AlgoManager::Handle(Confirmation::Ptr cm) {
@@ -361,7 +374,7 @@ Instrument* Algo::Subscribe(const Security& sec, DataSrc src, bool listen) {
361374
assert(adapter);
362375
auto inst = new Instrument(this, sec, DataSrc(adapter->src()));
363376
inst->md_ = &MarketDataManager::Instance().Get(sec, adapter->src());
364-
inst->id_ = ++Instrument::kIdCounter;
377+
inst->id_ = ++Instrument::id_counter_;
365378
inst->listen_ = listen;
366379
instruments_.insert(inst);
367380
if (listen) AlgoManager::Instance().Register(inst);

Diff for: src/opentrade/algo.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class Instrument {
158158
bool listen_ = true;
159159
friend class AlgoManager;
160160
friend class Algo;
161-
static inline std::atomic<size_t> kIdCounter = 0;
161+
static inline std::atomic<size_t> id_counter_ = 0;
162162
};
163163

164164
class AlgoRunner {

Diff for: src/opentrade/connection.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -1106,10 +1106,11 @@ void Connection::OnLogin(const std::string& action, const json& j) {
11061106
Send(json{"broker_account", pair.first, pair.second->name});
11071107
}
11081108
for (auto& pair : AlgoManager::Instance().adapters()) {
1109+
if (pair.first.at(0) == '_') continue;
11091110
auto& params = pair.second->GetParamDefs();
11101111
json j = {
11111112
"algo_def",
1112-
pair.second->name(),
1113+
pair.first,
11131114
};
11141115
for (auto& p : params) {
11151116
json j2 = {

Diff for: src/opentrade/main.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ int main(int argc, char *argv[]) {
152152
for (auto &section : prop_tree) {
153153
if (!section.second.size()) continue;
154154
auto section_name = section.first;
155+
if (section_name.empty()) continue;
155156
opentrade::Adapter::StrMap params;
156157
for (auto &item : section.second) {
157158
auto name = item.first;
@@ -206,7 +207,7 @@ int main(int argc, char *argv[]) {
206207
boost::make_iterator_range(fs::directory_iterator(kAlgoPath), {})) {
207208
auto path = entry.path();
208209
auto fn = path.filename().string();
209-
if (fn[0] == '_' && fn[0] == '.') continue;
210+
if (fn[0] == '.') continue;
210211
opentrade::Algo *algo = nullptr;
211212
auto algoname = fn.substr(0, fn.length() - 3);
212213
if (path.extension() == ".py") {

Diff for: src/opentrade/market_data.h

+25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define OPENTRADE_MARKET_DATA_H_
33

44
#include <tbb/concurrent_unordered_map.h>
5+
#include <any>
56
#include <map>
67
#include <string>
78

@@ -55,6 +56,30 @@ struct MarketData {
5556

5657
Trade trade;
5758
Depth depth;
59+
60+
void SetDerived(const std::string& name, std::any value) {
61+
std::lock_guard<std::mutex> lock(mutex_);
62+
if (!derived) derived = new AnyMap;
63+
derived->emplace(name, value);
64+
}
65+
66+
template <typename T>
67+
const T* GetDerived(const std::string& name) const {
68+
std::lock_guard<std::mutex> lock(mutex_);
69+
if (!derived) return {};
70+
auto it = derived->find(name);
71+
if (it == derived->end()) return {};
72+
try {
73+
return std::any_cast<const T*>(it->second);
74+
} catch (const std::bad_any_cast& e) {
75+
return {};
76+
}
77+
}
78+
79+
private:
80+
typedef std::map<std::string, std::any> AnyMap;
81+
AnyMap* derived = nullptr;
82+
static inline std::mutex mutex_;
5883
};
5984

6085
struct DataSrc {

Diff for: src/opentrade/server.cc

+3
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ void Server::Start(int port, int nthreads) {
272272
for (auto i = 0; i < nthreads; ++i) {
273273
threads.emplace_back([]() { kIoService->run(); });
274274
}
275+
if (fs::exists(fs::path("start.py"))) {
276+
system(("nohup ./start.py " + std::to_string(port) + " &").c_str());
277+
}
275278
for (auto& t : threads) t.join();
276279
} catch (std::runtime_error& e) {
277280
LOG_ERROR("failed to start web server: " << e.what());

0 commit comments

Comments
 (0)