forked from breaded-xyz/alphakit
-
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.
test: developing examples for optimize pkg
- Loading branch information
Showing
9 changed files
with
4,397 additions
and
9 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,14 @@ | ||
package market | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestReadKlinesFromCSV(t *testing.T) { | ||
|
||
prices, err := ReadKlinesFromCSV("testdata/BTCUSDT-1h-2021-Q1.csv") | ||
assert.NoError(t, err) | ||
assert.Len(t, prices, 2158) | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
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,53 @@ | ||
package optimize | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/thecolngroup/alphakit/market" | ||
"github.com/thecolngroup/alphakit/trader/trend" | ||
) | ||
|
||
func Example() { | ||
// Verbose error handling ommitted for brevity | ||
|
||
// Identify the bot (algo) to optimize by supplying a factory function | ||
// Here we're using the classic MA cross variant of trend bot | ||
bot := trend.MakeCrossBotFromConfig | ||
|
||
// Define the parameter space to optimize | ||
// Param names must match those expected by the MakeBot function passed to optimizer | ||
paramSpace := ParamMap{ | ||
"mafastlength": []float64{1, 10, 20, 30}, | ||
"maslowlength": []float64{30, 40, 50, 60}, | ||
"mmilength": []float64{200, 300}, | ||
} | ||
|
||
// Read price samples to use for optimization | ||
btcPriceSample, _ := market.ReadKlinesFromCSV("testdata/") | ||
ethPriceSample, _ := market.ReadKlinesFromCSV("testdata/") | ||
priceSamples := [][]market.Kline{btcPriceSample, ethPriceSample} | ||
|
||
// Create a new brute style optimizer | ||
optimizer := NewBruteOptimizer() | ||
optimizer.SampleSplitPct = 0.5 | ||
optimizer.WarmupBarCount = 300 | ||
optimizer.MakeBot = bot | ||
|
||
// Prepare the optimizer and get an estimate on the number of trials (backtests) required | ||
trialCount, _ := optimizer.Prepare(paramSpace, priceSamples) | ||
fmt.Printf("%d trials to run during optimization\n", trialCount) | ||
|
||
// Start the optimization process and monitor with a receive channel | ||
trials, _ := optimizer.Start(context.Background()) | ||
for range trials { | ||
} | ||
|
||
// Inspect the study to get the optimized param set and results | ||
study := optimizer.Study() | ||
optimaPSet := study.Validation[0] | ||
optimaResult := study.ValidationResults[optimaPSet.ID] | ||
|
||
// Output: Optima sharpe ratio is 0.0 | ||
fmt.Printf("Optima sharpe ratio is %.2f", optimaResult.Sharpe) | ||
} |
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