Skip to content

Commit

Permalink
upload
Browse files Browse the repository at this point in the history
  • Loading branch information
nickhuangxinyu1 committed Aug 20, 2020
1 parent a808446 commit ede1d55
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 0 deletions.
2 changes: 2 additions & 0 deletions copy_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def Copy():
continue
shutil.copy(header_path+f, target_path)
print('copying %s to %s' % (header_path+f, target_path))
if not os.path.exists("./lib-hft/lib/libnick.so"):
return
shutil.copy("./lib-hft/lib/libnick.so", './external/common/lib')

if __name__ == '__main__':
Expand Down
146 changes: 146 additions & 0 deletions external/common/include/core/backtest_container.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#ifndef BACKTEST_CONTAINER_H_
#define BACKTEST_CONTAINER_H_

#include <unordered_map>
#include <map>
#include <utility>
#include <string>
#include <vector>

#include <libconfig.h++>

#include "core/backtester.h"
#include "util/ThreadPool.h"
#include "util/time_controller.h"
#include "util/zmq_sender.hpp"
#include "util/zmq_recver.hpp"
#include "util/dater.h"
#include "util/history_worker.h"
#include "util/contract_worker.h"
#include "util/common_tools.h"
#include "struct/market_snapshot.h"



class BacktestContainer {
public:
BacktestContainer();
~BacktestContainer();

void Run() {
auto file_v = GetBacktestFile();
BTConfig bt_config = LoadConfig();
ThreadPool pool(6);
for (auto i : file_v) {
pool.enqueue(RunBacktest, bt_config, i.first, i.second);
}
}

private:
struct BTConfig {
std::string fixed_path;
std::string backtest_outputdir;
std::string start_date;
int period;
std::string test_mode;
ContractWorker* strat_cw;
ContractWorker* cw;
TimeController* tc;
inline std::tuple<ZmqSender<MarketSnapshot> *, ZmqSender<Order> *, std::ofstream*> GenSender(const std::string& date) {
std::string ui_address = "backtest_ui_" + date;
std::string order_address = "order_sender_" + date;
std::string ui_file = backtest_outputdir + "/mid_" + date + ".dat";
std::string order_file = backtest_outputdir + "/order_" + date + ".dat";
return std::make_tuple(new ZmqSender<MarketSnapshot>(ui_address, "bind", "ipc", ui_file), new ZmqSender<Order>(order_address, "connect", "ipc", order_file), new std::ofstream(backtest_outputdir + "/exchange_" + date + ".dat", ios::out | ios::binary));
}
inline HistoryWorker* GenHw(const std::string & date) {
return new HistoryWorker(Dater::FindOneValid(date, -20, fixed_path));
}
} bt_config;

static BTConfig LoadConfig() {
std::string default_path = GetDefaultPath();
libconfig::Config param_cfg;
std::string config_path = default_path + "/hft/config/backtest/backtest.config";
std::string contract_config_path = default_path + "/hft/config/contract/bk_contract.config";
param_cfg.readFile(config_path.c_str());
BTConfig bt_config;
try {
std::string fixed_path = param_cfg.lookup("fixed_path");
std::string start_date = param_cfg.lookup("start_date");
std::string backtest_outputdir = param_cfg.lookup("backtest_outputdir");
std::string test_mode = param_cfg.lookup("test_mode");
EnsureDir(backtest_outputdir);
Dater dt;
if (start_date == "today") {
start_date = dt.GetDate();
}
if (start_date == "yesterday") {
start_date = dt.GetDate("", -1);
}
int period = param_cfg.lookup("period");
bt_config.backtest_outputdir = backtest_outputdir;
bt_config.fixed_path = fixed_path;
bt_config.start_date = start_date;
bt_config.period = period;
bt_config.test_mode = test_mode;
} catch(const libconfig::SettingNotFoundException &nfex) {
printf("Setting '%s' is missing", nfex.getPath());
exit(1);
} catch(const libconfig::SettingTypeException &tex) {
printf("Setting '%s' has the wrong type", tex.getPath());
exit(1);
} catch (const std::exception& ex) {
printf("EXCEPTION: %s\n", ex.what());
exit(1);
}
std::string time_config_path = default_path + "/hft/config/prod/time.config";
bt_config.tc = new TimeController(time_config_path);
bt_config.cw = new ContractWorker(contract_config_path);
bt_config.strat_cw = new ContractWorker(config_path, "strategy");
return bt_config;
}

std::map<std::string, std::string> GetBacktestFile() {
auto dt = Dater();
return dt.GetValidMap(bt_config.start_date, bt_config.period, bt_config.fixed_path);
}

static std::unordered_map<std::string, std::vector<BaseStrategy*> > GetStratMap(BTConfig bt_config, std::string date) {
std::unordered_map<std::string, std::vector<BaseStrategy*> > ticker_strat_map;
ZmqSender<MarketSnapshot> * data_sender;
ZmqSender<Order> * order_sender;
std::ofstream* f;
StrategyMode::Enum mode;
if (bt_config.test_mode == "test") {
mode = StrategyMode::PlainTest;
} else if (bt_config.test_mode == "nexttest") {
mode = StrategyMode::NextTest;
} else {
throw std::invalid_argument(bt_config.test_mode.c_str());
}
std::tie(data_sender, order_sender, f) = bt_config.GenSender(date);
for (auto ticker : bt_config.strat_cw->GetTicker()) {
const libconfig::Setting & p = bt_config.strat_cw->Lookup(ticker);
// auto s = new Strategy(p, &ticker_strat_map, data_sender, order_sender, bt_config.tc, bt_config.cw, bt_config.GenHw(date), date, mode, f);
// s->Print();
}
return ticker_strat_map;
}

virtual void InitStrategy(std::unordered_map<std::string, std::vector<BaseStrategy*> >) {

}

static void RunBacktest(BTConfig bt_config, const std::string& date, const std::string& f) {
TimeController tc;
tc.StartTimer();
auto tsm = GetStratMap(bt_config, date);
Backtester bt(tsm);
bt.LoadData(f);
tc.EndTimer("Run@" + date);
}

};

#endif // BACKTEST_CONTAINER_H_
Binary file modified external/common/lib/libnick.so
Binary file not shown.
Binary file modified external/strategy/libcoinarb.so
Binary file not shown.
Binary file modified external/strategy/libdemostrat.so
Binary file not shown.
Binary file modified external/strategy/libpairtrading.so
Binary file not shown.
Binary file modified external/strategy/libsimplearb.so
Binary file not shown.
Binary file modified external/strategy/libsimplearb2.so
Binary file not shown.

0 comments on commit ede1d55

Please sign in to comment.