Search in sources :

Example 11 with ClosePriceIndicator

use of org.ta4j.core.indicators.helpers.ClosePriceIndicator in project ta4j by ta4j.

the class SimpleLinearRegressionIndicatorTest method setUp.

@Before
public void setUp() {
    double[] data = new double[] { 10, 20, 30, 40, 30, 40, 30, 20, 30, 50, 60, 70, 80 };
    closePrice = new ClosePriceIndicator(new MockTimeSeries(data));
}
Also used : MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) Before(org.junit.Before)

Example 12 with ClosePriceIndicator

use of org.ta4j.core.indicators.helpers.ClosePriceIndicator in project ta4j by ta4j.

the class StandardDeviationIndicatorTest method standardDeviationShouldBeZeroWhenTimeFrameIs1.

@Test
public void standardDeviationShouldBeZeroWhenTimeFrameIs1() {
    StandardDeviationIndicator sdv = new StandardDeviationIndicator(new ClosePriceIndicator(data), 1);
    assertDecimalEquals(sdv.getValue(3), 0);
    assertDecimalEquals(sdv.getValue(8), 0);
}
Also used : ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) Test(org.junit.Test)

Example 13 with ClosePriceIndicator

use of org.ta4j.core.indicators.helpers.ClosePriceIndicator in project ta4j by ta4j.

the class CashFlowToChart method main.

public static void main(String[] args) {
    // Getting the time series
    TimeSeries series = CsvTradesLoader.loadBitstampSeries();
    // Building the trading strategy
    Strategy strategy = MovingMomentumStrategy.buildStrategy(series);
    // Running the strategy
    TimeSeriesManager seriesManager = new TimeSeriesManager(series);
    TradingRecord tradingRecord = seriesManager.run(strategy);
    // Getting the cash flow of the resulting trades
    CashFlow cashFlow = new CashFlow(series, tradingRecord);
    /*
          Building chart datasets
         */
    TimeSeriesCollection datasetAxis1 = new TimeSeriesCollection();
    datasetAxis1.addSeries(buildChartTimeSeries(series, new ClosePriceIndicator(series), "Bitstamp Bitcoin (BTC)"));
    TimeSeriesCollection datasetAxis2 = new TimeSeriesCollection();
    datasetAxis2.addSeries(buildChartTimeSeries(series, cashFlow, "Cash Flow"));
    /*
          Creating the chart
         */
    JFreeChart chart = ChartFactory.createTimeSeriesChart(// title
    "Bitstamp BTC", // x-axis label
    "Date", // y-axis label
    "Price", // data
    datasetAxis1, // create legend?
    true, // generate tooltips?
    true, // generate URLs?
    false);
    XYPlot plot = (XYPlot) chart.getPlot();
    DateAxis axis = (DateAxis) plot.getDomainAxis();
    axis.setDateFormatOverride(new SimpleDateFormat("MM-dd HH:mm"));
    /*
          Adding the cash flow axis (on the right)
         */
    addCashFlowAxis(plot, datasetAxis2);
    /*
          Displaying the chart
         */
    displayChart(chart);
}
Also used : CashFlow(org.ta4j.core.analysis.CashFlow) DateAxis(org.jfree.chart.axis.DateAxis) XYPlot(org.jfree.chart.plot.XYPlot) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) MovingMomentumStrategy(ta4jexamples.strategies.MovingMomentumStrategy) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) SimpleDateFormat(java.text.SimpleDateFormat) JFreeChart(org.jfree.chart.JFreeChart)

Example 14 with ClosePriceIndicator

use of org.ta4j.core.indicators.helpers.ClosePriceIndicator in project ta4j by ta4j.

the class CandlestickChart method createAdditionalDataset.

/**
 * Builds an additional JFreeChart dataset from a ta4j time series.
 * @param series a time series
 * @return an additional dataset
 */
private static TimeSeriesCollection createAdditionalDataset(TimeSeries series) {
    ClosePriceIndicator indicator = new ClosePriceIndicator(series);
    TimeSeriesCollection dataset = new TimeSeriesCollection();
    org.jfree.data.time.TimeSeries chartTimeSeries = new org.jfree.data.time.TimeSeries("Btc price");
    for (int i = 0; i < series.getBarCount(); i++) {
        Bar bar = series.getBar(i);
        chartTimeSeries.add(new Second(new Date(bar.getEndTime().toEpochSecond() * 1000)), indicator.getValue(i).toDouble());
    }
    dataset.addSeries(chartTimeSeries);
    return dataset;
}
Also used : Bar(org.ta4j.core.Bar) TimeSeries(org.ta4j.core.TimeSeries) Second(org.jfree.data.time.Second) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) Date(java.util.Date)

Example 15 with ClosePriceIndicator

use of org.ta4j.core.indicators.helpers.ClosePriceIndicator in project ta4j by ta4j.

the class IndicatorsToCsv method main.

public static void main(String[] args) {
    /*
          Getting time series
         */
    TimeSeries series = CsvTradesLoader.loadBitstampSeries();
    /*
          Creating indicators
         */
    // Close price
    ClosePriceIndicator closePrice = new ClosePriceIndicator(series);
    // Typical price
    TypicalPriceIndicator typicalPrice = new TypicalPriceIndicator(series);
    // Price variation
    PriceVariationIndicator priceVariation = new PriceVariationIndicator(series);
    // Simple moving averages
    SMAIndicator shortSma = new SMAIndicator(closePrice, 8);
    SMAIndicator longSma = new SMAIndicator(closePrice, 20);
    // Exponential moving averages
    EMAIndicator shortEma = new EMAIndicator(closePrice, 8);
    EMAIndicator longEma = new EMAIndicator(closePrice, 20);
    // Percentage price oscillator
    PPOIndicator ppo = new PPOIndicator(closePrice, 12, 26);
    // Rate of change
    ROCIndicator roc = new ROCIndicator(closePrice, 100);
    // Relative strength index
    RSIIndicator rsi = new RSIIndicator(closePrice, 14);
    // Williams %R
    WilliamsRIndicator williamsR = new WilliamsRIndicator(series, 20);
    // Average true range
    ATRIndicator atr = new ATRIndicator(series, 20);
    // Standard deviation
    StandardDeviationIndicator sd = new StandardDeviationIndicator(closePrice, 14);
    /*
          Building header
         */
    StringBuilder sb = new StringBuilder("timestamp,close,typical,variation,sma8,sma20,ema8,ema20,ppo,roc,rsi,williamsr,atr,sd\n");
    /*
          Adding indicators values
         */
    final int nbBars = series.getBarCount();
    for (int i = 0; i < nbBars; i++) {
        sb.append(series.getBar(i).getEndTime()).append(',').append(closePrice.getValue(i)).append(',').append(typicalPrice.getValue(i)).append(',').append(priceVariation.getValue(i)).append(',').append(shortSma.getValue(i)).append(',').append(longSma.getValue(i)).append(',').append(shortEma.getValue(i)).append(',').append(longEma.getValue(i)).append(',').append(ppo.getValue(i)).append(',').append(roc.getValue(i)).append(',').append(rsi.getValue(i)).append(',').append(williamsR.getValue(i)).append(',').append(atr.getValue(i)).append(',').append(sd.getValue(i)).append('\n');
    }
    /*
          Writing CSV file
         */
    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new FileWriter("indicators.csv"));
        writer.write(sb.toString());
    } catch (IOException ioe) {
        Logger.getLogger(IndicatorsToCsv.class.getName()).log(Level.SEVERE, "Unable to write CSV file", ioe);
    } finally {
        try {
            if (writer != null) {
                writer.close();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}
Also used : TimeSeries(org.ta4j.core.TimeSeries) FileWriter(java.io.FileWriter) IOException(java.io.IOException) PriceVariationIndicator(org.ta4j.core.indicators.helpers.PriceVariationIndicator) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) BufferedWriter(java.io.BufferedWriter) TypicalPriceIndicator(org.ta4j.core.indicators.helpers.TypicalPriceIndicator) StandardDeviationIndicator(org.ta4j.core.indicators.statistics.StandardDeviationIndicator)

Aggregations

ClosePriceIndicator (org.ta4j.core.indicators.helpers.ClosePriceIndicator)81 Test (org.junit.Test)55 MockTimeSeries (org.ta4j.core.mocks.MockTimeSeries)26 TimeSeries (org.ta4j.core.TimeSeries)16 Before (org.junit.Before)14 SMAIndicator (org.ta4j.core.indicators.SMAIndicator)7 OverIndicatorRule (org.ta4j.core.trading.rules.OverIndicatorRule)6 ArrayList (java.util.ArrayList)5 TimeSeriesCollection (org.jfree.data.time.TimeSeriesCollection)5 MockBar (org.ta4j.core.mocks.MockBar)5 UnderIndicatorRule (org.ta4j.core.trading.rules.UnderIndicatorRule)5 SimpleDateFormat (java.text.SimpleDateFormat)4 JFreeChart (org.jfree.chart.JFreeChart)4 DateAxis (org.jfree.chart.axis.DateAxis)4 XYPlot (org.jfree.chart.plot.XYPlot)4 EMAIndicator (org.ta4j.core.indicators.EMAIndicator)4 CrossedDownIndicatorRule (org.ta4j.core.trading.rules.CrossedDownIndicatorRule)4 BaseStrategy (org.ta4j.core.BaseStrategy)3 Decimal (org.ta4j.core.Decimal)3 MaxPriceIndicator (org.ta4j.core.indicators.helpers.MaxPriceIndicator)3