gitbitex / gitbitex-new

an open source cryptocurrency exchange
Apache License 2.0
222 stars 84 forks source link

Candles #32

Closed Funk5Thousand closed 3 months ago

Funk5Thousand commented 1 year ago

http://127.0.0.1/api/products/BTC-USDT/candles?granularity=60 is returning candles of interval 1, or 60 seconds? Your granularity parameter is in seconds? I believe this is typically expressed as 1 (for 1 minute) 15 (for 15 minutes) 30, 60,120,240, 1440 (a day). I can resample the candles from one minute but I will have to query the candle maker every 6 hours and store the candles in a separate database.

As such the return of 1000 rows typically won't even cover a whole day and isn't very usefull on candle charts. At 1 hour intervals this will be less than 24 candles, which is not very usefull as "historical" data.

greensheng commented 1 year ago

The unit of granularity is seconds, because the API interface format of gitbitex references CoinbasePro. You can modify the code of ProductController.java to make adjustments.

@GetMapping("/api/products/{productId}/candles")
    public List<List<Object>> getProductCandles(@PathVariable String productId, @RequestParam int granularity,
                                                @RequestParam(defaultValue = "1000") int limit) {
        PagedList<Candle> candlePage = candleRepository.findAll(productId, granularity / 60, 1, limit);

        //[
        //    [ time, low, high, open, close, volume ],
        //    [ 1415398768, 0.32, 4.2, 0.35, 4.2, 12.3 ],
        //]
        List<List<Object>> lines = new ArrayList<>();
        candlePage.getItems().forEach(x -> {
            List<Object> line = new ArrayList<>();
            line.add(x.getTime());
            line.add(x.getLow().stripTrailingZeros());
            line.add(x.getHigh().stripTrailingZeros());
            line.add(x.getOpen().stripTrailingZeros());
            line.add(x.getClose().stripTrailingZeros());
            line.add(x.getVolume().stripTrailingZeros());
            lines.add(line);
        });
        return lines;
    }