Skip to content

Commit

Permalink
script: implementation of error insertion on return (thrasher-corp#986)
Browse files Browse the repository at this point in the history
* exchanges/account: shift credentials to account package and segregate funds to keys

* merge: fixes

* linter: fix

* Update exchanges/account/account.go

Co-authored-by: Scott <[email protected]>

* glorious: nits + protection for string panic

* glorious_suggestion: add method for matching keys

* linter: fix tests

* account: add protected method for credentials minimizing access, display full account details to rpc.

* linter: spelling kweeeeeeen

* accounts/portfolio: clean/check portfolio code and quickly check balances from change. Add protected method for future matching.

* accounts: theres no point in pointerising everything

* linter: ok pointerise this then...

* exchanges: fix regression add in little notes.

* glorious: nits

* Update exchanges/account/credentials.go

Co-authored-by: Scott <[email protected]>

* Update exchanges/account/credentials_test.go

Co-authored-by: Scott <[email protected]>

* Update exchanges/account/credentials_test.go

Co-authored-by: Scott <[email protected]>

* glorious: nits

* gloriously: fix glorious glorious test gloriously

* script: initial implementation of error insertion on return

* script: make script context aware(ish) and update error handle in examples

* script: add tests

* script: add syntax highlighting to readme

* Update gctscript/vm/vm.go

Co-authored-by: Scott <[email protected]>

* Update gctscript/vm/vm.go

Co-authored-by: Scott <[email protected]>

* Update gctscript/examples/exchange/account_info.gct

Co-authored-by: Scott <[email protected]>

* Update gctscript/examples/exchange/cancel_order.gct

Co-authored-by: Scott <[email protected]>

* Update gctscript/examples/verbose.gct

Co-authored-by: Scott <[email protected]>

* Update gctscript/modules/gct/gct_test.go

Co-authored-by: Scott <[email protected]>

* glorious: nits

* rm: bros

* scripts: handle errors in examples when they are going to use the data after fetching

* linter: fix rides again

* SCOTT_SPELL_CHECK_LINTER: fix

* gctscript: fix tests

* glorious: niiiiiiiiiiiiits

* scriptmodules/gct: standardize runtime errors

* Update gctscript/modules/gct/exchange.go

Co-authored-by: Scott <[email protected]>

* Update gctscript/modules/gct/exchange.go

Co-authored-by: Scott <[email protected]>

* Update gctscript/modules/gct/exchange.go

Co-authored-by: Scott <[email protected]>

* Update gctscript/modules/gct/exchange.go

Co-authored-by: Scott <[email protected]>

* Update gctscript/modules/gct/gct.go

Co-authored-by: Scott <[email protected]>

* Update gctscript/modules/gct/gct.go

Co-authored-by: Scott <[email protected]>

* Update gctscript/modules/gct/gct.go

Co-authored-by: Scott <[email protected]>

* Update gctscript/modules/gct/gct.go

Co-authored-by: Scott <[email protected]>

* Update gctscript/modules/gct/gct.go

Co-authored-by: Scott <[email protected]>

* Update gctscript/modules/gct/gct.go

Co-authored-by: Scott <[email protected]>

* Update gctscript/modules/gct/gct.go

Co-authored-by: Scott <[email protected]>

* glorious: nits/reverts

* go mod: tidy

Co-authored-by: Ryan O'Hara-Reid <[email protected]>
Co-authored-by: Scott <[email protected]>
  • Loading branch information
3 people authored Aug 17, 2022
1 parent 6858856 commit e93ee83
Show file tree
Hide file tree
Showing 42 changed files with 1,098 additions and 360 deletions.
2 changes: 1 addition & 1 deletion dispatch/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func IsRunning() bool {
return dispatcher.isRunning()
}

// start compares atomic running value, sets defaults, overides with
// start compares atomic running value, sets defaults, overrides with
// configuration, then spawns workers
func (d *Dispatcher) start(workers, channelCapacity int) error {
if d == nil {
Expand Down
2 changes: 1 addition & 1 deletion engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func loadConfigWithSettings(settings *Settings, flagSet map[string]bool) (*confi
// FlagSet defines set flags from command line args for comparison methods
type FlagSet map[string]bool

// WithBool checks the supplied flag. If set it will overide the config boolean
// WithBool checks the supplied flag. If set it will override the config boolean
// value as a command line takes precedence. If not set will fall back to config
// options.
func (f FlagSet) WithBool(key string, flagValue *bool, configValue bool) {
Expand Down
4 changes: 2 additions & 2 deletions engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func TestFlagSetWith(t *testing.T) {

flags["IS SET"] = true
isRunning = true
// Flag set true which will overide config
// Flag set true which will override config
flags.WithBool("IS SET", &isRunning, true)
if !isRunning {
t.Fatalf("received: '%v' but expected: '%v'", isRunning, true)
Expand All @@ -283,7 +283,7 @@ func TestFlagSetWith(t *testing.T) {

flags["IS SET"] = true
isRunning = false
// Flag set false which will overide config
// Flag set false which will override config
flags.WithBool("IS SET", &isRunning, true)
if isRunning {
t.Fatalf("received: '%v' but expected: '%v'", isRunning, false)
Expand Down
6 changes: 3 additions & 3 deletions exchanges/account/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func ParseCredentialsMetadata(ctx context.Context, md metadata.MD) (context.Cont
}
if ctxCreds.IsEmpty() && subAccountHere != "" {
// This will override default sub account details if needed.
return deploySubAccountOverrideToContext(ctx, subAccountHere), nil
return DeploySubAccountOverrideToContext(ctx, subAccountHere), nil
}
// merge sub account to main context credentials
ctxCreds.SubAccount = subAccountHere
Expand All @@ -204,9 +204,9 @@ func DeployCredentialsToContext(ctx context.Context, creds *Credentials) context
return context.WithValue(ctx, flag, store)
}

// deploySubAccountOverrideToContext sets subaccount as override to credentials
// DeploySubAccountOverrideToContext sets subaccount as override to credentials
// as a separate flag.
func deploySubAccountOverrideToContext(ctx context.Context, subAccount string) context.Context {
func DeploySubAccountOverrideToContext(ctx context.Context, subAccount string) context.Context {
return context.WithValue(ctx, ContextSubAccountFlag, subAccount)
}

Expand Down
4 changes: 4 additions & 0 deletions gctscript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader

To Enable database logging support you must have an active migrated database by following the [database setup guide](../database/README.md)

##### Syntax Highlighting

To enable syntax highlighting for vscode download extension [graphman65/vscode-tengo](https://github.com/graphman65/vscode-tengo/) then add `".gct"` to vscode-tengo package.json [settings](https://github.com/graphman65/vscode-tengo/blob/master/package.json#L27) to enable highlighting of our files.

##### Configuration

The gctscript configuration struct is currently:
Expand Down
31 changes: 31 additions & 0 deletions gctscript/examples/account.gct
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
global := import("global")
exch := import("exchange")
fmt := import("fmt")

load := func() {
// '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.

// Set account func allows the setting of account details via script
// which can then be passed into auth functions to specifically target
// different subaccounts while trading or retrieving fund details.
// Basic required implementation below:
ctx = global.set_account(ctx, "api_key_str", "api_secret_str")

// Full implementation:
// ctx = global.set_account(ctx, "api_key_str", "api_secret_str", "sub_account_str", "client_Id_str", "PEM_key_str", "OTP_Str")

// Set sub account func allows the setting of just the individual sub
// account details while utilizing the configured config.json apikeys.
// ctx = global.set_sub_account(ctx, "sub_account_str")

info := exch.accountinfo(ctx, "ftx", "spot")
if is_error(info) {
// handle error
}

fmt.println(info)
}

load()
15 changes: 14 additions & 1 deletion gctscript/examples/csv.gct
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
exch := import("exchange")
t := import("times")
fmt := import("fmt")
// Import all the indicators you want
atr := import("indicator/atr")
sma := import("indicator/sma")
Expand All @@ -12,7 +13,12 @@ load := func() {
end := t.add_date(start, 0, 6 , 0)

// This fetches the ohlcv
ohlcvData := exch.ohlcv("binance", "BTC-USDT", "-", "SPOT", start, end, "1d")
// To add debugging information to the request, see verbose.gct. To add account credentials, see account.gct
ohlcvData := exch.ohlcv(ctx, "binance", "BTC-USDT", "-", "SPOT", start, end, "1d")
if is_error(ohlcvData) {
fmt.println(ohlcvData)
return
}

// construct ta values
avgtr := atr.calculate(ohlcvData.candles, 14)
Expand All @@ -22,7 +28,14 @@ load := func() {
// '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.
// To add debugging information to the request, see verbose.gct. To add account credentials, see account.gct
common.writeascsv(ctx, ohlcvData, avgtr, simma, expma)

// A custom filename can also be declared when using a string instead of the
// context variable like below. This will continue to save in the output
// folder of the scripts file and will look something like this
// 'super_cool_filename-1658465844999067400.csv'.
// common.writeascsv("super_cool_filename", ohlcvData, avgtr, simma, expma)
}

load()
10 changes: 8 additions & 2 deletions gctscript/examples/exchange/account_info.gct
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ fmt := import("fmt")
exch := import("exchange")

load := func() {
// retrieve account information from exchange and store in info variable
info := exch.accountinfo("BTC Markets", "spot")
// Retrieve account information from exchange and store in info variable
// 'ctx' is already defined when we construct our bytecode from file.
// To add debugging information to the request, see verbose.gct. To add account credentials, see account.gct
// for more details.
info := exch.accountinfo(ctx, "BTC Markets", "spot")
if is_error(info) {
// handle error
}
// print out info
fmt.println(info)
}
Expand Down
7 changes: 6 additions & 1 deletion gctscript/examples/exchange/cancel_order.gct
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ fmt := import("fmt")
exch := import("exchange")

load := func() {
info := exch.ordercancel("binance","13371337", "btc-usdt", "spot")
// 'ctx' is already defined when we construct our bytecode from file.
// To add debugging information to the request, see verbose.gct. To add account credentials, see account.gct
info := exch.ordercancel(ctx, "binance","13371337", "btc-usdt", "spot")
if is_error(info) {
// handle error
}
fmt.println(info)
}

Expand Down
3 changes: 3 additions & 0 deletions gctscript/examples/exchange/deposit_address.gct
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ exch := import("exchange")

load := func() {
info := exch.depositaddress("BTC Markets", "BTC", "")
if is_error(info) {
// handle error
}
fmt.println(info)
}

Expand Down
7 changes: 6 additions & 1 deletion gctscript/examples/exchange/ohlcv.gct
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ t := import("times")

load := func() {
start := t.add(t.now(), -t.hour*24)
ohlcvData := exch.ohlcv("coinbasepro", "BTC-USD", "-", "SPOT", start, t.now(), "1h")
// 'ctx' is already defined when we construct our bytecode from file.
// To add debugging information to the request, see verbose.gct. To add account credentials, see account.gct
ohlcvData := exch.ohlcv(ctx, "coinbasepro", "BTC-USD", "-", "SPOT", start, t.now(), "1h")
if is_error(ohlcvData) {
// handle error
}
fmt.println(ohlcvData)
}

Expand Down
7 changes: 6 additions & 1 deletion gctscript/examples/exchange/orderbook.gct
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ name := "run"
timer := "5s"

load := func() {
tx := exch.orderbook("btc markets", "btc-aud", "-", "spot")
// 'ctx' is already defined when we construct our bytecode from file.
// To add debugging information to the request, see verbose.gct. To add account credentials, see account.gct
tx := exch.orderbook(ctx, "btc markets", "btc-aud", "-", "spot")
if is_error(tx) {
// handle error
}
fmt.println(tx)
}

Expand Down
3 changes: 3 additions & 0 deletions gctscript/examples/exchange/pairs.gct
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ exch := import("exchange")

load := func() {
info := exch.pairs("BTC Markets", false, "SPOT")
if is_error(info) {
// handle error
}
fmt.println(info)
}

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

load := func() {
info := exch.orderquery("binance", "4491600698", "BTC-USDT", "spot")
// 'ctx' is already defined when we construct our bytecode from file.
// To add debugging information to the request, see verbose.gct. To add account credentials, see account.gct
info := exch.orderquery(ctx, "binance", "4491600698", "BTC-USDT", "spot")
if is_error(info) {
// handle error
}
fmt.println(info)
}

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

load := func() {
info := exch.ordersubmit("BTC Markets","BTC-AUD","-","LIMIT","SELL",1000000, 1,"", "spot")
// 'ctx' is already defined when we construct our bytecode from file.
// To add debugging information to the request, see verbose.gct. To add account credentials, see account.gct
info := exch.ordersubmit(ctx, "BTC Markets","BTC-AUD","-","LIMIT","SELL",1000000, 1,"", "spot")
if is_error(info) {
// handle error
}
fmt.println(info)
}

Expand Down
7 changes: 6 additions & 1 deletion gctscript/examples/exchange/ticker.gct
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ name := "run"
timer := "5s"

load := func() {
tx := exch.ticker("btc markets", "btc-aud", "-", "spot")
// 'ctx' is already defined when we construct our bytecode from file.
// To add debugging information to the request, see verbose.gct. To add account credentials, see account.gct
tx := exch.ticker(ctx, "btc markets", "btc-aud", "-", "spot")
if is_error(tx) {
// handle error
}
fmt.println(tx)
}

Expand Down
10 changes: 7 additions & 3 deletions gctscript/examples/exchange/withdraw_crypto.gct
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ load := func() {
// 4: address tag
// 5: amount
// 6: fee amount
// 7: trade password
// 8: OTP
// 7: description

info := exch.withdrawcrypto("BTC Markets","BTC", "1234562362", "1231", 1.0, 0.0, "","" )
// 'ctx' is already defined when we construct our bytecode from file.
// To add debugging information to the request, see verbose.gct. To add account credentials, see account.gct
info := exch.withdrawcrypto(ctx, "BTC Markets", "BTC", "1234562362", "1231", 1.0, 0.0, "")
if is_error(info) {
// handle error
}
// print out info
fmt.println(info)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ load := func() {
// 7: trade password
// 8: OTP

// submit request to withdraw funds
info := exch.withdrawfiat("BTC Markets", "AUD", "hello", 1, "-")
// Submit request to withdraw funds
// 'ctx' is already defined when we construct our bytecode from file.
// To add debugging information to the request, see verbose.gct. To add account credentials, see account.gct
info := exch.withdrawfiat(ctx, "BTC Markets", "AUD", "hello", 1, "-")
if is_error(info) {
// handle error
}
// print out info
fmt.println(info)
}
Expand Down
6 changes: 4 additions & 2 deletions gctscript/examples/exit.gct
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
fmt := import("fmt")

// 'timer' is a GCT key word that is captured at compilation and used to execute
// this script task every defined duration.
timer := "5s"

exit := func() {
timer = 0
timer = "0s" // This will reset the timer to zero and shutdown the script.
}

load := func() {
for x := 0 ; x < 20; x++ {
fmt.printf("Hello %v", x)
fmt.printf("Hello %v\n", x)
}
exit()
}
Expand Down
9 changes: 8 additions & 1 deletion gctscript/examples/ta/atr.gct
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ atr := import("indicator/atr")
load := func() {
start := t.date(2017, 8 , 17, 0 , 0 , 0, 0)
end := t.add_date(start, 0, 6 , 0)
ohlcvData := exch.ohlcv("binance", "BTC-USDT", "-", "SPOT", start, end, "1d")
// 'ctx' is already defined when we construct our bytecode from file.
// To add debugging information to the request, see verbose.gct. To add account credentials, see account.gct
ohlcvData := exch.ohlcv(ctx, "binance", "BTC-USDT", "-", "SPOT", start, end, "1d")
if is_error(ohlcvData) {
// handle error
fmt.println(ohlcvData)
return
}

ret := atr.calculate(ohlcvData.candles, 14)
fmt.println(ret)
Expand Down
9 changes: 8 additions & 1 deletion gctscript/examples/ta/bbands.gct
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ bbands := import("indicator/bbands")
load := func() {
start := t.date(2017, 8 , 17 , 0 , 0 , 0, 0)
end := t.add_date(start, 0, 6 , 0)
ohlcvData := exch.ohlcv("binance", "BTC-USDT", "-", "SPOT", start, end, "1d")
// 'ctx' is already defined when we construct our bytecode from file.
// To add debugging information to the request, see verbose.gct. To add account credentials, see account.gct
ohlcvData := exch.ohlcv(ctx, "binance", "BTC-USDT", "-", "SPOT", start, end, "1d")
if is_error(ohlcvData) {
// handle error
fmt.println(ohlcvData)
return
}

ret := bbands.calculate("close", ohlcvData.candles, 20, 2.0, 2.0, "sma")
fmt.println(ret)
Expand Down
16 changes: 14 additions & 2 deletions gctscript/examples/ta/correlation.gct
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,20 @@ cc := import("indicator/correlationcoefficient")
load := func() {
start := t.date(2017, 8 , 17 , 0 , 0 , 0, 0)
end := t.add_date(start, 0, 6 , 0)
ohlcvDataBTC := exch.ohlcv("binance", "BTC-USDT", "-", "SPOT", start, end, "1d")
ohlcvDataETH := exch.ohlcv("binance", "ETH-USDT", "-", "SPOT", start, end, "1d")
// 'ctx' is already defined when we construct our bytecode from file.
// To add debugging information to the request, see verbose.gct. To add account credentials, see account.gct
ohlcvDataBTC := exch.ohlcv(ctx, "binance", "BTC-USDT", "-", "SPOT", start, end, "1d")
if is_error(ohlcvDataBTC) {
// handle error
fmt.println(ohlcvDataBTC)
return
}
ohlcvDataETH := exch.ohlcv(ctx, "binance", "ETH-USDT", "-", "SPOT", start, end, "1d")
if is_error(ohlcvDataETH) {
// handle error
fmt.println(ohlcvDataETH)
return
}
ret := cc.calculate(ohlcvDataBTC.candles, ohlcvDataETH.candles, 20)
fmt.println(ret)
}
Expand Down
10 changes: 8 additions & 2 deletions gctscript/examples/ta/ema.gct
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ ema := import("indicator/ema")
load := func() {
start := t.date(2017, 8 , 17 , 0 , 0 , 0, 0)
end := t.add_date(start, 0, 6 , 0)
ohlcvData := exch.ohlcv("binance", "BTC-USDT", "-", "SPOT", start, end, "1d")

// 'ctx' is already defined when we construct our bytecode from file.
// To add debugging information to the request, see verbose.gct. To add account credentials, see account.gct
ohlcvData := exch.ohlcv(ctx, "binance", "BTC-USDT", "-", "SPOT", start, end, "1d")
if is_error(ohlcvData) {
// handle error
fmt.println(ohlcvData)
return
}
ret := ema.calculate(ohlcvData.candles, 9)
fmt.println(ret)

Expand Down
Loading

0 comments on commit e93ee83

Please sign in to comment.