Skip to content
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

fix: refactor codebase #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 42 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ import (
func main() {
randomOdd := 4.051

index1, odd1, err := bfutils.OddShift(bfutils.RoundType_Floor, randomOdd, 10)
index1, odd1, err := bfutils.OddShift(randomOdd, 10, bfutils.RoundType_Floor)
if err != nil {
panic(err)
}

fmt.Printf("Odd1: %.2f - position in the ladder: %d\n", odd1, index1+1)

index2, odd2, err := bfutils.OddShift(bfutils.RoundType_Floor, randomOdd, -10)
index2, odd2, err := bfutils.OddShift(randomOdd, -10, bfutils.RoundType_Floor)
if err != nil {
panic(err)
}

fmt.Printf("Odd2: %.2f - position in the ladder: %d\n", odd2, index2+1)

ticksDiff, err := bfutils.OddsTicksDiff(bfutils.RoundType_Floor, odd1, odd2)
ticksDiff, err := bfutils.OddsTicksDiff(odd1, odd2, bfutils.RoundType_Floor)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -82,29 +82,29 @@ import (
)

func main() {
selection := betting.Selection{
Bets: []betting.Bet{
{Type: betting.BetType_Back, Odd: 4, Amount: 5},
{Type: betting.BetType_Lay, Odd: 3, Amount: 5},
{Type: betting.BetType_Back, Odd: 3.5, Amount: 10},
{Type: betting.BetType_Lay, Odd: 3.2, Amount: 10},
},
CurrentBackOdd: 2.4,
CurrentLayOdd: 2.42,
}

bet, err := betting.GreenBookSelection(selection)
if err != nil {
panic(err)
}

fmt.Printf("In order to green book this selection, put a {%s} bet at {%.2f} for £%.2f.\n",
bet.Type, bet.Odd, bet.Amount)

fmt.Printf("P&L\n")
fmt.Printf("---\n")
fmt.Printf("If this selection wins: £%.2f\n", bet.WinPL)
fmt.Printf("If this selection loses: £%.2f\n", bet.LosePL)
selection := betting.Selection{
Bets: []betting.Bet{
{Type: betting.BetType_Back, Odd: 4, Amount: 5},
{Type: betting.BetType_Lay, Odd: 3, Amount: 5},
{Type: betting.BetType_Back, Odd: 3.5, Amount: 10},
{Type: betting.BetType_Lay, Odd: 3.2, Amount: 10},
},
CurrentBackOdd: 2.4,
CurrentLayOdd: 2.42,
}

bet, err := betting.GreenBookSelection(selection)
if err != nil {
panic(err)
}

fmt.Printf("In order to green book this selection, put a {%s} bet at {%.2f} for £%.2f.\n",
bet.Type, bet.Odd, bet.Amount)

fmt.Printf("P&L\n")
fmt.Printf("---\n")
fmt.Printf("If this selection wins: £%.2f\n", bet.WinPL)
fmt.Printf("If this selection loses: £%.2f\n", bet.LosePL)
}

```
Expand All @@ -127,16 +127,16 @@ import (
)

func main() {
raceBetfairName := "2m3f Hcap"
raceBetfairName := "2m3f Hcap"

name, distance := horserace.GetClassAndDistance(raceBetfairName)
class, err := horserace.GetClassificationFromAbbrev(name)
if err != nil {
panic(err)
}
name, distance := horserace.GetClassAndDistance(raceBetfairName)
class, err := horserace.GetClassificationFromAbbrev(name)
if err != nil {
panic(err)
}

fmt.Printf("Race classification: %s\n", class)
fmt.Printf("Race distance: %s\n", distance)
fmt.Printf("Race classification: %s\n", class)
fmt.Printf("Race distance: %s\n", distance)
}

```
Expand All @@ -159,16 +159,16 @@ import (
)

func main() {
raceDistance := "2m5f100y"
raceDistance := "2m5f100y"

d, err := conversion.ParseDistance(raceDistance)
if err != nil {
panic(err)
}
d, err := conversion.ParseDistance(raceDistance)
if err != nil {
panic(err)
}

fmt.Printf("This race distance [%s] has %d miles, %d furlongs and %d yards.\n",
d, d.Miles, d.Furlongs, d.Yards)
fmt.Printf("This race distance in meters is: %.2f\n", d.ToMeters())
fmt.Printf("This race distance [%s] has %d miles, %d furlongs and %d yards.\n",
d, d.Miles, d.Furlongs, d.Yards)
fmt.Printf("This race distance in meters is: %.2f\n", d.ToMeters())
}

```
Expand Down
6 changes: 3 additions & 3 deletions betting/betting.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func SelectionIsEdged(bets []Bet) (bool, error) {
return false, nil
}

// GreenBookSelection computes what bet to make in order to greenbook a selection.
// GreenBookSelection computes what bet to make in order to achieve a greenbook in that selection.
func GreenBookSelection(selection Selection) (bet Bet, err error) {
layAvgOdd := 0.0
layAmount := 0.0
Expand All @@ -129,7 +129,7 @@ func GreenBookSelection(selection Selection) (bet Bet, err error) {
currentBackOdd := selection.CurrentBackOdd
currentLayOdd := selection.CurrentLayOdd

if bets == nil || len(bets) == 0 {
if len(bets) == 0 {
return bet, fmt.Errorf("no bets in this selection")
}

Expand Down Expand Up @@ -203,7 +203,7 @@ func GreenBookSelection(selection Selection) (bet Bet, err error) {
// return
// }

// GreenBookAtAllOdds returns the ladder with P&L and volumed matched by bets.
// GreenBookAtAllOdds returns the ladder with P&L and volume matched by bets.
func GreenBookAtAllOdds(bets []Bet) ([]LadderStep, error) {
layAvgOdd := 0.0
layAmount := 0.0
Expand Down
2 changes: 1 addition & 1 deletion betting/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Selection struct {
CurrentLayOdd float64
}

// LadderStep represents a trading ladder.
// LadderStep represents a step in the trading ladder for a given selection.
type LadderStep struct {
// Odd in the market.
Odd float64
Expand Down
4 changes: 2 additions & 2 deletions conversion/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/gustavooferreira/bfutils/conversion"
)

// This example parses a horse race's distance and then prints a human friendly
// message showing the distance. It also converts the distance into meters.
// This example parses a horse race's distance and then prints a human friendly message showing the distance.
// It also converts the distance into meters.
func Example_a() {
raceDistance := "2m5f100y"

Expand Down
6 changes: 3 additions & 3 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ import (
func Example_a() {
randomOdd := 4.051

index1, odd1, err := bfutils.OddShift(bfutils.RoundType_Floor, randomOdd, 10)
index1, odd1, err := bfutils.OddShift(randomOdd, 10, bfutils.RoundType_Floor)
if err != nil {
panic(err)
}

fmt.Printf("Odd1: %.2f - position in the ladder: %d\n", odd1, index1+1)

index2, odd2, err := bfutils.OddShift(bfutils.RoundType_Floor, randomOdd, -10)
index2, odd2, err := bfutils.OddShift(randomOdd, -10, bfutils.RoundType_Floor)
if err != nil {
panic(err)
}

fmt.Printf("Odd2: %.2f - position in the ladder: %d\n", odd2, index2+1)

ticksDiff, err := bfutils.OddsTicksDiff(bfutils.RoundType_Floor, odd1, odd2)
ticksDiff, err := bfutils.OddsTicksDiff(odd1, odd2, bfutils.RoundType_Floor)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions horserace/classifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package horserace
// disregard Novice, Beginners etc as using the three abbreviations will not fit.
// Therefore Hcap Hrd, or Hcap Chs.

// classToAbbrev is a map from Classification to Abbreviation
// classToAbbrev is a map from Classification to Abbreviation.
var classToAbbrev = map[string]string{
"Listed Race": "Listed",
"Group 1": "Grp1",
Expand Down Expand Up @@ -36,7 +36,7 @@ var classToAbbrev = map[string]string{
"(Non Of the Above)": "Stks",
}

// abbrevToClass is a map from Abbreviation to Classification
// abbrevToClass is a map from Abbreviation to Classification.
var abbrevToClass = map[string][]string{
"Listed": {"Listed Race"},
"Grp1": {"Group 1"},
Expand Down
3 changes: 1 addition & 2 deletions horserace/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
"github.com/gustavooferreira/bfutils/horserace"
)

// This example parses a horse race's betfair market name and returns the distance and
// the race classification.
// This example parses a horse race's betfair market name and returns the distance and the race classification.
func Example_a() {
raceBetfairName := "2m3f Hcap"

Expand Down
8 changes: 4 additions & 4 deletions horserace/racecourses.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package horserace

// ukTrackToAbbrev is a map from Track to Abbreviation
// ukTrackToAbbrev is a map from Track to Abbreviation.
var ukTrackToAbbrev = map[string]string{
"Aintree": "Aint",
"Ascot": "Ascot",
Expand Down Expand Up @@ -65,7 +65,7 @@ var ukTrackToAbbrev = map[string]string{
"York": "York",
}

// ukAbbrevToTrack is a map from Abbreviation to Track
// ukAbbrevToTrack is a map from Abbreviation to Track.
var ukAbbrevToTrack = map[string]string{
"Aint": "Aintree",
"Ascot": "Ascot",
Expand Down Expand Up @@ -130,7 +130,7 @@ var ukAbbrevToTrack = map[string]string{
"York": "York",
}

// ireTrackToAbbrev is a map from Track to Abbreviation
// ireTrackToAbbrev is a map from Track to Abbreviation.
var ireTrackToAbbrev = map[string]string{
"Ballinrobe": "Ballin",
"Bellewstown": "Belle",
Expand Down Expand Up @@ -162,7 +162,7 @@ var ireTrackToAbbrev = map[string]string{
"Wexford": "Wex",
}

// ireAbbrevToTrack is a map from Abbreviation to Track
// ireAbbrevToTrack is a map from Abbreviation to Track.
var ireAbbrevToTrack = map[string]string{
"Ballin": "Ballinrobe",
"Belle": "Bellewstown",
Expand Down
2 changes: 1 addition & 1 deletion internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package internal

import "math"

// EqualWithTolerance is a helper function and constant to help estimate whether odd matches or not
// EqualWithTolerance is a helper function to help estimate whether a given odd matches or not (within a threshold).
func EqualWithTolerance(a float64, b float64) bool {
const float64EqualityThreshold = 1e-9
return math.Abs(a-b) <= float64EqualityThreshold
Expand Down
30 changes: 16 additions & 14 deletions internal/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,29 @@ package internal_test
import (
"testing"

"github.com/gustavooferreira/bfutils/internal"
"github.com/stretchr/testify/assert"

"github.com/gustavooferreira/bfutils/internal"
)

func TestEqualWithTolerance(t *testing.T) {
tests := map[string]struct {
a float64
b float64
expected bool
testCases := []struct {
name string
a float64
b float64
expectedResult bool
}{
"compare 1": {a: 0, b: 0, expected: true},
"compare 2": {a: 10.0000000001, b: 10.0000000002, expected: true},
"compare 3": {a: 9.999999999999, b: 10, expected: true},
"compare 4": {a: 5, b: 10, expected: false},
"compare 5": {a: 9.555555, b: 9.5555556, expected: false},
{name: "compare 1", a: 0, b: 0, expectedResult: true},
{name: "compare 2", a: 10.0000000001, b: 10.0000000002, expectedResult: true},
{name: "compare 3", a: 9.999999999999, b: 10, expectedResult: true},
{name: "compare 4", a: 5, b: 10, expectedResult: false},
{name: "compare 5", a: 9.555555, b: 9.5555556, expectedResult: false},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
value := internal.EqualWithTolerance(test.a, test.b)
assert.Equal(t, test.expected, value, "expected boolean")
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
value := internal.EqualWithTolerance(tc.a, tc.b)
assert.Equal(t, tc.expectedResult, value)
})
}
}
Loading