Skip to content

🔨 Lookback-functions#71

Merged
serkor1 merged 12 commits into
develfrom
70-lookback-function
Jun 21, 2026
Merged

🔨 Lookback-functions#71
serkor1 merged 12 commits into
develfrom
70-lookback-function

Conversation

@serkor1

@serkor1 serkor1 commented Jun 8, 2026

Copy link
Copy Markdown
Owner

📚 What?

This PR introduces unexported lookback-functions for calculating lookback-periods independently of the indicator functions. The goal is to provide an input validation step for downstream wrappers like {ledgr}, and similar packages that may or may not use different control-flows.

📋 TODO

  • Test the overhead of calculating lookback (should be O(1))
  • Implement unit-tests (attributes vs lookback)
  • Implement high-level wrapper talib::lookback(FUN, ...) similar to indicator()

⚠️ Caveats

When returning as as.double(x) the attributes gets stripped. So all numeric methods are broken - and all rolling-functions.

To solve this, strip the dim() to retain the one-dimensional return values.

This PR closes #70

@serkor1 serkor1 added the enhancement New feature or request label Jun 8, 2026
@serkor1 serkor1 linked an issue Jun 8, 2026 that may be closed by this pull request
@codecov

codecov Bot commented Jun 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.68237% with 143 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.56%. Comparing base (a2d960c) to head (ede73f2).

Files with missing lines Patch % Lines
R/lookback.R 70.45% 13 Missing ⚠️
R/ta_BBANDS.R 88.88% 2 Missing ⚠️
R/ta_HT_PHASOR.R 85.71% 2 Missing ⚠️
R/ta_HT_SINE.R 85.71% 2 Missing ⚠️
R/ta_MACD.R 88.23% 2 Missing ⚠️
R/ta_MACDEXT.R 90.00% 2 Missing ⚠️
R/ta_MACDFIX.R 86.66% 2 Missing ⚠️
R/ta_MAMA.R 87.50% 2 Missing ⚠️
R/ta_STOCHRSI.R 88.88% 2 Missing ⚠️
R/ta_VOLUME.R 90.90% 2 Missing ⚠️
... and 112 more
Additional details and impacted files
@@            Coverage Diff             @@
##            devel      #71      +/-   ##
==========================================
- Coverage   96.67%   96.56%   -0.12%     
==========================================
  Files         280      281       +1     
  Lines       21012    24264    +3252     
==========================================
+ Hits        20314    23431    +3117     
- Misses        698      833     +135     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@serkor1 serkor1 force-pushed the 70-lookback-function branch from d012108 to c672e7d Compare June 8, 2026 16:02
@serkor1 serkor1 force-pushed the 70-lookback-function branch from c672e7d to 3764c17 Compare June 8, 2026 16:41
serkor1 added 4 commits June 20, 2026 21:36
* The lookback function is a utility/helper function
  for downstream packages that wants to implement their
  own wrapper based on {talib} - the function gives a higher
  degree of freedom in the way the indicators are called and can
  be used in any custom control flows.
* The unit-test tests whether the lookback
  is equal between the lookback() and attribute.

  This is mainly to test the lookback() and not the
  the indicators. They are calculated the same way.
@serkor1

serkor1 commented Jun 20, 2026

Copy link
Copy Markdown
Owner Author

volume() is a pain. The lookback period depends on the MA-input. By construction it would need at minimum the maximum of (MA_1, MA_2, ..., MA_k).

serkor1 added 3 commits June 20, 2026 22:04
* The function returns the max(MA1, MA2, ..., MAk) when
  passed - otherwise zero.

* The ta_VOLUME.c has gotten some love and refactoring
  but the underlying logic is the same.
@serkor1 serkor1 marked this pull request as ready for review June 20, 2026 21:19
serkor1 added 4 commits June 21, 2026 00:35
* as.double() strips all attributes which means that the
  lookback attribute gets lost in the meatgrinder - an alternative
  option is to set set the dim(x) <- NULL (which should be a safe alternative)

* is.vector() only returns true if the input value if the only attribute
  is names; this implies (I guess, not tested) that all data containers would return
  TRUE in some form or the other. A separate test condition is to check whether
  the output has no dim()-value.
* TA-Lib returns -1 for invalid indicator and data pairs; if
  the lookback is invalid R will return it as <NA>.
  This behaviour has been documented internally and in the roxygen
  documentation.

* lookback() will return max(lookback, 1) to avoid insensible return
  values like 0 (this is the case for volume without moving averages).
* In 82c5f88 the lookback values
  were set to minimum 1 for a valid indicator and data pair.

  This commit reflects this change such that the attributes behaves in
  a similar way - which also has to be reflected in the parity-tests so
  the comparision is apples-to-apples.
* When using `dim() <- NULL`, the storage.mode() is preserved
  and therefore indicators that returns one-dimensional indicators as
  vectors fails the as.double() test; it should be safe to test for
  is.double and is.integer at the same time - if there is any coercions
  these will be caught by the parity-tests either way.
@serkor1 serkor1 merged commit 98f6c78 into devel Jun 21, 2026
23 of 25 checks passed
@serkor1 serkor1 deleted the 70-lookback-function branch June 21, 2026 07:49
@serkor1 serkor1 mentioned this pull request Jun 21, 2026
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement loookback-function as development tools

1 participant