Skip to content

Commit

Permalink
revise strategy init method
Browse files Browse the repository at this point in the history
  • Loading branch information
nickhuangxinyu committed Nov 22, 2019
1 parent e3e2e49 commit ceadc20
Show file tree
Hide file tree
Showing 22 changed files with 1,106 additions and 323 deletions.
801 changes: 801 additions & 0 deletions config/contract/bk_contract.config

Large diffs are not rendered by default.

284 changes: 169 additions & 115 deletions config/contract/contract.config

Large diffs are not rendered by default.

6 changes: 0 additions & 6 deletions config/prod/prod.config
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
time_controller :
{
sleep_time = [ "10:14:20-10:30:00", "11:29:20-13:00:00", "03:58:20-08:55:00" ];
close_time = [ "14:00:59-14:59:00", "23:00:00-23:59:59" ];
force_close_time = [ "16:59:59-17:30:00" ];
};
strategy = (
{
unique_name = "au";
Expand Down
6 changes: 6 additions & 0 deletions config/prod/time.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
time_controller :
{
sleep_time = [ "10:14:20-10:30:00", "11:29:20-13:00:00", "03:58:20-08:55:00" ];
close_time = [ "14:00:59-14:59:00", "23:00:00-23:59:59" ];
force_close_time = [ "16:59:59-17:30:00" ];
};
14 changes: 0 additions & 14 deletions external/common/include/Contractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,6 @@
#include "./common_tools.h"
#include "./timecontroller.h"

/*
struct ticker_volume {
std::string ticker;
int volume;
bool operator<(const struct ticker_volume & tv) const {
if (this->volume < tv.volume) {
return false;
}
return true;
}
};
*/

class Contractor {
public:
Contractor(std::string file_path);
Expand All @@ -52,7 +39,6 @@ class Contractor {
std::unordered_map<std::string, std::pair<std::string, int> > tick_pair;
std::unordered_set<std::string> tick_set;
std::unordered_set<std::string> ticker_set;
// std::unordered_map<std::string, std::ofstream> file_map;
TimeController tc;
bool is_ready;
};
Expand Down
8 changes: 5 additions & 3 deletions external/common/include/base_strategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "market_snapshot.h"
#include "order.h"
#include "sender.h"
#include "Dater.h"
#include "define.h"
#include "command.h"
#include "exchange_info.h"
Expand All @@ -12,6 +13,7 @@
#include "strategy_status.h"
#include "Contractor.h"
#include "timecontroller.h"
#include <libconfig.h++>
#include <unordered_map>

#include <cmath>
Expand Down Expand Up @@ -42,6 +44,7 @@ class BaseStrategy {
virtual void UpdateTicker();
virtual void HandleCommand(const Command& shot);
protected:
BaseStrategy(const std::string& contract_config_path);
void UpdateAvgCost(const std::string & ticker, double trade_price, int size);
std::string GenOrderRef();
Order* NewOrder(const std::string & ticker, OrderSide::Enum side, int size, bool control_price, bool sleep_order, const std::string & tbd, bool no_today = false);
Expand Down Expand Up @@ -72,9 +75,6 @@ class BaseStrategy {
pthread_mutex_t cancel_mutex;
pthread_mutex_t order_ref_mutex;
pthread_mutex_t mod_mutex;
std::ofstream* order_file;
std::ofstream* exchange_file;
std::ofstream* strat_file;
std::string m_strat_name;
TimeController* m_tc;
int ticker_size;
Expand All @@ -84,8 +84,10 @@ class BaseStrategy {
MarketSnapshot last_shot;
long int build_position_time;
int max_holding_sec;
Dater dt;
Contractor m_ct;
bool init_ticker;
libconfig::Config contract_config;
private:
virtual void DoOperationAfterCancelled(Order* o);
virtual void Run() = 0;
Expand Down
59 changes: 59 additions & 0 deletions external/common/include/bk_timecontroller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#ifndef TIMECONTROLLER_H_
#define TIMECONTROLLER_H_

#include "market_snapshot.h"
#include "order.h"
#include "sender.h"
#include "exchange_info.h"
#include "order_status.h"
#include "useful_function.h"
#include "time_status.h"
#include <unordered_map>
#include <time.h>
#include <sys/time.h>

#include <cmath>
#include <vector>
#include <iostream>
#include <string>

class TimeController {
public:
TimeController(std::vector<std::string>sleep_time, std::vector<std::string> close_time, std::vector<std::string> force_close, const std::string & mode="real");
TimeController();
TimeController(const TimeController & t);

~TimeController();

TimeStatus::Enum CheckCurrentTime(const MarketSnapshot& shot);

TimeStatus::Enum CheckTime(int check_time) const;

int GetTodaySec();
int GetSec(timeval t = {0,0}) const;
bool IsMix(int s1, int e1, int s2, int e2) const;
void StartTimer();
void EndTimer(const std::string & label="");
int GetStratSec(timeval t = {0, 0}) const;
std::string TimeToStr(timeval t = {0, 0}, bool show_time = false) const;

private:
int last_sec;
int timezone_diff;
std::vector<int>sleep_start;
std::vector<int>sleep_stop;
std::vector<int>close_start;
std::vector<int>close_stop;
std::vector<int>force_close_start;
std::vector<int>force_close_stop;
std::string mode;
long int timer_sec;
long int timer_usec;
bool is_timer_on;

int Translate(const std::string & time) const;
bool Check() const;
void Push(std::vector<std::string> timestr, std::vector<int>& a, std::vector<int>& b);
};

#endif // TIMECONTROLLER_H_
3 changes: 3 additions & 0 deletions external/common/include/common_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ static inline std::vector<std::string> Split(const std::string &str, const T &de

template<typename T>
void SaveBin(std::ofstream &stream, const T& t) {
if (!stream) {
return;
}
stream.write((char*)&t, sizeof(T));
stream.flush();
}
Expand Down
8 changes: 4 additions & 4 deletions external/common/include/sender.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "exchange_info.h"
#include "market_snapshot.h"
#include "pricer_data.h"
#include "common_tools.h"
#include "define.h"
#include "order.h"
#include "command.h"
Expand All @@ -16,23 +17,22 @@ using namespace std;

class Sender {
public:
explicit Sender(const std::string& name, const std::string & bs_mode = "bind", const std::string & mode = "ipc", std::ofstream* shot_recorder = nullptr, std::ofstream* order_recorder = nullptr, std::ofstream* info_recorder = nullptr);
explicit Sender(const std::string& name, const std::string & bs_mode = "bind", const std::string & mode = "ipc", std::string file_name = "");

~Sender();
inline void Bind(const std::string & address);

template <typename T>
inline void Send(const T & t) {
sock.get()->send(&t, sizeof(T));
SaveBin(*f.get(), t);
}

private:
unique_ptr<zmq::context_t> con;
unique_ptr<zmq::socket_t> sock;
pthread_mutex_t mutex;
unique_ptr<std::ofstream> shoter;
unique_ptr<std::ofstream> orderer;
unique_ptr<std::ofstream> infoer;
unique_ptr<std::ofstream> f;
};

#endif // SENDER_H_
19 changes: 10 additions & 9 deletions external/common/include/timecontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "time_status.h"
#include <unordered_map>
#include <time.h>
#include <libconfig.h++>
#include <sys/time.h>

#include <cmath>
Expand All @@ -19,23 +20,22 @@

class TimeController {
public:
TimeController(std::vector<std::string>sleep_time, std::vector<std::string> close_time, std::vector<std::string> force_close, const std::string & mode="real");
TimeController(string time_config);
TimeController();
TimeController(const TimeController & t);

~TimeController();

TimeStatus::Enum CheckCurrentTime(const MarketSnapshot& shot);
TimeStatus::Enum ShotStatus(const MarketSnapshot& shot);
TimeStatus::Enum CurrentStatus();
TimeStatus::Enum IntStatus(int check_time) const;

TimeStatus::Enum CheckTime(int check_time) const;
int TimevalInt(timeval t = {0,0}) const;
int CurrentInt() const;
std::string TimevalStr(timeval t = {0, 0}, bool show_time = false) const;
std::string IntStr(int sec) const;

int GetTodaySec();
int GetSec(timeval t = {0,0}) const;
bool IsMix(int s1, int e1, int s2, int e2) const;
void StartTimer();
void EndTimer(const std::string & label="");
int GetStratSec(timeval t = {0, 0}) const;
std::string TimeToStr(timeval t = {0, 0}, bool show_time = false) const;

private:
int last_sec;
Expand All @@ -51,6 +51,7 @@ class TimeController {
long int timer_usec;
bool is_timer_on;

bool IsMix(int s1, int e1, int s2, int e2) const;
int Translate(const std::string & time) const;
bool Check() const;
void Push(std::vector<std::string> timestr, std::vector<int>& a, std::vector<int>& b);
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.
25 changes: 5 additions & 20 deletions src/backtest/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,27 +130,12 @@ int main() {
libconfig::Config ticker_cfg;
std::string param_config_path = default_path + "/hft/config/backtest/backtest.config";
std::string ticker_config_path = default_path + "/hft/config/ticker/ticker.config";
std::string time_config_path = default_path + "/hft/config/prod/time.config";
param_cfg.readFile(param_config_path.c_str());
ticker_cfg.readFile(ticker_config_path.c_str());
try {
const libconfig::Setting &sleep_time = param_cfg.lookup("time_controller")["sleep_time"];
const libconfig::Setting &close_time = param_cfg.lookup("time_controller")["close_time"];
const libconfig::Setting &force_close_time = param_cfg.lookup("time_controller")["force_close_time"];
std::vector<std::string> sleep_time_v;
std::vector<std::string> close_time_v;
std::vector<std::string> force_close_time_v;
for (int i = 0; i < sleep_time.getLength(); i++) {
sleep_time_v.push_back(sleep_time[i]);
}
for (int i = 0; i < close_time.getLength(); i++) {
close_time_v.push_back(close_time[i]);
}
for (int i = 0; i < force_close_time.getLength(); i++) {
force_close_time_v.push_back(force_close_time[i]);
}
std::unordered_map<std::string, std::vector<BaseStrategy*> > ticker_strat_map;
TimeController tc(sleep_time_v, close_time_v, force_close_time_v, "test");
Recver data_recver("data_pub");
TimeController tc(time_config_path);

const libconfig::Setting & strategies = param_cfg.lookup("strategy");
const libconfig::Setting & ticker_setting_map = ticker_cfg.lookup("map");
Expand Down Expand Up @@ -237,7 +222,7 @@ int main() {
std::vector<std::string> file_v = dt.GetDataFilesNameByDate(start_date, period, true);
Contractor ct(dt.GetValidFile(start_date, -40));
PrintVector(ct.GetAllTick());
std::unique_ptr<Sender> sender(new Sender("*:33333", "bind", "tcp"));
std::unique_ptr<Sender> sender(new Sender("*:33333", "bind", "tcp", "mid_backtest.dat"));
std::vector<BaseStrategy*> sv;
// while (true) {
sv.clear();
Expand All @@ -252,12 +237,12 @@ int main() {
for (int j = 0; j < s_list.getLength(); j++) {
std::string con = s_list[j];
const libconfig::Setting & ticker_setting = ticker_setting_map[ticker_index_map[con]];
sv.emplace_back(new Strategy(param_setting, ticker_setting, tc, &ticker_strat_map, ct, sender.get(), "test", &order_file, &exchange_file, &strat_file, no_close_today));
sv.emplace_back(new Strategy(param_setting, ticker_setting, tc, &ticker_strat_map, ct, sender.get(), "test", no_close_today));
}
} else {
std::string con = param_setting["unique_name"];
const libconfig::Setting & ticker_setting = ticker_setting_map[ticker_index_map[con]];
sv.emplace_back(new Strategy(param_setting, ticker_setting, tc, &ticker_strat_map, ct, sender.get(), "test", &order_file, &exchange_file, &strat_file, no_close_today));
sv.emplace_back(new Strategy(param_setting, ticker_setting, tc, &ticker_strat_map, ct, sender.get(), "test", no_close_today));
}
}
tc.StartTimer();
Expand Down
6 changes: 3 additions & 3 deletions src/backtest/strategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,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);
explicit Strategy(const libconfig::Setting & param_setting, const libconfig::Setting & ticker_setting, const TimeController& tc, std::unordered_map<std::string, std::vector<BaseStrategy*> >*ticker_strat_map, Contractor& ct, Sender* sender, const std::string & mode = "real", bool no_close_today = false);
virtual ~Strategy();

void Start() override;
Expand All @@ -38,7 +38,7 @@ class Strategy : public BaseStrategy {
void DoOperationAfterUpdatePos(Order* o, const ExchangeInfo& info) override;
void DoOperationAfterFilled(Order* o, const ExchangeInfo& info) override;
void DoOperationAfterCancelled(Order* o) override;
void ModerateOrders(const std::string & contract) override;
void ModerateOrders(const std::string & ticker) override;
// void InitTicker();
// void InitTimer();
// void InitFile();
Expand All @@ -52,7 +52,7 @@ class Strategy : public BaseStrategy {

void UpdateBuildPosTime();

double OrderPrice(const std::string & contract, OrderSide::Enum side, bool control_price) override;
double OrderPrice(const std::string & ticker, OrderSide::Enum side, bool control_price) override;

OrderSide::Enum OpenLogicSide();
bool OpenLogic();
Expand Down
2 changes: 1 addition & 1 deletion src/demostrat/strategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void Strategy::Init() {
std::vector<string> sleep_time_v;
std::vector<string> close_time_v;
std::vector<string> force_close_time_v;
m_tc = new TimeController(sleep_time_v, close_time_v, force_close_time_v, "data");;
m_tc = new TimeController("/root/hft/config/prod/time.config");
}

bool Strategy::Ready() {
Expand Down
Loading

0 comments on commit ceadc20

Please sign in to comment.