config json monitor
前端框架dashboard
https://clickhouse.com/docs
前端发送更改策略配置参数请求,然后服务器接收,给策略引擎 1
前端发送修改数据库请求,服务器去数据库中不停读取 2
三种办法目前
1
发送json请求,服务器识别后共享内存发给策略引擎(通过包装成event来发送),然后在loadjson的代码出init 就 重新init (init的时候涉及资源的释放) onConfigJsonChange 来处理这个任务。
关于init问题,涉及的资源较多,如果下游有什么任务还在处理中,上游的某个服务启停让任务处理找不到方向,或者说风控的启停间隙有别的任务没走风控,所以如果可以单独修改策略的参数不修改比如网关参数比如风控参数,局部修改。个别init。
2
涉及json文件的比对,读取后比较old 与 new,有变化就重新init
3
~/test/TCP/pythoncliTocppServer
使用之前的持仓互通,随便指定个header
volume
将main函数
int main(int argc, char* argv[])
这两行代码放到 onConfigJsonChange
函数中调用
main_engine->initStrategies();
main_engine->startStrategies();
、、谁有权利调用这两行代码
void MainEngine::initStrategies(void)
{
this->getStrategyEngine()->loadAllStrategies();
this->getStrategyEngine()->initAllStrategies();
}
void MainEngine::initialize(void) 这里面新建strategy_engine 绑定回调 实例名字 不影响策略参数 不读取策略参数
init+start之前可以先stop发出, 副作用会cancel orders
void ArbitrageStrategy::onStop(void)
{
this->putEvent();
this->cancelAllOrders();
}
副作用:行情没人接收,那就调整json位置
可以处理event的模块有 DataEngine strategy rmEngine
this->event_engine->registerEventHandler(EVENT_TIMER_USEC, umhdl);
我想在mainEngine里面使用,当然mainengine 里面有
随便找个地方加上
EventHandler handler(this, &mainEngine::onConfigJsonChange);
this->event_engine->rigisterEventHandler(Event_ConfigJson_Change, handler);
void MainEngine::onConfigJsonChange(const Event& e)
{
stopStrategies();
initStrategies();
startStrategies();
}
第一种:涉及json文件的比对,读取后比较old 与 new,有变化就重新init
同理mainengine加一个定时器,检查配置文件更新了没得定时器,可以新建一个5秒定时器。这里使用现成的。
EventHandler mhdl(this, &mainEngine::updateTimerForConfigJsonChange);
this->event_engine->registerEventHandler(EVENT_TIMER_SEC, mhdl);
void MainEngine::updateTimerForConfigJsonChange(const Event& e)
{
// 检查json是否变化,1.比较日期时间 2. 比较内容
// 这里使用 nlohmann/json.hpp
// 保存上一个修改时间和内容,修改loadjson地方保存
std::string file = "stragegy.json";
if (compareJsonFiles(file)) {
std::cout << "JSON files are identical." << std::endl;
} else {
std::cout << "JSON files are different." << std::endl;
Event e;
e.event_type = Event_ConfigJson_Change;
//e.event_data = quote;// 此处可优化
this->putEvent(e);
}
}
// 使用 nlohmann::json 库
using json = nlohmann::json;
namespace fs = std::filesystem;
// 获取文件的最后修改时间
std::time_t getFileModificationTime(const std::string& filePath) {
std::error_code ec;
auto ftime = fs::last_write_time(filePath, ec);
if (ec) {
std::cerr << "Error getting file modification time: " << ec.message() << std::endl;
return 0;
}
return decltype(ftime)::clock::to_time_t(ftime);
}
bool compareJsonFiles(const std::string& file1) {
// 获取文件的最后修改时间
std::time_t modTime1 = getFileModificationTime(file1);
std::time_t modTime2 = this->lastModifyTime_;//上一个时间
// 比较修改时间
if (modTime1 == modTime2) {
std::cout << "Files have the same modification time. No need to compare content." << std::endl;
return true;
} else {
std::cout << "Files have different modification times. Comparing content..." << std::endl;
std::ifstream f1(file1);
// 解析文件内容为JSON对象
json json1;
f1 >> json1;
json json2 = this->lastJsonContent_;//上一个json对象
// 比较JSON内容
return json1 == json2;
}
}
struct ConfigJson {
nlohmann::json lastJsonContent_;
std::time_t lastModifyTime_;
};
ConfigJson configJson_;
第二种
第三种
pythoncliTocppServer
// TLV
enum class SendMessageTag : uint32_t {
OTHERTDONRTNORDER = 10001,
OTHERTDONRTNTRADE = 10002,
OTHERSCONFIGJSONCHANGE = 10010,
OTHERS = 0
};
} else if (msgid == static_cast<uint32_t>(CtpTdSpi::SendMessageTag::OTHERSCONFIGJSONCHANGE)) {
Task task;
task.task_name = TDONFRONTCONNECTED;
this->task_queue.push(task);
队列 异步
void CtpTradeConnection::processTask()
case OTHERTDONRTNTRADE :
this->onFrontDisconnected(task) {
VtErrorData data;
data.set_user_id(this->user_id);
...
data.set_error_time(getCurrentTime());
data.set_extra_info("");
this->gateway_->onError(data) {
VtGateway::onError(VtQuoteError& error)
Event e;
e.event_type = EVENT_QUOTE_ERROR;
e.event_data = error;
this->event_engine->putEvent(e);
};
}
快捷的办法,绕开 task 数据结构 直接 event
#include "event/event.h"
#include "event/eventengine.h"
VtGateway have eventEngine protected
CtpGateway : public VtGateway getter
class CtpTradeConnection have CtpGateway* gateway_; need getter
绕不开 异步, ctpTdSpi 里面有个 队列没有别的与外界沟通的(发送东西的媒介,没CtpTradeConnection指针)