Skip to content

Commit

Permalink
upload
Browse files Browse the repository at this point in the history
  • Loading branch information
nickhuangxinyu1 committed May 26, 2020
1 parent fff730d commit 5e99476
Show file tree
Hide file tree
Showing 24 changed files with 803 additions and 329 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ simplemaker:
simplearb:
$(WAF) configure simplearb $(PARAMS)

simplearb2:
$(WAF) configure simplearb2 $(PARAMS)

pairtrading:
$(WAF) configure pairtrading $(PARAMS)

Expand Down
28 changes: 18 additions & 10 deletions config/backtest/backtest.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
fixed_path="/home/nick/future";
backtest_outputdir = "backtest_pr_516";
backtest_outputdir = "backtest_ni10";
strategy = (
/*
{
unique_name = "IC";
ticker = "IC";
Expand All @@ -17,9 +18,11 @@ strategy = (
stop_loss_margin = 10.0;
max_loss_times = 2;
no_close_today = true;
}/*,
}*/
/*,
{
unique_name = "IH";
split_num = 6;
ticker = "IH";
max_position = 1;
max_round = 1000;
Expand All @@ -45,11 +48,12 @@ strategy = (
min_profit = 1.0;
add_margin = 3.0;
spread_threshold = 3.0;
split_num = 6;
max_holding_sec = 360000000;
stop_loss_margin = 10.0;
max_loss_times = 2;
no_close_today = true;
},
},*/

{
unique_name = "cu";
Expand All @@ -61,6 +65,7 @@ strategy = (
contract_size = 5;
train_samples= 600;
cancel_threshhold = 1000000000;
split_num = 6;
min_range = 4.0;
min_profit = 1.0;
add_margin = 1.0;
Expand All @@ -75,16 +80,17 @@ strategy = (
ticker = "ni";
max_position = 1;
max_round = 1000;
range_width = 2.0;
range_width = 1.0;
train_samples = 600;
min_range = 4.0;
min_range = 3.0;
split_num = 6;
min_profit = 1.0;
add_margin = 3.0;
spread_threshold = 3.0;
max_holding_sec = 3600000;
stop_loss_margin = 3.0;
stop_loss_margin = 300.0;
max_loss_times = 2;
},
}/*,

{
unique_name = "sn";
Expand All @@ -95,6 +101,7 @@ strategy = (
train_samples = 600;
min_range = 4.0;
min_profit = 1.0;
split_num = 6;
add_margin = 3.0;
spread_threshold = 3.0;
max_holding_sec = 3600000;
Expand All @@ -111,6 +118,7 @@ strategy = (
train_samples = 600;
min_range = 2.0;
min_profit = 1.0;
split_num = 6;
add_margin = 1.0;
spread_threshold = 3.0;
max_holding_sec = 3600000;
Expand All @@ -121,9 +129,9 @@ strategy = (

// start_date = "today";
// start_date = "yesterday";
test_mode = "nexttest";
start_date = "2019-01-11";
period = 1000;
test_mode = "test";
start_date = "2019-03-01";
period = 10;

order_file = "order_backtest.dat";
exchange_file = "exchange_backtest.dat";
Expand Down
6 changes: 3 additions & 3 deletions config/prod/prod.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ strategy = (
{
unique_name = "IC";
max_position = 1;
max_round = 4;
max_round = 4000;
range_width = 2.0;
train_samples = 1200;
min_range = 4.0;
Expand All @@ -14,7 +14,7 @@ strategy = (
max_loss_times = 2;
split_num = 6;
no_close_today = true;
}/*,
},

{
unique_name = "au";
Expand Down Expand Up @@ -114,7 +114,7 @@ strategy = (
stop_loss_margin = 3.0;
max_loss_times = 2;
split_num = 6;
}*/
}
);
order_file = "order.dat";
exchange_file = "exchange.dat";
Expand Down
4 changes: 2 additions & 2 deletions config/prod/time.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
time_controller :
{
sleep_time = ["09:30:00-09:45:59", "11:30:04-13:10:00"];
close_time = [ "14:20:00-14:50:00", "23:00:00-23:59:59", "11:15:00-11:29:00" ];
sleep_time = ["09:30:00-09:45:59", "11:30:04-13:10:00", "21:00:00-23:59:59", "00:00:00-03:30:00"];
close_time = [ "14:20:00-14:50:00", "11:15:00-11:29:00" ];
force_close_time = [ "14:50:01-14:59:59", "16:59:59-17:30:00", "11:29:01-11:29:59" ];
};
136 changes: 81 additions & 55 deletions external/common/include/core/base_strategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,26 @@

class BaseStrategy {
public:
BaseStrategy();

virtual ~BaseStrategy();

virtual void Start() = 0;
virtual void Stop() = 0;
// constructor and deconstructor
BaseStrategy() = default;
// forbid copy constructor and operator =
BaseStrategy(const BaseStrategy&) = delete;
BaseStrategy& operator=(const BaseStrategy&) = delete;
virtual ~BaseStrategy() {}

// interface function
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;
virtual void Init() = 0;

// utility function
void SendPlainText(const std::string & flag, const std::string & s);
void Print() const {}
virtual void HandleCommand(const Command& shot) {};

virtual void Clear();
void debug() const;
double GetMid(const std::string & ticker);
virtual void UpdateTicker();
virtual void HandleCommand(const Command& shot);
protected:
void UpdateAvgCost(const std::string & ticker, double trade_price, int size);
// order operation
std::string GenUniqueId();
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);
Order* PlaceOrder(const std::string & ticker, double price, int size, bool no_today = false, const std::string & orderinfo = "");
Expand All @@ -62,58 +61,85 @@ class BaseStrategy {
void ClearAll();
void Wakeup();
void Wakeup(Order* o);
void CheckStatus(const MarketSnapshot& shot);

void RequestQryPos();
// automatic control function
void CheckStatus(const MarketSnapshot& shot);
void UpdatePos(Order* o, const ExchangeInfo& info);
void UpdateAvgCost(const std::string & ticker, double trade_price, int size);
bool TimeUp() const;

void UpdatePos(Order* o, const ExchangeInfo& info);
virtual void SimulateTrade(Order* o);
// set mode
void SetStrategyMode(StrategyMode::Enum mode, std::ofstream* exchange_file);

bool position_ready;
BaseSender<Order>* order_sender;
BaseSender<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;
unordered_map<std::string, double> avgcost_map;
int ref_num;
std::mutex cancel_mutex;
std::mutex order_ref_mutex;
std::mutex mod_mutex;

// automatic generate simulate trade info
virtual void SimulateTrade(Order* o);

// utility function
double GetMid(const std::string & ticker);

// variable with default value
bool m_position_ready = false;
int m_ref_num = 0;
long int m_build_position_time = MAX_UNIX_TIME;
int m_max_holding_sec = 36000;
bool m_init_ticker = false;
StrategyMode::Enum m_mode = StrategyMode::Real;
std::ofstream* m_sim_exchange_file_ = nullptr;
StrategyStatus::Enum m_ss = StrategyStatus::Init;

// communication worker
BaseSender<Order>* m_order_sender;
BaseSender<MarketSnapshot>* m_ui_sender;
unordered_map<std::string, MarketSnapshot> m_shot_map;
unordered_map<std::string, MarketSnapshot> m_next_shot_map;
unordered_map<std::string, Order*> m_order_map;
unordered_map<std::string, Order*> m_sleep_order_map;
unordered_map<std::string, int> m_position_map;
unordered_map<std::string, double> m_avgcost_map;
std::unordered_map<std::string, int> m_cancel_map;

// mutex
std::mutex m_cancel_mutex;
std::mutex m_order_ref_mutex;
std::mutex m_mod_mutex;

// for gen unique id
std::string m_strat_name;
TimeController* m_tc;
int ticker_size;
std::unordered_map<std::string, int> cancel_map;
StrategyStatus::Enum ss;
std::unordered_map<std::string, bool> ticker_map;
MarketSnapshot last_shot;
long int build_position_time;
int max_holding_sec;
ContractWorker * m_cw;
HistoryWorker m_hw;
bool init_ticker;
StrategyMode::Enum mode_;
std::ofstream* sim_exchange_file_;
private:
virtual void DoOperationAfterCancelled(Order* o);
virtual void Run() = 0;
virtual void Resume();
virtual void Pause();
virtual void Train();
virtual void Flatting();
virtual void ForceFlat();
HistoryWorker* m_hw;

virtual bool Ready() = 0;
virtual void ModerateOrders(const std::string & ticker);
// not use now, save for future
int m_ticker_size;
MarketSnapshot m_last_shot;

// private virtual function = public virtual function
private:
virtual bool PriceChange(double current_price, double reasonable_price, OrderSide::Enum side, double edurance);
virtual void DoOperationAfterUpdatePos(Order* o, const ExchangeInfo& info);
virtual void DoOperationAfterUpdateData(const MarketSnapshot& shot);
virtual void DoOperationAfterFilled(Order* o, const ExchangeInfo& info);

// status control function
virtual void Start() {}
virtual void Stop() {}
virtual void Resume() {}
virtual void Pause() {}
virtual void Train() {}
virtual void Flatting() {}
virtual void ForceFlat() {}

// order control
virtual void ModerateOrders(const std::string & ticker) {}

// interface
virtual void DoOperationAfterUpdatePos(Order* o, const ExchangeInfo& info) {}
virtual void DoOperationAfterUpdateData(const MarketSnapshot& shot) {}
virtual void DoOperationAfterFilled(Order* o, const ExchangeInfo& info) {}
virtual void DoOperationAfterCancelled(Order* o) {}

// pure virtual function, must be implemented
virtual double OrderPrice(const std::string & ticker, OrderSide::Enum side, bool control_price) = 0;
virtual void Run() = 0;
virtual bool Ready() = 0;
};

#endif // BASE_STRATEGY_H_
Binary file modified external/common/lib/libnick.so
Binary file not shown.
2 changes: 1 addition & 1 deletion lib-hft
24 changes: 23 additions & 1 deletion rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ class ctporder_class(BuildContext):
class manual_ctp_class(BuildContext):
cmd = "manual_ctp"
class getins_class(BuildContext):
cmd = "get_ins"
cmd = "getins"
class simplemaker_class(BuildContext):
cmd = "simplemaker"
class simplearb_class(BuildContext):
cmd = "simplearb"
class simplearb2_class(BuildContext):
cmd = "simplearb2"
class pairtrading_class(BuildContext):
cmd = "pairtrading"
class backtest_class(BuildContext):
Expand Down Expand Up @@ -93,6 +95,9 @@ def build(bld):
if bld.cmd == "simplearb":
run_simplearb(bld)
return
if bld.cmd == "simplearb2":
run_simplearb2(bld)
return
if bld.cmd == "pairtrading":
run_pairtrading(bld)
return
Expand Down Expand Up @@ -213,6 +218,22 @@ def run_simplearb(bld):
use = 'zmq nick pthread config++ shm' # simplearb'
)

def run_simplearb2(bld):
#bld.read_shlib('nick', paths=['external/common/lib'])
bld.read_shlib('nick', paths=['external/common/lib'])
#bld.read_shlib('simplearb2', paths=['external/strategy/simplearb2/lib'])
bld.program(
target = 'bin/simplearb2',
source = ['src/simplearb2/main.cpp',
'src/simplearb2/strategy.cpp'
],
includes = [
#'external/strategy/simplearb/include',
'external/zeromq/include'
],
use = 'zmq nick pthread config++ shm' # simplearb'
)

def run_pairtrading(bld):
bld.read_shlib('nick', paths=['external/common/lib'])
bld.program(
Expand Down Expand Up @@ -291,6 +312,7 @@ def run_all(bld):
run_manual_ctp(bld)
run_getins(bld)
run_simplearb(bld)
run_simplearb2(bld)
run_pairtrading(bld)
run_backtest(bld)
run_backtestpr(bld)
Expand Down
2 changes: 1 addition & 1 deletion src/GetInstrument/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Listener : public CThostFtdcTraderSpi {

snprintf(request.BrokerID, sizeof(request.BrokerID), "%s", "9999");
snprintf(request.UserID, sizeof(request.UserID), "%s", "115686");
snprintf(request.Password, sizeof(request.Password), "%s", "fz567789");
snprintf(request.Password, sizeof(request.Password), "%s", "");

printf("Connected\n");

Expand Down
2 changes: 1 addition & 1 deletion src/backtest/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ std::unordered_map<std::string, std::vector<BaseStrategy*> > GetStratMap(std::st
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, date, mode, f);
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;
Expand Down
Loading

0 comments on commit 5e99476

Please sign in to comment.