-
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Describe Request Inverse strategy is added. Fixed # (issue) # Change Type New strategy.
- Loading branch information
Showing
5 changed files
with
442 additions
and
1 deletion.
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,72 @@ | ||
// Copyright (c) 2021-2024 Onur Cinar. | ||
// The source code is provided under GNU AGPLv3 License. | ||
// https://github.com/cinar/indicator | ||
|
||
package decorator | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cinar/indicator/v2/asset" | ||
"github.com/cinar/indicator/v2/helper" | ||
"github.com/cinar/indicator/v2/strategy" | ||
) | ||
|
||
// InverseStrategy reverses the advice of another strategy. For example, if the original strategy suggests buying an | ||
// asset, InverseStrategy would recommend selling it. | ||
type InverseStrategy struct { | ||
strategy.Strategy | ||
|
||
// InnerStrategy is the inner strategy. | ||
InnerStrategy strategy.Strategy | ||
} | ||
|
||
// NewInverseStrategy function initializes a new inverse strategy instance. | ||
func NewInverseStrategy(innerStrategy strategy.Strategy) *InverseStrategy { | ||
return &InverseStrategy{ | ||
InnerStrategy: innerStrategy, | ||
} | ||
} | ||
|
||
// Name returns the name of the strategy. | ||
func (i *InverseStrategy) Name() string { | ||
return fmt.Sprintf("Inverse Strategy (%s)", i.InnerStrategy.Name()) | ||
} | ||
|
||
// Compute processes the provided asset snapshots and generates a stream of actionable recommendations. | ||
func (i *InverseStrategy) Compute(snapshots <-chan *asset.Snapshot) <-chan strategy.Action { | ||
return helper.Map(i.InnerStrategy.Compute(snapshots), func(action strategy.Action) strategy.Action { | ||
switch action { | ||
case strategy.Buy: | ||
return strategy.Sell | ||
|
||
case strategy.Sell: | ||
return strategy.Buy | ||
|
||
default: | ||
return strategy.Hold | ||
} | ||
}) | ||
} | ||
|
||
// Report processes the provided asset snapshots and generates a report annotated with the recommended actions. | ||
func (i *InverseStrategy) Report(c <-chan *asset.Snapshot) *helper.Report { | ||
snapshots := helper.Duplicate(c, 3) | ||
|
||
dates := asset.SnapshotsAsDates(snapshots[0]) | ||
closings := asset.SnapshotsAsClosings(snapshots[1]) | ||
|
||
actions, outcomes := strategy.ComputeWithOutcome(i, snapshots[2]) | ||
annotations := strategy.ActionsToAnnotations(actions) | ||
outcomes = helper.MultiplyBy(outcomes, 100) | ||
|
||
report := helper.NewReport(i.Name(), dates) | ||
report.AddChart() | ||
|
||
report.AddColumn(helper.NewNumericReportColumn("Close", closings)) | ||
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,60 @@ | ||
// Copyright (c) 2021-2024 Onur Cinar. | ||
// The source code is provided under GNU AGPLv3 License. | ||
// https://github.com/cinar/indicator | ||
|
||
package decorator_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/decorator" | ||
"github.com/cinar/indicator/v2/strategy/trend" | ||
) | ||
|
||
func TestInverseStrategy(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/inverse_strategy.csv", true) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
expected := helper.Map(results, func(r *strategy.Result) strategy.Action { return r.Action }) | ||
|
||
innerStrategy := trend.NewMacdStrategy() | ||
strategy := decorator.NewInverseStrategy(innerStrategy) | ||
|
||
actual := strategy.Compute(snapshots) | ||
|
||
err = helper.CheckEquals(actual, expected) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestInverseStrategyReport(t *testing.T) { | ||
snapshots, err := helper.ReadFromCsvFile[asset.Snapshot]("testdata/brk-b.csv", true) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
innerStrategy := trend.NewMacdStrategy() | ||
strategy := decorator.NewInverseStrategy(innerStrategy) | ||
|
||
report := strategy.Report(snapshots) | ||
|
||
fileName := "inverse_strategy.html" | ||
defer os.Remove(fileName) | ||
|
||
err = report.WriteToFile(fileName) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
Oops, something went wrong.