-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Add Indicators.jl functionality with TSFrames #155
Draft
smishr
wants to merge
9
commits into
xKDR:main
Choose a base branch
from
smishr:indicators
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
50c9a5f
test indicators with TSFrames
smishr 3a443b0
Update src/indicators.jl
smishr 50d85ca
Add package extension for Indicators
smishr 2e8c7f2
Add test for indicators
smishr 69b331d
Add indicators test
smishr 328fb2e
Updated integration
smishr cd7281d
fixed dependency structure
smishr f7122c6
Abstracted code for arbitrary cols
smishr f0a2154
save default close and vol funcs
smishr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
""" | ||
Methods for using Indicators.jl functions on TSFrame objects from TSFrames.jl package. | ||
See respective documentation on Indicators.jl for full description of the methods | ||
""" | ||
module IndicatorsExt | ||
|
||
using TSFrames, Indicators | ||
|
||
""" | ||
Abstracted function for Indicators.jl methods that take a single input, such as SMA, EMA, etc. | ||
""" | ||
function apply_func(X::TSFrame, f::Function, input_flds::Symbol, rename_flds::Vector{Symbol}; kwargs...) | ||
return TSFrames.rename!(TSFrame(f(X[:, input_flds]; kwargs...), index(X)), rename_flds) | ||
end | ||
|
||
""" | ||
Abstracted function for Indicators.jl methods that take multiple input columns as Matrix, such as VWMA, VWAP, etc. | ||
""" | ||
function apply_func(X::TSFrame, f::Function, input_flds::Vector{Symbol}, rename_flds::Vector{Symbol}; kwargs...) | ||
return TSFrames.rename!(TSFrame(f(Matrix(X[:, input_flds]); kwargs...), index(X)), rename_flds) | ||
end | ||
|
||
########################################################################################## | ||
### Dispatches to allow user to specify Symbol or Vector{Symbol} for input arguments | ||
|
||
# Indicators.jl/src/ma.jl | ||
Indicators.runmean(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.runmean, x, [:RUNMEAN]; kwargs...) | ||
Indicators.sma(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.sma, x, [:SMA]; kwargs...) | ||
Indicators.trima(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.trima, x, [:TRIMA]; kwargs...) | ||
Indicators.wma(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.wma, x, [:WMA]; kwargs...) | ||
Indicators.ema(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.ema, x, [:EMA]; kwargs...) | ||
Indicators.mma(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.mma, x, [:MMA]; kwargs...) | ||
Indicators.dema(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.dema, x, [:DEMA]; kwargs...) | ||
Indicators.tema(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.tema, x, [:TEMA]; kwargs...) | ||
Indicators.mama(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.mama, x, [:MAMA, :FAMA]; kwargs...) | ||
Indicators.hma(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.hma, x, [:HMA]; kwargs...) | ||
Indicators.swma(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.swma, x, [:SWMA]; kwargs...) | ||
Indicators.kama(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.kama, x, [:KAMA]; kwargs...) | ||
Indicators.alma(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.alma, x, [:ALMA]; kwargs...) | ||
Indicators.zlema(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.zlema, x, [:ZLEMA]; kwargs...) | ||
Indicators.hama(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.hama, x, [:HammingMA]; kwargs...) | ||
Indicators.vwma(X::TSFrame, x::Vector{Symbol}; kwargs...) = apply_func(X, Indicators.vwma, x, [:VWMA]; kwargs...) | ||
Indicators.vwap(X::TSFrame, x::Vector{Symbol}; kwargs...) = apply_func(X, Indicators.vwap, x, [:VWAP]; kwargs...) | ||
|
||
# Indicators.jl/src/reg.jl | ||
Indicators.mlr_beta(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.mlr_beta, x, [:Intercept, :Slope]; kwargs...) | ||
Indicators.mlr_slope(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.mlr_slope, x, [:Slope]; kwargs...) | ||
Indicators.mlr_intercept(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.mlr_intercept, x, [:Intercept]; kwargs...) | ||
Indicators.mlr(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.mlr, x, [:MLR]; kwargs...) | ||
Indicators.mlr_se(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.mlr_se, x, [:StdErr]; kwargs...) | ||
Indicators.mlr_ub(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.mlr_ub, x, [:MLRUB]; kwargs...) | ||
Indicators.mlr_lb(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.mlr_lb, x, [:MLRLB]; kwargs...)' | ||
Indicators.mlr_bands(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.mlr_bands, x, [:MLRLB, :MLR, :MLRUB]; kwargs...) | ||
Indicators.mlr_rsq(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.mlr_rsq, x, [:RSquared]; kwargs...) | ||
|
||
# Indicators.jl/src/mom.jl | ||
Indicators.aroon(X::TSFrame, x::Vector{Symbol}; kwargs...) = apply_func(X, Indicators.aroon, x, [:AroonUp, :AroonDn, :AroonOsc]; kwargs...) | ||
Indicators.donch(X::TSFrame, x::Vector{Symbol}; kwargs...) = apply_func(X, Indicators.donch, x, [:Low, :Mid, :High]; kwargs...) | ||
Indicators.ichimoku(X::TSFrame, x::Vector{Symbol}; kwargs...) = apply_func(X, Indicators.ichimoku, x, [:Tenkan, :Kijun, :SenkouA, :SenkouB, :Chikou]; kwargs...) | ||
Indicators.momentum(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.momentum, x, [:Momentum]; kwargs...) | ||
Indicators.roc(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.roc, x, [:ROC]; kwargs...) | ||
Indicators.macd(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.macd, x, [:MACD, :Signal, :Histogram]; kwargs...) | ||
Indicators.rsi(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.rsi, x, [:RSI]; kwargs...) | ||
Indicators.adx(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.adx, x, [:DiPlus, :DiMinus, :ADX]; kwargs...) | ||
Indicators.heikinashi(X::TSFrame, x::Vector{Symbol}; kwargs...) = apply_func(X, Indicators.heikinashi, x, [:Open, :High, :Low, :Close]; kwargs...) | ||
Indicators.psar(X::TSFrame, x::Vector{Symbol}; kwargs...) = apply_func(X, Indicators.psar, x, [:PSAR]; kwargs...) | ||
Indicators.kst(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.kst, x, [:KST]; kwargs...) | ||
Indicators.wpr(X::TSFrame, x::Vector{Symbol}; kwargs...) = apply_func(X, Indicators.wpr, x, [:WPR]; kwargs...) | ||
Indicators.cci(X::TSFrame, x::Vector{Symbol}; kwargs...) = apply_func(X, Indicators.cci, x, [:CCI]; kwargs...) | ||
Indicators.stoch(X::TSFrame, x::Vector{Symbol}; kwargs...) = apply_func(X, Indicators.stoch, x, [:Stochastic, :Signal]; kwargs...) | ||
Indicators.smi(X::TSFrame, x::Vector{Symbol}; kwargs...) = apply_func(X, Indicators.smi, x, [:SMI, :Signal]; kwargs...) | ||
|
||
# Indicators.jl/src/vol.jl | ||
Indicators.bbands(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.bbands, x, [:LB, :MA, :UB]; kwargs...) | ||
Indicators.tr(X::TSFrame, x::Vector{Symbol}; kwargs...) = apply_func(X, Indicators.tr, x, [:TR]; kwargs...) | ||
Indicators.atr(X::TSFrame, x::Vector{Symbol}; kwargs...) = apply_func(X, Indicators.atr, x, [:ATR]; kwargs...) | ||
Indicators.keltner(X::TSFrame, x::Vector{Symbol}; kwargs...) = apply_func(X, Indicators.keltner, x, [:KeltnerLower, :KeltnerMiddle, :KeltnerUpper]; kwargs...) | ||
|
||
#Indicators.jl/src/trendy.jl | ||
Indicators.maxima(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.maxima, x, [:Maxima]; kwargs...) | ||
Indicators.minima(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.minima, x, [:Minima]; kwargs...) | ||
Indicators.support(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.support, x, [:Support]; kwargs...) | ||
Indicators.resistance(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.resistance, x, [:Resistance]; kwargs...) | ||
|
||
# Indicators.jl/src/utils.jl | ||
## Assumes both x and y are in same TSFrame object | ||
Indicators.crossover(X::TSFrame, x::Symbol, y::Symbol) = apply_func(X, Indicators.crossover, [x, y], [:CrossOver]) | ||
Indicators.crossunder(X::TSFrame, x::Symbol, y::Symbol) = apply_func(X, Indicators.crossunder, [x, y], [:CrossUnder]) | ||
|
||
# Indicators.jl/src/chaos.jl | ||
Indicators.hurst(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.hurst, x, [:Hurst]; kwargs...) | ||
Indicators.rsrange(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.rsrange, x, [:RS]; kwargs...) | ||
|
||
########################################################################################## | ||
### Multiple dispatches for "default" column labels, e.g. :Close, :High, :Low, :Open, :Volume | ||
close = :Close | ||
high = :High | ||
low = :Low | ||
open = :Open | ||
volume = :Volume | ||
|
||
has_close(X::TSFrame)::Bool = :Close in propertynames(X.coredata) | ||
close_error = error("Argument must have Close field") | ||
has_close_volume(X::TSFrame)::Bool = all(fld -> fld in propertynames(X.coredata), [:Close, :Volume]) | ||
close_vol_error = error("Argument must have Close and Volume field") | ||
|
||
# Indicators.jl/src/ma.jl | ||
Indicators.runmean(X::TSFrame; kwargs...) = has_close(X) ? Indicators.runmean(X, close; kwargs...) : close_error | ||
Indicators.sma(X::TSFrame; kwargs...) = has_close(X) ? Indicators.sma(X, close; kwargs...) : close_error | ||
Indicators.trima(X::TSFrame; kwargs...) = has_close(X) ? Indicators.trima(X, close; kwargs...) : close_error | ||
Indicators.wma(X::TSFrame; kwargs...) = has_close(X) ? Indicators.wma(X, close; kwargs...) : close_error | ||
Indicators.ema(X::TSFrame; kwargs...) = has_close(X) ? Indicators.ema(X, close; kwargs...) : close_error | ||
Indicators.mma(X::TSFrame; kwargs...) = has_close(X) ? Indicators.mma(X, close; kwargs...) : close_error | ||
Indicators.dema(X::TSFrame; kwargs...) = has_close(X) ? Indicators.dema(X, close; kwargs...) : close_error | ||
Indicators.tema(X::TSFrame; kwargs...) = has_close(X) ? Indicators.tema(X, close; kwargs...) : close_error | ||
Indicators.mama(X::TSFrame; kwargs...) = has_close(X) ? Indicators.mama(X, close; kwargs...) : close_error | ||
Indicators.hma(X::TSFrame; kwargs...) = has_close(X) ? Indicators.hma(X, close; kwargs...) : close_error | ||
Indicators.swma(X::TSFrame; kwargs...) = has_close(X) ? Indicators.swma(X, close; kwargs...) : close_error | ||
Indicators.kama(X::TSFrame; kwargs...) = has_close(X) ? Indicators.kama(X, close; kwargs...) : close_error | ||
Indicators.alma(X::TSFrame; kwargs...) = has_close(X) ? Indicators.alma(X, close; kwargs...) : close_error | ||
Indicators.zlema(X::TSFrame; kwargs...) = has_close(X) ? Indicators.zlema(X, close; kwargs...) : close_error | ||
Indicators.hama(X::TSFrame; kwargs...) = has_close(X) ? Indicators.hama(X, close; kwargs...) : close_error | ||
Indicators.vwma(X::TSFrame; kwargs...) = has_close_volume(X) ? Indicators.vwma(X, [close, volume]; kwargs...) : close_vol_error | ||
Indicators.vwap(X::TSFrame; kwargs...) = has_close_volume(X) ? Indicators.vwap(X, [close, volume]; kwargs...) : close_vol_error | ||
|
||
# Indicators.jl/src/reg.jl | ||
Indicators.mlr_beta(X::TSFrame; kwargs...) = has_close(X) ? Indicators.mlr_beta(X, close; kwargs...) : close_error | ||
Indicators.mlr_slope(X::TSFrame; kwargs...) = has_close(X) ? Indicators.mlr_slope(X, close; kwargs...) : close_error | ||
Indicators.mlr_intercept(X::TSFrame; kwargs...) = has_close(X) ? Indicators.mlr_intercept(X, close; kwargs...) : close_error | ||
Indicators.mlr(X::TSFrame; kwargs...) = has_close(X) ? Indicators.mlr(X, close; kwargs...) : close_error | ||
Indicators.mlr_se(X::TSFrame; kwargs...) = has_close(X) ? Indicators.mlr_se(X, close; kwargs...) : close_error | ||
Indicators.mlr_ub(X::TSFrame; kwargs...) = has_close(X) ? Indicators.mlr_ub(X, close; kwargs...) : close_error | ||
Indicators.mlr_lb(X::TSFrame; kwargs...) = has_close(X) ? Indicators.mlr_lb(X, close; kwargs...) : close_error | ||
Indicators.mlr_bands(X::TSFrame; kwargs...) = has_close(X) ? Indicators.mlr_bands(X, close; kwargs...) : close_error | ||
Indicators.mlr_rsq(X::TSFrame; kwargs...) = has_close(X) ? Indicators.mlr_rsq(X, close; kwargs...) : close_error | ||
|
||
# Indicators.jl/src/mom.jl | ||
Indicators.aroon(X::TSFrame, x::Vector{Symbol}; kwargs...) = apply_func(X, Indicators.aroon, x, [:AroonUp, :AroonDn, :AroonOsc]; kwargs...) | ||
Indicators.donch(X::TSFrame, x::Vector{Symbol}; kwargs...) = apply_func(X, Indicators.donch, x, [:Low, :Mid, :High]; kwargs...) | ||
Indicators.ichimoku(X::TSFrame, x::Vector{Symbol}; kwargs...) = apply_func(X, Indicators.ichimoku, x, [:Tenkan, :Kijun, :SenkouA, :SenkouB, :Chikou]; kwargs...) | ||
Indicators.momentum(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.momentum, x, [:Momentum]; kwargs...) | ||
Indicators.roc(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.roc, x, [:ROC]; kwargs...) | ||
Indicators.macd(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.macd, x, [:MACD, :Signal, :Histogram]; kwargs...) | ||
Indicators.rsi(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.rsi, x, [:RSI]; kwargs...) | ||
Indicators.adx(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.adx, x, [:DiPlus, :DiMinus, :ADX]; kwargs...) | ||
Indicators.heikinashi(X::TSFrame, x::Vector{Symbol}; kwargs...) = apply_func(X, Indicators.heikinashi, x, [:Open, :High, :Low, :Close]; kwargs...) | ||
Indicators.psar(X::TSFrame, x::Vector{Symbol}; kwargs...) = apply_func(X, Indicators.psar, x, [:PSAR]; kwargs...) | ||
Indicators.kst(X::TSFrame, x::Symbol; kwargs...) = apply_func(X, Indicators.kst, x, [:KST]; kwargs...) | ||
Indicators.wpr(X::TSFrame, x::Vector{Symbol}; kwargs...) = apply_func(X, Indicators.wpr, x, [:WPR]; kwargs...) | ||
Indicators.cci(X::TSFrame, x::Vector{Symbol}; kwargs...) = apply_func(X, Indicators.cci, x, [:CCI]; kwargs...) | ||
Indicators.stoch(X::TSFrame, x::Vector{Symbol}; kwargs...) = apply_func(X, Indicators.stoch, x, [:Stochastic, :Signal]; kwargs...) | ||
Indicators.smi(X::TSFrame, x::Vector{Symbol}; kwargs...) = apply_func(X, Indicators.smi, x, [:SMI, :Signal]; kwargs...) | ||
|
||
|
||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module TSFrames | ||
|
||
using DataFrames, Dates, ShiftedArrays, RecipesBase, RollingFunctions, Tables | ||
using DataFrames, Dates, ShiftedArrays, RecipesBase, RollingFunctions, Tables # , Indicators | ||
|
||
import Base.convert | ||
import Base.diff | ||
|
@@ -76,6 +76,8 @@ export TSFrame, | |
vcat, | ||
DataFrame, | ||
Date | ||
# sma, | ||
# runmean | ||
|
||
include("TSFrame.jl") | ||
include("utils.jl") | ||
|
@@ -97,5 +99,6 @@ include("to_period.jl") | |
include("vcat.jl") | ||
include("broadcasting.jl") | ||
include("tables.jl") | ||
# include("indicators.jl") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rm comments |
||
|
||
end # END module TSFrames |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using MarketData | ||
|
||
@testset "indicators_integration" begin | ||
sp500 = TSFrame(MarketData.yahoo("^GSPC")) | ||
date_from = Date(2021, 03, 1) | ||
date_to = Date(2023, 03, 1) | ||
sp500_2y = TSFrames.subset(sp500, date_from, date_to) | ||
@test sma(sp500_2y, :AdjClose) |> typeof == TSFrame | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make tests more diverse and meaningful, by using kwargs from upstream method |
||
@test runmean(sp500_2y, :Close) |> typeof == TSFrame | ||
@test trima(sp500_2y, :Close) |> typeof == TSFrame | ||
@test wma(sp500_2y, :AdjClose) |> typeof == TSFrame | ||
@test ema(sp500_2y, :AdjClose) |> typeof == TSFrame | ||
@test mma(sp500_2y, :AdjClose) |> typeof == TSFrame | ||
@test dema(sp500_2y, :AdjClose) |> typeof == TSFrame | ||
@test tema(sp500_2y, :AdjClose) |> typeof == TSFrame | ||
@test mama(sp500_2y, :AdjClose) |> typeof == TSFrame | ||
@test hma(sp500_2y, :AdjClose) |> typeof == TSFrame | ||
@test swma(sp500_2y, :AdjClose) |> typeof == TSFrame | ||
@test kama(sp500_2y, :AdjClose) |> typeof == TSFrame | ||
@test alma(sp500_2y, :AdjClose) |> typeof == TSFrame | ||
@test zlema(sp500_2y, :AdjClose) |> typeof == TSFrame | ||
@test hama(sp500_2y, :AdjClose) |> typeof == TSFrame | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To remove comment