Skip to content

Commit

Permalink
upload from ali
Browse files Browse the repository at this point in the history
  • Loading branch information
nickhuangxinyu committed Nov 21, 2019
1 parent dd8a26a commit 6dcb677
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 7 deletions.
5 changes: 3 additions & 2 deletions config/backtest/backtest.config
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ strategy = (
},

{
unique_name = "ni";
unique_name = "sn";
max_position = 3;
range_width = 2.0;
min_train_samples = 600;
Expand All @@ -95,8 +95,9 @@ strategy = (
}
);

start_date = "today";
// start_date = "today";
// start_date = "yesterday";
start_date = "today"
period = 1;
matcher_mode = "python";
message_line = 100000;
Expand Down
2 changes: 1 addition & 1 deletion config/prod/prod.config
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ strategy = (
},

{
unique_name = "ni";
unique_name = "sn";
max_position = 3;
range_width = 2.0;
min_train_samples = 600;
Expand Down
Binary file modified external/common/lib/libcommontools.so
Binary file not shown.
Binary file modified external/common/libcommontools.so
Binary file not shown.
3 changes: 3 additions & 0 deletions src/backtest/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ int main() {
order_file.close();
exchange_file.close();
strat_file.close();
for (auto i : sv) {
delete i;
}
printf("backtest over!\n");
} catch(const libconfig::SettingNotFoundException &nfex) {
printf("Setting '%s' is missing", nfex.getPath());
Expand Down
7 changes: 6 additions & 1 deletion src/backtest/strategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
class Strategy : public BaseStrategy {
public:
explicit Strategy(const libconfig::Setting & param_setting, const libconfig::Setting & contract_setting, const TimeController& tc, std::unordered_map<std::string, std::vector<BaseStrategy*> >*ticker_strat_map, Contractor& ct, Sender* sender, const std::string & mode = "real", std::ofstream* order_file = nullptr, std::ofstream* exchange_file = nullptr, std::ofstream* strat_file = nullptr, bool no_close_today = false);
~Strategy();
virtual ~Strategy();

void Start() override;
void Stop() override;
Expand Down Expand Up @@ -121,6 +121,11 @@ class Strategy : public BaseStrategy {
bool no_close_today;
int open_count;
int close_count;
int main_record;
int hedge_record;
MarketSnapshot main_last;
MarketSnapshot hedge_last;
double total_slip_loss;
};

#endif // SRC_BACKTEST_STRATEGY_H_
34 changes: 32 additions & 2 deletions src/simplearb/strategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ Strategy::Strategy(const libconfig::Setting & param_setting, const libconfig::Se
this_strat_file(strat_file),
no_close_today(no_close_today),
open_count(0),
close_count(0) {
close_count(0),
main_record(0),
hedge_record(0),
total_slip_loss(0.0) {
caler = new CALER(contract_setting);
try {
m_ct = ct;
Expand All @@ -32,6 +35,7 @@ Strategy::Strategy(const libconfig::Setting & param_setting, const libconfig::Se
double m_r = param_setting["min_range"];
double m_p = param_setting["min_profit"];
min_price_move = contract_setting["min_price_move"];
contract_size = contract_setting["contract_size"];
printf("[%s, %s] mpv is %lf\n", main_ticker.c_str(), hedge_ticker.c_str(), min_price_move);
min_profit = m_p * min_price_move;
min_range = m_r * min_price_move;
Expand Down Expand Up @@ -76,6 +80,7 @@ Strategy::Strategy(const libconfig::Setting & param_setting, const libconfig::Se
}

Strategy::~Strategy() {
printf("[%s %s]total slip loss is %lf\n", main_ticker.c_str(), hedge_ticker.c_str(), total_slip_loss);
delete caler;
}

Expand Down Expand Up @@ -260,6 +265,8 @@ bool Strategy::Close(bool force_flat) {
if (order_map.empty()) {
PrintMap(avgcost_map);
Order* o = NewOrder(main_ticker, close_side, abs(pos), false, false, force_flat ? "force_flat_close" : "close", no_close_today); // close
main_record = (close_side == OrderSide::Sell) ? 1:-1;
main_last = shot_map[main_ticker];
o->Show(stdout);
HandleTestOrder(o);
if (mode == "real") {
Expand Down Expand Up @@ -337,6 +344,8 @@ void Strategy::Open(OrderSide::Enum side) {
printf("[%s %s] open %s: pos is %d, diff is %lf\n", main_ticker.c_str(), hedge_ticker.c_str(), OrderSide::ToString(side), pos, GetPairMid());
if (order_map.empty()) { // no block order, can add open
Order* o = NewOrder(main_ticker, side, 1, false, false, "", no_close_today);
main_record = (side == OrderSide::Sell) ? 1:-1;
main_last = shot_map[main_ticker];
o->Show(stdout);
// printf("spread is %lf %lf min_profit is %lf, next open will be %lf\n", shot_map[main_ticker].asks[0]-shot_map[main_ticker].bids[0], shot_map[hedge_ticker].asks[0]-shot_map[hedge_ticker].bids[0], min_profit, side == OrderSide::Buy ? down_diff: up_diff);
HandleTestOrder(o);
Expand Down Expand Up @@ -401,6 +410,26 @@ void Strategy::Init() {
}

void Strategy::DoOperationAfterUpdateData(const MarketSnapshot& shot) {
if (main_record && shot.ticker == main_ticker) {
double signal_price = main_record == 1 ? main_last.bids[0]: main_last.asks[0];
double next_price = main_record == 1 ? shot.bids[0]: shot.asks[0];
double slip_loss = -(next_price - signal_price)*main_record*contract_size;
printf("[%s %s]%ld main_record: %s order, from %lf->%lf, slip loss is %lf, shot is %s\n",
main_ticker.c_str(), hedge_ticker.c_str(), main_last.time.tv_sec, main_record == 1 ? "Sell" : "Buy",
signal_price, next_price, slip_loss, main_last.Copy().c_str());
total_slip_loss += slip_loss;
main_record = false;
}
if (hedge_record && shot.ticker == hedge_ticker) {
double signal_price = hedge_record == 1 ? hedge_last.bids[0]: hedge_last.asks[0];
double next_price = hedge_record == 1 ? shot.bids[0]: shot.asks[0];
double slip_loss = -(next_price - signal_price)*hedge_record*contract_size;
printf("[%s %s]%ld hedge_record: %s order, from %lf->%lf, slip loss is %lf, shot is %s\n",
main_ticker.c_str(), hedge_ticker.c_str(), hedge_last.time.tv_sec, hedge_record == 1 ? "Sell" : "Buy",
signal_price, next_price, slip_loss, hedge_last.Copy().c_str());
total_slip_loss += slip_loss;
hedge_record = false;
}
mid_map[shot.ticker] = (shot.bids[0]+shot.asks[0]) / 2; // mid_map saved the newest mid, no matter it is aligned or not
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()) {
Expand Down Expand Up @@ -560,7 +589,6 @@ void Strategy::HandleTestOrder(Order* o) {
DoOperationAfterFilled(o, info);
}


void Strategy::UpdateBuildPosTime() {
int hedge_pos = position_map[hedge_ticker];
if (hedge_pos == 0) { // closed all position, reinitialize build_position_time
Expand All @@ -576,6 +604,8 @@ void Strategy::DoOperationAfterFilled(Order* o, const ExchangeInfo& info) {
std::string a = o->tbd;
a.find("close") == string::npos ? open_count++ : close_count++;
Order* order = NewOrder(hedge_ticker, (o->side == OrderSide::Buy)?OrderSide::Sell : OrderSide::Buy, info.trade_size, false, false, "", no_close_today);
hedge_record = (o->side == OrderSide::Sell) ? -1:1;
hedge_last = shot_map[hedge_ticker];
order->Show(stdout);
HandleTestOrder(order);
} else if (strcmp(o->contract, hedge_ticker.c_str()) == 0) {
Expand Down
7 changes: 6 additions & 1 deletion src/simplearb/strategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
class Strategy : public BaseStrategy {
public:
explicit Strategy(const libconfig::Setting & param_setting, const libconfig::Setting & contract_setting, const TimeController& tc, std::unordered_map<std::string, std::vector<BaseStrategy*> >*ticker_strat_map, Contractor& ct, Sender* sender, const std::string & mode = "real", std::ofstream* order_file = nullptr, std::ofstream* exchange_file = nullptr, std::ofstream* strat_file = nullptr, bool no_close_today = false);
~Strategy();
virtual ~Strategy();

void Start() override;
void Stop() override;
Expand Down Expand Up @@ -121,6 +121,11 @@ class Strategy : public BaseStrategy {
bool no_close_today;
int open_count;
int close_count;
int main_record;
int hedge_record;
MarketSnapshot main_last;
MarketSnapshot hedge_last;
double total_slip_loss;
};

#endif // SRC_SIMPLEARB_STRATEGY_H_

0 comments on commit 6dcb677

Please sign in to comment.