add payup/less price calc utils (Security.h)'

This commit is contained in:
John Colvin committed 2020-05-12 18:55:37 -05:00
1 parent 6a7e3d739d
commit 9a8e80e149
2 files changed
+91 -2

No files matched your search

+78
View File
@@ -88,5 +88,83 @@ bool is_expired(api::Security const &security) {
return is_expired(security, datetime::local_now());
}
// This method accounts for every combination of valid/invalid
// tick components. Since there are 3 components: low_tick, high_tick, and price_break,
// This leaves 8 combinations (16 if the comparison 'low_tick < high_tick'
// is considered a fourth component, which is either valid, or invalid).
// Needless to say, there are a lot if/else blocks in here.
// todo: unit tests!
Price tick_up(const api::ContractSpecification& spec, double price, int num_ticks) {
if (!os::is_finite(price) || num_ticks == 0 || spec.low_tick() > spec.high_tick()) {
return Price(price);
}
if (spec.low_tick() > market_epsilon()) {
if (spec.high_tick() > (spec.low_tick() + market_epsilon())) {
if (os::is_finite(spec.price_break())) {
// normal path (all tick components are valid).
double pbdiff = price - spec.price_break();
if (std::abs(pbdiff) < market_epsilon()) {
return Price(price + num_ticks * (num_ticks < 0 ? spec.low_tick() : spec.high_tick()));
} else if (price < spec.price_break()) {
if (num_ticks < 0) {
// price is below the price break, and num ticks is negative.
return Price(price + spec.low_tick() * num_ticks);
} else {
// price is below the price break, but num ticks is positive.
auto ticks_to_break = price_diff_to_ticks(price, spec.price_break(), spec.low_tick());
if (ticks_to_break == 0) {
// price lies somewhere between (price_break - epsilon) and (price_break - low_tick/2); just use high tick.
// Note: this is probably indicative of either a rounding error upstream, or a goofy contract spec.
return Price(price + num_ticks * spec.high_tick());
} else {
// It would be absurd for ticks_to_break to be less than 0.
bts_assert(ticks_to_break > 0);
if (ticks_to_break >= num_ticks) {
return Price(price + num_ticks * spec.low_tick());
} else {
// Don't assume that the price break lies exactly on a tick (even though it should).
// In other words, don't return price_break + (num_ticks - ticks_to_break) * spec.high_tick().
return Price(price + ticks_to_break * spec.low_tick() + (num_ticks - ticks_to_break) * spec.high_tick());
}
}
}
} else if (num_ticks > 0) {
// price is above the price break, and num ticks is positive.
return Price(price + spec.high_tick() * num_ticks);
} else {
// price is above the price break, but num ticks is negative.
auto ticks_to_break = price_diff_to_ticks(price, spec.price_break(), spec.high_tick());
if (ticks_to_break == 0) {
// price lies somewhere between (price_break + epsilon) and (price_break + high_tick/2); just use low tick.
// Note: this is probably indicative of either a rounding error upstream, or a goofy contract spec.
return Price(price + num_ticks * spec.low_tick());
} else {
// It would be absurd for ticks_to_break to be greater than 0.
bts_assert(ticks_to_break < 0);
if (ticks_to_break <= num_ticks) {
return Price(price + num_ticks * spec.high_tick());
} else {
// Don't assume that the price break lies exactly on a tick (even though it should).
// In other words, don't return price_break + (num_ticks - ticks_to_break) * spec.low_tick().
return Price(price + ticks_to_break * spec.high_tick() + (num_ticks - ticks_to_break) * spec.low_tick());
}
}
}
} else {
// low tick and high tick are valid, but price break isn't; just use low tick.
return Price(price + num_ticks * spec.low_tick());
}
} else {
// low tick is valid, but high tick isn't.
return Price(price + num_ticks * spec.low_tick());
}
} else if (spec.high_tick() > 0) {
// high tick is valid, but low tick isn't.
return Price(price + num_ticks * spec.high_tick());
} else {
// don't negotiate with negative ticks; just return the price.
return Price(price);
}
}
} // namespace securities
} // namespace bts
+13 -2
View File
@@ -25,6 +25,17 @@ SecurityKey security_key(api::Security const &);
std::string to_str(api::Security const &);
std::pair<bool, api::Security> from_str(std::string const &);
std::string descriptor(api::Security const &);
// supports ticking down as well (i.e., num_ticks can be negative).
Price tick_up(const api::ContractSpecification &spec, double price, int num_ticks);
inline Price tick_down(const api::ContractSpecification& spec, double price, int num_ticks) {
return tick_up(spec, price, -num_ticks);
}
inline Price payup(api::Side side, const api::ContractSpecification& spec, double price, int num_ticks) {
return tick_up(spec, price, side == api::SIDE_SELL ? -num_ticks : num_ticks);
}
inline Price payless(api::Side side, const api::ContractSpecification& spec, double price, int num_ticks) {
return tick_up(spec, price, side == api::SIDE_SELL ? num_ticks : -num_ticks);
}
inline std::string const &side_name(api::Side side) {
return api::Side_Name(side);
@@ -212,5 +223,5 @@ inline bool gr_than(double p1, double p2, double tick_size) {
!test_price_equality(p1, p2, tick_size);
}
}}
} // namespace securities
} // namespace bts