added early entry
This commit is contained in:
1 parent
27c410c485
commit
afefaa36d1
3 files changed
+120
-31
No files matched your search
@@ -303,21 +303,36 @@ struct Brick {
|
||||
double brick_size, BrickEdges edges, BrickMoveDirection prior_move)
|
||||
: brick_size(brick_size), edges(edges), last_move_direction(prior_move) {
|
||||
}
|
||||
std::optional<BrickEdges> check_bounds(double price) {
|
||||
BrickEdges up_brick = get_up_edge();
|
||||
BrickEdges down_brick = get_down_edge();
|
||||
// A new brick will be created when the print price >= 10002 (ie price >=
|
||||
// up_brick.high), and a down brick will be created when the print price
|
||||
// <= 9999 (price <= down_brick.low)
|
||||
if (price >= up_brick.high)
|
||||
return up_brick;
|
||||
if (price <= down_brick.low)
|
||||
return down_brick;
|
||||
return std::nullopt;
|
||||
}
|
||||
// creates new brick
|
||||
std::optional<BrickMoveDirection> new_brick(BrickMoveDirection direction,
|
||||
BrickEdges new_edges) {
|
||||
|
||||
edges = new_edges;
|
||||
last_move_direction = direction;
|
||||
brick_id++;
|
||||
n_values = 0;
|
||||
return last_move_direction;
|
||||
return direction;
|
||||
}
|
||||
const BrickEdges get_up_edge() { return edges + brick_size; }
|
||||
const BrickEdges get_down_edge() { return edges - brick_size; }
|
||||
std::optional<BrickMoveDirection> on_price(double price) {
|
||||
// This is the logic for creating new bricks.
|
||||
// lets suppose the current brick = {low = 10000, high = 10001}, then
|
||||
// up_brick = {10001, 10002} and down_brick = {9999, 10000}
|
||||
BrickEdges up_brick = edges + brick_size;
|
||||
BrickEdges down_brick = edges - brick_size;
|
||||
BrickEdges up_brick = get_up_edge();
|
||||
BrickEdges down_brick = get_down_edge();
|
||||
// A new brick will be created when the print price >= 10002 (ie price >=
|
||||
// up_brick.high), and a down brick will be created when the print price
|
||||
// <= 9999 (price <= down_brick.low)
|
||||
@@ -325,7 +340,6 @@ struct Brick {
|
||||
return new_brick(BrickMoveDirection::UP, up_brick);
|
||||
if (price <= down_brick.low)
|
||||
return new_brick(BrickMoveDirection::DOWN, down_brick);
|
||||
|
||||
n_values++;
|
||||
return std::nullopt;
|
||||
}
|
||||
@@ -418,6 +432,25 @@ struct GanData {
|
||||
high_sum += brick.edges.high;
|
||||
low_sum += brick.edges.low;
|
||||
}
|
||||
// for early entry exit
|
||||
[[nodiscard]] std::optional<double>
|
||||
high_average_hyp(std::optional<BrickEdges> temp) const {
|
||||
if (!has_data() || !temp.has_value()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
auto h_sum = std::accumulate(highs.begin(), highs.end(), (double)0);
|
||||
h_sum = h_sum + temp.value().high - highs.front();
|
||||
return h_sum / (double)highs.size();
|
||||
}
|
||||
[[nodiscard]] std::optional<double>
|
||||
low_average_hyp(std::optional<BrickEdges> temp) const {
|
||||
if (!has_data() || !temp.has_value()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
auto l_sum = std::accumulate(lows.begin(), lows.end(), (double)0);
|
||||
l_sum = l_sum + temp.value().low - lows.front();
|
||||
return l_sum / (double)lows.size();
|
||||
}
|
||||
// high_average (and subsequenly low_average) below, is simply adding up the
|
||||
// contents of the highs (or lows) and dividing it by the number of elements
|
||||
[[nodiscard]] std::optional<double> high_average() const {
|
||||
@@ -455,10 +488,29 @@ struct MomentumAlgoParams {
|
||||
const size_t edge_ticks_loss_limit;
|
||||
const size_t gan_buffer_sz;
|
||||
const size_t orders_quantity;
|
||||
const double early_threshold;
|
||||
const double cash_per_point;
|
||||
const TimeHandler start_time, end_time;
|
||||
const bool log_graph, file_read, fixed_edge_gain, fixed_edge_loss,
|
||||
reverse_logic, timed_start;
|
||||
reverse_logic, timed_start, early_brick;
|
||||
MomentumAlgoParams(SecurityKey security_key_id, size_t renko_ticks,
|
||||
size_t edge_ticks_gain_limit,
|
||||
size_t edge_ticks_loss_limit, size_t gan_buffer_sz,
|
||||
size_t orders_quantity, double cash_per_point,
|
||||
TimeHandler start_time, TimeHandler end_time,
|
||||
std::string security_name_str, bool file_read,
|
||||
bool fixed_edge_gain, bool fixed_edge_loss,
|
||||
bool reverse_logic, bool timed_start, bool log_graph,
|
||||
bool early_brick, double early_threshold)
|
||||
: security_key_id(security_key_id), security_name_str(security_name_str),
|
||||
renko_ticks(renko_ticks), edge_ticks_gain_limit(edge_ticks_gain_limit),
|
||||
edge_ticks_loss_limit(edge_ticks_loss_limit),
|
||||
gan_buffer_sz(gan_buffer_sz), orders_quantity(orders_quantity),
|
||||
early_threshold(early_threshold), cash_per_point(cash_per_point),
|
||||
start_time(start_time), end_time(end_time), log_graph(log_graph),
|
||||
file_read(file_read), fixed_edge_gain(fixed_edge_gain),
|
||||
fixed_edge_loss(fixed_edge_loss), reverse_logic(reverse_logic),
|
||||
timed_start(timed_start), early_brick(early_brick) {}
|
||||
std::string print() {
|
||||
std::string sep_obj = ", ";
|
||||
std::string sep_key_val = ":";
|
||||
@@ -487,22 +539,6 @@ struct MomentumAlgoParams {
|
||||
s += sep_obj + "timed_start" + sep_key_val + std::to_string(timed_start);
|
||||
return s;
|
||||
}
|
||||
MomentumAlgoParams(SecurityKey security_key_id, size_t renko_ticks,
|
||||
size_t edge_ticks_gain_limit,
|
||||
size_t edge_ticks_loss_limit, size_t gan_buffer_sz,
|
||||
size_t orders_quantity, double cash_per_point,
|
||||
TimeHandler start_time, TimeHandler end_time,
|
||||
std::string security_name_str, bool file_read,
|
||||
bool fixed_edge_gain, bool fixed_edge_loss,
|
||||
bool reverse_logic, bool timed_start, bool log_graph)
|
||||
: security_key_id(security_key_id), security_name_str(security_name_str),
|
||||
renko_ticks(renko_ticks), edge_ticks_gain_limit(edge_ticks_gain_limit),
|
||||
edge_ticks_loss_limit(edge_ticks_loss_limit),
|
||||
gan_buffer_sz(gan_buffer_sz), orders_quantity(orders_quantity),
|
||||
cash_per_point(cash_per_point), start_time(start_time),
|
||||
end_time(end_time), log_graph(log_graph), file_read(file_read),
|
||||
fixed_edge_gain(fixed_edge_gain), fixed_edge_loss(fixed_edge_loss),
|
||||
reverse_logic(reverse_logic), timed_start(timed_start) {}
|
||||
};
|
||||
enum Signal { BUY = -1, FLAT = 0, SELL = 1 };
|
||||
struct OrderDetails {
|
||||
@@ -533,6 +569,7 @@ struct MomentumAlgo::PImpl {
|
||||
std::optional<double> last_order_price;
|
||||
std::optional<int> pos_signals = 0;
|
||||
std::optional<int> neg_signals = 0;
|
||||
std::optional<int> front_runners = 0;
|
||||
int actual_pos = 0;
|
||||
bool first_signal = true;
|
||||
size_t packet_no = 0, book_no = 0;
|
||||
@@ -554,7 +591,8 @@ struct MomentumAlgo::PImpl {
|
||||
auto l = {"00 Last Price", "01 Position", "02 P&L",
|
||||
"03 Brick count", "10 Holding Price", "20 Last Order Price",
|
||||
"30 Lower Brick", "31 Upper Brick", "40 Lower Average",
|
||||
"41 Upper Average", "50 + Signals", "51 - Signals"};
|
||||
"41 Upper Average", "50 + Signals", "51 - Signals",
|
||||
"60 Front Runners"};
|
||||
|
||||
for (auto index : l) {
|
||||
m[index].set_as_string("None");
|
||||
@@ -589,6 +627,8 @@ bool _convert_params(IBeforeLaunchAlgoServices &serv,
|
||||
auto file_sim = get_if<bool>(params, "File Sim?");
|
||||
auto timed_start = get_if<bool>(params, "Timed Start?");
|
||||
auto log_graph = get_if<bool>(params, "Log Graph?");
|
||||
auto early_brick = get_if<bool>(params, "Early Brick?");
|
||||
|
||||
auto renko_ticks = get_if<uint32_t>(params, "Renko Ticks");
|
||||
auto gan_buffer_sz = get_if<uint32_t>(params, "Gan Buffer Size");
|
||||
auto open_position_on_close = get_if<bool>(params, "Open Position On Close");
|
||||
@@ -596,6 +636,7 @@ bool _convert_params(IBeforeLaunchAlgoServices &serv,
|
||||
auto edge_ticks_loss_limit = get_if<uint32_t>(params, "Edge To lose");
|
||||
|
||||
auto run_duration = get_if<double>(params, "Run Duration (Hours-decimal)");
|
||||
auto early_threshold = get_if<double>(params, "Early Threshold");
|
||||
|
||||
auto st_hr = get_if<uint32_t>(params, "Time Start Hours (24 HRS)");
|
||||
auto st_min = get_if<uint32_t>(params, "Time Start Minutes");
|
||||
@@ -651,7 +692,9 @@ bool _convert_params(IBeforeLaunchAlgoServices &serv,
|
||||
serv.security().contract_spec().cash_per_point(), start_time, end_time,
|
||||
serv.security().glbx().security_desc(), file_sim.value(),
|
||||
edge_target.value(), edge_loss.value(), reverse_logic.value(),
|
||||
timed_start.value(), log_graph.has_value() ? log_graph.value() : true));
|
||||
timed_start.value(), log_graph.has_value() ? log_graph.value() : true,
|
||||
early_brick.has_value() ? early_brick.value() : false,
|
||||
early_threshold.has_value() ? early_threshold.value() : 0));
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -786,12 +829,51 @@ void MomentumAlgo::_handle_fixed_edge() {
|
||||
}
|
||||
}
|
||||
}
|
||||
void MomentumAlgo::_early_brick() {
|
||||
auto new_high_price =
|
||||
me->curr_price.value() + me->algo_params->early_threshold;
|
||||
auto new_low_price =
|
||||
me->curr_price.value() - me->algo_params->early_threshold;
|
||||
auto new_high_brick = me->renko->current_brick->check_bounds(new_high_price);
|
||||
auto new_low_brick = me->renko->current_brick->check_bounds(new_low_price);
|
||||
auto new_high_avg = me->gandata->high_average_hyp(new_high_brick);
|
||||
auto new_low_avg = me->gandata->low_average_hyp(new_low_brick);
|
||||
bool sell_signal = new_low_avg.has_value()
|
||||
? new_low_brick.value().low >= new_low_price
|
||||
: false;
|
||||
bool buy_signal = new_high_avg.has_value()
|
||||
? new_high_brick.value().high >= new_high_price
|
||||
: false;
|
||||
if (buy_signal || sell_signal) {
|
||||
double n_p = me->curr_price.value();
|
||||
if (me->curr_pos == 0) {
|
||||
if (sell_signal && me->prev_signal != Signal::SELL) {
|
||||
n_p = new_low_price;
|
||||
} else if (buy_signal && me->prev_signal != Signal::BUY) {
|
||||
n_p = new_high_price;
|
||||
}
|
||||
// close position only enters if mode is siganl to signal and sell/buy
|
||||
// signal is true
|
||||
} else if (me->curr_pos > 0 && sell_signal) {
|
||||
n_p = new_low_price;
|
||||
} else if (me->curr_pos < 0 && buy_signal) {
|
||||
n_p = new_high_price;
|
||||
}
|
||||
|
||||
if (n_p != me->curr_price.value() && !me->first_signal) {
|
||||
me->front_runners = me->front_runners.value() + 1;
|
||||
_on_data(n_p);
|
||||
}
|
||||
}
|
||||
}
|
||||
// sends new brick to gan data, and sends data to analyse trade if enough
|
||||
// data gathered
|
||||
void MomentumAlgo::_on_brick(Brick &brick, bool is_new_brick) {
|
||||
if (is_new_brick) {
|
||||
me->gandata->on_data(brick);
|
||||
_analyze_trade();
|
||||
} else if (me->algo_params->early_brick) {
|
||||
_early_brick();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -819,7 +901,8 @@ void MomentumAlgo::_analyze_trade() {
|
||||
if (!me->gandata->has_data() || !is_time()) {
|
||||
if (me->algo_params->log_graph)
|
||||
LOG(OrderMan, Info,
|
||||
"MomentumAlgo::{}::type:New_brick, brick_id:{}, brick_lower:{}, "
|
||||
"MomentumAlgo::{}::type:New_brick, brick_id:{}, "
|
||||
"brick_lower:{}, "
|
||||
"brick_higher:{}, avg_low:{}, avg_high:{}, order_id:{}, "
|
||||
"book_buy:{}, "
|
||||
"book_sell:{}, c_time:{}, s_time:{}, e_time:{}",
|
||||
@@ -845,7 +928,8 @@ void MomentumAlgo::_analyze_trade() {
|
||||
if (me->algo_params->log_graph)
|
||||
LOG(OrderMan, Info,
|
||||
"MomentumAlgo::{}::type:New_brick, brick_id:{}, brick_lower:{}, "
|
||||
"brick_higher:{}, avg_low:{}, avg_high:{}, order_id:{}, book_buy:{}, "
|
||||
"brick_higher:{}, avg_low:{}, avg_high:{}, order_id:{}, "
|
||||
"book_buy:{}, "
|
||||
"book_sell:{}, c_time:{}, s_time:{}, e_time:{}",
|
||||
me->self_key.value(), me->renko->current_brick->brick_id, edges.low,
|
||||
edges.high, low_avg, high_avg, me->packet_no,
|
||||
@@ -900,8 +984,9 @@ void MomentumAlgo::report_state(bool to_gui = true) {
|
||||
update_val(m["31 Upper Brick"], me->renko->get_high());
|
||||
update_val(m["40 Lower Average"], me->gandata->low_average());
|
||||
update_val(m["41 Upper Average"], me->gandata->high_average());
|
||||
update_val(m["50 + Signals "], me->pos_signals);
|
||||
update_val(m["51 - Signals "], me->neg_signals);
|
||||
update_val(m["50 + Signals"], me->pos_signals);
|
||||
update_val(m["51 - Signals"], me->neg_signals);
|
||||
update_val(m["60 Front Runners"], me->front_runners);
|
||||
|
||||
for (auto pair : me->slippage_buffer) {
|
||||
m["9" + std::to_string(index++) + ": base, actual"].set_as_string(
|
||||
@@ -914,7 +999,8 @@ void MomentumAlgo::report_state(bool to_gui = true) {
|
||||
LOG(OrderMan, Info,
|
||||
"MomentumAlgo::{}::type:result, {}, Last_Price:{}, Position:{}, "
|
||||
"P&L:{}, Brick_Count:{}, Holding_Price:{}, Last_order_price:{}, "
|
||||
"Lower_Brick:{}, Upper_Brick:{}, Lower_Average:{}, Upper_Average:{}, "
|
||||
"Lower_Brick:{}, Upper_Brick:{}, Lower_Average:{}, "
|
||||
"Upper_Average:{}, "
|
||||
"pos_signals:{}, neg_signals:{}",
|
||||
me->self_key, me->algo_params->print(),
|
||||
m["00 Last Price"].as_string(), m["01 Position"].as_string(),
|
||||
@@ -923,7 +1009,7 @@ void MomentumAlgo::report_state(bool to_gui = true) {
|
||||
m["20 Last Order Price"].as_string(),
|
||||
m["30 Lower Brick "].as_string(), m["31 Upper Brick"].as_string(),
|
||||
m["40 Lower Average"].as_string(), m["41 Upper Average"].as_string(),
|
||||
m["50 + Signals "].as_string(), m["51 - Signals "].as_string());
|
||||
m["50 + Signals"].as_string(), m["51 - Signals"].as_string());
|
||||
_algo().update_custom_state(me->out_state);
|
||||
}
|
||||
}
|
||||
@@ -1054,7 +1140,7 @@ void MomentumAlgo::_fire_orders(size_t count, api::Side side,
|
||||
LOG(OrderMan, Info,
|
||||
"MomentumAlgo::{}::type:_fire_orders, order_key:{}, brick_id:{}, "
|
||||
"price:{}, "
|
||||
"count:{}, BUY?:{}, source:{}, pos:{}, order_id:{}, book_buy:{}, "
|
||||
"count:{}, BUY?:{}, source:{}, pos:{}, order_id:{}, book_bu y:{}, "
|
||||
"book_sell:{}",
|
||||
me->self_key.value(), me->child_key.value(),
|
||||
me->renko->current_brick->brick_id, price, count, is_buy, source,
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
namespace bts {
|
||||
namespace orderman {
|
||||
struct Brick;
|
||||
struct OrderEntryDetails;
|
||||
class MomentumAlgo : public IAlgoOrder {
|
||||
public:
|
||||
MomentumAlgo();
|
||||
@@ -26,6 +27,7 @@ class MomentumAlgo : public IAlgoOrder {
|
||||
void report_state(bool to_gui);
|
||||
void _handle_fixed_edge();
|
||||
void run_sim();
|
||||
void _early_brick();
|
||||
|
||||
private:
|
||||
struct PImpl;
|
||||
|
||||
@@ -266,6 +266,7 @@ extern "C" void init_momentum_algo_plugin(bts::SparkServices &srv) {
|
||||
*momentum.add_param_desc() = param_bool("File Sim?");
|
||||
*momentum.add_param_desc() = param_bool("Timed Start?");
|
||||
*momentum.add_param_desc() = param_bool("Log Graph?");
|
||||
*momentum.add_param_desc() = param_bool("Early Brick?");
|
||||
|
||||
// *momentum.add_param_desc() = param_string("time( 24H HHMM)");
|
||||
*momentum.add_param_desc() = param_uint32("Renko Ticks");
|
||||
@@ -275,7 +276,7 @@ extern "C" void init_momentum_algo_plugin(bts::SparkServices &srv) {
|
||||
*momentum.add_param_desc() = param_uint32("Edge To lose");
|
||||
|
||||
*momentum.add_param_desc() = param_double("Run Duration (Hours-decimal)");
|
||||
|
||||
*momentum.add_param_desc() = param_double("Early Threshold");
|
||||
// *momentum.add_param_desc() = param_uint32("Time End Hours (24 HRS )");
|
||||
// *momentum.add_param_desc() = param_uint32("Time End Minutes");
|
||||
*momentum.add_param_desc() = param_uint32("Time Start Hours (24 HRS)");
|
||||
|
||||
Reference in new issue
Block a user