Search in sources :

Example 1 with IntervalPrice

use of io.altanalytics.domain.currency.IntervalPrice in project bibot by alfintech.

the class ElasticPublisher method publishMarketData.

@Override
public void publishMarketData(List<IntervalPrice> marketDataList) throws IOException {
    BulkRequestBuilder bulkRequest = client.prepareBulk();
    for (IntervalPrice marketData : marketDataList) {
        System.out.println(marketData);
        XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("currencyPair", marketData.getCurrencyPair().toString()).field("openDate", marketData.getOpenTime()).field("closeDate", marketData.getCloseTime()).field("openPrice", BigDecimalUtil.toDouble(marketData.getOpen())).field("closePrice", BigDecimalUtil.toDouble(marketData.getClose())).field("highPrice", BigDecimalUtil.toDouble(marketData.getHigh())).field("lowPrice", BigDecimalUtil.toDouble(marketData.getLow())).field("intervalVolume", BigDecimalUtil.toDouble(marketData.getIntervalVolume())).field("dayVolume", BigDecimalUtil.toDouble(marketData.getDayVolume())).endObject();
        bulkRequest.add(client.prepareIndex("marketdata", "minutely", marketData.getCurrencyPair().toString() + marketData.getOpenTime().getTime()).setSource(builder));
    }
    BulkResponse response = bulkRequest.get();
    LOG.debug(response.toString());
}
Also used : IntervalPrice(io.altanalytics.domain.currency.IntervalPrice) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 2 with IntervalPrice

use of io.altanalytics.domain.currency.IntervalPrice in project bibot by alfintech.

the class ElasticReader method getIntervalPrices.

@Override
public List<IntervalPrice> getIntervalPrices(Date fromDate, Date toDate, CurrencyPair currencyPair) throws Exception {
    List<IntervalPrice> intervalPrices = new ArrayList<IntervalPrice>();
    SearchResponse response = client.prepareSearch("marketdata").setTypes("minutely").setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(QueryBuilders.matchQuery("currencyPair", currencyPair.toString())).setPostFilter(QueryBuilders.rangeQuery("openDate").from(fromDate.getTime()).to(toDate.getTime())).addSort("openDate", SortOrder.ASC).setSize(1000).get();
    response.getHits().forEach(new Consumer<SearchHit>() {

        @Override
        public void accept(SearchHit searchHit) {
            try {
                intervalPrices.add(parseIntervalPrice(searchHit, currencyPair));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    });
    return intervalPrices;
}
Also used : IntervalPrice(io.altanalytics.domain.currency.IntervalPrice) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) ParseException(java.text.ParseException) IOException(java.io.IOException) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 3 with IntervalPrice

use of io.altanalytics.domain.currency.IntervalPrice in project bibot by alfintech.

the class CryptoCompareRequestManager method fetch.

public List<IntervalPrice> fetch(List<IntervalPriceRequest> requests) throws InterruptedException, ExecutionException {
    List<IntervalPrice> results = new ArrayList<IntervalPrice>();
    List<Future<IntervalPrice>> futures = new ArrayList<Future<IntervalPrice>>();
    ExecutorService service = new ForkJoinPool(10);
    for (IntervalPriceRequest request : requests) {
        CryptoCompareRequestConsumer consumer = new CryptoCompareRequestConsumer(client, request);
        futures.add(service.submit(consumer));
    }
    for (Future<IntervalPrice> future : futures) {
        results.add(future.get());
    }
    return results;
}
Also used : IntervalPrice(io.altanalytics.domain.currency.IntervalPrice) ArrayList(java.util.ArrayList) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) IntervalPriceRequest(io.altanalytics.domain.currency.IntervalPriceRequest) ForkJoinPool(java.util.concurrent.ForkJoinPool)

Example 4 with IntervalPrice

use of io.altanalytics.domain.currency.IntervalPrice in project bibot by alfintech.

the class CryptoCompareHistoricRecorder method interpolateHour.

private List<IntervalPrice> interpolateHour(IntervalPrice marketData) {
    List<IntervalPrice> interpolated = new ArrayList<IntervalPrice>();
    Date slidingWindow = new Date(marketData.getOpenTime().getTime());
    while (slidingWindow.before(marketData.getCloseTime())) {
        Date openTime = new Date(slidingWindow.getTime());
        slidingWindow = DateUtil.shiftToFuture(openTime, MS_IN_TEN_SECS);
        Date closeTime = new Date(slidingWindow.getTime());
        interpolated.add(new IntervalPrice(marketData.getCurrencyPair(), openTime, closeTime, marketData.getOpen(), marketData.getClose(), marketData.getLow(), marketData.getHigh(), BigDecimalUtil.divide(marketData.getIntervalVolume(), new BigDecimal(SECONDS_IN_HOUR)), BigDecimal.ZERO));
    }
    return interpolated;
}
Also used : IntervalPrice(io.altanalytics.domain.currency.IntervalPrice) ArrayList(java.util.ArrayList) Date(java.util.Date) BigDecimal(java.math.BigDecimal)

Example 5 with IntervalPrice

use of io.altanalytics.domain.currency.IntervalPrice in project bibot by alfintech.

the class CryptoCompareHistoricClient method parseResponse.

private IntervalPrice parseResponse(CurrencyPair currencyPair, String response) throws IOException, ParseException {
    JSONParser jsonParser = new JSONParser();
    JSONObject jsonObject = (JSONObject) jsonParser.parse(response);
    JSONArray data = (JSONArray) jsonObject.get(JSON_FIELD_DATA);
    Long closeEpochTime = ((Long) jsonObject.get(JSON_FIELD_DATA_CLOSE_TIME)) * MS_IN_SEC;
    Long openEpochTime = ((Long) jsonObject.get(JSON_FIELD_DATA_OPEN_TIME)) * MS_IN_SEC;
    JSONObject dataRecord = (JSONObject) data.get(data.size() == 0 ? 0 : 1);
    Double high = parseNumber(dataRecord.get(JSON_FIELD_DATA_HIGH));
    Double low = parseNumber(dataRecord.get(JSON_FIELD_DATA_LOW));
    Double volume = parseNumber(dataRecord.get(JSON_FIELD_DATA_VOLUME));
    Double close = parseNumber(dataRecord.get(JSON_FIELD_DATA_CLOSE));
    Double open = parseNumber(dataRecord.get(JSON_FIELD_DATA_OPEN));
    return new IntervalPrice(currencyPair, new Date(openEpochTime), new Date(closeEpochTime), new BigDecimal(open), new BigDecimal(low), new BigDecimal(high), new BigDecimal(close), new BigDecimal(volume), new BigDecimal(volume));
}
Also used : IntervalPrice(io.altanalytics.domain.currency.IntervalPrice) JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) Date(java.util.Date) BigDecimal(java.math.BigDecimal)

Aggregations

IntervalPrice (io.altanalytics.domain.currency.IntervalPrice)11 Date (java.util.Date)7 ArrayList (java.util.ArrayList)5 BigDecimal (java.math.BigDecimal)4 CurrencyPair (io.altanalytics.domain.currency.CurrencyPair)3 IntervalPriceRequest (io.altanalytics.domain.currency.IntervalPriceRequest)3 JSONObject (org.json.simple.JSONObject)2 JSONParser (org.json.simple.parser.JSONParser)2 Scheduled (org.springframework.scheduling.annotation.Scheduled)2 Analytic (io.altanalytics.domain.currency.Analytic)1 IOException (java.io.IOException)1 DateFormat (java.text.DateFormat)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ExecutorService (java.util.concurrent.ExecutorService)1 ForkJoinPool (java.util.concurrent.ForkJoinPool)1 Future (java.util.concurrent.Future)1 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)1 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1