Skip to content

Commit

Permalink
upload new backtester
Browse files Browse the repository at this point in the history
  • Loading branch information
nickhuangxinyu1 committed Mar 19, 2020
1 parent 9491225 commit 65fb98b
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 14 deletions.
3 changes: 2 additions & 1 deletion config/backtest/backtest.config
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ strategy = (

// start_date = "today";
// start_date = "yesterday";
start_date = "today"
test_mode = "test";
start_date = "2020-03-17"
period = 1;
matcher_mode = "python";
message_line = 100000;
Expand Down
2 changes: 1 addition & 1 deletion external/common/include/core/backtester.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Backtester : public DataHandler {

~Backtester();

void HandleShot(MarketSnapshot* shot) override;
void HandleShot(MarketSnapshot* this_shot, MarketSnapshot* next_shot) override;

private:
BaseStrategy * bs;
Expand Down
2 changes: 2 additions & 0 deletions external/common/include/core/base_strategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class BaseStrategy {
virtual void Start() = 0;
virtual void Stop() = 0;
void UpdateData(const MarketSnapshot& shot);
void UpdateData(const MarketSnapshot& this_shot, const MarketSnapshot& next_shot);
void UpdateExchangeInfo(const ExchangeInfo& info);
void RequestQryPos();
virtual void Print() const;
Expand Down Expand Up @@ -71,6 +72,7 @@ class BaseStrategy {
ShmSender<Order>* order_sender;
Sender<MarketSnapshot>* ui_sender;
unordered_map<std::string, MarketSnapshot> shot_map;
unordered_map<std::string, MarketSnapshot> next_shot_map;
unordered_map<std::string, Order*> order_map;
unordered_map<std::string, Order*> sleep_order_map;
unordered_map<std::string, int> position_map;
Expand Down
5 changes: 4 additions & 1 deletion external/common/include/util/data_handler.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef DATA_HANDLER_H_
#define DATA_HANDLER_H_
#include <iostream>
#include <unordered_map>
#include <string>
#include <zlib.h>
#include "struct/market_snapshot.h"
Expand All @@ -12,7 +13,9 @@ class DataHandler {
DataHandler();
~DataHandler();
void LoadData(const std::string& file_path);
virtual void HandleShot(MarketSnapshot* shot) = 0;
virtual void HandleShot(MarketSnapshot* this_shot, MarketSnapshot* next_shot) = 0;
private:
std::unordered_map<std::string, MarketSnapshot> lastshot_map;
};

#endif // DATA_HANDLER_H_
5 changes: 5 additions & 0 deletions external/common/include/util/shm_worker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ShmWorker {
}

virtual ~ShmWorker() {
printf("shmworker deconstructor called\n");
shmdt(m_data);
if (create_new) {
shmctl(shmid, IPC_RMID, 0);
Expand Down Expand Up @@ -62,6 +63,10 @@ class ShmWorker {
printf("errno is %s\n", strerror(errno));
if (errno == ENOENT || errno == EINVAL) {
shmid = shmget(m_key, header_size+sizeof(T)*size, 0666 | IPC_CREAT | O_EXCL);
if (shmid == -1) {
printf("both connet and create are failed for shm\n");
exit(1);
}
printf("creating new shm\n");
create_new = true;
m_data = (char*)shmat(shmid, NULL, 0);
Expand Down
Binary file modified external/common/lib/libnick.so
Binary file not shown.
File renamed without changes.
3 changes: 2 additions & 1 deletion src/backtest/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ std::pair< std::unordered_map<std::string, std::vector<BaseStrategy*> >, std::ve

Dater dt;
std::string start_date = param_cfg.lookup("start_date");
std::string test_mode = param_cfg.lookup("test_mode");
if (start_date == "today") {
start_date = dt.GetDate();
}
Expand All @@ -53,7 +54,7 @@ std::pair< std::unordered_map<std::string, std::vector<BaseStrategy*> >, std::ve
if (param_setting.exists("no_close_today")) {
no_close_today = param_setting["no_close_today"];
}
auto s = new Strategy(param_setting, &ticker_strat_map, ui_sender.get(), order_sender.get(), &hw, "test", no_close_today);
auto s = new Strategy(param_setting, &ticker_strat_map, ui_sender.get(), order_sender.get(), &hw, test_mode, no_close_today);
s->Print();
}
} catch(const libconfig::SettingNotFoundException &nfex) {
Expand Down
33 changes: 23 additions & 10 deletions src/simplearb/strategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void Strategy::RunningSetup(std::unordered_map<std::string, std::vector<BaseStra
shot_map[hedge_ticker] = shot;
avgcost_map[main_ticker] = 0.0;
avgcost_map[hedge_ticker] = 0.0;
if (mode == "test") {
if (mode == "test" || mode == "nexttest") {
position_ready = true;
}
}
Expand Down Expand Up @@ -178,13 +178,26 @@ void Strategy::DoOperationAfterCancelled(Order* o) {
}

double Strategy::OrderPrice(const std::string & ticker, OrderSide::Enum side, bool control_price) {
if (ticker == hedge_ticker) {
return (side == OrderSide::Buy)?shot_map[hedge_ticker].asks[0]:shot_map[hedge_ticker].bids[0];
} else if (ticker == main_ticker) {
return (side == OrderSide::Buy)?shot_map[main_ticker].asks[0]:shot_map[main_ticker].bids[0];
if (mode == "nexttest") {
if (ticker == hedge_ticker) {
printf("[%s] %s: %lf %lf ->> %lf %lf\n", hedge_ticker.c_str(), OrderSide::ToString(side), shot_map[hedge_ticker].asks[0], shot_map[hedge_ticker].bids[0], next_shot_map[hedge_ticker].asks[0], next_shot_map[hedge_ticker].bids[0]);
return (side == OrderSide::Buy)?next_shot_map[hedge_ticker].asks[0]:next_shot_map[hedge_ticker].bids[0];
} else if (ticker == main_ticker) {
printf("[%s] %s: %lf %lf ->> %lf %lf\n", main_ticker.c_str(), OrderSide::ToString(side), shot_map[main_ticker].asks[0], shot_map[main_ticker].bids[0], next_shot_map[main_ticker].asks[0], next_shot_map[main_ticker].bids[0]);
return (side == OrderSide::Buy)?next_shot_map[main_ticker].asks[0]:next_shot_map[main_ticker].bids[0];
} else {
printf("error ticker %s\n", ticker.c_str());
return -1.0;
}
} else {
printf("error ticker %s\n", ticker.c_str());
return -1.0;
if (ticker == hedge_ticker) {
return (side == OrderSide::Buy)?shot_map[hedge_ticker].asks[0]:shot_map[hedge_ticker].bids[0];
} else if (ticker == main_ticker) {
return (side == OrderSide::Buy)?shot_map[main_ticker].asks[0]:shot_map[main_ticker].bids[0];
} else {
printf("error ticker %s\n", ticker.c_str());
return -1.0;
}
}
}

Expand Down Expand Up @@ -316,7 +329,7 @@ void Strategy::CloseLogic() {
}

if (TimeUp()) {
printf("[%s %s] holding time up, start from %ld, now is %ld, max_hold is %d close diff is %lf force to close position!\n", main_ticker.c_str(), hedge_ticker.c_str(), build_position_time, mode == "test" ? shot_map[main_ticker].time.tv_sec : m_tc.CurrentInt(), max_holding_sec, GetPairMid());
printf("[%s %s] holding time up, start from %ld, now is %ld, max_hold is %d close diff is %lf force to close position!\n", main_ticker.c_str(), hedge_ticker.c_str(), build_position_time, mode == "test" || mode == "nexttest" ? shot_map[main_ticker].time.tv_sec : m_tc.CurrentInt(), max_holding_sec, GetPairMid());
ForceFlat();
return;
}
Expand Down Expand Up @@ -387,7 +400,7 @@ void Strategy::DoOperationAfterUpdateData(const MarketSnapshot& shot) {
current_spread = shot_map[main_ticker].asks[0] - shot_map[main_ticker].bids[0] + shot_map[hedge_ticker].asks[0] - shot_map[hedge_ticker].bids[0];
if (IsAlign()) {
double mid = GetPairMid();
if (mode != "test") {
if (mode != "test" && mode != "nexttest") {
printf("%ld [%s, %s]mid_diff is %lf\n", shot.time.tv_sec, main_ticker.c_str(), hedge_ticker.c_str(), mid_map[main_ticker]-mid_map[hedge_ticker]);
}
if (ss == StrategyStatus::Training) {
Expand Down Expand Up @@ -529,7 +542,7 @@ void Strategy::UpdateBound(OrderSide::Enum side) {
}

void Strategy::HandleTestOrder(Order* o) {
if (mode != "test") {
if (mode != "test" && mode != "nexttest") {
return;
}
ExchangeInfo info;
Expand Down

0 comments on commit 65fb98b

Please sign in to comment.