Skip to content

Commit

Permalink
handlecommand added and revise recver sender
Browse files Browse the repository at this point in the history
  • Loading branch information
nickhuangxinyu committed Nov 21, 2019
1 parent 70d1d74 commit 32ed5dc
Show file tree
Hide file tree
Showing 35 changed files with 111 additions and 841 deletions.
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ ctporder:
getins:
$(WAF) configure getins $(PARAMS)

arbmaker:
$(WAF) configure arbmaker $(PARAMS)

simplemaker:
$(WAF) configure simplemaker $(PARAMS)

Expand Down
8 changes: 2 additions & 6 deletions external/common/include/base_strategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "order.h"
#include "sender.h"
#include "define.h"
#include "command.h"
#include "exchange_info.h"
#include "order_status.h"
#include "common_tools.h"
Expand Down Expand Up @@ -32,19 +33,14 @@ class BaseStrategy {
void RequestQryPos();
virtual void Print() const;
virtual void Init() = 0;
/*
virtual void InitTicker() = 0;
virtual void InitTimer() = 0;
virtual void InitFile() = 0;
*/
void SendPlainText(const std::string & flag, const std::string & s);

virtual void Clear();
void debug() const;
double GetMid(const std::string & ticker);
void UpdateCT(const Contractor& ct);
virtual void UpdateTicker();
virtual void HandleCommand(const MarketSnapshot& shot);
virtual void HandleCommand(const Command& shot);
protected:
void UpdateAvgCost(const std::string & ticker, double trade_price, int size);
std::string GenOrderRef();
Expand Down
46 changes: 18 additions & 28 deletions external/common/include/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,42 @@
#include <fstream>

struct Command {
timeval show_time;
timeval send_time;
CommandType type;
char contract[MAX_CONTRACT_LENGTH];
char order_ref[MAX_ORDERREF_SIZE];
OrderAction::Enum action;
OrderStatus::Enum status;
Offset::Enum offset;
CommandType::Enum type;
char ticker[MAX_CONTRACT_LENGTH];
int vint[8];
double vdouble[8];
char tbd[128];

Command()
: size(0),
traded_size(0),
offset(Offset::UNINITED) {
Command() {
snprintf(tbd, sizeof(tbd), "%s", "null");
}

bool Valid() const {
if (status == OrderStatus::SubmitNew || status == OrderStatus::New || status == OrderStatus::CancelRej) {
if (type != CommandType::Uninited) {
return true;
}
return false;
}

void Show(std::ofstream &stream) const {
stream.write((char*)this, sizeof(*this));
stream.flush();
}

void ShowCsv(FILE* stream) const {
char shot_time_s[32];
snprintf(shot_time_s, sizeof(shot_time_s), "%ld.%ld", shot_time.tv_sec, shot_time.tv_usec);
double shot_time_sec = atof(shot_time_s);
char send_time_s[32];
snprintf(send_time_s, sizeof(send_time_s), "%ld.%ld", send_time.tv_sec, send_time.tv_usec);
double send_time_sec = atof(send_time_s);
fprintf(stream, "%lf,%lf,%s,%lf,%d,%d,%s,%s,%s,%s,%s,%s\n",shot_time_sec,send_time_sec,contract,price,size,traded_size,OrderSide::ToString(side),order_ref,OrderAction::ToString(action),OrderStatus::ToString(status),Offset::ToString(offset),tbd);
void Show(FILE* stream, int depth=3) const {
timeval show_time;
gettimeofday(&show_time, NULL);
fprintf(stream, "%ld %04ld %ld %04ld Command %s |",
send_time.tv_sec, send_time.tv_usec, show_time.tv_sec, show_time.tv_usec, ticker);
for (int i = 0; i < depth; i++) {
fprintf(stream, "%d", vint[i]);
}
for (int i = 0; i < depth; i++) {
fprintf(stream, "%.2f", vdouble[i]);
}
}

void Show(FILE* stream) const {
// timeval show_time;
// gettimeofday(&show_time, NULL);
fprintf(stream, "%ld %04ld %ld %04ld Order %s |",
send_time.tv_sec, send_time.tv_usec, shot_time.tv_sec, shot_time.tv_usec, contract);

fprintf(stream, " %lf@%d %d %s %s %s %s %s %s\n", price, size, traded_size, OrderSide::ToString(side), order_ref, OrderAction::ToString(action), OrderStatus::ToString(status), Offset::ToString(offset), tbd);
}
};

#endif // COMMAND_H_
6 changes: 6 additions & 0 deletions external/common/include/common_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,10 @@ static inline std::vector<std::string> Split(const std::string &str, const T &de
return tokens;
}

template<typename T>
void SaveBin(std::ofstream &stream, const T& t) {
stream.write((char*)&t, sizeof(T));
stream.flush();
}

#endif // COMMON_TOOLS_H_
4 changes: 0 additions & 4 deletions external/common/include/exchange_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ struct ExchangeInfo {
trade_price(-1) {
}

void Show(std::ofstream &stream) const {
stream.write((char*)this, sizeof(*this));
}

void ShowCsv(FILE* stream) const {
/*
char time_s[32];
Expand Down
4 changes: 0 additions & 4 deletions external/common/include/market_snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ struct MarketSnapshot {
}
}

void Show(std::ofstream &stream, int depth = MARKET_DATA_DEPTH) const {
stream.write((char*)this, sizeof(*this));
}

void ShowCsv(FILE* stream, int depth = MARKET_DATA_DEPTH) const {
char time_s[32];
snprintf(time_s, sizeof(time_s), "%ld.%ld", time.tv_sec, time.tv_usec);
Expand Down
4 changes: 0 additions & 4 deletions external/common/include/order.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ struct Order {
}
return false;
}
void Show(std::ofstream &stream) const {
stream.write((char*)this, sizeof(*this));
stream.flush();
}

void ShowCsv(FILE* stream) const {
char shot_time_s[32];
Expand Down
16 changes: 16 additions & 0 deletions external/common/include/recver.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,29 @@ class Recver {

~Recver();


template <typename T>
void Recv(T& t) {
/*
if (BUFFER_SIZE < sizeof(T)) {
printf("buffer size is not enough!\n");
exit(1);
}
*/
void* buffer = nullptr;
sock.get()->recv(buffer, sizeof(T));
t = *reinterpret_cast<T*>(buffer); //alias-strict, use union to slove this
}

/*
MarketSnapshot Recv(const MarketSnapshot& shot);
Order Recv(const Order& order);
ExchangeInfo Recv(const ExchangeInfo& i);
PricerData Recv(const PricerData& p);
*/

private:
unique_ptr<zmq::context_t> con;
Expand Down
17 changes: 17 additions & 0 deletions external/common/include/sender.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "pricer_data.h"
#include "define.h"
#include "order.h"
#include "command.h"

using namespace std;

Expand All @@ -20,11 +21,27 @@ class Sender {
~Sender();
void Bind(const std::string & address);

template <typename T>
void Send(const T & t) {
char buffer[BUFFER_SIZE];
if (BUFFER_SIZE < sizeof(t)) {
printf("buffer size is not enough, 28\n");
exit(1);
}
memcpy(buffer, &t, sizeof(t));
pthread_mutex_lock(&mutex);
sock.get()->send(buffer, sizeof(buffer));
pthread_mutex_unlock(&mutex);
}

/*
void Send(const MarketSnapshot& shot);
void Send(const Order& order);
void Send(const ExchangeInfo& info);
void Send(const PricerData& p);
void Send(const Command& p);
void Send(const char* s);
*/

private:
unique_ptr<zmq::context_t> con;
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.
17 changes: 0 additions & 17 deletions rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ class ctporder_class(BuildContext):
cmd = "ctporder"
class getins_class(BuildContext):
cmd = "get_ins"
class arbmaker_class(BuildContext):
cmd = "arbmaker"
class simplemaker_class(BuildContext):
cmd = "simplemaker"
class simplearb_class(BuildContext):
Expand Down Expand Up @@ -109,9 +107,6 @@ def build(bld):
if bld.cmd == "getins":
run_getins(bld)
return
if bld.cmd == "arbmaker":
run_arbmaker(bld)
return
if bld.cmd == "simplemaker":
run_simplemaker(bld)
return
Expand Down Expand Up @@ -247,17 +242,6 @@ def run_getins(bld):
use = 'zmq commontools thosttraderapi pthread config++'
)

def run_arbmaker(bld):
bld.read_shlib('zmq', paths=['external/zeromq/lib'])
bld.read_shlib('commontools', paths=['external/common/lib'])
bld.program(
target = 'bin/arbmaker',
source = ['src/arbmaker/main.cpp',
'src/arbmaker/strategy.cpp'],
includes = ['external/zeromq/include'],
use = 'zmq commontools pthread config++'
)

def run_simplemaker(bld):
bld.read_shlib('commontools', paths=['external/common/lib'])
bld.program(
Expand Down Expand Up @@ -381,7 +365,6 @@ def run_all(bld):
run_ctpdata(bld)
run_ctporder(bld)
run_getins(bld)
#run_arbmaker(bld)
run_simplearb(bld)
run_newsimplearb(bld)
run_backtest(bld)
Expand Down
24 changes: 0 additions & 24 deletions src/arbmaker/arbmaker.pro

This file was deleted.

89 changes: 0 additions & 89 deletions src/arbmaker/main.cpp

This file was deleted.

Loading

0 comments on commit 32ed5dc

Please sign in to comment.