Im trying to code for an trading bot using the java binance api. what i`d like to do is use to async client from the library to create a method to fetch data/candlesticks and then return it.
My problem is that the async client returns the response to a callback and i have now idea how to handle it, make my method return the data like the example below:
public List<Candlestick> asyncGetCandles(){
//get the data
return response /**List<Candlestick>response*/
}
This is what i got so far:
public void asyncGetCandles() throws ParseException, IOException {
BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance();
BinanceApiAsyncRestClient asyncClient = factory.newAsyncRestClient();
long start = dateTime.startOfListing();
long now = System.currentTimeMillis();
BinanceApiCallback<List<Candlestick>> callback = response -> {
System.out.println(response);//prints 1000 candles
};
asyncClient.getCandlestickBars("BTCUSDT", CandlestickInterval.HOURLY, 1000, start, now, callback);
/**How to return the response here?*/
}
Any help on how to do this would be really appreciated!
It should look something like this:
package stackoverflow;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
public class ReturnAsyncCallback {
interface BinanceApiCallback<T> { // either this usecase with interface
BinanceApiCallback<T> run(String response);
}
abstract class BinanceApiCallback2<T> { // or this usecase with abstract base class
abstract BinanceApiCallback<T> run(String response);
}
static class Candlestick {
}
public BinanceApiCallback<List<Candlestick>> asyncGetCandles() throws ParseException, IOException {
final BinanceApiClientFactory factory = BinanceApiClientFactory.newInstance();
final BinanceApiAsyncRestClient asyncClient = factory.newAsyncRestClient();
final long start = dateTime.startOfListing();
final long now = System.currentTimeMillis();
final BinanceApiCallback<List<Candlestick>> callback = (response) -> {
System.out.println(response);//prints 1000 candles
};
asyncClient.getCandlestickBars("BTCUSDT", CandlestickInterval.HOURLY, 1000, start, now, callback);
/**How to return the response here?*/
return callback;
}
}
Note, that there are TWO ways for lambda expressions. Both need a class with exactly ONE abstract (unimplemented) method:
Additional notes for my example:
Hints for further questions: