TimeStamps fixed
This commit is contained in:
1 parent
28e9c66675
commit
90bc20575a
5 files changed
+202
-57
No files matched your search
@@ -7,6 +7,7 @@
|
||||
#include "lib/utils/PBUtils.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include <google/protobuf/timestamp.pb.h>
|
||||
#include <google/protobuf/util/time_util.h>
|
||||
#include <sstream>
|
||||
#include <sys/time.h>
|
||||
|
||||
@@ -53,7 +54,7 @@ timeval SODPosition::sod_time() {
|
||||
// if the server started before the sod for today
|
||||
if (rv.tv_sec >= now_epoch)
|
||||
rv = subtract_days (rv,1);
|
||||
return rv;
|
||||
return rv;
|
||||
}
|
||||
|
||||
timeval SODPosition::get_order_cutoff_time(
|
||||
@@ -98,6 +99,41 @@ timeval SODPosition::now(){
|
||||
return tv;
|
||||
}
|
||||
|
||||
google::protobuf::Timestamp SODPosition::today_local_time_to_utc_timestamp (
|
||||
uint16_t hours,
|
||||
uint16_t mins,
|
||||
uint16_t secs,
|
||||
bool add_day_if_less,
|
||||
bool subtract_day_if_more){
|
||||
time_t now_epoch;
|
||||
time(&now_epoch); // seconds since epoch (utc) (independent of machine timezone)
|
||||
auto now_ts = google::protobuf::util::TimeUtil::TimeTToTimestamp(now_epoch);
|
||||
struct tm *ptm_local = localtime(&now_epoch); // in local timezone
|
||||
ptm_local->tm_hour = hours;
|
||||
ptm_local->tm_min = mins;
|
||||
ptm_local->tm_sec = secs;
|
||||
ptm_local->tm_isdst = -1;
|
||||
time_t ovrd_epoch = mktime(ptm_local); // converts local time to UTC epoch time
|
||||
if(ovrd_epoch < now_epoch){
|
||||
if (add_day_if_less)
|
||||
ovrd_epoch += 24*60*60;
|
||||
}
|
||||
else{
|
||||
if(subtract_day_if_more)
|
||||
ovrd_epoch -= 24*60*60;
|
||||
}
|
||||
auto ovrd_ts = google::protobuf::util::TimeUtil::TimeTToTimestamp(ovrd_epoch);
|
||||
return ovrd_ts;
|
||||
}
|
||||
|
||||
google::protobuf::Timestamp SODPosition::add_seconds (
|
||||
const google::protobuf::Timestamp& ts,
|
||||
int secs_diff){
|
||||
auto secs = google::protobuf::util::TimeUtil::TimestampToSeconds(ts);
|
||||
secs += secs_diff;
|
||||
return google::protobuf::util::TimeUtil::SecondsToTimestamp(secs);
|
||||
}
|
||||
|
||||
google::protobuf::Timestamp SODPosition::to_timestamp (timeval tv){
|
||||
google::protobuf::Timestamp rv;
|
||||
rv.set_seconds(tv.tv_sec);
|
||||
|
||||
@@ -31,6 +31,15 @@ public:
|
||||
const std::map<std::string,std::string>& ordermap);
|
||||
static timeval now();
|
||||
static google::protobuf::Timestamp to_timestamp (timeval);
|
||||
static google::protobuf::Timestamp today_local_time_to_utc_timestamp (
|
||||
uint16_t hours,
|
||||
uint16_t mins,
|
||||
uint16_t secs,
|
||||
bool add_day_if_less,
|
||||
bool subtract_day_if_more);
|
||||
static google::protobuf::Timestamp add_seconds (
|
||||
const google::protobuf::Timestamp& ts,
|
||||
int secs);
|
||||
bool is_sod (const google::protobuf::Timestamp& fill_time) const;
|
||||
};
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "DormanAlgo.h"
|
||||
#include "PyramidAlgo.h"
|
||||
#include "TASAlgo.h"
|
||||
#include "TASCounterAlgo.h"
|
||||
|
||||
using namespace bts;
|
||||
using namespace api::orderman;
|
||||
@@ -179,7 +180,7 @@ extern "C" void init_tascounter_plugin(bts::SparkServices& srv) {
|
||||
*join_desc.add_param_desc() = param_uint32("T1 Third Qty");
|
||||
*join_desc.add_param_desc() = param_bool("T1 Third Auto");
|
||||
|
||||
srv.register_dynamic_algo_order(join_desc, []() { return std::make_unique<orderman::PyramidAlgo>(); });
|
||||
srv.register_dynamic_algo_order(join_desc, []() { return std::make_unique<orderman::TASCounterAlgo>(); });
|
||||
}
|
||||
|
||||
extern "C" void init_tas_plugin(bts::SparkServices& srv) {
|
||||
|
||||
@@ -10,6 +10,9 @@
|
||||
#include "lib/domain_model/Security.h"
|
||||
#include "lib/utils/PBUtils.h"
|
||||
#include "lib/utils/datetime.h"
|
||||
#include <google/protobuf/timestamp.pb.h>
|
||||
#include "lib/orderman/actors/SODPosition.h"
|
||||
#include <google/protobuf/util/time_util.h>
|
||||
#include <stdint.h>
|
||||
|
||||
namespace bts {
|
||||
@@ -17,6 +20,7 @@ namespace orderman {
|
||||
|
||||
using namespace api::orderman;
|
||||
|
||||
|
||||
enum class TASCounterAlgo::Status { WORKING, DONE };
|
||||
|
||||
struct ContractMarketData{
|
||||
@@ -34,12 +38,15 @@ struct ContractMarketData{
|
||||
};
|
||||
|
||||
struct ContractAutoOrder{
|
||||
uint32_t n_secs;
|
||||
uint32_t qty;
|
||||
bool enabled;
|
||||
// inputs
|
||||
uint32_t n_secs = 0;
|
||||
uint32_t qty = 0;
|
||||
bool enabled = false;
|
||||
// dont know yet which side will be sent
|
||||
ChildTemplate* buy_ord;
|
||||
ChildTemplate* sell_ord;
|
||||
ChildTemplate* buy_ord = nullptr;
|
||||
ChildTemplate* sell_ord = nullptr;
|
||||
// output
|
||||
std::optional<OrderKey> tas_order = std::nullopt;
|
||||
};
|
||||
|
||||
struct AlgoData{
|
||||
@@ -62,18 +69,26 @@ struct TASCounterAlgo::PImpl {
|
||||
uint16_t reporting_period_msecs = 100;
|
||||
bool reporting_dirty = false;
|
||||
bts::datetime::PTime last_report_time = bts::datetime::now();
|
||||
// state
|
||||
// immutable state
|
||||
IAlgoServices* algo_services = nullptr;
|
||||
google::protobuf::Timestamp settle_start =
|
||||
SODPosition::today_local_time_to_utc_timestamp(13,30,00,true,false);
|
||||
google::protobuf::Timestamp settle_stop =
|
||||
SODPosition::add_seconds(settle_start,60);
|
||||
// mutable state
|
||||
AlgoData data;
|
||||
std::map<SecurityKey,ContractMarketData*> md_map;
|
||||
// state machine
|
||||
Status state = Status::WORKING;
|
||||
};
|
||||
|
||||
bool _setup_data(IBeforeLaunchAlgoServices&,
|
||||
AlgoData&,
|
||||
bool _setup_data(IBeforeLaunchAlgoServices&, AlgoData&,
|
||||
std::map<SecurityKey,ContractMarketData*>&);
|
||||
|
||||
bool within_settle_win (const google::protobuf::Timestamp&,
|
||||
const google::protobuf::Timestamp&,
|
||||
const TickerUpdateEvent::Trade& t);
|
||||
|
||||
|
||||
TASCounterAlgo::TASCounterAlgo() : me(new PImpl()) {}
|
||||
TASCounterAlgo::~TASCounterAlgo() {}
|
||||
// initialization /////////////////////////////////////////////////////////////
|
||||
@@ -92,7 +107,7 @@ void TASCounterAlgo::do_launch(IAlgoServices& algo_services) {
|
||||
for (const auto& kp : me->md_map){
|
||||
algo_services.subscribe_book(kp.first);
|
||||
algo_services.subscribe_ticker(kp.first);
|
||||
}
|
||||
}
|
||||
}
|
||||
void TASCounterAlgo::do_launch_attached(
|
||||
IAlgoServices& algo_services,
|
||||
@@ -103,34 +118,38 @@ void TASCounterAlgo::do_launch_attached(
|
||||
// updates ////////////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
void TASCounterAlgo::do_update(const api::orderman::Update&) {
|
||||
// the only update allowed is the auto execution
|
||||
if(me->state == Status::DONE)
|
||||
return;
|
||||
// copy
|
||||
// the only update allowed is the auto execution
|
||||
_check_and_send_order();
|
||||
}
|
||||
// callbacks //////////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
void TASCounterAlgo::on_book(SecurityKey k, const Book& b) {
|
||||
if(me->state == Status::DONE)
|
||||
return;
|
||||
const auto& it = me->md_map.find(k);
|
||||
if(it != me->md_map.end()){
|
||||
me->reporting_dirty = true;
|
||||
it->second->book = b;
|
||||
_check_and_send_order();
|
||||
}
|
||||
}
|
||||
void TASCounterAlgo::on_ticker(SecurityKey k, const TradeVector& v){
|
||||
const auto& it = me->md_map.find(k);
|
||||
auto contract_md = it->second;
|
||||
if(me->state == Status::DONE)
|
||||
return;
|
||||
const auto& it = me->md_map.find(k);
|
||||
if(it != me->md_map.end()){
|
||||
me->reporting_dirty = true;
|
||||
for (const auto& t : v){
|
||||
// TODO (if in settle period)
|
||||
contract_md->on_trade(t);
|
||||
t.ts.time_since_epoch();
|
||||
}
|
||||
auto contract_md = it->second;
|
||||
for (const auto& t : v)
|
||||
if (within_settle_win(me->settle_start,me->settle_stop,t)){
|
||||
contract_md->on_trade(t);
|
||||
me->reporting_dirty = true;
|
||||
}
|
||||
_check_and_send_order();
|
||||
}
|
||||
}
|
||||
void TASCounterAlgo::on_child_change(OrderKey,
|
||||
const ChildState&){
|
||||
}
|
||||
void TASCounterAlgo::on_status(SecurityKey, co2::TradingStatus){
|
||||
}
|
||||
|
||||
// this only gets called on am explicit algo cancel
|
||||
// from the outside
|
||||
@@ -139,6 +158,9 @@ TASCounterAlgo::OnCancelResponse TASCounterAlgo::on_cancel(){
|
||||
}
|
||||
|
||||
void TASCounterAlgo::on_timer(){
|
||||
if(me->state == Status::DONE)
|
||||
return;
|
||||
_check_and_send_order();
|
||||
if(me->reporting_dirty &&
|
||||
bts::datetime::msecs_since(me->last_report_time) >=
|
||||
me->reporting_period_msecs){
|
||||
@@ -149,6 +171,55 @@ void TASCounterAlgo::on_timer(){
|
||||
}
|
||||
|
||||
IAlgoServices& TASCounterAlgo::_algo() const { return *me->algo_services; }
|
||||
|
||||
void TASCounterAlgo::_switch_state(TASCounterAlgo::Status new_state){
|
||||
switch (new_state){
|
||||
case (TASCounterAlgo::Status::WORKING):
|
||||
bts_assert(false);
|
||||
break;
|
||||
case (TASCounterAlgo::Status::DONE):
|
||||
bts_assert(me->state != TASCounterAlgo::Status::WORKING);
|
||||
break;
|
||||
}
|
||||
LOG(OrderMan, Info,"TASCounterAlgo::_switch_state key:{} {}->{} ",
|
||||
_algo().order_key(),
|
||||
static_cast<int>(me->state),
|
||||
static_cast<int>(new_state));
|
||||
me->state = new_state;
|
||||
}
|
||||
|
||||
void TASCounterAlgo::_cxl_and_end (bool is_error, const std::string& err_msg){
|
||||
if(me->state == Status::DONE)
|
||||
return;
|
||||
if(is_error)
|
||||
_algo().terminate_error(err_msg);
|
||||
else
|
||||
_algo().complete();
|
||||
_switch_state(TASCounterAlgo::Status::DONE);
|
||||
}
|
||||
|
||||
// the decision to send an order can be caused by
|
||||
// 1. change in settings
|
||||
// 2. book update (for the TAS contracts)
|
||||
// 3. ticker
|
||||
// 4. time passing
|
||||
void TASCounterAlgo::_check_and_send_order(){
|
||||
auto _send_order_tn = [&](
|
||||
ContractMarketData&,
|
||||
ContractAutoOrder& o,
|
||||
ContractMarketData&) -> void {
|
||||
if(o.enabled && !o.tas_order.has_value() && o.n_secs > 0 && o.qty > 0){
|
||||
//check time condition and price condition
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
_send_order_tn(me->data.t1_front,me->data.t1_front_order,me->data.t1_front_tas);
|
||||
|
||||
// t1
|
||||
// t2
|
||||
// t3
|
||||
}
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// stateless conversion ///////////////////////////////////////////////////////
|
||||
|
||||
@@ -184,56 +255,84 @@ void _fill_auto_execute (
|
||||
// coverts the input params, returns true if
|
||||
// conversion is sucessful
|
||||
bool _setup_data(IBeforeLaunchAlgoServices& serv,
|
||||
AlgoData& d,
|
||||
std::map<SecurityKey,ContractMarketData*>& md_map){
|
||||
AlgoData& out_d,
|
||||
std::map<SecurityKey,ContractMarketData*>& out_md_map){
|
||||
auto& def = serv.definition();
|
||||
// [ captures ] (parameters) -> returnTypesDeclaration { lambdaStatements; }
|
||||
auto init_contract_bk = [&md_map](auto& out_contract_md,
|
||||
auto init_contract_bk = [&out_md_map](auto& out_contract_md,
|
||||
const auto& params,
|
||||
auto param_name) -> void {
|
||||
auto k = get_if<uint32_t>(params, param_name);
|
||||
if(k.has_value()){
|
||||
out_contract_md.book = Book(SecurityKey(k.value()));
|
||||
md_map[out_contract_md.book.security_key()] = &out_contract_md;
|
||||
out_md_map[out_contract_md.book.security_key()] = &out_contract_md;
|
||||
}
|
||||
};
|
||||
};
|
||||
if (def.has_custom_def() && def.custom_def().has_dynamic()){
|
||||
const auto& p = def.custom_def().dynamic().order_params();
|
||||
// main contracts
|
||||
init_contract_bk(d.t1_front, p,"T1 Front");
|
||||
init_contract_bk(d.t1_second, p,"T1 Second");
|
||||
init_contract_bk(d.t1_third, p,"T1 Third");
|
||||
init_contract_bk(d.t2_front, p,"T2 Front");
|
||||
init_contract_bk(d.t2_second, p,"T2 Second");
|
||||
init_contract_bk(d.t3_front, p,"T3 Front");
|
||||
init_contract_bk(out_d.t1_front, p,"T1 Front");
|
||||
init_contract_bk(out_d.t1_second, p,"T1 Second");
|
||||
init_contract_bk(out_d.t1_third, p,"T1 Third");
|
||||
init_contract_bk(out_d.t2_front, p,"T2 Front");
|
||||
init_contract_bk(out_d.t2_second, p,"T2 Second");
|
||||
init_contract_bk(out_d.t3_front, p,"T3 Front");
|
||||
// TAS contracts
|
||||
init_contract_bk(d.t1_front_tas, p,"T1 Front TAS");
|
||||
init_contract_bk(d.t1_second_tas, p,"T1 Second TAS");
|
||||
init_contract_bk(d.t1_third_tas, p,"T1 Third TAS");
|
||||
init_contract_bk(out_d.t1_front_tas, p,"T1 Front TAS");
|
||||
init_contract_bk(out_d.t1_second_tas, p,"T1 Second TAS");
|
||||
init_contract_bk(out_d.t1_third_tas, p,"T1 Third TAS");
|
||||
// auto orders
|
||||
_fill_auto_execute(d.t1_front_order,p,"T1 Front Time","T1 Front Qty",
|
||||
_fill_auto_execute(out_d.t1_front_order,p,"T1 Front Time","T1 Front Qty",
|
||||
"T1 Front Auto Execute");
|
||||
_fill_auto_execute(d.t1_front_order,p,"T1 Second Time","T1 Second Qty",
|
||||
_fill_auto_execute(out_d.t1_front_order,p,"T1 Second Time","T1 Second Qty",
|
||||
"T1 Second Auto Execute");
|
||||
_fill_auto_execute(d.t1_front_order,p,"T1 Third Time","T1 Third Qty",
|
||||
_fill_auto_execute(out_d.t1_front_order,p,"T1 Third Time","T1 Third Qty",
|
||||
"T1 Third Auto Execute");
|
||||
// order template
|
||||
d.t1_front_order.buy_ord = _create_order_template(serv,
|
||||
d.t1_front.book.security_key(),api::Side::SIDE_BUY);
|
||||
d.t1_front_order.sell_ord = _create_order_template(serv,
|
||||
d.t1_front.book.security_key(),api::Side::SIDE_SELL);
|
||||
d.t1_second_order.buy_ord = _create_order_template(serv,
|
||||
d.t1_second.book.security_key(),api::Side::SIDE_BUY);
|
||||
d.t1_second_order.sell_ord = _create_order_template(serv,
|
||||
d.t1_second.book.security_key(),api::Side::SIDE_SELL);
|
||||
d.t1_third_order.buy_ord = _create_order_template(serv,
|
||||
d.t1_third.book.security_key(),api::Side::SIDE_BUY);
|
||||
d.t1_third_order.sell_ord = _create_order_template(serv,
|
||||
d.t1_third.book.security_key(),api::Side::SIDE_SELL);
|
||||
out_d.t1_front_order.buy_ord = _create_order_template(serv,
|
||||
out_d.t1_front.book.security_key(),api::Side::SIDE_BUY);
|
||||
out_d.t1_front_order.sell_ord = _create_order_template(serv,
|
||||
out_d.t1_front.book.security_key(),api::Side::SIDE_SELL);
|
||||
out_d.t1_second_order.buy_ord = _create_order_template(serv,
|
||||
out_d.t1_second.book.security_key(),api::Side::SIDE_BUY);
|
||||
out_d.t1_second_order.sell_ord = _create_order_template(serv,
|
||||
out_d.t1_second.book.security_key(),api::Side::SIDE_SELL);
|
||||
out_d.t1_third_order.buy_ord = _create_order_template(serv,
|
||||
out_d.t1_third.book.security_key(),api::Side::SIDE_BUY);
|
||||
out_d.t1_third_order.sell_ord = _create_order_template(serv,
|
||||
out_d.t1_third.book.security_key(),api::Side::SIDE_SELL);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// converts std::chrono::system_clock::time_point to google timestamp
|
||||
google::protobuf::Timestamp to_ts (const bts::TimeStamp& ts){
|
||||
auto ts_us = std::chrono::time_point_cast<std::chrono::microseconds>(ts);
|
||||
auto ts_us_val = ts_us.time_since_epoch();
|
||||
auto usecs = ts_us_val.count();
|
||||
return google::protobuf::util::TimeUtil::MillisecondsToTimestamp(usecs);
|
||||
}
|
||||
|
||||
long t1_minus_t2_usecs (const google::protobuf::Timestamp& t1,
|
||||
const google::protobuf::Timestamp& t2){
|
||||
return
|
||||
google::protobuf::util::TimeUtil::TimestampToMicroseconds(t1) -
|
||||
google::protobuf::util::TimeUtil::TimestampToMicroseconds(t2);
|
||||
}
|
||||
|
||||
bool within_settle_win (const google::protobuf::Timestamp& beg,
|
||||
const google::protobuf::Timestamp& end,
|
||||
const google::protobuf::Timestamp& ts){
|
||||
return t1_minus_t2_usecs(ts,beg)>=0 &&
|
||||
t1_minus_t2_usecs(ts,end)<=0;
|
||||
}
|
||||
|
||||
bool within_settle_win (const google::protobuf::Timestamp& beg,
|
||||
const google::protobuf::Timestamp& end,
|
||||
const TickerUpdateEvent::Trade& t){
|
||||
auto ts = to_ts(t.ts);
|
||||
return within_settle_win(beg,end,ts);
|
||||
}
|
||||
|
||||
} // namespace orderman
|
||||
|
||||
|
||||
@@ -20,8 +20,6 @@ public:
|
||||
void do_update(api::orderman::Update const &) override;
|
||||
void on_book(SecurityKey, Book const &) override;
|
||||
void on_ticker(SecurityKey, TradeVector const&) override;
|
||||
void on_child_change(OrderKey, ChildState const &) override;
|
||||
void on_status(SecurityKey, co2::TradingStatus) override;
|
||||
IAlgoOrder::OnCancelResponse on_cancel() override;
|
||||
void on_timer() override;
|
||||
private:
|
||||
@@ -36,6 +34,8 @@ private:
|
||||
static TASCounterAlgoParams _convert_params (
|
||||
const bts::api::orderman::CustomOrderDefinition&);
|
||||
void _switch_state (Status new_status);
|
||||
void _cxl_and_end (bool is_error, const std::string& err_msg);
|
||||
void _check_and_send_order();
|
||||
};
|
||||
|
||||
} // namespace orderman
|
||||
|
||||
Reference in new issue
Block a user