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;
}
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);
}
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);
}
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);
}
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);
}
Aggregations