Skip to content

Commit

Permalink
Update to GCT script library (thrasher-corp#496)
Browse files Browse the repository at this point in the history
* * Adds script link to GCT logger package
* Adds ability to save data as csv via script

* addr nits

* go mod tidy

* add glorious suggestion

* rm unused function

* fix linter issues

* clean up some issues

* Add in configuration fields to object for reflection to the csv file

* RM line :D

* address nits

* update to check for target already being set and add more test coverage

* force usage of .csv file extention && append date to client filename as to not overwrite file if collision occurs

* fix whoopsie

* linter issues

* purge getter methods

* Added glorious suggestion

* go mod tidy after merge

* niterinos
  • Loading branch information
shazbert authored May 14, 2020
1 parent bfab151 commit 0adf39d
Show file tree
Hide file tree
Showing 29 changed files with 944 additions and 75 deletions.
18 changes: 0 additions & 18 deletions common/common.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package common

import (
"encoding/csv"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -262,23 +261,6 @@ func ExtractPort(host string) int {
return port
}

// OutputCSV dumps data into a file as comma-separated values
func OutputCSV(filePath string, data [][]string) error {
file, err := os.Create(filePath)
if err != nil {
return err
}

writer := csv.NewWriter(file)
if err = writer.WriteAll(data); err != nil {
file.Close()
return err
}

file.Close()
return nil
}

// GetURIPath returns the path of a URL given a URI
func GetURIPath(uri string) string {
urip, err := url.Parse(uri)
Expand Down
17 changes: 0 additions & 17 deletions common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,23 +314,6 @@ func TestExtractPort(t *testing.T) {
}
}

func TestOutputCSV(t *testing.T) {
path := "../testdata/dump"
var data [][]string
rowOne := []string{"Appended", "to", "two", "dimensional", "array"}
rowTwo := []string{"Appended", "to", "two", "dimensional", "array", "two"}
data = append(data, rowOne, rowTwo)

err := OutputCSV(path, data)
if err != nil {
t.Errorf("common OutputCSV error: %s", err)
}
err = OutputCSV("/:::notapath:::", data)
if err == nil {
t.Error("common OutputCSV, tried writing to invalid path")
}
}

func TestGetURIPath(t *testing.T) {
t.Parallel()
// mapping of input vs expected result
Expand Down
33 changes: 33 additions & 0 deletions common/file/file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package file

import (
"bytes"
"encoding/csv"
"errors"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -58,3 +61,33 @@ func Exists(name string) bool {
_, err := os.Stat(name)
return !os.IsNotExist(err)
}

// WriteAsCSV takes a table of records and writes it as CSV
func WriteAsCSV(filename string, records [][]string) error {
if len(records) == 0 {
return errors.New("no records in matrix")
}

buf := bytes.Buffer{}
w := csv.NewWriter(&buf)

alignment := len(records[0])
for i := range records {
if len(records[i]) != alignment {
return errors.New("incorrect alignment")
}

err := w.Write(records[i])
if err != nil {
return err
}
}

w.Flush()

err := w.Error()
if err != nil {
return err
}
return Write(filename, buf.Bytes())
}
60 changes: 60 additions & 0 deletions common/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,63 @@ func TestExists(t *testing.T) {
t.Errorf("unable to remove %s, manual deletion is required", tmpFile)
}
}

func TestWriteAsCSV(t *testing.T) {
tester := func(in string, data [][]string) error {
err := WriteAsCSV(in, data)
if err != nil {
return err
}
return os.Remove(in)
}

type testTable struct {
InFile string
Payload [][]string
ErrExpected bool
}

records := [][]string{
{"title", "first_name", "last_name"},
{"King", "Robert", "Baratheon"},
{"Lord Regent of the Seven Kingdoms", "Eddard", "Stark"},
{"Lord of Baelish Castle", "Petyr", "Baelish"},
}

missAligned := [][]string{
{"first_name", "last_name", "username"},
{"Sup", "bra"},
}

testFile, err := ioutil.TempFile(os.TempDir(), "gct-csv-test.*.csv")
if err != nil {
t.Fatal(err)
}
testFile.Close()
defer os.Remove(testFile.Name())

tests := []testTable{
{InFile: testFile.Name(), Payload: nil, ErrExpected: true},
{InFile: testFile.Name(), Payload: records, ErrExpected: false},
{InFile: testFile.Name(), Payload: missAligned, ErrExpected: true},
}
switch runtime.GOOS {
case "windows":
tests = append(tests,
testTable{InFile: "*", Payload: [][]string{}, ErrExpected: true},
testTable{InFile: "*", Payload: nil, ErrExpected: true},
)
default:
tests = append(tests,
testTable{InFile: "", Payload: [][]string{}, ErrExpected: true},
testTable{InFile: "", Payload: nil, ErrExpected: true},
)
}

for x := range tests {
err := tester(tests[x].InFile, tests[x].Payload)
if err != nil && !tests[x].ErrExpected {
t.Errorf("Test %d failed, unexpected err %s\n", x, err)
}
}
}
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,12 @@ func (c *Config) checkGCTScriptConfig() error {
return err
}

outputPath := filepath.Join(scriptPath, "output")
err = common.CreateDir(outputPath)
if err != nil {
return err
}

gctscript.ScriptPath = scriptPath
gctscript.GCTScriptConfig = &c.GCTScript

Expand Down
1 change: 1 addition & 0 deletions engine/gctscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (g *gctScriptManager) run() {
log.Debugln(log.Global, gctscriptManagerName, MsgSubSystemStarted)

Bot.ServicesWG.Add(1)
vm.SetDefaultScriptOutput()
g.autoLoad()
defer func() {
atomic.CompareAndSwapInt32(&g.stopped, 1, 0)
Expand Down
28 changes: 28 additions & 0 deletions gctscript/examples/csv.gct
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
exch := import("exchange")
t := import("times")
// Import all the indicators you want
atr := import("indicator/atr")
sma := import("indicator/sma")
ema := import("indicator/ema")
common := import("common")

load := func() {
// define your start and end within reason.
start := t.date(2017, 8 , 17, 0 , 0 , 0, 0)
end := t.add_date(start, 0, 6 , 0)

// This fetches the ohlcv
ohlcvData := exch.ohlcv("binance", "BTC-USDT", "-", "SPOT", start, end, "1d")

// construct ta values
avgtr := atr.calculate(ohlcvData.candles, 14)
simma := sma.calculate(ohlcvData.candles, 9)
expma := ema.calculate(ohlcvData.candles, 9)

// 'ctx' is already defined when we construct our bytecode from file.
// It contains script ID and shortname of file as save details to default
// script output directory.
common.writeascsv(ctx, ohlcvData, avgtr, simma, expma)
}

load()
2 changes: 1 addition & 1 deletion gctscript/examples/exchange/account_info.gct
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ load := func() {
// retrieve account information from exchange and store in info variable
info := exch.accountinfo("BTC Markets")
// print out info
fmt.print(info)
fmt.println(info)
}

load()
2 changes: 1 addition & 1 deletion gctscript/examples/exchange/pairs.gct
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ exch := import("exchange")

load := func() {
info := exch.pairs("BTC Markets", false, "SPOT")
fmt.print(info)
fmt.println(info)
}

load()
2 changes: 1 addition & 1 deletion gctscript/examples/exchange/query_order.gct
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ exch := import("exchange")

load := func() {
info := exch.orderquery("BTC Markets", "4491600698")
fmt.print(info)
fmt.println(info)
}

load()
2 changes: 1 addition & 1 deletion gctscript/examples/exchange/submit_order.gct
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ exch := import("exchange")

load := func() {
info := exch.ordersubmit("BTC Markets","BTC-AUD","-","LIMIT","SELL",1000000, 1,"")
fmt.print(info)
fmt.println(info)
}

load()
2 changes: 1 addition & 1 deletion gctscript/examples/exchange/withdraw.gct
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ load := func() {
// submit request to withdraw funds
info := exch.withdrawfiat("BTC Markets", "AUD", "hello", 1, "-")
// print out info
fmt.print(info)
fmt.println(info)
}

load()
2 changes: 1 addition & 1 deletion gctscript/examples/exchange/withdraw_crypto.gct
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ load := func() {

info := exch.withdrawcrypto("BTC Markets","BTC", "1234562362", "1231", 1.0, 0.0, "","" )
// print out info
fmt.print(info)
fmt.println(info)
}

load()
Loading

0 comments on commit 0adf39d

Please sign in to comment.