Search in sources :

Example 31 with Bar

use of org.ta4j.core.Bar in project ta4j by ta4j.

the class CsvTradesLoader method buildBars.

/**
 * Builds a list of populated bars from csv data.
 * @param beginTime the begin time of the whole period
 * @param endTime the end time of the whole period
 * @param duration the bar duration (in seconds)
 * @param lines the csv data returned by CSVReader.readAll()
 * @return the list of populated bars
 */
private static List<Bar> buildBars(ZonedDateTime beginTime, ZonedDateTime endTime, int duration, List<String[]> lines) {
    List<Bar> bars = new ArrayList<>();
    Duration barDuration = Duration.ofSeconds(duration);
    ZonedDateTime barEndTime = beginTime;
    // line number of trade data
    int i = 0;
    do {
        // build a bar
        barEndTime = barEndTime.plus(barDuration);
        Bar bar = new BaseBar(barDuration, barEndTime);
        do {
            // get a trade
            String[] tradeLine = lines.get(i);
            ZonedDateTime tradeTimeStamp = ZonedDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(tradeLine[0]) * 1000), ZoneId.systemDefault());
            // if the trade happened during the bar
            if (bar.inPeriod(tradeTimeStamp)) {
                // add the trade to the bar
                double tradePrice = Double.parseDouble(tradeLine[1]);
                double tradeAmount = Double.parseDouble(tradeLine[2]);
                bar.addTrade(tradeAmount, tradePrice);
            } else {
                // this break will drop us after the inner "while", skipping the increment
                break;
            }
            i++;
        } while (i < lines.size());
        // this is where the break drops to
        if (bar.getTrades() > 0) {
            bars.add(bar);
        }
    } while (barEndTime.isBefore(endTime));
    return bars;
}
Also used : Bar(org.ta4j.core.Bar) BaseBar(org.ta4j.core.BaseBar) ZonedDateTime(java.time.ZonedDateTime) BaseBar(org.ta4j.core.BaseBar) ArrayList(java.util.ArrayList) Duration(java.time.Duration)

Example 32 with Bar

use of org.ta4j.core.Bar in project ta4j by ta4j.

the class CsvTradesLoader method loadBitstampSeries.

/**
 * @return a time series from Bitstamp (bitcoin exchange) trades
 */
public static TimeSeries loadBitstampSeries() {
    // Reading all lines of the CSV file
    InputStream stream = CsvTradesLoader.class.getClassLoader().getResourceAsStream("bitstamp_trades_from_20131125_usd.csv");
    CSVReader csvReader = null;
    List<String[]> lines = null;
    try {
        csvReader = new CSVReader(new InputStreamReader(stream, Charset.forName("UTF-8")), ',');
        lines = csvReader.readAll();
        // Removing header line
        lines.remove(0);
    } catch (IOException ioe) {
        Logger.getLogger(CsvTradesLoader.class.getName()).log(Level.SEVERE, "Unable to load trades from CSV", ioe);
    } finally {
        if (csvReader != null) {
            try {
                csvReader.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }
    List<Bar> bars = null;
    if ((lines != null) && !lines.isEmpty()) {
        // Getting the first and last trades timestamps
        ZonedDateTime beginTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(lines.get(0)[0]) * 1000), ZoneId.systemDefault());
        ZonedDateTime endTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(lines.get(lines.size() - 1)[0]) * 1000), ZoneId.systemDefault());
        if (beginTime.isAfter(endTime)) {
            Instant beginInstant = beginTime.toInstant();
            Instant endInstant = endTime.toInstant();
            beginTime = ZonedDateTime.ofInstant(endInstant, ZoneId.systemDefault());
            endTime = ZonedDateTime.ofInstant(beginInstant, ZoneId.systemDefault());
            // Since the CSV file has the most recent trades at the top of the file, we'll reverse the list to feed the List<Bar> correctly.
            Collections.reverse(lines);
        }
        // build the list of populated bars
        bars = buildBars(beginTime, endTime, 300, lines);
    }
    return new BaseTimeSeries("bitstamp_trades", bars);
}
Also used : Bar(org.ta4j.core.Bar) BaseBar(org.ta4j.core.BaseBar) InputStreamReader(java.io.InputStreamReader) CSVReader(com.opencsv.CSVReader) ZonedDateTime(java.time.ZonedDateTime) InputStream(java.io.InputStream) Instant(java.time.Instant) BaseTimeSeries(org.ta4j.core.BaseTimeSeries) IOException(java.io.IOException)

Example 33 with Bar

use of org.ta4j.core.Bar in project ta4j by ta4j.

the class PlusDMIndicatorTest method zeroDirectionalMovement3.

@Test
public void zeroDirectionalMovement3() {
    MockBar yesterdayBar = new MockBar(0, 0, 6, 20);
    MockBar todayBar = new MockBar(0, 0, 12, 4);
    List<Bar> bars = new ArrayList<Bar>();
    bars.add(yesterdayBar);
    bars.add(todayBar);
    MockTimeSeries series = new MockTimeSeries(bars);
    PlusDMIndicator dup = new PlusDMIndicator(series);
    assertDecimalEquals(dup.getValue(1), 0);
}
Also used : MockBar(org.ta4j.core.mocks.MockBar) Bar(org.ta4j.core.Bar) MockBar(org.ta4j.core.mocks.MockBar) ArrayList(java.util.ArrayList) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) Test(org.junit.Test)

Example 34 with Bar

use of org.ta4j.core.Bar in project ta4j by ta4j.

the class PlusDMIndicatorTest method positiveDirectionalMovement.

@Test
public void positiveDirectionalMovement() {
    MockBar yesterdayBar = new MockBar(0, 0, 6, 6);
    MockBar todayBar = new MockBar(0, 0, 12, 4);
    List<Bar> bars = new ArrayList<Bar>();
    bars.add(yesterdayBar);
    bars.add(todayBar);
    MockTimeSeries series = new MockTimeSeries(bars);
    PlusDMIndicator dup = new PlusDMIndicator(series);
    assertDecimalEquals(dup.getValue(1), 6);
}
Also used : MockBar(org.ta4j.core.mocks.MockBar) Bar(org.ta4j.core.Bar) MockBar(org.ta4j.core.mocks.MockBar) ArrayList(java.util.ArrayList) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) Test(org.junit.Test)

Example 35 with Bar

use of org.ta4j.core.Bar in project ta4j by ta4j.

the class PlusDMIndicatorTest method zeroDirectionalMovement2.

@Test
public void zeroDirectionalMovement2() {
    MockBar yesterdayBar = new MockBar(0, 0, 6, 12);
    MockBar todayBar = new MockBar(0, 0, 12, 6);
    List<Bar> bars = new ArrayList<Bar>();
    bars.add(yesterdayBar);
    bars.add(todayBar);
    MockTimeSeries series = new MockTimeSeries(bars);
    PlusDMIndicator dup = new PlusDMIndicator(series);
    assertDecimalEquals(dup.getValue(1), 0);
}
Also used : MockBar(org.ta4j.core.mocks.MockBar) Bar(org.ta4j.core.Bar) MockBar(org.ta4j.core.mocks.MockBar) ArrayList(java.util.ArrayList) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) Test(org.junit.Test)

Aggregations

Bar (org.ta4j.core.Bar)104 ArrayList (java.util.ArrayList)60 MockBar (org.ta4j.core.mocks.MockBar)48 MockTimeSeries (org.ta4j.core.mocks.MockTimeSeries)42 Before (org.junit.Before)37 Test (org.junit.Test)33 Decimal (org.ta4j.core.Decimal)20 IOException (java.io.IOException)16 BaseTimeSeries (org.ta4j.core.BaseTimeSeries)16 BitfinexCurrencyPair (com.github.jnidzwetzki.bitfinex.v2.entity.BitfinexCurrencyPair)15 Timeframe (com.github.jnidzwetzki.bitfinex.v2.entity.Timeframe)15 ZonedDateTime (java.time.ZonedDateTime)15 CountDownLatch (java.util.concurrent.CountDownLatch)15 BiConsumer (java.util.function.BiConsumer)15 BarMerger (com.github.jnidzwetzki.cryptobot.util.BarMerger)14 ParseException (java.text.ParseException)14 SimpleDateFormat (java.text.SimpleDateFormat)14 Assert (org.junit.Assert)14 BaseBar (org.ta4j.core.BaseBar)12 TimeSeries (org.ta4j.core.TimeSeries)9