-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbol.go
69 lines (61 loc) · 1.28 KB
/
symbol.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package ddf
import (
"fmt"
"regexp"
"strconv"
"strings"
)
type SymbolType int
const (
Unknown SymbolType = 0
Future SymbolType = 1
FutureOption SymbolType = 2
)
type Symbol struct {
Type SymbolType
Root string
Month string
Year int
Strike int
CallPut string
}
func ParseSymbol(s string) (Symbol, error) {
var (
symbol Symbol
err error
)
if len(s) < 1 {
return symbol, fmt.Errorf("Invalid symbol length (0)")
}
switch ch := s[len(s)-1:][0]; {
case ch >= '0' && ch <= '9':
symbol.Type = Future
yr := ""
Loop:
for i := len(s) - 1; i > 0; i-- {
switch ch2 := s[i : i+1][0]; {
case ch2 >= '0' && ch2 <= '9':
yr = string(ch2) + yr
default:
symbol.Year, _ = strconv.Atoi(yr)
symbol.Month = string(ch2)
symbol.Root = strings.TrimSpace(s[0:i])
break Loop // regular break jumps out of the switch
}
}
default:
// Futures option, and others ....
var reFutOpt = regexp.MustCompile(`(?i)^(.{1,2})([A-Z])([0-9]{1,4})([A-Z])$`)
arr := reFutOpt.FindStringSubmatch(s)
if len(arr) == 5 { // original + 4 parts
symbol.Type = FutureOption
symbol.Root = arr[1]
symbol.Month = arr[2]
symbol.Strike, _ = strconv.Atoi(arr[3])
symbol.CallPut = arr[4]
} else {
fmt.Println("???", s)
}
}
return symbol, err
}