-
-
Notifications
You must be signed in to change notification settings - Fork 359
FEATURE: [indicator] porting SuperTrend to indicatorv2 #2365
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
Open
dboyliao
wants to merge
1
commit into
main
Choose a base branch
from
dboy/indicator/supertrend
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.
Open
Changes from all commits
Commits
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 hidden or 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 hidden or 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,108 @@ | ||
| package indicatorv2 | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/c9s/bbgo/pkg/types" | ||
| ) | ||
|
|
||
| type SuperTrendBand struct { | ||
| UppderBand float64 | ||
| LowerBand float64 | ||
| Direction int // 1 for uptrend, -1 for downtrend | ||
| Time time.Time | ||
| } | ||
|
|
||
| func (s *SuperTrendBand) Band() float64 { | ||
| if s.Direction == 1 { | ||
| return s.LowerBand | ||
| } | ||
| return s.UppderBand | ||
| } | ||
|
|
||
| type SuperTrendStream struct { | ||
| *types.Float64Series | ||
|
|
||
| source KLineSubscription | ||
| atr *ATRStream | ||
| multiplier float64 | ||
| entities []*SuperTrendBand | ||
| } | ||
|
|
||
| func (st *SuperTrendStream) update(kline types.KLine) { | ||
| currentATR := st.atr.Last(0) | ||
| if currentATR == 0 { | ||
| // not enough data, skip | ||
| return | ||
| } | ||
| hl2 := (kline.High.Float64() + kline.Low.Float64()) / 2 | ||
| basicUpperBand := hl2 + st.multiplier*currentATR | ||
| basicLowerBand := hl2 - st.multiplier*currentATR | ||
|
|
||
| prevUpperBand := 0.0 | ||
| prevLowerBand := 0.0 | ||
| prevClose := st.source.Last(1).Close.Float64() | ||
| prevDirection := 1 | ||
| if len(st.entities) > 0 { | ||
| prevEntity := st.entities[len(st.entities)-1] | ||
| prevUpperBand = prevEntity.UppderBand | ||
| prevLowerBand = prevEntity.LowerBand | ||
| prevDirection = st.entities[len(st.entities)-1].Direction | ||
| } | ||
|
|
||
| // final upper band and lower band calculation | ||
| // if we have a lower/higher basic band or the previous close crosses the bands -> adjust the bands | ||
| finalUpperBand := prevUpperBand | ||
| finalLowerBand := prevLowerBand | ||
| if basicUpperBand < prevUpperBand || prevClose > prevUpperBand { | ||
| finalUpperBand = basicUpperBand | ||
| } | ||
| if basicLowerBand > prevLowerBand || prevClose < prevLowerBand { | ||
| finalLowerBand = basicLowerBand | ||
| } | ||
|
|
||
| // trend direction | ||
| direction := 1 | ||
| currentClose := kline.Close.Float64() | ||
| if prevDirection == 1 { | ||
| // was bullish | ||
| if currentClose < finalLowerBand { | ||
| // switch to bearish | ||
| direction = -1 | ||
| } else { | ||
| // stay bullish | ||
| direction = 1 | ||
| } | ||
| } else { | ||
| // was bearish | ||
| if currentClose > finalUpperBand { | ||
| // switch to bullish | ||
| direction = 1 | ||
| } else { | ||
| // stay bearish | ||
| direction = -1 | ||
| } | ||
| } | ||
|
|
||
| e := &SuperTrendBand{ | ||
| UppderBand: finalUpperBand, | ||
| LowerBand: finalLowerBand, | ||
| Direction: direction, | ||
| Time: kline.EndTime.Time(), | ||
| } | ||
| st.entities = append(st.entities, e) | ||
| st.PushAndEmit(e.Band()) | ||
| } | ||
|
|
||
| func SuperTrend(source KLineSubscription, window int, multiplier float64) *SuperTrendStream { | ||
| atr := ATR2(source, window) | ||
| st := &SuperTrendStream{ | ||
| Float64Series: types.NewFloat64Series(), | ||
| source: source, | ||
| atr: atr, | ||
| multiplier: multiplier, | ||
| entities: make([]*SuperTrendBand, 0), | ||
| } | ||
| source.AddSubscriber(st.update) | ||
| return st | ||
| } | ||
Oops, something went wrong.
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.
we can just store one band value and use the direction to remember which side it is ?
Uh oh!
There was an error while loading. Please reload this page.
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.
I am thinking we can draw the tunnel if we have both upper and lower band.
That's why I have a method
.Band()to do the side selection for convenience.