Search in sources :

Example 1 with StandardDeviationIndicator

use of org.ta4j.core.indicators.statistics.StandardDeviationIndicator in project crypto-bot by jnidzwetzki.

the class BBreakoutStrategy method getStrategy.

public Strategy getStrategy() {
    final ClosePriceIndicator closePrice = new ClosePriceIndicator(timeSeries);
    final SMAIndicator sma = new SMAIndicator(closePrice, bbPeriod);
    final BollingerBandsMiddleIndicator bbmiddle = new BollingerBandsMiddleIndicator(sma);
    final StandardDeviationIndicator sd = new StandardDeviationIndicator(closePrice, bbPeriod);
    final BollingerBandsUpperIndicator bbup = new BollingerBandsUpperIndicator(bbmiddle, sd, Decimal.valueOf(deviationUp));
    final BollingerBandsUpperIndicator bbdown = new BollingerBandsUpperIndicator(bbmiddle, sd, Decimal.valueOf(deviationDown));
    final Rule buyingRule = new UnderIndicatorRule(closePrice, bbdown);
    final Rule sellingRule = new OverIndicatorRule(closePrice, bbup).or(new StopLossRule(closePrice, Decimal.valueOf(2)));
    final BaseStrategy strategy = new BaseStrategy(buyingRule, sellingRule);
    return strategy;
}
Also used : SMAIndicator(org.ta4j.core.indicators.SMAIndicator) OverIndicatorRule(org.ta4j.core.trading.rules.OverIndicatorRule) UnderIndicatorRule(org.ta4j.core.trading.rules.UnderIndicatorRule) BollingerBandsMiddleIndicator(org.ta4j.core.indicators.bollinger.BollingerBandsMiddleIndicator) BaseStrategy(org.ta4j.core.BaseStrategy) StopLossRule(org.ta4j.core.trading.rules.StopLossRule) StandardDeviationIndicator(org.ta4j.core.indicators.statistics.StandardDeviationIndicator) BollingerBandsUpperIndicator(org.ta4j.core.indicators.bollinger.BollingerBandsUpperIndicator) Rule(org.ta4j.core.Rule) StopLossRule(org.ta4j.core.trading.rules.StopLossRule) OverIndicatorRule(org.ta4j.core.trading.rules.OverIndicatorRule) UnderIndicatorRule(org.ta4j.core.trading.rules.UnderIndicatorRule) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator)

Example 2 with StandardDeviationIndicator

use of org.ta4j.core.indicators.statistics.StandardDeviationIndicator 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)

Example 3 with StandardDeviationIndicator

use of org.ta4j.core.indicators.statistics.StandardDeviationIndicator in project ta4j by ta4j.

the class BollingerBandWidthIndicatorTest method bollingerBandWidthUsingSMAAndStandardDeviation.

@Test
public void bollingerBandWidthUsingSMAAndStandardDeviation() {
    SMAIndicator sma = new SMAIndicator(closePrice, 5);
    StandardDeviationIndicator standardDeviation = new StandardDeviationIndicator(closePrice, 5);
    BollingerBandsMiddleIndicator bbmSMA = new BollingerBandsMiddleIndicator(sma);
    BollingerBandsUpperIndicator bbuSMA = new BollingerBandsUpperIndicator(bbmSMA, standardDeviation);
    BollingerBandsLowerIndicator bblSMA = new BollingerBandsLowerIndicator(bbmSMA, standardDeviation);
    BollingerBandWidthIndicator bandwidth = new BollingerBandWidthIndicator(bbuSMA, bbmSMA, bblSMA);
    assertDecimalEquals(bandwidth.getValue(0), 0.0);
    assertDecimalEquals(bandwidth.getValue(1), 36.3636);
    assertDecimalEquals(bandwidth.getValue(2), 66.6423);
    assertDecimalEquals(bandwidth.getValue(3), 60.2443);
    assertDecimalEquals(bandwidth.getValue(4), 71.0767);
    assertDecimalEquals(bandwidth.getValue(5), 69.9394);
    assertDecimalEquals(bandwidth.getValue(6), 62.7043);
    assertDecimalEquals(bandwidth.getValue(7), 56.0178);
    assertDecimalEquals(bandwidth.getValue(8), 27.683);
    assertDecimalEquals(bandwidth.getValue(9), 12.6491);
    assertDecimalEquals(bandwidth.getValue(10), 12.6491);
    assertDecimalEquals(bandwidth.getValue(11), 24.2956);
    assertDecimalEquals(bandwidth.getValue(12), 68.3332);
    assertDecimalEquals(bandwidth.getValue(13), 85.1469);
    assertDecimalEquals(bandwidth.getValue(14), 112.8481);
    assertDecimalEquals(bandwidth.getValue(15), 108.1682);
    assertDecimalEquals(bandwidth.getValue(16), 66.9328);
    assertDecimalEquals(bandwidth.getValue(17), 56.5194);
    assertDecimalEquals(bandwidth.getValue(18), 28.1091);
    assertDecimalEquals(bandwidth.getValue(19), 32.5362);
}
Also used : SMAIndicator(org.ta4j.core.indicators.SMAIndicator) StandardDeviationIndicator(org.ta4j.core.indicators.statistics.StandardDeviationIndicator) Test(org.junit.Test)

Example 4 with StandardDeviationIndicator

use of org.ta4j.core.indicators.statistics.StandardDeviationIndicator in project ta4j by ta4j.

the class BollingerBandsLowerIndicatorTest method bollingerBandsLowerUsingSMAAndStandardDeviation.

@Test
public void bollingerBandsLowerUsingSMAAndStandardDeviation() {
    BollingerBandsMiddleIndicator bbmSMA = new BollingerBandsMiddleIndicator(sma);
    StandardDeviationIndicator standardDeviation = new StandardDeviationIndicator(closePrice, timeFrame);
    BollingerBandsLowerIndicator bblSMA = new BollingerBandsLowerIndicator(bbmSMA, standardDeviation);
    assertDecimalEquals(bblSMA.getK(), 2);
    assertDecimalEquals(bblSMA.getValue(0), 1);
    assertDecimalEquals(bblSMA.getValue(1), 0.5);
    assertDecimalEquals(bblSMA.getValue(2), 0.367);
    assertDecimalEquals(bblSMA.getValue(3), 1.367);
    assertDecimalEquals(bblSMA.getValue(4), 2.3905);
    assertDecimalEquals(bblSMA.getValue(5), 2.7239);
    assertDecimalEquals(bblSMA.getValue(6), 2.367);
    BollingerBandsLowerIndicator bblSMAwithK = new BollingerBandsLowerIndicator(bbmSMA, standardDeviation, Decimal.valueOf("1.5"));
    assertDecimalEquals(bblSMAwithK.getK(), 1.5);
    assertDecimalEquals(bblSMAwithK.getValue(0), 1);
    assertDecimalEquals(bblSMAwithK.getValue(1), 0.75);
    assertDecimalEquals(bblSMAwithK.getValue(2), 0.7752);
    assertDecimalEquals(bblSMAwithK.getValue(3), 1.7752);
    assertDecimalEquals(bblSMAwithK.getValue(4), 2.6262);
    assertDecimalEquals(bblSMAwithK.getValue(5), 2.9595);
    assertDecimalEquals(bblSMAwithK.getValue(6), 2.7752);
}
Also used : StandardDeviationIndicator(org.ta4j.core.indicators.statistics.StandardDeviationIndicator) Test(org.junit.Test)

Example 5 with StandardDeviationIndicator

use of org.ta4j.core.indicators.statistics.StandardDeviationIndicator in project ta4j by ta4j.

the class BollingerBandsUpperIndicatorTest method bollingerBandsUpperUsingSMAAndStandardDeviation.

@Test
public void bollingerBandsUpperUsingSMAAndStandardDeviation() {
    BollingerBandsMiddleIndicator bbmSMA = new BollingerBandsMiddleIndicator(sma);
    StandardDeviationIndicator standardDeviation = new StandardDeviationIndicator(closePrice, timeFrame);
    BollingerBandsUpperIndicator bbuSMA = new BollingerBandsUpperIndicator(bbmSMA, standardDeviation);
    assertDecimalEquals(bbuSMA.getK(), 2);
    assertDecimalEquals(bbuSMA.getValue(0), 1);
    assertDecimalEquals(bbuSMA.getValue(1), 2.5);
    assertDecimalEquals(bbuSMA.getValue(2), 3.633);
    assertDecimalEquals(bbuSMA.getValue(3), 4.633);
    assertDecimalEquals(bbuSMA.getValue(4), 4.2761);
    assertDecimalEquals(bbuSMA.getValue(5), 4.6094);
    assertDecimalEquals(bbuSMA.getValue(6), 5.633);
    assertDecimalEquals(bbuSMA.getValue(7), 5.2761);
    assertDecimalEquals(bbuSMA.getValue(8), 5.633);
    assertDecimalEquals(bbuSMA.getValue(9), 4.2761);
    BollingerBandsUpperIndicator bbuSMAwithK = new BollingerBandsUpperIndicator(bbmSMA, standardDeviation, Decimal.valueOf("1.5"));
    assertDecimalEquals(bbuSMAwithK.getK(), 1.5);
    assertDecimalEquals(bbuSMAwithK.getValue(0), 1);
    assertDecimalEquals(bbuSMAwithK.getValue(1), 2.25);
    assertDecimalEquals(bbuSMAwithK.getValue(2), 3.2247);
    assertDecimalEquals(bbuSMAwithK.getValue(3), 4.2247);
    assertDecimalEquals(bbuSMAwithK.getValue(4), 4.0404);
    assertDecimalEquals(bbuSMAwithK.getValue(5), 4.3737);
    assertDecimalEquals(bbuSMAwithK.getValue(6), 5.2247);
    assertDecimalEquals(bbuSMAwithK.getValue(7), 5.0404);
    assertDecimalEquals(bbuSMAwithK.getValue(8), 5.2247);
    assertDecimalEquals(bbuSMAwithK.getValue(9), 4.0404);
}
Also used : StandardDeviationIndicator(org.ta4j.core.indicators.statistics.StandardDeviationIndicator) Test(org.junit.Test)

Aggregations

StandardDeviationIndicator (org.ta4j.core.indicators.statistics.StandardDeviationIndicator)6 Test (org.junit.Test)3 ClosePriceIndicator (org.ta4j.core.indicators.helpers.ClosePriceIndicator)3 TimeSeries (org.ta4j.core.TimeSeries)2 SMAIndicator (org.ta4j.core.indicators.SMAIndicator)2 BollingerBandsMiddleIndicator (org.ta4j.core.indicators.bollinger.BollingerBandsMiddleIndicator)2 BollingerBandsUpperIndicator (org.ta4j.core.indicators.bollinger.BollingerBandsUpperIndicator)2 BufferedWriter (java.io.BufferedWriter)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 JFreeChart (org.jfree.chart.JFreeChart)1 DateAxis (org.jfree.chart.axis.DateAxis)1 XYPlot (org.jfree.chart.plot.XYPlot)1 TimeSeriesCollection (org.jfree.data.time.TimeSeriesCollection)1 BaseStrategy (org.ta4j.core.BaseStrategy)1 Rule (org.ta4j.core.Rule)1 EMAIndicator (org.ta4j.core.indicators.EMAIndicator)1 BollingerBandsLowerIndicator (org.ta4j.core.indicators.bollinger.BollingerBandsLowerIndicator)1 PriceVariationIndicator (org.ta4j.core.indicators.helpers.PriceVariationIndicator)1