-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request c9s#799 from andycheng123/improve/supertrend-strategy
Improve supertrend strategy
- Loading branch information
Showing
5 changed files
with
320 additions
and
111 deletions.
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
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,65 @@ | ||
package supertrend | ||
|
||
import ( | ||
"github.com/c9s/bbgo/pkg/bbgo" | ||
"github.com/c9s/bbgo/pkg/indicator" | ||
"github.com/c9s/bbgo/pkg/types" | ||
) | ||
|
||
type DoubleDema struct { | ||
Interval types.Interval `json:"interval"` | ||
|
||
// FastDEMAWindow DEMA window for checking breakout | ||
FastDEMAWindow int `json:"fastDEMAWindow"` | ||
// SlowDEMAWindow DEMA window for checking breakout | ||
SlowDEMAWindow int `json:"slowDEMAWindow"` | ||
fastDEMA *indicator.DEMA | ||
slowDEMA *indicator.DEMA | ||
} | ||
|
||
// getDemaSignal get current DEMA signal | ||
func (dd *DoubleDema) getDemaSignal(openPrice float64, closePrice float64) types.Direction { | ||
var demaSignal types.Direction = types.DirectionNone | ||
|
||
if closePrice > dd.fastDEMA.Last() && closePrice > dd.slowDEMA.Last() && !(openPrice > dd.fastDEMA.Last() && openPrice > dd.slowDEMA.Last()) { | ||
demaSignal = types.DirectionUp | ||
} else if closePrice < dd.fastDEMA.Last() && closePrice < dd.slowDEMA.Last() && !(openPrice < dd.fastDEMA.Last() && openPrice < dd.slowDEMA.Last()) { | ||
demaSignal = types.DirectionDown | ||
} | ||
|
||
return demaSignal | ||
} | ||
|
||
// preloadDema preloads DEMA indicators | ||
func (dd *DoubleDema) preloadDema(kLineStore *bbgo.MarketDataStore) { | ||
if klines, ok := kLineStore.KLinesOfInterval(dd.fastDEMA.Interval); ok { | ||
for i := 0; i < len(*klines); i++ { | ||
dd.fastDEMA.Update((*klines)[i].GetClose().Float64()) | ||
} | ||
} | ||
if klines, ok := kLineStore.KLinesOfInterval(dd.slowDEMA.Interval); ok { | ||
for i := 0; i < len(*klines); i++ { | ||
dd.slowDEMA.Update((*klines)[i].GetClose().Float64()) | ||
} | ||
} | ||
} | ||
|
||
// setupDoubleDema initializes double DEMA indicators | ||
func (dd *DoubleDema) setupDoubleDema(kLineStore *bbgo.MarketDataStore, interval types.Interval) { | ||
dd.Interval = interval | ||
|
||
// DEMA | ||
if dd.FastDEMAWindow == 0 { | ||
dd.FastDEMAWindow = 144 | ||
} | ||
dd.fastDEMA = &indicator.DEMA{IntervalWindow: types.IntervalWindow{Interval: dd.Interval, Window: dd.FastDEMAWindow}} | ||
dd.fastDEMA.Bind(kLineStore) | ||
|
||
if dd.SlowDEMAWindow == 0 { | ||
dd.SlowDEMAWindow = 169 | ||
} | ||
dd.slowDEMA = &indicator.DEMA{IntervalWindow: types.IntervalWindow{Interval: dd.Interval, Window: dd.SlowDEMAWindow}} | ||
dd.slowDEMA.Bind(kLineStore) | ||
|
||
dd.preloadDema(kLineStore) | ||
} |
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,73 @@ | ||
package supertrend | ||
|
||
import ( | ||
"github.com/c9s/bbgo/pkg/bbgo" | ||
"github.com/c9s/bbgo/pkg/indicator" | ||
"github.com/c9s/bbgo/pkg/types" | ||
) | ||
|
||
// LinGre is Linear Regression baseline | ||
type LinGre struct { | ||
types.IntervalWindow | ||
baseLineSlope float64 | ||
} | ||
|
||
// Update Linear Regression baseline slope | ||
func (lg *LinGre) Update(klines []types.KLine) { | ||
if len(klines) < lg.Window { | ||
lg.baseLineSlope = 0 | ||
return | ||
} | ||
|
||
var sumX, sumY, sumXSqr, sumXY float64 = 0, 0, 0, 0 | ||
end := len(klines) - 1 // The last kline | ||
for i := end; i >= end-lg.Window+1; i-- { | ||
val := klines[i].GetClose().Float64() | ||
per := float64(end - i + 1) | ||
sumX += per | ||
sumY += val | ||
sumXSqr += per * per | ||
sumXY += val * per | ||
} | ||
length := float64(lg.Window) | ||
slope := (length*sumXY - sumX*sumY) / (length*sumXSqr - sumX*sumX) | ||
average := sumY / length | ||
endPrice := average - slope*sumX/length + slope | ||
startPrice := endPrice + slope*(length-1) | ||
lg.baseLineSlope = (length - 1) / (endPrice - startPrice) | ||
|
||
log.Debugf("linear regression baseline slope: %f", lg.baseLineSlope) | ||
} | ||
|
||
func (lg *LinGre) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) { | ||
if lg.Interval != interval { | ||
return | ||
} | ||
|
||
lg.Update(window) | ||
} | ||
|
||
func (lg *LinGre) Bind(updater indicator.KLineWindowUpdater) { | ||
updater.OnKLineWindowUpdate(lg.handleKLineWindowUpdate) | ||
} | ||
|
||
// GetSignal get linear regression signal | ||
func (lg *LinGre) GetSignal() types.Direction { | ||
var lgSignal types.Direction = types.DirectionNone | ||
|
||
switch { | ||
case lg.baseLineSlope > 0: | ||
lgSignal = types.DirectionUp | ||
case lg.baseLineSlope < 0: | ||
lgSignal = types.DirectionDown | ||
} | ||
|
||
return lgSignal | ||
} | ||
|
||
// preloadLinGre preloads linear regression indicator | ||
func (lg *LinGre) preload(kLineStore *bbgo.MarketDataStore) { | ||
if klines, ok := kLineStore.KLinesOfInterval(lg.Interval); ok { | ||
lg.Update((*klines)[0:]) | ||
} | ||
} |
Oops, something went wrong.