mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-02 23:16:51 +00:00
exchanges: Types and unmarshalling methods update (#1899)
* Added TimeInForce type and updated related files * Linter issue fix and minor coinbasepro type update * Bitrex consts update * added unit test and minor changes in bittrex * Unit tests update * Fix minor linter issues * Update TestStringToTimeInForce unit test * fix conflict with gateio timeInForce * Update order tests * Complete updating the order unit tests * update kucoin and deribit wrapper to match the time in force change * fix time-in-force related test errors * linter issue fix * time in force constants, functions and unit tests update * shift tif policies to TimeInForce * Update time-in-force, related functions, and unit tests * fix linter issue and time-in-force processing * added a good till crossing tif value * order type fix and fix related tim-in-force entries * update time-in-force unmarshaling and unit test * fix time-in-force error in gateio * linter issue fix * update based on review comments * add unit test and fix missing issues * minor fix and added benchmark unit test * change GTT to GTC for limit * fix linter issue * added time-in-force value to place order param * fix minor issues based on review comment and move tif code to separate files * update on exchanges linked to time-in-force * resolve missing review comments * minor linter issues fix * added time-in-force handler and update timeInForce parametered endpoint * minor fixes based on review * nits fix * update based on review * linter fix * rm getTimeInForce func and minor change to time-in-force * minor change * update based on review comments * wrappers and time-in-force calling approach * update slice data processing * fix linter issues * remove unnecessary Unmarshal methods and replace type delatration and added unit tests * minor change * minor changes to types * update gateio string to timeInForce conversion and unit test * removed unused timeInForceString func from kraken * removed redundant parentheses in slice unmarshaling * array to slice conversion and other updates * reverted slice target to array * Binanceus unit test NotNil check to Len check * change NotNil to Len check * rename unmarshal unit test funcs name * wrap json strings with []byte
This commit is contained in:
@@ -289,28 +289,8 @@ func (d *Deribit) GetHistoricalVolatility(ctx context.Context, ccy currency.Code
|
||||
}
|
||||
params := url.Values{}
|
||||
params.Set("currency", ccy.String())
|
||||
var data [][2]any
|
||||
err := d.SendHTTPRequest(ctx, exchange.RestFutures, nonMatchingEPL,
|
||||
common.EncodeURLValues(getHistoricalVolatility, params), &data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp := make([]HistoricalVolatilityData, len(data))
|
||||
for x := range data {
|
||||
timeData, ok := data[x][0].(float64)
|
||||
if !ok {
|
||||
return resp, common.GetTypeAssertError("float64", data[x][0], "time data")
|
||||
}
|
||||
val, ok := data[x][1].(float64)
|
||||
if !ok {
|
||||
return resp, common.GetTypeAssertError("float64", data[x][1], "volatility value")
|
||||
}
|
||||
resp[x] = HistoricalVolatilityData{
|
||||
Timestamp: timeData,
|
||||
Value: val,
|
||||
}
|
||||
}
|
||||
return resp, nil
|
||||
var data []HistoricalVolatilityData
|
||||
return data, d.SendHTTPRequest(ctx, exchange.RestFutures, nonMatchingEPL, common.EncodeURLValues(getHistoricalVolatility, params), &data)
|
||||
}
|
||||
|
||||
// GetCurrencyIndexPrice retrieves the current index price for the instruments, for the selected currency.
|
||||
|
||||
@@ -29,6 +29,7 @@ import (
|
||||
testexch "github.com/thrasher-corp/gocryptotrader/internal/testing/exchange"
|
||||
testsubs "github.com/thrasher-corp/gocryptotrader/internal/testing/subscriptions"
|
||||
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
|
||||
"github.com/thrasher-corp/gocryptotrader/types"
|
||||
)
|
||||
|
||||
// Please supply your own keys here to do authenticated endpoint testing
|
||||
@@ -246,6 +247,8 @@ func TestGetMarkPriceHistory(t *testing.T) {
|
||||
var resp []MarkPriceHistory
|
||||
err := json.Unmarshal([]byte(`[[1608142381229,0.5165791606037885],[1608142380231,0.5165737855432504],[1608142379227,0.5165768236356326]]`), &resp)
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, resp, 3)
|
||||
|
||||
_, err = d.GetMarkPriceHistory(t.Context(), "", time.Now().Add(-5*time.Minute), time.Now())
|
||||
require.ErrorIs(t, err, errInvalidInstrumentName)
|
||||
|
||||
@@ -457,6 +460,19 @@ func TestWSRetrieveFundingRateValue(t *testing.T) {
|
||||
assert.NotNil(t, result)
|
||||
}
|
||||
|
||||
func TestHistoricalVolatilityDataUnmarshalJSON(t *testing.T) {
|
||||
t.Parallel()
|
||||
data := []byte(`[[1746532800000,33.926694663144644],[1746536400000,33.86888345738641],[1746540000000,33.87689653120242],[1746543600000,33.92229949556179],[1746547200000,33.35430439982866],[1746550800000,33.405720857822644],[1746554400000,33.041661194903895],[1746558000000,33.026907604467596],[1746561600000,33.147012362654635],[1746565200000,32.948314953334105],[1746568800000,32.97264616801311],[1746572400000,32.97051874896058],[1746576000000,33.94405253940284],[1746579600000,34.01745935786804],[1746583200000,34.133772136604854],[1746586800000,33.89032454069847],[1746590400000,34.008502172420556],[1746594000000,34.01444591222428],[1746597600000,34.01154352323321],[1746601200000,33.97800061398224],[1746604800000,33.980501315033024]]`)
|
||||
var targets []HistoricalVolatilityData
|
||||
err := json.Unmarshal(data, &targets)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, targets, 21)
|
||||
assert.Equal(t, HistoricalVolatilityData{
|
||||
Timestamp: types.Time(time.UnixMilli(1746532800000)),
|
||||
Value: 33.926694663144644,
|
||||
}, targets[0])
|
||||
}
|
||||
|
||||
func TestGetHistoricalVolatility(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := d.GetHistoricalVolatility(t.Context(), currency.EMPTYCODE)
|
||||
|
||||
@@ -165,8 +165,13 @@ type FundingRateHistory struct {
|
||||
|
||||
// HistoricalVolatilityData stores volatility data for requested symbols
|
||||
type HistoricalVolatilityData struct {
|
||||
Timestamp float64
|
||||
Value float64
|
||||
Timestamp types.Time
|
||||
Value types.Number
|
||||
}
|
||||
|
||||
// UnmarshalJSON parses volatility data from a JSON array into HistoricalVolatilityData fields.
|
||||
func (h *HistoricalVolatilityData) UnmarshalJSON(data []byte) error {
|
||||
return json.Unmarshal(data, &[2]any{&h.Timestamp, &h.Value})
|
||||
}
|
||||
|
||||
// IndexPrice holds index price for the instruments
|
||||
@@ -266,14 +271,7 @@ type MarkPriceHistory struct {
|
||||
|
||||
// UnmarshalJSON deserialises the JSON info, including the timestamp.
|
||||
func (a *MarkPriceHistory) UnmarshalJSON(data []byte) error {
|
||||
var resp [2]float64
|
||||
err := json.Unmarshal(data, &resp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
a.Timestamp = types.Time(time.UnixMilli(int64(resp[0])))
|
||||
a.MarkPriceValue = resp[1]
|
||||
return nil
|
||||
return json.Unmarshal(data, &[2]any{&a.Timestamp, &a.MarkPriceValue})
|
||||
}
|
||||
|
||||
// Orderbook stores orderbook data
|
||||
|
||||
@@ -160,27 +160,8 @@ func (d *Deribit) WSRetrieveHistoricalVolatility(ccy currency.Code) ([]Historica
|
||||
}{
|
||||
Currency: ccy,
|
||||
}
|
||||
var data [][2]any
|
||||
err := d.SendWSRequest(nonMatchingEPL, getHistoricalVolatility, input, &data, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp := make([]HistoricalVolatilityData, len(data))
|
||||
for x := range data {
|
||||
timeData, ok := data[x][0].(float64)
|
||||
if !ok {
|
||||
return resp, common.GetTypeAssertError("float64", data[x][0], "time data")
|
||||
}
|
||||
val, ok := data[x][1].(float64)
|
||||
if !ok {
|
||||
return resp, common.GetTypeAssertError("float64", data[x][1], "volatility value")
|
||||
}
|
||||
resp[x] = HistoricalVolatilityData{
|
||||
Timestamp: timeData,
|
||||
Value: val,
|
||||
}
|
||||
}
|
||||
return resp, nil
|
||||
var data []HistoricalVolatilityData
|
||||
return data, d.SendWSRequest(nonMatchingEPL, getHistoricalVolatility, input, &data, false)
|
||||
}
|
||||
|
||||
// WSRetrieveCurrencyIndexPrice the current index price for the instruments, for the selected currency through the websocket connection.
|
||||
|
||||
Reference in New Issue
Block a user