Skip to content

Commit 24c20e0

Browse files
committed
feat: converted candles
1 parent 592c238 commit 24c20e0

11 files changed

+353
-16
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.cryptomarket.sdk.models;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
import com.squareup.moshi.Json;
7+
8+
/**
9+
* Candle
10+
*/
11+
public class ConvertedCandles {
12+
@Json(name = "target_currency")
13+
private String targetCurrency;
14+
private Map<String, List<Candle>> data;
15+
16+
public String getTargetCurrency() {
17+
return targetCurrency;
18+
}
19+
20+
public void setTargetCurrency(String targetCurrency) {
21+
this.targetCurrency = targetCurrency;
22+
}
23+
24+
public Map<String, List<Candle>> getData() {
25+
return data;
26+
}
27+
28+
public void setData(Map<String, List<Candle>> data) {
29+
this.data = data;
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return "ConvertedCandles [targetCurrency=" + targetCurrency + ", data=" + data + "]";
35+
}
36+
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.cryptomarket.sdk.models;
2+
3+
import java.util.List;
4+
5+
import com.squareup.moshi.Json;
6+
7+
/**
8+
* Candle
9+
*/
10+
public class ConvertedCandlesBySymbol {
11+
@Json(name = "target_currency")
12+
private String targetCurrency;
13+
private List<Candle> data;
14+
15+
public String getTargetCurrency() {
16+
return targetCurrency;
17+
}
18+
19+
public void setTargetCurrency(String targetCurrency) {
20+
this.targetCurrency = targetCurrency;
21+
}
22+
23+
public List<Candle> getData() {
24+
return data;
25+
}
26+
27+
public void setData(List<Candle> data) {
28+
this.data = data;
29+
}
30+
31+
@Override
32+
public String toString() {
33+
return "ConvertedCandles [targetCurrency=" + targetCurrency + ", data=" + data + "]";
34+
}
35+
36+
}

src/main/java/com/cryptomarket/sdk/rest/CryptomarketRestClient.java

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import com.cryptomarket.sdk.models.Balance;
2929
import com.cryptomarket.sdk.models.Candle;
3030
import com.cryptomarket.sdk.models.Commission;
31+
import com.cryptomarket.sdk.models.ConvertedCandles;
32+
import com.cryptomarket.sdk.models.ConvertedCandlesBySymbol;
3133
import com.cryptomarket.sdk.models.Currency;
3234
import com.cryptomarket.sdk.models.Fee;
3335
import com.cryptomarket.sdk.models.FeeRequest;
@@ -485,6 +487,122 @@ public List<Candle> getCandlesBySymbol(
485487
public List<Candle> getCandlesBySymbol(ParamsBuilder paramsBuilder)
486488
throws CryptomarketSDKException;
487489

490+
/**
491+
* Gets data with a map of candles regarding the last price converted to the
492+
* target currency for all symbols or for the specified symbols. indexed by
493+
* symbol
494+
* <p>
495+
* Candels are used for OHLC representation
496+
* <p>
497+
* The result contains candles with non-zero volume only (no trades = no
498+
* candles)
499+
* <p>
500+
* Conversion from the symbol quote currency to the target currency is the mean
501+
* of "best" bid price and "best" ask price in the order book. If there is no
502+
* "best" bid or ask price, the last price is returned.
503+
* <p>
504+
* Requires no API key Access Rights
505+
* <p>
506+
* https://api.exchange.cryptomkt.com/#candles
507+
*
508+
* @param targetCurrency Target currency for conversion
509+
* @param symbols A list of symbol ids
510+
* @param period Optional. A valid tick interval. 'M1' (one minute),
511+
* 'M3', 'M5', 'M15', 'M30', 'H1' (one hour), 'H4', 'D1'
512+
* (one day), 'D7', '1M' (one month). Default is 'M30'
513+
* @param sort Optional. Sort direction. 'ASC' or 'DESC'. Default is
514+
* 'DESC'
515+
* @param from Optional. Initial value of the queried interval. As
516+
* DateTime
517+
* @param till Optional. Last value of the queried interval. As
518+
* DateTime
519+
* @param limit Optional. Prices per currency pair. Defaul is 10. Min
520+
* is 1. Max is 1000
521+
* @return A class with the target currency and data with a map with a list of
522+
* candles for each symbol of the query. indexed by symbol
523+
* @throws CryptomarketSDKException
524+
*/
525+
public ConvertedCandles getConvertedCandles(
526+
String targetCurrency,
527+
@Nullable List<String> symbols,
528+
@Nullable Period period,
529+
@Nullable Sort sort,
530+
@Nullable String from,
531+
@Nullable String till,
532+
@Nullable Integer limit)
533+
throws CryptomarketSDKException;
534+
535+
/**
536+
* @see #getConvertedCandles(String, List, Period, Sort, String, String,
537+
* Integer)
538+
* @param paramsBuilder
539+
* @return
540+
* @throws CryptomarketSDKException
541+
*/
542+
public ConvertedCandles getConvertedCandles(ParamsBuilder paramsBuilder)
543+
throws CryptomarketSDKException;
544+
545+
/**
546+
* Gets data with a list of candles regarding the last price converted to the
547+
* target currency for the specified symbols
548+
* <p>
549+
* Candels are used for OHLC representation
550+
* <p>
551+
* The result contains candles with non-zero volume only (no trades = no
552+
* candles)
553+
* <p>
554+
* Conversion from the symbol quote currency to the target currency is the mean
555+
* of "best" bid price and "best" ask price in the order book. If there is no
556+
* "best" bid or ask price, the last price is returned.
557+
* <p>
558+
* Requires no API key Access Rights
559+
* <p>
560+
* https://api.exchange.cryptomkt.com/#candles
561+
*
562+
* @param targetCurrency Target currency for conversion
563+
* @param symbol A symbol id
564+
* @param period Optional. A valid tick interval. 'M1' (one minute),
565+
* 'M3',
566+
* 'M5',
567+
* 'M15', 'M30', 'H1' (one hour), 'H4', 'D1' (one day),
568+
* 'D7',
569+
* '1M'
570+
* (one month). Default is 'M30'
571+
* @param sort Optional. Sort direction. 'ASC' or 'DESC'. Default is
572+
* 'DESC'
573+
* @param from Optional. Initial value of the queried interval. As
574+
* DateTime
575+
* @param till Optional. Last value of the queried interval. As
576+
* DateTime
577+
* @param limit Optional. Prices per currency pair. Defaul is 10. Min
578+
* is 1. Max is 1000
579+
* @param offset Optional. Default is 0. Min is 0. Max is 100000
580+
* @return A class with the target currency and data with a list of candles for
581+
* the symbol of the query.
582+
* @throws CryptomarketSDKException
583+
*/
584+
public ConvertedCandlesBySymbol getConvertedCandlesBySymbol(
585+
String targetCurrency,
586+
@Nullable String symbol,
587+
@Nullable Period period,
588+
@Nullable Sort sort,
589+
@Nullable String from,
590+
@Nullable String till,
591+
@Nullable Integer limit,
592+
@Nullable Integer offset)
593+
throws CryptomarketSDKException;
594+
595+
/**
596+
* @see #getConvertedCandlesBySymbol(String, String, Period, Sort, String,
597+
* String, Integer,
598+
* Integer)
599+
* @param paramsBuilder
600+
* @return
601+
* @throws CryptomarketSDKException
602+
*/
603+
public ConvertedCandlesBySymbol getConvertedCandlesBySymbol(ParamsBuilder paramsBuilder)
604+
throws CryptomarketSDKException;
605+
488606
/// AUTHENTICATED CALLS ///
489607

490608
// SPOT TRADING
@@ -1418,7 +1536,8 @@ public SubAccount getSubAccount(String subAccountId)
14181536
* @return The transaction id of the tranfer
14191537
* @throws CryptomarketSDKException
14201538
*/
1421-
public String transferFunds(String subAccountId, String amount, String currency, SubAccountTransferType transferType)
1539+
public String transferFunds(String subAccountId, String amount, String currency,
1540+
SubAccountTransferType transferType)
14221541
throws CryptomarketSDKException;
14231542

14241543
/**

src/main/java/com/cryptomarket/sdk/rest/CryptomarketRestClientImpl.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import com.cryptomarket.sdk.models.Balance;
3232
import com.cryptomarket.sdk.models.Candle;
3333
import com.cryptomarket.sdk.models.Commission;
34+
import com.cryptomarket.sdk.models.ConvertedCandles;
35+
import com.cryptomarket.sdk.models.ConvertedCandlesBySymbol;
3436
import com.cryptomarket.sdk.models.Currency;
3537
import com.cryptomarket.sdk.models.Fee;
3638
import com.cryptomarket.sdk.models.FeeRequest;
@@ -374,6 +376,50 @@ public List<Candle> getCandlesBySymbol(ParamsBuilder paramsBuilder)
374376
return adapter.listFromJson(jsonResponse, Candle.class);
375377
}
376378

379+
@Override
380+
public ConvertedCandles getConvertedCandles(String targetCurrency, List<String> symbols, Period period, Sort sort,
381+
String from, String till, Integer limit) throws CryptomarketSDKException {
382+
return getConvertedCandles(new ParamsBuilder()
383+
.targetCurrency(targetCurrency)
384+
.symbols(symbols)
385+
.period(period)
386+
.sort(sort)
387+
.from(from)
388+
.till(till)
389+
.limit(limit));
390+
}
391+
392+
@Override
393+
public ConvertedCandles getConvertedCandles(ParamsBuilder paramsBuilder) throws CryptomarketSDKException {
394+
paramsBuilder.checkRequired(Arrays.asList(ArgNames.TARGET_CURRENCY));
395+
String jsonResponse = httpClient.publicGet("public/converted/candles", paramsBuilder.build());
396+
return adapter.objectFromJson(jsonResponse, ConvertedCandles.class);
397+
}
398+
399+
@Override
400+
public ConvertedCandlesBySymbol getConvertedCandlesBySymbol(String targetCurrency, String symbol, Period period,
401+
Sort sort, String from, String till, Integer limit, Integer offset) throws CryptomarketSDKException {
402+
return getConvertedCandlesBySymbol(new ParamsBuilder()
403+
.targetCurrency(targetCurrency)
404+
.symbol(symbol)
405+
.period(period)
406+
.sort(sort)
407+
.from(from)
408+
.till(till)
409+
.limit(limit)
410+
.offset(offset));
411+
}
412+
413+
@Override
414+
public ConvertedCandlesBySymbol getConvertedCandlesBySymbol(ParamsBuilder paramsBuilder)
415+
throws CryptomarketSDKException {
416+
paramsBuilder.checkRequired(Arrays.asList(ArgNames.TARGET_CURRENCY, ArgNames.SYMBOL));
417+
String symbol = (String) paramsBuilder.remove(ArgNames.SYMBOL);
418+
String jsonResponse = httpClient.publicGet(
419+
String.format("public/converted/candles/%s", symbol),
420+
paramsBuilder.build());
421+
return adapter.objectFromJson(jsonResponse, ConvertedCandlesBySymbol.class);
422+
}
377423
// SPOT TRADING
378424

379425
@Override
@@ -749,7 +795,7 @@ public List<Fee> getEstimateWithdrawalFees(List<FeeRequest> feeRequests) throws
749795
String jsonResponse = httpClient.post(
750796
"wallet/crypto/fees/estimate",
751797
payload);
752-
System.out.println(jsonResponse);
798+
System.out.println(jsonResponse);
753799
return adapter.listFromJson(jsonResponse, Fee.class);
754800
}
755801

src/main/java/com/cryptomarket/sdk/rest/HttpClientImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ private URI buildUri(String endpoint, Map<String, String> params) throws Cryptom
145145
}
146146
}
147147

148+
@Override
148149
public String post(String endpoint, Map<String, String> payload) throws CryptomarketSDKException {
149150
String strPayload = "";
150151
if (payload != null)

src/main/java/com/cryptomarket/sdk/websocket/CryptomarketWSMarketDataClient.java

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void subscribeToTrades(
7676
* @param period Optional. A valid tick interval. 'M1' (one
7777
* minute), 'M3', 'M5', 'M15', 'M30', 'H1' (one
7878
* hour), 'H4', 'D1' (one day), 'D7', '1M' (one
79-
* month). Default is 'M30'
79+
* month).
8080
* @param symbols A list of symbol ids to subscribe
8181
* @param limit Optional. Number of historical entries returned
8282
* in the first feed. Min is 0. Max is 1000.
@@ -93,6 +93,44 @@ public void subscribeToCandles(
9393
@Nullable Integer limit,
9494
@Nullable BiConsumer<List<String>, CryptomarketSDKException> resultBiConsumer);
9595

96+
/**
97+
* subscribe to a feed of candles regarding the last price converted to the
98+
* target currency for the specified symbols
99+
* <p>
100+
* subscription is for the specified symbols
101+
* <p>
102+
* normal subscriptions have one update message per symbol
103+
* <p>
104+
* Conversion from the symbol quote currency to the target currency is the mean
105+
* of "best" bid price and "best" ask price in the order book. If there is no
106+
* "best" bid of ask price, the last price is returned.
107+
* <p>
108+
* https://api.exchange.cryptomkt.com/#subscribe-to-candles
109+
*
110+
* @param notificationBiConsumer recieves a feed of candles as a map of them,
111+
* indexed by symbol id, and the
112+
* type of notification, either SNAPSHOT or UPDATE
113+
* @param symbols A list of symbol ids to subscribe
114+
* @param period Optional. A valid tick interval. 'M1' (one
115+
* minute), 'M3', 'M5', 'M15', 'M30', 'H1' (one
116+
* hour), 'H4', 'D1' (one day), 'D7', '1M' (one
117+
* month).
118+
* @param limit Optional. Number of historical entries returned
119+
* in the first feed. Min is 0. Max is 1000.
120+
* Default is 0
121+
* @param resultBiConsumer Optional. recieves a list of successfully
122+
* subscribed symbols, and a
123+
* CryptomarketSDKException if
124+
* there was a problem (or null if there was none)
125+
*/
126+
public void subscribeToConvertedCandles(
127+
BiConsumer<Map<String, List<WSCandle>>, NotificationType> notificationBiConsumer,
128+
String targetCurrency,
129+
Period period,
130+
List<String> symbols,
131+
@Nullable Integer limit,
132+
@Nullable BiConsumer<List<String>, CryptomarketSDKException> resultBiConsumer);
133+
96134
/**
97135
* subscribe to a feed of price rates
98136
* <p>
@@ -101,7 +139,7 @@ public void subscribeToCandles(
101139
* normal subscription have one update message per currency
102140
* <p>
103141
* https://api.exchange.cryptomkt.com/#subscribe-to-price-rates
104-
*
142+
*
105143
* @param notificationBiConsumer recieves a feed of price rates as a map of
106144
* them,
107145
* indexed by currency id, and the
@@ -130,7 +168,7 @@ public void subscribeToPriceRates(
130168
* batch subscriptions have a joined update for all symbols
131169
* <p>
132170
* https://api.exchange.cryptomkt.com/#subscribe-to-price-rates
133-
*
171+
*
134172
* @param notificationBiConsumer recieves a feed of price rates as a map of
135173
* them,
136174
* indexed by currency id, and the

src/main/java/com/cryptomarket/sdk/websocket/CryptomarketWSMarketDataClientImpl.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,21 @@ public void subscribeToCandles(
147147
resultBiConsumer);
148148
}
149149

150+
@Override
151+
public void subscribeToConvertedCandles(
152+
BiConsumer<Map<String, List<WSCandle>>, NotificationType> notificationBiConsumer, String targetCurrency,
153+
Period period, List<String> symbols, Integer limit,
154+
BiConsumer<List<String>, CryptomarketSDKException> resultBiConsumer) {
155+
ParamsBuilder params = new ParamsBuilder().targetCurrency(targetCurrency).symbolList(symbols).limit(limit);
156+
String channel = String.format("converted/candles/%s", period);
157+
makeSubscriptionWithListInterceptors(
158+
channel,
159+
params,
160+
WSCandle.class,
161+
notificationBiConsumer,
162+
resultBiConsumer);
163+
}
164+
150165
@Override
151166
public void subscribeToPriceRates(
152167
BiConsumer<Map<String, WSPriceRate>, NotificationType> notificationBiConsumer,

0 commit comments

Comments
 (0)