Skip to content

Commit

Permalink
upload
Browse files Browse the repository at this point in the history
  • Loading branch information
nickhuangxinyu1 committed Apr 26, 2020
1 parent c902004 commit 7f2d887
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 646 deletions.
4 changes: 2 additions & 2 deletions config/backtest/backtest.config
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ strategy = (
// start_date = "yesterday";
// test_mode = "nexttest";
test_mode = "nexttest";
start_date = "today";
period = 1;
start_date = "2019-01-03";
period = 1000;

order_file = "order_backtest.dat";
exchange_file = "exchange_backtest.dat";
Expand Down
11 changes: 6 additions & 5 deletions external/common/include/struct/exchange_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
// #include "wrapstruct.h"

struct ExchangeInfo {
timeval time;
timeval show_time;
timeval shot_time;
InfoType::Enum type;
char ticker[MAX_CONTRACT_LENGTH];
char order_ref[MAX_ORDERREF_SIZE];
int trade_size;
double trade_price;
char reason[EXCHANGE_INFO_SIZE];
OrderSide::Enum side;
char reason[EXCHANGE_INFO_SIZE];

ExchangeInfo()
: trade_size(0),
Expand All @@ -26,14 +27,14 @@ struct ExchangeInfo {

void ShowCsv(FILE* stream) const {
char time_s[32];
snprintf(time_s, sizeof(time_s), "%ld.%ld", time.tv_sec, time.tv_usec);
snprintf(time_s, sizeof(time_s), "%ld.%06ld %ld.%06ld", show_time.tv_sec, show_time.tv_usec, shot_time.tv_sec, shot_time.tv_usec);
double time_sec = atof(time_s);
fprintf(stream, "%lf,%s,%s,%s,%d,%lf,%s,%s\n", time_sec, InfoType::ToString(type),ticker,order_ref,trade_size,trade_price,reason,OrderSide::ToString(side));
}

void Show(FILE* stream) const {
fprintf(stream, "%ld %06ld exchangeinfo %s |",
time.tv_sec, time.tv_usec, order_ref);
fprintf(stream, "%ld %06ld %ld %06ld exchangeinfo %s |",
show_time.tv_sec, show_time.tv_usec, shot_time.tv_sec, shot_time.tv_usec, order_ref);
fprintf(stream, " %lf@%d %s %s %s\n", trade_price, trade_size, InfoType::ToString(type), ticker, OrderSide::ToString(side));
}
};
Expand Down
Binary file modified external/common/lib/libnick.so
Binary file not shown.
11 changes: 7 additions & 4 deletions src/backtest/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ struct BTConfig {
ContractWorker* strat_cw;
ContractWorker* cw;
TimeController* tc;
inline std::pair<Sender<MarketSnapshot> *, Sender<Order> *> GenSender(const std::string& date) {
inline std::tuple<Sender<MarketSnapshot> *, Sender<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_pair(new Sender<MarketSnapshot>(ui_address, "bind", "ipc", ui_file), new Sender<Order>(order_address, "connect", "ipc", order_file));
return std::make_tuple(new Sender<MarketSnapshot>(ui_address, "bind", "ipc", ui_file), new Sender<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));
Expand Down Expand Up @@ -92,11 +92,14 @@ std::map<std::string, std::string> GetBacktestFile() {

std::unordered_map<std::string, std::vector<BaseStrategy*> > GetStratMap(std::string date) {
std::unordered_map<std::string, std::vector<BaseStrategy*> > ticker_strat_map;
auto senders = bt_config.GenSender(date);
Sender<MarketSnapshot> * data_sender;
Sender<Order> * order_sender;
std::ofstream* f;
std::tie(data_sender, order_sender, f) = bt_config.GenSender(date);
auto hw = bt_config.GenHw(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, senders.first, senders.second, hw, bt_config.tc, bt_config.cw, bt_config.test_mode);
auto s = new Strategy(p, &ticker_strat_map, data_sender, order_sender, hw, bt_config.tc, bt_config.cw, bt_config.test_mode, f);
s->Print();
}
return ticker_strat_map;
Expand Down
Loading

0 comments on commit 7f2d887

Please sign in to comment.