Skip to content

Commit

Permalink
Address shazbert's nitters
Browse files Browse the repository at this point in the history
  • Loading branch information
thrasher- committed Feb 28, 2025
1 parent 6da8dc6 commit 175e53c
Show file tree
Hide file tree
Showing 13 changed files with 354 additions and 440 deletions.
21 changes: 10 additions & 11 deletions backtester/btcli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"errors"
"fmt"
"path/filepath"
"strconv"
"time"

"github.com/thrasher-corp/gocryptotrader/backtester/btrpc"
"github.com/thrasher-corp/gocryptotrader/backtester/config"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/urfave/cli/v2"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"
)

Expand Down Expand Up @@ -50,10 +50,10 @@ var executeStrategyFromFileCommand = &cli.Command{
Aliases: []string{"e"},
Usage: fmt.Sprintf("override the strategy file's end time using your local time. eg '%v'", time.Now().Truncate(time.Hour).Format(time.DateTime)),
},
&cli.Int64Flag{
&cli.DurationFlag{
Name: "intervaloverride",
Aliases: []string{"i"},
Usage: "override the strategy file's candle interval, in seconds. eg 60 = 1 minute",
Usage: "override the strategy file's candle interval in the format of a time duration. eg '1m' for 1 minute",
},
},
}
Expand Down Expand Up @@ -119,18 +119,17 @@ func executeStrategyFromFile(c *cli.Context) error {
}
}

var intervalOverride int64
var intervalOverride time.Duration
if c.IsSet("intervaloverride") {
intervalOverride = c.Int64("intervaloverride")
intervalOverride = c.Duration("intervaloverride")
} else if c.Args().Get(5) != "" {
intervalOverride, err = strconv.ParseInt(c.Args().Get(5), 10, 64)
intervalOverride, err = time.ParseDuration(c.Args().Get(5))
if err != nil {
return err
}
}
overrideDuration := time.Duration(intervalOverride) * time.Second
if overrideDuration < 0 {
return errors.New("interval override cannot be less than 0")
if intervalOverride < 0 {
return errors.New("interval override duration cannot be less than 0")
}

client := btrpc.NewBacktesterServiceClient(conn)
Expand All @@ -142,7 +141,7 @@ func executeStrategyFromFile(c *cli.Context) error {
DoNotStore: dns,
StartTimeOverride: timestamppb.New(s),
EndTimeOverride: timestamppb.New(e),
IntervalOverride: int64(overrideDuration),
IntervalOverride: durationpb.New(intervalOverride),
},
)

Expand Down Expand Up @@ -507,7 +506,7 @@ func executeStrategyFromConfig(c *cli.Context) error {
}

dataSettings := &btrpc.DataSettings{
Interval: defaultConfig.DataSettings.Interval.Duration().Nanoseconds(),
Interval: durationpb.New(defaultConfig.DataSettings.Interval.Duration()),
Datatype: defaultConfig.DataSettings.DataType,
}
if defaultConfig.DataSettings.APIData != nil {
Expand Down
592 changes: 301 additions & 291 deletions backtester/btrpc/btrpc.pb.go

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions backtester/btrpc/btrpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package btrpc;

import "google/api/annotations.proto";
import "google/protobuf/timestamp.proto";
import "google/protobuf/duration.proto";

option go_package = "github.com/thrasher-corp/gocryptotrader/backtester/btrpc";

Expand Down Expand Up @@ -151,7 +152,7 @@ message ExchangeCredentials {
}

message DataSettings {
int64 interval = 1;
google.protobuf.Duration interval = 1;
string datatype = 2;
ApiData api_data = 3;
DatabaseData database_data = 4;
Expand Down Expand Up @@ -205,7 +206,7 @@ message ExecuteStrategyFromFileRequest {
bool do_not_store = 3;
google.protobuf.Timestamp start_time_override = 4;
google.protobuf.Timestamp end_time_override = 5;
int64 interval_override = 6;
google.protobuf.Duration interval_override = 6;
}

message ExecuteStrategyResponse {
Expand Down
9 changes: 3 additions & 6 deletions backtester/btrpc/btrpc.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@
"name": "config.dataSettings.interval",
"in": "query",
"required": false,
"type": "string",
"format": "int64"
"type": "string"
},
{
"name": "config.dataSettings.datatype",
Expand Down Expand Up @@ -426,8 +425,7 @@
"name": "intervalOverride",
"in": "query",
"required": false,
"type": "string",
"format": "int64"
"type": "string"
}
],
"tags": [
Expand Down Expand Up @@ -723,8 +721,7 @@
"type": "object",
"properties": {
"interval": {
"type": "string",
"format": "int64"
"type": "string"
},
"datatype": {
"type": "string"
Expand Down
11 changes: 9 additions & 2 deletions backtester/btrpc/buf.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ deps:
- remote: buf.build
owner: googleapis
repository: googleapis
commit: 62f35d8aed1149c291d606d958a7ce32
commit: 546238c53f7340c6a2a6099fb863bc1b
digest: shake256:8d75c12f391e392b24c076d05117b47aeddb090add99c70247a8f4389b906a65f61a933c68e54ed8b73a050b967b6b712ba194348b67c3ab3ee26cc2cb25852c
- remote: buf.build
owner: grpc-ecosystem
repository: grpc-gateway
commit: bc28b723cd774c32b6fbc77621518765
commit: 4c5ba75caaf84e928b7137ae5c18c26a
digest: shake256:e174ad9408f3e608f6157907153ffec8d310783ee354f821f57178ffbeeb8faa6bb70b41b61099c1783c82fe16210ebd1279bc9c9ee6da5cffba9f0e675b8b99
- remote: buf.build
owner: protocolbuffers
repository: wellknowntypes
commit: d4f14e5e0a9c40889c90d373c74e95eb
digest: shake256:c9824714afd6cc432c2e1fafa20df47c87a8a0aca9e27192cd5732619453997af1721c2eac5c0fbbe7b29a741af5b8d7ba4ee89c85903e782d9c725d7b9436b5
1 change: 1 addition & 0 deletions backtester/btrpc/buf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ breaking:
- FILE
deps:
- buf.build/googleapis/googleapis
- buf.build/protocolbuffers/wellknowntypes
- buf.build/grpc-ecosystem/grpc-gateway
10 changes: 5 additions & 5 deletions backtester/engine/grpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,11 @@ func (s *GRPCServer) ExecuteStrategyFromFile(_ context.Context, request *btrpc.E
return nil, err
}

if request.IntervalOverride > 0 {
if request.IntervalOverride < gctkline.FifteenSecond.Duration().Nanoseconds() {
return nil, fmt.Errorf("%w, interval must be >= 15 seconds, received '%v'", gctkline.ErrInvalidInterval, time.Duration(request.IntervalOverride))
if io := request.IntervalOverride.AsDuration(); io > 0 {
if io < gctkline.FifteenSecond.Duration() {
return nil, fmt.Errorf("%w, interval must be >= 15 seconds, received '%v'", gctkline.ErrInvalidInterval, io)
}
cfg.DataSettings.Interval = gctkline.Interval(request.IntervalOverride)
cfg.DataSettings.Interval = gctkline.Interval(io)
}

if startTime := request.StartTimeOverride.AsTime(); startTime.Unix() != 0 && !startTime.IsZero() {
Expand Down Expand Up @@ -591,7 +591,7 @@ func (s *GRPCServer) ExecuteStrategyFromConfig(_ context.Context, request *btrpc
},
CurrencySettings: configSettings,
DataSettings: config.DataSettings{
Interval: gctkline.Interval(request.Config.DataSettings.Interval),
Interval: gctkline.Interval(request.Config.DataSettings.Interval.AsDuration()),
DataType: request.Config.DataSettings.Datatype,
APIData: apiData,
DatabaseData: dbData,
Expand Down
7 changes: 4 additions & 3 deletions backtester/engine/grpcserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/strategies/binancecashandcarry"
gctcommon "github.com/thrasher-corp/gocryptotrader/common"
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"
)

Expand Down Expand Up @@ -80,7 +81,7 @@ func TestExecuteStrategyFromFile(t *testing.T) {
StrategyFilePath: dcaConfigPath,
StartTimeOverride: timestamppb.New(time.Now().Add(-time.Minute)),
EndTimeOverride: timestamppb.New(time.Now()),
IntervalOverride: 1,
IntervalOverride: durationpb.New(time.Duration(1)),
})
if !errors.Is(err, gctkline.ErrInvalidInterval) {
t.Errorf("received '%v' expecting '%v'", err, gctkline.ErrInvalidInterval)
Expand All @@ -90,7 +91,7 @@ func TestExecuteStrategyFromFile(t *testing.T) {
StrategyFilePath: dcaConfigPath,
StartTimeOverride: timestamppb.New(time.Now().Add(-time.Hour * 6).Truncate(time.Hour)),
EndTimeOverride: timestamppb.New(time.Now().Add(-time.Hour * 2).Truncate(time.Hour)),
IntervalOverride: time.Hour.Nanoseconds(),
IntervalOverride: durationpb.New(time.Hour),
})
if !errors.Is(err, nil) {
t.Errorf("received '%v' expecting '%v'", err, nil)
Expand Down Expand Up @@ -218,7 +219,7 @@ func TestExecuteStrategyFromConfig(t *testing.T) {
}

dataSettings := &btrpc.DataSettings{
Interval: defaultConfig.DataSettings.Interval.Duration().Nanoseconds(),
Interval: durationpb.New(defaultConfig.DataSettings.Interval.Duration()),
Datatype: defaultConfig.DataSettings.DataType,
}
if defaultConfig.DataSettings.APIData != nil {
Expand Down
19 changes: 13 additions & 6 deletions config/config_encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
const (
saltRandomLength = 12
encryptionVersion = 1
versionSize = 2 // 2 bytes as uint16, allows for 65535 versions (at our current rate of 1 version per decade, should last a few generations)
)

// Public errors
Expand Down Expand Up @@ -130,12 +131,18 @@ func (c *Config) encryptConfigData(configData []byte) ([]byte, error) {

ciphertext := aead.Seal(nil, nil, configData, nil)

appendedFile := append(bytes.Clone(encryptionPrefix), c.storedSalt...)
appendedFile = append(appendedFile, encryptionVersionPrefix...)
versionOffset := len(encryptionPrefix) + len(c.storedSalt) + len(encryptionVersionPrefix)
appendedFile = append(appendedFile, make([]byte, 2)...)
binary.BigEndian.PutUint16(appendedFile[versionOffset:], encryptionVersion)
appendedFile = append(appendedFile, ciphertext...)
totalLen := len(encryptionPrefix) + len(c.storedSalt) + len(encryptionVersionPrefix) + versionSize + len(ciphertext)
appendedFile := make([]byte, totalLen)
offset := 0
copy(appendedFile[offset:], encryptionPrefix)
offset += len(encryptionPrefix)
copy(appendedFile[offset:], c.storedSalt)
offset += len(c.storedSalt)
copy(appendedFile[offset:], encryptionVersionPrefix)
offset += len(encryptionVersionPrefix)
binary.BigEndian.PutUint16(appendedFile[offset:offset+versionSize], encryptionVersion)
offset += versionSize
copy(appendedFile[offset:], ciphertext)
return appendedFile, nil
}

Expand Down
2 changes: 1 addition & 1 deletion config/config_encryption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TestDecryptConfigFile(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, `{"test":123}`, string(data))

badVersion := make([]byte, len(encryptionPrefix)+len(encryptionVersionPrefix)+2)
badVersion := make([]byte, len(encryptionPrefix)+len(encryptionVersionPrefix)+versionSize)
copy(badVersion, encryptionPrefix)
copy(badVersion[len(encryptionPrefix):], encryptionVersionPrefix)
binary.BigEndian.PutUint16(badVersion[len(encryptionPrefix)+len(encryptionVersionPrefix):], 69)
Expand Down
2 changes: 1 addition & 1 deletion exchanges/bithumb/bithumb.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func (b *Bithumb) GetAccountBalance(ctx context.Context, c string) (FullBalance,
}

// Added due to increasing of the usable currencies on exchange, usually
// without notificatation, so we dont need to update structs later on
// without notification, so we dont need to update structs later on
for tag, datum := range response.Data {
splitTag := strings.Split(tag, "_")
if len(splitTag) < 2 {
Expand Down
109 changes: 0 additions & 109 deletions exchanges/hitbtc/hitbtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ const (
apiV2OrderHistory = "api/2/history/order"
apiv2OpenOrders = "api/2/order"
apiV2FeeInfo = "api/2/trading/fee"
orders = "order"
apiOrder = "api/2/order"
orderMove = "moveOrder"
tradableBalances = "returnTradableBalances"
transferBalance = "transferBalance"
)

// HitBTC is the overarching type across the hitbtc package
Expand Down Expand Up @@ -253,18 +249,6 @@ func (h *HitBTC) GenerateNewAddress(ctx context.Context, currency string) (Depos
return resp, err
}

// GetActiveorders returns all your active orders
func (h *HitBTC) GetActiveorders(ctx context.Context, currency string) ([]Order, error) {
var resp []Order
err := h.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodGet,
orders+"?symbol="+currency,
url.Values{},
tradingRequests,
&resp)

return resp, err
}

// GetTradeHistoryForCurrency returns your trade history
func (h *HitBTC) GetTradeHistoryForCurrency(ctx context.Context, currency, start, end string) (AuthenticatedTradeHistoryResponse, error) {
values := url.Values{}
Expand Down Expand Up @@ -398,34 +382,6 @@ func (h *HitBTC) CancelAllExistingOrders(ctx context.Context) ([]Order, error) {
&result)
}

// MoveOrder generates a new move order
func (h *HitBTC) MoveOrder(ctx context.Context, orderID int64, rate, amount float64) (MoveOrderResponse, error) {
result := MoveOrderResponse{}
values := url.Values{}
values.Set("orderNumber", strconv.FormatInt(orderID, 10))
values.Set("rate", strconv.FormatFloat(rate, 'f', -1, 64))

if amount != 0 {
values.Set("amount", strconv.FormatFloat(amount, 'f', -1, 64))
}

err := h.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost,
orderMove,
values,
tradingRequests,
&result)

if err != nil {
return result, err
}

if result.Success != 1 {
return result, errors.New(result.Error)
}

return result, nil
}

// Withdraw allows for the withdrawal to a specific address
func (h *HitBTC) Withdraw(ctx context.Context, currency, address string, amount float64) (bool, error) {
result := Withdraw{}
Expand Down Expand Up @@ -464,71 +420,6 @@ func (h *HitBTC) GetFeeInfo(ctx context.Context, currencyPair string) (Fee, erro
return result, err
}

// GetTradableBalances returns current tradable balances
func (h *HitBTC) GetTradableBalances(ctx context.Context) (map[string]map[string]float64, error) {
type Response struct {
Data map[string]map[string]interface{}
}
result := Response{}

err := h.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost,
tradableBalances,
url.Values{},
tradingRequests,
&result.Data)

if err != nil {
return nil, err
}

balances := make(map[string]map[string]float64)

for x, y := range result.Data {
balances[x] = make(map[string]float64)
for z, w := range y {
bal, ok := w.(string)
if !ok {
return nil, common.GetTypeAssertError("string", w, "balance")
}

balance, err := strconv.ParseFloat(bal, 64)
if err != nil {
return nil, err
}
balances[x][z] = balance
}
}

return balances, nil
}

// TransferBalance transfers a balance
func (h *HitBTC) TransferBalance(ctx context.Context, currency, from, to string, amount float64) (bool, error) {
values := url.Values{}
result := GenericResponse{}

values.Set("currency", currency)
values.Set("amount", strconv.FormatFloat(amount, 'f', -1, 64))
values.Set("fromAccount", from)
values.Set("toAccount", to)

err := h.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost,
transferBalance,
values,
otherRequests,
&result)

if err != nil {
return false, err
}

if result.Error != "" && result.Success != 1 {
return false, errors.New(result.Error)
}

return true, nil
}

// SendHTTPRequest sends an unauthenticated HTTP request
func (h *HitBTC) SendHTTPRequest(ctx context.Context, ep exchange.URL, path string, result interface{}) error {
endpoint, err := h.API.Endpoints.GetURL(ep)
Expand Down
Loading

0 comments on commit 175e53c

Please sign in to comment.