-
Notifications
You must be signed in to change notification settings - Fork 0
Fix percentChange formula and Sharpe ratio calculation #82
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package calculator | ||
|
|
||
| import ( | ||
| "math" | ||
| "testing" | ||
| ) | ||
|
|
||
| func Test_percentChange(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| end float64 | ||
| start float64 | ||
| expected float64 | ||
| }{ | ||
| { | ||
| name: "simple increase", | ||
| end: 110, | ||
| start: 100, | ||
| expected: 10.0, | ||
| }, | ||
| { | ||
| name: "simple decrease", | ||
| end: 90, | ||
| start: 100, | ||
| expected: -10.0, | ||
| }, | ||
| { | ||
| name: "no change", | ||
| end: 100, | ||
| start: 100, | ||
| expected: 0.0, | ||
| }, | ||
| { | ||
| name: "double", | ||
| end: 200, | ||
| start: 100, | ||
| expected: 100.0, | ||
| }, | ||
| { | ||
| name: "50 percent drop", | ||
| end: 50, | ||
| start: 100, | ||
| expected: -50.0, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got := percentChange(tt.end, tt.start) | ||
| if math.Abs(got-tt.expected) > 0.0001 { | ||
| t.Errorf("percentChange(%v, %v) = %v, want %v", tt.end, tt.start, got, tt.expected) | ||
| } | ||
| }) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package data | ||
|
|
||
| import ( | ||
| "math" | ||
| "testing" | ||
| ) | ||
|
|
||
| func Test_percentChange(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| end float64 | ||
| start float64 | ||
| expected float64 | ||
| }{ | ||
| { | ||
| name: "simple increase", | ||
| end: 110, | ||
| start: 100, | ||
| expected: 10.0, | ||
| }, | ||
| { | ||
| name: "simple decrease", | ||
| end: 90, | ||
| start: 100, | ||
| expected: -10.0, | ||
| }, | ||
| { | ||
| name: "no change", | ||
| end: 100, | ||
| start: 100, | ||
| expected: 0.0, | ||
| }, | ||
| { | ||
| name: "double", | ||
| end: 200, | ||
| start: 100, | ||
| expected: 100.0, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got := percentChange(tt.end, tt.start) | ||
| if math.Abs(got-tt.expected) > 0.0001 { | ||
| t.Errorf("percentChange(%v, %v) = %v, want %v", tt.end, tt.start, got, tt.expected) | ||
| } | ||
| }) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -97,7 +97,7 @@ func (pr *PriceCache) Get(symbol string, date time.Time) (float64, error) { | |||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| func percentChange(end, start float64) float64 { | ||||||||||||||||||||||
| return ((end - start) / end) * 100 | ||||||||||||||||||||||
| return ((end - start) / start) * 100 | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Comment on lines
99
to
101
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a zero-denominator guard in Line 100 divides by 💡 Proposed fix func percentChange(end, start float64) float64 {
+ if start == 0 {
+ return math.NaN()
+ }
return ((end - start) / start) * 100
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| func stdevsFromPriceMap(minMaxMap map[string]*minMax, priceCache map[string]map[string]float64, stdevInputs []LoadStdevCacheInput, tradingDays []time.Time) (*stdevCache, error) { | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
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.
Guard zero-volatility cases before Sharpe division.
Line 57 can emit
Inf/NaNwhenannualizedStdev == 0, and this value flows to persistence/API consumers.💡 Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents