|
1 | 1 | /* |
2 | | - * ta_misc.cpp — stats + helpers + free functions: Linreg, PercentRank, PercentileNearestRank, PercentileLinearInterpolation, Correlation, BarsSince, ValueWhen, pivot_point_levels |
| 2 | + * ta_misc.cpp — stats + helpers + free functions: Linreg, PercentRank, PercentileNearestRank, PercentileLinearInterpolation, Correlation, BarsSince, ValueWhen, pivot_point_levels, PivotPointLevels |
3 | 3 | * |
4 | 4 | * Carved out of ta.cpp during the v0.1 file-split (phase 6) so the |
5 | 5 | * 66-class TA library becomes navigable. Every class declared in |
|
14 | 14 | #include <algorithm> |
15 | 15 | #include <cmath> |
16 | 16 | #include <numeric> |
| 17 | +#include <stdexcept> |
17 | 18 | #include <string> |
18 | 19 | #include <vector> |
19 | 20 |
|
@@ -438,5 +439,155 @@ std::vector<double> pivot_point_levels(const std::string& method, |
438 | 439 | return out; |
439 | 440 | } |
440 | 441 |
|
| 442 | +// ============================================================================ |
| 443 | +// Pivot point levels of an anchored period |
| 444 | +// ============================================================================ |
| 445 | + |
| 446 | +namespace { |
| 447 | + |
| 448 | +// The levels of the period (o, h, l, c) -- next_open is the open of the |
| 449 | +// period the levels are for, which only Woodie reads -- into out[11], each |
| 450 | +// formula evaluated operation by operation as the standard definition spells |
| 451 | +// it. A formula missing one of its inputs leaves all eleven na, as the free |
| 452 | +// function does for a missing high, low or close. |
| 453 | +void pivot_levels_of(PivotLevelsType type, double o, double h, double l, double c, |
| 454 | + double next_open, double* out) { |
| 455 | + for (int i = 0; i < 11; ++i) out[i] = na<double>(); |
| 456 | + switch (type) { |
| 457 | + case PivotLevelsType::Woodie: { |
| 458 | + if (is_na(h) || is_na(l) || is_na(next_open)) return; |
| 459 | + const double P = (h + l + 2.0 * next_open) / 4.0; |
| 460 | + const double R3 = h + 2.0 * (P - l); |
| 461 | + const double S3 = l - 2.0 * (h - P); |
| 462 | + out[0] = P; |
| 463 | + out[1] = 2.0 * P - l; out[2] = 2.0 * P - h; |
| 464 | + out[3] = P + (h - l); out[4] = P - (h - l); |
| 465 | + out[5] = R3; out[6] = S3; |
| 466 | + out[7] = R3 + (h - l); out[8] = S3 - (h - l); |
| 467 | + return; |
| 468 | + } |
| 469 | + case PivotLevelsType::DM: { |
| 470 | + if (is_na(o) || is_na(h) || is_na(l) || is_na(c)) return; |
| 471 | + double x; |
| 472 | + if (o == c) x = h + l + 2.0 * c; |
| 473 | + else if (c > o) x = 2.0 * h + l + c; |
| 474 | + else x = 2.0 * l + h + c; |
| 475 | + out[0] = x / 4.0; |
| 476 | + out[1] = x / 2.0 - l; out[2] = x / 2.0 - h; |
| 477 | + return; |
| 478 | + } |
| 479 | + case PivotLevelsType::Traditional: |
| 480 | + case PivotLevelsType::Fibonacci: |
| 481 | + case PivotLevelsType::Classic: |
| 482 | + case PivotLevelsType::Camarilla: |
| 483 | + break; |
| 484 | + } |
| 485 | + if (is_na(h) || is_na(l) || is_na(c)) return; |
| 486 | + const double P = (h + l + c) / 3.0; |
| 487 | + out[0] = P; |
| 488 | + switch (type) { |
| 489 | + case PivotLevelsType::Traditional: |
| 490 | + out[1] = P * 2.0 - l; out[2] = P * 2.0 - h; |
| 491 | + out[3] = P + (h - l); out[4] = P - (h - l); |
| 492 | + out[5] = P * 2.0 + (h - 2.0 * l); out[6] = P * 2.0 - (2.0 * h - l); |
| 493 | + out[7] = P * 3.0 + (h - 3.0 * l); out[8] = P * 3.0 - (3.0 * h - l); |
| 494 | + out[9] = P * 4.0 + (h - 4.0 * l); out[10] = P * 4.0 - (4.0 * h - l); |
| 495 | + return; |
| 496 | + case PivotLevelsType::Fibonacci: |
| 497 | + out[1] = P + 0.382 * (h - l); out[2] = P - 0.382 * (h - l); |
| 498 | + out[3] = P + 0.618 * (h - l); out[4] = P - 0.618 * (h - l); |
| 499 | + out[5] = P + (h - l); out[6] = P - (h - l); |
| 500 | + return; |
| 501 | + case PivotLevelsType::Classic: |
| 502 | + out[1] = 2.0 * P - l; out[2] = 2.0 * P - h; |
| 503 | + out[3] = P + (h - l); out[4] = P - (h - l); |
| 504 | + out[5] = P + 2.0 * (h - l); out[6] = P - 2.0 * (h - l); |
| 505 | + out[7] = P + 3.0 * (h - l); out[8] = P - 3.0 * (h - l); |
| 506 | + return; |
| 507 | + case PivotLevelsType::Camarilla: { |
| 508 | + out[1] = c + 1.1 * (h - l) / 12.0; out[2] = c - 1.1 * (h - l) / 12.0; |
| 509 | + out[3] = c + 1.1 * (h - l) / 6.0; out[4] = c - 1.1 * (h - l) / 6.0; |
| 510 | + out[5] = c + 1.1 * (h - l) / 4.0; out[6] = c - 1.1 * (h - l) / 4.0; |
| 511 | + out[7] = c + 1.1 * (h - l) / 2.0; out[8] = c - 1.1 * (h - l) / 2.0; |
| 512 | + const double R5 = l != 0.0 ? (h / l) * c : na<double>(); |
| 513 | + out[9] = R5; |
| 514 | + out[10] = is_na(R5) ? na<double>() : c - (R5 - c); |
| 515 | + return; |
| 516 | + } |
| 517 | + case PivotLevelsType::Woodie: |
| 518 | + case PivotLevelsType::DM: |
| 519 | + return; |
| 520 | + } |
| 521 | +} |
| 522 | + |
| 523 | +} // namespace |
| 524 | + |
| 525 | +PivotLevelsType pivot_levels_type(const std::string& name) { |
| 526 | + if (name == "Traditional") return PivotLevelsType::Traditional; |
| 527 | + if (name == "Fibonacci") return PivotLevelsType::Fibonacci; |
| 528 | + if (name == "Woodie") return PivotLevelsType::Woodie; |
| 529 | + if (name == "Classic") return PivotLevelsType::Classic; |
| 530 | + if (name == "DM") return PivotLevelsType::DM; |
| 531 | + if (name == "Camarilla") return PivotLevelsType::Camarilla; |
| 532 | + throw std::invalid_argument("pivot point levels: unknown type '" + name + "'"); |
| 533 | +} |
| 534 | + |
| 535 | +void PivotPointLevels::Period::enter(double o, double h, double l, double c) { |
| 536 | + if (is_na(open) && !is_na(o)) open = o; |
| 537 | + if (!is_na(h) && (is_na(high) || h > high)) high = h; |
| 538 | + if (!is_na(l) && (is_na(low) || l < low)) low = l; |
| 539 | + if (!is_na(c)) close = c; |
| 540 | +} |
| 541 | + |
| 542 | +PivotPointLevels::Levels PivotPointLevels::no_levels() { |
| 543 | + Levels levels; |
| 544 | + for (double& v : levels.v) v = na<double>(); |
| 545 | + return levels; |
| 546 | +} |
| 547 | + |
| 548 | +std::vector<double> PivotPointLevels::compute(PivotLevelsType type, bool anchor, bool developing, |
| 549 | + double open, double high, double low, double close) { |
| 550 | + if (developing && type == PivotLevelsType::Woodie) { |
| 551 | + throw std::runtime_error( |
| 552 | + "pivot point levels: the Woodie type has no developing levels (a Woodie " |
| 553 | + "period's levels need the open of the period after it)"); |
| 554 | + } |
| 555 | + saved_period_ = period_; |
| 556 | + saved_held_ = held_; |
| 557 | + if (anchor) { |
| 558 | + // This bar closes the period in progress and opens the next one: the |
| 559 | + // held levels are the closed period's, with this bar's open as the |
| 560 | + // open of the period they are for. |
| 561 | + pivot_levels_of(type, period_.open, period_.high, period_.low, period_.close, open, |
| 562 | + held_.v); |
| 563 | + period_ = Period{}; |
| 564 | + } |
| 565 | + period_.enter(open, high, low, close); |
| 566 | + if (!developing) return std::vector<double>(held_.v, held_.v + 11); |
| 567 | + std::vector<double> out(11); |
| 568 | + pivot_levels_of(type, period_.open, period_.high, period_.low, period_.close, na<double>(), |
| 569 | + out.data()); |
| 570 | + return out; |
| 571 | +} |
| 572 | + |
| 573 | +std::vector<double> PivotPointLevels::recompute(PivotLevelsType type, bool anchor, bool developing, |
| 574 | + double open, double high, double low, |
| 575 | + double close) { |
| 576 | + period_ = saved_period_; |
| 577 | + held_ = saved_held_; |
| 578 | + return compute(type, anchor, developing, open, high, low, close); |
| 579 | +} |
| 580 | + |
| 581 | +std::vector<double> PivotPointLevels::compute(const std::string& type, bool anchor, bool developing, |
| 582 | + double open, double high, double low, double close) { |
| 583 | + return compute(pivot_levels_type(type), anchor, developing, open, high, low, close); |
| 584 | +} |
| 585 | + |
| 586 | +std::vector<double> PivotPointLevels::recompute(const std::string& type, bool anchor, |
| 587 | + bool developing, double open, double high, |
| 588 | + double low, double close) { |
| 589 | + return recompute(pivot_levels_type(type), anchor, developing, open, high, low, close); |
| 590 | +} |
| 591 | + |
441 | 592 | } // namespace ta |
442 | 593 | } // namespace pineforge |
0 commit comments