-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Envelope trend indicator and Envelope strategy are added. (#233)
# Describe Request Envelope trend indicator and Envelope strategy are added. Fixed #228 # Change Type New feature. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced a new `Envelope` indicator and multiple new strategies, enhancing the library's capabilities. - Added dedicated test data in CSV format for better validation of indicators and strategies. - **Documentation** - Added detailed documentation for the new `Envelope` type and its methods. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
- Loading branch information
Showing
12 changed files
with
1,262 additions
and
4 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 @@ | ||
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
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,117 @@ | ||
// Copyright (c) 2021-2024 Onur Cinar. | ||
// The source code is provided under GNU AGPLv3 License. | ||
// https://github.com/cinar/indicator | ||
|
||
package trend | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cinar/indicator/v2/asset" | ||
"github.com/cinar/indicator/v2/helper" | ||
"github.com/cinar/indicator/v2/strategy" | ||
"github.com/cinar/indicator/v2/trend" | ||
) | ||
|
||
// EnvelopeStrategy represents the configuration parameters for calculating the Envelope strategy. When the closing | ||
// is above the upper band suggests a Sell recommendation, and when the closing is below the lower band suggests a | ||
// buy recommendation. | ||
type EnvelopeStrategy struct { | ||
// Envelope is the envelope indicator instance. | ||
Envelope *trend.Envelope[float64] | ||
} | ||
|
||
// NewEnvelopeStrategy function initializes a new Envelope strategy with the default parameters. | ||
func NewEnvelopeStrategy() *EnvelopeStrategy { | ||
return NewEnvelopeStrategyWith( | ||
trend.NewEnvelopeWithSma[float64](), | ||
) | ||
} | ||
|
||
// NewEnvelopeStrategyWith function initializes a new Envelope strategy with the given Envelope instance. | ||
func NewEnvelopeStrategyWith(envelope *trend.Envelope[float64]) *EnvelopeStrategy { | ||
return &EnvelopeStrategy{ | ||
Envelope: envelope, | ||
} | ||
} | ||
|
||
// Name returns the name of the strategy. | ||
func (e *EnvelopeStrategy) Name() string { | ||
return fmt.Sprintf("Envelope Strategy (%s)", e.Envelope.String()) | ||
} | ||
|
||
// Compute processes the provided asset snapshots and generates a stream of actionable recommendations. | ||
func (e *EnvelopeStrategy) Compute(snapshots <-chan *asset.Snapshot) <-chan strategy.Action { | ||
closingsSplice := helper.Duplicate( | ||
asset.SnapshotsAsClosings(snapshots), | ||
2, | ||
) | ||
|
||
closingsSplice[1] = helper.Skip(closingsSplice[1], e.Envelope.IdlePeriod()) | ||
|
||
uppers, middles, lowers := e.Envelope.Compute(closingsSplice[0]) | ||
go helper.Drain(middles) | ||
|
||
actions := helper.Operate3(uppers, lowers, closingsSplice[1], func(upper, lower, closing float64) strategy.Action { | ||
// When the closing is below the lower band suggests a buy recommendation. | ||
if closing < lower { | ||
return strategy.Buy | ||
} | ||
|
||
// When the closing is above the upper band suggests a Sell recommendation. | ||
if closing > upper { | ||
return strategy.Sell | ||
} | ||
|
||
return strategy.Hold | ||
}) | ||
|
||
// Envelope start only after a full period. | ||
actions = helper.Shift(actions, e.Envelope.IdlePeriod(), strategy.Hold) | ||
|
||
return actions | ||
} | ||
|
||
// Report processes the provided asset snapshots and generates a report annotated with the recommended actions. | ||
func (e *EnvelopeStrategy) Report(c <-chan *asset.Snapshot) *helper.Report { | ||
// | ||
// snapshots[0] -> dates | ||
// snapshots[1] -> closings[0] -> closings | ||
// closings[1] -> envelope -> upper | ||
// -> middle | ||
// -> lower | ||
// snapshots[2] -> actions -> annotations | ||
// -> outcomes | ||
// | ||
snapshotsSplice := helper.Duplicate(c, 3) | ||
|
||
dates := helper.Skip( | ||
asset.SnapshotsAsDates(snapshotsSplice[0]), | ||
e.Envelope.IdlePeriod(), | ||
) | ||
|
||
closingsSplice := helper.Duplicate(asset.SnapshotsAsClosings(snapshotsSplice[1]), 2) | ||
closingsSplice[0] = helper.Skip(closingsSplice[0], e.Envelope.IdlePeriod()) | ||
|
||
uppers, middles, lowers := e.Envelope.Compute(closingsSplice[1]) | ||
|
||
actions, outcomes := strategy.ComputeWithOutcome(e, snapshotsSplice[2]) | ||
actions = helper.Skip(actions, e.Envelope.IdlePeriod()) | ||
outcomes = helper.Skip(outcomes, e.Envelope.IdlePeriod()) | ||
|
||
annotations := strategy.ActionsToAnnotations(actions) | ||
outcomes = helper.MultiplyBy(outcomes, 100) | ||
|
||
report := helper.NewReport(e.Name(), dates) | ||
report.AddChart() | ||
|
||
report.AddColumn(helper.NewNumericReportColumn("Close", closingsSplice[0])) | ||
report.AddColumn(helper.NewNumericReportColumn("Upper", uppers)) | ||
report.AddColumn(helper.NewNumericReportColumn("Middle", middles)) | ||
report.AddColumn(helper.NewNumericReportColumn("Lower", lowers)) | ||
report.AddColumn(helper.NewAnnotationReportColumn(annotations)) | ||
|
||
report.AddColumn(helper.NewNumericReportColumn("Outcome", outcomes), 1) | ||
|
||
return report | ||
} |
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,55 @@ | ||
// Copyright (c) 2021-2024 Onur Cinar. | ||
// The source code is provided under GNU AGPLv3 License. | ||
// https://github.com/cinar/indicator | ||
|
||
package trend_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/cinar/indicator/v2/asset" | ||
"github.com/cinar/indicator/v2/helper" | ||
"github.com/cinar/indicator/v2/strategy" | ||
"github.com/cinar/indicator/v2/strategy/trend" | ||
) | ||
|
||
func TestEnvelopeStrategy(t *testing.T) { | ||
snapshots, err := helper.ReadFromCsvFile[asset.Snapshot]("testdata/brk-b.csv", true) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
results, err := helper.ReadFromCsvFile[strategy.Result]("testdata/envelope_strategy.csv", true) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
expected := helper.Map(results, func(r *strategy.Result) strategy.Action { return r.Action }) | ||
|
||
envelope := trend.NewEnvelopeStrategy() | ||
actual := envelope.Compute(snapshots) | ||
|
||
err = helper.CheckEquals(actual, expected) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestEnvelopeStrategyReport(t *testing.T) { | ||
snapshots, err := helper.ReadFromCsvFile[asset.Snapshot]("testdata/brk-b.csv", true) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
envelope := trend.NewEnvelopeStrategy() | ||
report := envelope.Report(snapshots) | ||
|
||
fileName := "envelope_strategy.html" | ||
defer os.Remove(fileName) | ||
|
||
err = report.WriteToFile(fileName) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
Oops, something went wrong.