Skip to content

Commit 545198f

Browse files
author
Pedro Pablo Bustamante Barrera
committed
Merge branch 'develop'
2 parents d9fb187 + a5cf75b commit 545198f

16 files changed

+609
-75
lines changed

internal/channels.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package internal
22

33
const (
4-
ChannelTrades = "trades"
5-
ChannelCandles = "candles/%s"
6-
ChannelPriceRate = "price/rate/%s"
4+
ChannelTrades = "trades"
5+
ChannelCandles = "candles/%s"
6+
ChannelConvertedCandles = "converted/candles/%s"
7+
ChannelPriceRates = "price/rate/%s"
8+
ChannelPriceRatesInBatches = "price/rate/%s/batch"
79

810
ChannelMiniTickerInBatch = "ticker/price/%s/batch"
911
ChannelTicker = "ticker/%s"

models/models.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,15 @@ type Candle struct {
244244
VolumeQuote string `json:"volume_quote"`
245245
}
246246

247+
type ConvertedCandles struct {
248+
TargetCurrency string `json:"target_currency"`
249+
Data map[string][]Candle `json:"data"`
250+
}
251+
type ConvertedCandlesOfSymbol struct {
252+
TargetCurrency string `json:"target_currency"`
253+
Data []Candle `json:"data"`
254+
}
255+
247256
type Ticker struct {
248257
Timestamp string `json:"timestamp"`
249258
Open string `json:"open"`

rest/client.go

Lines changed: 92 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func (client *Client) handleResponseData(
198198
//
199199
// Arguments:
200200
//
201-
// Currencies([]CurrenciesType) // Optional. A list of currencies ids
201+
// Currencies([]CurrenciesType) // Optional. A list of currencies ids. If empty gets for all currencies
202202
// PreferredNetwork(String) // Optional. Code of the default network of the currency
203203
func (client *Client) GetCurrencies(
204204
ctx context.Context,
@@ -228,7 +228,7 @@ func (client *Client) GetCurrency(
228228
}
229229
err = client.publicGet(
230230
ctx,
231-
endpointCurrency+"/"+params["currency"].(string),
231+
endpointCurrency+"/"+params[internal.ArgNameCurrency].(string),
232232
nil,
233233
&result,
234234
)
@@ -245,7 +245,7 @@ func (client *Client) GetCurrency(
245245
//
246246
// Arguments:
247247
//
248-
// Symbols([]string) // Optional. A list of symbol ids
248+
// Symbols([]string) // Optional. A list of symbol ids. If empty then gets for all symbols
249249
func (client *Client) GetSymbols(
250250
ctx context.Context,
251251
arguments ...args.Argument,
@@ -276,7 +276,7 @@ func (client *Client) GetSymbol(
276276
}
277277
err = client.publicGet(
278278
ctx,
279-
endpointSymbol+"/"+params["symbol"].(string),
279+
endpointSymbol+"/"+params[internal.ArgNameSymbol].(string),
280280
nil,
281281
&result,
282282
)
@@ -291,7 +291,7 @@ func (client *Client) GetSymbol(
291291
//
292292
// Arguments:
293293
//
294-
// Symbols([]string) // Optional. A list of symbol ids
294+
// Symbols([]string) // Optional. A list of symbol ids. If empty then gets for all symbols
295295
func (client *Client) GetTickers(
296296
ctx context.Context,
297297
arguments ...args.Argument,
@@ -320,7 +320,7 @@ func (client *Client) GetTickerOfSymbol(
320320
}
321321
err = client.publicGet(
322322
ctx,
323-
endpointTicker+"/"+params["symbol"].(string),
323+
endpointTicker+"/"+params[internal.ArgNameSymbol].(string),
324324
nil,
325325
&result,
326326
)
@@ -384,7 +384,7 @@ func (client *Client) GetPricesHistory(
384384
//
385385
// Arguments:
386386
//
387-
// Symbols([]string) // Optional. A list of symbol ids
387+
// Symbols([]string) // Optional. A list of symbol ids. If empty then gets for all symbols
388388
func (client *Client) GetTickerLastPrices(
389389
ctx context.Context,
390390
arguments ...args.Argument,
@@ -416,7 +416,7 @@ func (client *Client) GetTickerLastPricesOfSymbol(
416416
}
417417
err = client.publicGet(
418418
ctx,
419-
endpointPriceTicker+"/"+params["symbol"].(string),
419+
endpointPriceTicker+"/"+params[internal.ArgNameSymbol].(string),
420420
params,
421421
&result,
422422
)
@@ -433,7 +433,7 @@ func (client *Client) GetTickerLastPricesOfSymbol(
433433
//
434434
// Arguments:
435435
//
436-
// Symbols([]string) // Optional. A list of symbol ids
436+
// Symbols([]string) // Optional. A list of symbol ids. If empty then gets for all symbols
437437
// SortBy(SortByType) // Optional. Sorting parameter. SortByID or SortByTimestamp. Default is SortByTimestamp
438438
// Sort(SortType) // Optional. Sort direction. SortASC or SortDESC. Default is SortDESC
439439
// From(string) // Optional. Initial value of the queried interval
@@ -473,7 +473,7 @@ func (client *Client) GetTradesOfSymbol(
473473
}
474474
err = client.publicGet(
475475
ctx,
476-
endpointTrade+"/"+params["symbol"].(string),
476+
endpointTrade+"/"+params[internal.ArgNameSymbol].(string),
477477
params,
478478
&result,
479479
)
@@ -490,7 +490,7 @@ func (client *Client) GetTradesOfSymbol(
490490
//
491491
// Arguments:
492492
//
493-
// Symbols([]string) // Optional. A list of symbol ids
493+
// Symbols([]string) // Optional. A list of symbol ids. If empty then gets for all symbols
494494
// Depth(int) // Optional. Order Book depth. Default value is 100. Set to 0 to view the full Order Book
495495
func (client *Client) GetOrderbooks(
496496
ctx context.Context,
@@ -536,7 +536,7 @@ func (client *Client) GetOrderBookOfSymbol(
536536
}
537537
err = client.publicGet(
538538
ctx,
539-
endpointOrderbook+"/"+params["symbol"].(string),
539+
endpointOrderbook+"/"+params[internal.ArgNameSymbol].(string),
540540
params,
541541
&response,
542542
)
@@ -576,7 +576,7 @@ func (client *Client) GetOrderBookVolumeOfSymbol(
576576
}
577577
err = client.publicGet(
578578
ctx,
579-
endpointOrderbook+"/"+params["symbol"].(string),
579+
endpointOrderbook+"/"+params[internal.ArgNameSymbol].(string),
580580
params,
581581
&response,
582582
)
@@ -587,7 +587,7 @@ func (client *Client) GetOrderBookVolumeOfSymbol(
587587

588588
// GetCandles gets a map of candles for all symbols or for specified symbols
589589
//
590-
// # Candels are used for OHLC representation
590+
// # Candles are used for OHLC representation
591591
//
592592
// The result contains candles with non-zero volume only (no trades = no candles)
593593
//
@@ -597,7 +597,7 @@ func (client *Client) GetOrderBookVolumeOfSymbol(
597597
//
598598
// Arguments:
599599
//
600-
// Symbols([]string) // A list of symbol ids
600+
// Symbols([]string) // Optional. A list of symbol ids. If empty then gets for all symbols
601601
// Period(PeriodType) // Optional. A valid tick interval. Period1Minute, Period3Minutes, Period5Minutes, Period15Minutes, Period30Minutes, Period1Hour, Period4Hours, Period1Day, Period7Days, Period1Month. Default is Period30Minutes
602602
// Sort(SortType) // Optional. Sort direction. SortASC or SortDESC. Default is SortDESC
603603
// From(string) // Optional. Initial value of the queried interval. As DateTime
@@ -608,13 +608,13 @@ func (client *Client) GetCandles(
608608
arguments ...args.Argument,
609609
) (result map[string][]models.Candle, err error) {
610610
params, _ := args.BuildParams(arguments)
611-
err = client.publicGet(ctx, endpointCandle, params, &result)
611+
err = client.publicGet(ctx, endpointCandles, params, &result)
612612
return
613613
}
614614

615615
// GetCandlesOfSymbol get candles of a symbol
616616
//
617-
// # Candels are used for OHLC representation
617+
// # Candles are used for OHLC representation
618618
//
619619
// The result contains candles with non-zero volume only (no trades = no candles)
620620
//
@@ -641,13 +641,80 @@ func (client *Client) GetCandlesOfSymbol(
641641
}
642642
err = client.publicGet(
643643
ctx,
644-
endpointCandle+"/"+params["symbol"].(string),
644+
endpointCandles+"/"+params[internal.ArgNameSymbol].(string),
645645
nil,
646646
&result,
647647
)
648648
return
649649
}
650650

651+
// GetConvertedCandles gets candles regarding the last price converted to the target currency for all symbols or for the specified symbols
652+
//
653+
// # Candles are used for OHLC representation
654+
//
655+
// The result contains candles with non-zero volume only (no trades = no candles)
656+
//
657+
// Conversion from the symbol quote currency to the target currency is the mean of "best" bid price and "best" ask price in the order book. If there is no "best" bid of ask price, the last price is returned.
658+
//
659+
// # Requires no API key Access Rights
660+
//
661+
// https://api.exchange.cryptomkt.com/#candles
662+
//
663+
// Arguments:
664+
//
665+
// TargetCurrency(string) // Target currency for conversion
666+
// Symbols([]string) // Optional. A list of symbol ids. If empty then gets for all symbols
667+
// Period(PeriodType) // Optional. A valid tick interval. Period1Minute, Period3Minutes, Period5Minutes, Period15Minutes, Period30Minutes, Period1Hour, Period4Hours, Period1Day, Period7Days, Period1Month. Default is Period30Minutes
668+
// Sort(SortType) // Optional. Sort direction. SortASC or SortDESC. Default is SortDESC
669+
// From(string) // Optional. Initial value of the queried interval. As DateTime
670+
// Till(string) // Optional. Last value of the queried interval. As DateTime
671+
// Limit(int) // Optional. Prices per currency pair. Defaul is 10. Min is 1. Max is 1000
672+
func (client *Client) GetConvertedCandles(
673+
ctx context.Context,
674+
arguments ...args.Argument,
675+
) (result models.ConvertedCandles, err error) {
676+
params, err := args.BuildParams(arguments, internal.ArgNameTargetCurrency)
677+
if err != nil {
678+
return
679+
}
680+
err = client.publicGet(ctx, endpointConvertedCandles, params, &result)
681+
return
682+
}
683+
684+
// GetConvertedCandlesOfSymbol gets candles regarding the last price converted to the target currency for the specified symbol
685+
//
686+
// # Candles are used for OHLC representation
687+
//
688+
// The result contains candles with non-zero volume only (no trades = no candles)
689+
//
690+
// Conversion from the symbol quote currency to the target currency is the mean of "best" bid price and "best" ask price in the order book. If there is no "best" bid of ask price, the last price is returned.
691+
//
692+
// # Requires no API key Access Rights
693+
//
694+
// https://api.exchange.cryptomkt.com/#candles
695+
//
696+
// Arguments:
697+
//
698+
// TargetCurrency(string) // Target currency for conversion
699+
// Symbol(string) // A symbol id
700+
// Period(PeriodType) // Optional. A valid tick interval. Period1Minute, Period3Minutes, Period5Minutes, Period15Minutes, Period30Minutes, Period1Hour, Period4Hours, Period1Day, Period7Days, Period1Month. Default is Period30Minutes
701+
// Sort(SortType) // Optional. Sort direction. SortASC or SortDESC. Default is SortDESC
702+
// From(string) // Optional. Initial value of the queried interval. As DateTime
703+
// Till(string) // Optional. Last value of the queried interval. As DateTime
704+
// Limit(int) // Optional. Prices per currency pair. Defaul is 10. Min is 1. Max is 1000
705+
// Offset(int) // Optional. Default is 0. Min is 0. Max is 100000
706+
func (client *Client) GetConvertedCandlesOfSymbol(
707+
ctx context.Context,
708+
arguments ...args.Argument,
709+
) (result models.ConvertedCandlesOfSymbol, err error) {
710+
params, err := args.BuildParams(arguments, internal.ArgNameSymbol, internal.ArgNameTargetCurrency)
711+
if err != nil {
712+
return
713+
}
714+
err = client.publicGet(ctx, endpointConvertedCandles+"/"+params[internal.ArgNameSymbol].(string), params, &result)
715+
return
716+
}
717+
651718
//////////////////
652719
// SPOT TRADING //
653720
//////////////////
@@ -728,7 +795,7 @@ func (client *Client) GetActiveSpotOrder(
728795
params, _ := args.BuildParams(arguments, internal.ArgNameClientOrderID)
729796
err = client.privateGet(
730797
ctx,
731-
endpointOrder+"/"+params["client_order_id"].(string),
798+
endpointOrder+"/"+params[internal.ArgNameClientOrderID].(string),
732799
nil,
733800
&result,
734801
)
@@ -803,7 +870,7 @@ func (client *Client) ReplaceSpotOrder(
803870
if err != nil {
804871
return
805872
}
806-
ClientOrderID := params["client_order_id"].(string)
873+
ClientOrderID := params[internal.ArgNameClientOrderID].(string)
807874
delete(params, "client_order_id")
808875
err = client.patch(ctx, endpointOrder+"/"+ClientOrderID, params, &result)
809876
return
@@ -898,7 +965,7 @@ func (client *Client) CancelSpotOrder(
898965
}
899966
err = client.delete(
900967
ctx,
901-
endpointOrder+"/"+params["client_order_id"].(string),
968+
endpointOrder+"/"+params[internal.ArgNameClientOrderID].(string),
902969
nil,
903970
&result,
904971
)
@@ -1049,7 +1116,7 @@ func (client *Client) GetWalletBalanceOfCurrency(
10491116
}
10501117
err = client.privateGet(
10511118
ctx,
1052-
endpointWalletBalance+"/"+params["currency"].(string),
1119+
endpointWalletBalance+"/"+params[internal.ArgNameCurrency].(string),
10531120
nil,
10541121
&result)
10551122
if err != nil {
@@ -1262,7 +1329,7 @@ func (client *Client) WithdrawCryptoCommit(
12621329
response := models.BooleanResponse{}
12631330
err = client.put(
12641331
ctx,
1265-
endpointCryptoWithdraw+"/"+params["id"].(string),
1332+
endpointCryptoWithdraw+"/"+params[internal.ArgNameID].(string),
12661333
nil,
12671334
&response,
12681335
)
@@ -1290,7 +1357,7 @@ func (client *Client) WithdrawCryptoRollback(
12901357
response := models.BooleanResponse{}
12911358
err = client.delete(
12921359
ctx,
1293-
endpointCryptoWithdraw+"/"+params["id"].(string),
1360+
endpointCryptoWithdraw+"/"+params[internal.ArgNameID].(string),
12941361
nil,
12951362
&response,
12961363
)
@@ -1564,7 +1631,7 @@ func (client *Client) GetTransaction(
15641631
}
15651632
err = client.privateGet(
15661633
ctx,
1567-
endpointTransactions+"/"+params["id"].(string),
1634+
endpointTransactions+"/"+params[internal.ArgNameID].(string),
15681635
nil,
15691636
&result,
15701637
)

rest/client_public_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,3 +442,58 @@ func TestGetCandlesOfSymbol(t *testing.T) {
442442
t.Error(result)
443443
})
444444
}
445+
446+
func TestGetConvertedCandles(t *testing.T) {
447+
client := NewPublicClient()
448+
t.Run("from all symbols, no arguments", func(t *testing.T) {
449+
result, err := client.GetConvertedCandles(context.Background(), args.TargetCurrency("BTC"))
450+
if err != nil {
451+
t.Fatal(err)
452+
return
453+
}
454+
for _, candles := range result.Data {
455+
for _, candle := range candles {
456+
if err = checkCandle(&candle); err != nil {
457+
t.Fatal(err)
458+
}
459+
}
460+
}
461+
})
462+
t.Run("from some symbols, limit at 2", func(t *testing.T) {
463+
result, err := client.GetConvertedCandles(context.Background(), args.Symbols([]string{"EOSETH", "ETHBTC"}), args.Limit(2), args.TargetCurrency("ETH"))
464+
if err != nil {
465+
t.Fatal(err)
466+
return
467+
}
468+
for _, candles := range result.Data {
469+
for _, candle := range candles {
470+
if err = checkCandle(&candle); err != nil {
471+
t.Fatal(err)
472+
}
473+
}
474+
}
475+
})
476+
}
477+
478+
func TestGetConvertedCandlesOfSymbol(t *testing.T) {
479+
client := NewPublicClient()
480+
t.Run("from valid symbol", func(t *testing.T) {
481+
result, err := client.GetConvertedCandlesOfSymbol(context.Background(), args.Symbol("EOSETH"), args.Limit(2), args.TargetCurrency("ETH"))
482+
if err != nil {
483+
t.Fatal(err)
484+
return
485+
}
486+
for _, candle := range result.Data {
487+
if err = checkCandle(&candle); err != nil {
488+
t.Fatal(err)
489+
}
490+
}
491+
})
492+
t.Run("from invalid symbol", func(t *testing.T) {
493+
result, err := client.GetConvertedCandlesOfSymbol(context.Background(), args.Symbol("orange"))
494+
if err != nil {
495+
return
496+
}
497+
t.Error(result)
498+
})
499+
}

rest/client_spot_trading_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func TestOrderFlow(t *testing.T) {
8585
if err = checkOrder(order); err != nil {
8686
t.Fatal(err)
8787
}
88+
time.Sleep(4 * time.Second)
8889
newClientOrderID := strconv.FormatInt(time.Now().UnixMicro(), 10)
8990
order, err = client.ReplaceSpotOrder(bg,
9091
args.ClientOrderID(order.ClientOrderID),

0 commit comments

Comments
 (0)