Skip to content

Commit

Permalink
0.3.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaime Lopez committed Jun 27, 2024
1 parent 60686af commit ec3ce06
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@
0.3.3: 2024-06-24

* ToJSON function for History

0.3.4: 2024-06-26

* Functions to manage list of symbols included
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/apache/arrow/go/v14 v14.0.2 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/flatbuffers v23.5.26+incompatible // indirect
github.com/jailop/yfgo v0.0.0-20240625010026-e622d8117d4c // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
Expand Down
102 changes: 102 additions & 0 deletions listmgr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package yfgo

import (
"log"
"os"
"bufio"
"strings"
)

func SymbolList() []string {
symlist := make([]string, 0)
listPath, err := FilePath("list.txt")
if err != nil {
log.Fatal(err)
return symlist
}
if !FileExists(listPath) {
log.Fatal("List of ticker symbols doesn't exist")
log.Print("Create a new one at ", listPath)
return symlist
}
file, err := os.Open(listPath)
if err != nil {
log.Fatal(err)
return symlist
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
vs := strings.Split(scanner.Text(), " ")
for _, sym := range vs {
symlist = append(symlist, strings.TrimSpace(sym))
}
}
if err = scanner.Err(); err != nil {
log.Fatal(err)
}
return symlist
}

func SymbolExists(sym string) bool {
symlist := SymbolList()
flag := false
for _, val := range symlist {
if sym == strings.TrimSpace(val) {
flag = true
break
}
}
return flag
}

func SymbolAdd(sym string) {
if SymbolExists(sym) {
log.Print("SymboAdd: symbol already exists")
return
}
listPath, err := FilePath("list.txt")
file, err := os.OpenFile(listPath, os.O_APPEND | os.O_CREATE | os.O_WRONLY, 0644)
if err != nil {
log.Fatal("SymbolAdd: File cannot be openned")
return
}
defer file.Close()
if _, err = file.WriteString(strings.TrimSpace(sym) + "\n"); err != nil {
log.Fatal("SymbolAdd: Symbol cannot be appended")
log.Fatal(err)
}
}

func SymbolListWrite(symlist []string) {
listPath, err := FilePath("list.txt")
file, err := os.OpenFile(listPath, os.O_RDWR | os.O_CREATE, 0644)
if err != nil {
log.Fatal("SymbolAdd: File cannot be openned")
return
}
for _, val := range symlist {
if _, err = file.WriteString(strings.TrimSpace(val) + "\n"); err != nil {
log.Fatal("SymbolAdd: Symbol cannot be appended")
}
}
println("Here >>>")
if err = file.Close(); err != nil {
log.Fatal(err)
}
}

func SymbolRemove(sym string) bool {
flag := false
symlist := SymbolList()
newlist := make([]string, 0)
for _, val := range symlist {
if strings.TrimSpace(val) == strings.TrimSpace(sym) {
flag = true
} else {
newlist = append(newlist, val)
}
}
SymbolListWrite(newlist)
return flag
}
24 changes: 24 additions & 0 deletions listmgr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package yfgo

import (
"testing"
)

func TestSymbolList(t *testing.T) {
symlist := SymbolList()
if len(symlist) == 0 {
t.Errorf("The list of symbols is empy")
}
}

func TestSymbolAddRemove(t *testing.T) {
sym := "__TEST__"
SymbolAdd(sym)
if !SymbolExists(sym) {
t.Errorf("Symbol was not added")
}
SymbolRemove(sym)
if SymbolExists(sym) {
t.Errorf("Symbol was not removed")
}
}
3 changes: 0 additions & 3 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ func TestQuery(t *testing.T) {
if err != nil {
t.Errorf("Query was not succesful %s", err)
}
if len(history.Time) == 0 {
t.Errorf("No data was retrieved")
}
flag := false
for i := range len(history.Close) {
if math.IsNaN(history.Close[i]) {
Expand Down

0 comments on commit ec3ce06

Please sign in to comment.