Search in sources :

Example 1 with Analytic

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

the class ElasticPublisher method publishAnalytics.

@Override
public void publishAnalytics(List<Analytic> analytics) throws IOException {
    BulkRequestBuilder bulkRequest = client.prepareBulk();
    for (Analytic analytic : analytics) {
        System.out.println(analytic);
        XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("currencyPair", analytic.getCurrencyPair().toString()).field("intervalVolume", BigDecimalUtil.toDouble(analytic.getIntervalVolume())).field("dayVolume", BigDecimalUtil.toDouble(analytic.getDayAverage())).field("percentageVolume", BigDecimalUtil.toDouble(analytic.getPercentageVolume())).field("percentageAllTimeHigh", BigDecimalUtil.toDouble(analytic.getPercentageAllTimeHigh())).field("date", analytic.getDate()).endObject();
        bulkRequest.add(client.prepareIndex("marketdataanalytic", "minutely", analytic.getCurrencyPair().toString() + analytic.getDate().getTime()).setSource(builder));
    }
    BulkResponse response = bulkRequest.get();
    LOG.debug(response.toString());
}
Also used : Analytic(io.altanalytics.domain.currency.Analytic) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 2 with Analytic

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

the class AnalyticsRecorder method tick.

@Scheduled(cron = "${recorder.analytics.schedule}")
public void tick() throws Exception {
    if (active) {
        List<CurrencyPair> currencyPairs = CurrencyPairUtil.constructCurrencyPairs(tradeCurrencies, baseCurrencies);
        Date analyticsEndDate = DateUtil.now();
        Date analyticsStartDate = DateUtil.shiftToPast(analyticsEndDate, interval);
        List<Analytic> analytics = new ArrayList<Analytic>();
        for (CurrencyPair currencyPair : currencyPairs) {
            // Volume calcs
            List<IntervalPrice> intervalPrices = reader.getIntervalPrices(analyticsStartDate, analyticsEndDate, currencyPair);
            BigDecimal intervalVolume = VolumeCalculator.cumulative(intervalPrices);
            BigDecimal dayVolume = intervalPrices.get(0).getDayVolume();
            BigDecimal dayAverageVolume = VolumeCalculator.average(dayVolume, interval);
            BigDecimal percentageVolume = BigDecimalUtil.multiply(BigDecimalUtil.divide(intervalVolume, dayAverageVolume), new BigDecimal(100));
            // ATH % calcs
            BigDecimal allTimeHigh = reader.getAllTimeHigh(currencyPair).getClose();
            BigDecimal currentPrice = intervalPrices.get(intervalPrices.size() - 1).getClose();
            BigDecimal percentageATH = BigDecimalUtil.divide(currentPrice, allTimeHigh);
            Analytic analytic = new Analytic(currencyPair, intervalVolume, dayAverageVolume, percentageVolume, percentageATH, analyticsEndDate);
            analytics.add(analytic);
        }
        publisher.publishAnalytics(analytics);
    }
}
Also used : IntervalPrice(io.altanalytics.domain.currency.IntervalPrice) Analytic(io.altanalytics.domain.currency.Analytic) ArrayList(java.util.ArrayList) Date(java.util.Date) BigDecimal(java.math.BigDecimal) CurrencyPair(io.altanalytics.domain.currency.CurrencyPair) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Aggregations

Analytic (io.altanalytics.domain.currency.Analytic)2 CurrencyPair (io.altanalytics.domain.currency.CurrencyPair)1 IntervalPrice (io.altanalytics.domain.currency.IntervalPrice)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)1 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 Scheduled (org.springframework.scheduling.annotation.Scheduled)1