use of org.ta4j.core.indicators.helpers.ClosePriceIndicator in project ta4j by ta4j.
the class IndicatorsToChart method main.
public static void main(String[] args) {
/*
Getting time series
*/
TimeSeries series = CsvBarsLoader.loadAppleIncSeries();
/*
Creating indicators
*/
// Close price
ClosePriceIndicator closePrice = new ClosePriceIndicator(series);
EMAIndicator avg14 = new EMAIndicator(closePrice, 14);
StandardDeviationIndicator sd14 = new StandardDeviationIndicator(closePrice, 14);
// Bollinger bands
BollingerBandsMiddleIndicator middleBBand = new BollingerBandsMiddleIndicator(avg14);
BollingerBandsLowerIndicator lowBBand = new BollingerBandsLowerIndicator(middleBBand, sd14);
BollingerBandsUpperIndicator upBBand = new BollingerBandsUpperIndicator(middleBBand, sd14);
/*
Building chart dataset
*/
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(buildChartTimeSeries(series, closePrice, "Apple Inc. (AAPL) - NASDAQ GS"));
dataset.addSeries(buildChartTimeSeries(series, lowBBand, "Low Bollinger Band"));
dataset.addSeries(buildChartTimeSeries(series, upBBand, "High Bollinger Band"));
/*
Creating the chart
*/
JFreeChart chart = ChartFactory.createTimeSeriesChart(// title
"Apple Inc. 2013 Close Prices", // x-axis label
"Date", // y-axis label
"Price Per Unit", // data
dataset, // create legend?
true, // generate tooltips?
true, // generate URLs?
false);
XYPlot plot = (XYPlot) chart.getPlot();
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd"));
/*
Displaying the chart
*/
displayChart(chart);
}
use of org.ta4j.core.indicators.helpers.ClosePriceIndicator in project ta4j by ta4j.
the class StopGainRuleTest method setUp.
@Before
public void setUp() {
tradingRecord = new BaseTradingRecord();
closePrice = new ClosePriceIndicator(new MockTimeSeries(100, 105, 110, 120, 150, 120, 160, 180));
}
use of org.ta4j.core.indicators.helpers.ClosePriceIndicator in project ta4j by ta4j.
the class StopLossRuleTest method setUp.
@Before
public void setUp() {
tradingRecord = new BaseTradingRecord();
closePrice = new ClosePriceIndicator(new MockTimeSeries(100, 105, 110, 120, 100, 150, 110, 100));
}
use of org.ta4j.core.indicators.helpers.ClosePriceIndicator in project ta4j by ta4j.
the class Quickstart method main.
public static void main(String[] args) {
// Getting a time series (from any provider: CSV, web service, etc.)
TimeSeries series = CsvTradesLoader.loadBitstampSeries();
// Getting the close price of the bars
Decimal firstClosePrice = series.getBar(0).getClosePrice();
System.out.println("First close price: " + firstClosePrice.doubleValue());
// Or within an indicator:
ClosePriceIndicator closePrice = new ClosePriceIndicator(series);
// Here is the same close price:
// equal to firstClosePrice
System.out.println(firstClosePrice.isEqual(closePrice.getValue(0)));
// Getting the simple moving average (SMA) of the close price over the last 5 bars
SMAIndicator shortSma = new SMAIndicator(closePrice, 5);
// Here is the 5-bars-SMA value at the 42nd index
System.out.println("5-bars-SMA value at the 42nd index: " + shortSma.getValue(42).doubleValue());
// Getting a longer SMA (e.g. over the 30 last bars)
SMAIndicator longSma = new SMAIndicator(closePrice, 30);
// Ok, now let's building our trading rules!
// Buying rules
// We want to buy:
// - if the 5-bars SMA crosses over 30-bars SMA
// - or if the price goes below a defined price (e.g $800.00)
Rule buyingRule = new CrossedUpIndicatorRule(shortSma, longSma).or(new CrossedDownIndicatorRule(closePrice, Decimal.valueOf("800")));
// Selling rules
// We want to sell:
// - if the 5-bars SMA crosses under 30-bars SMA
// - or if if the price looses more than 3%
// - or if the price earns more than 2%
Rule sellingRule = new CrossedDownIndicatorRule(shortSma, longSma).or(new StopLossRule(closePrice, Decimal.valueOf("3"))).or(new StopGainRule(closePrice, Decimal.valueOf("2")));
// Running our juicy trading strategy...
TimeSeriesManager seriesManager = new TimeSeriesManager(series);
TradingRecord tradingRecord = seriesManager.run(new BaseStrategy(buyingRule, sellingRule));
System.out.println("Number of trades for our strategy: " + tradingRecord.getTradeCount());
// Analysis
// Getting the cash flow of the resulting trades
CashFlow cashFlow = new CashFlow(series, tradingRecord);
// Getting the profitable trades ratio
AnalysisCriterion profitTradesRatio = new AverageProfitableTradesCriterion();
System.out.println("Profitable trades ratio: " + profitTradesRatio.calculate(series, tradingRecord));
// Getting the reward-risk ratio
AnalysisCriterion rewardRiskRatio = new RewardRiskRatioCriterion();
System.out.println("Reward-risk ratio: " + rewardRiskRatio.calculate(series, tradingRecord));
// Total profit of our strategy
// vs total profit of a buy-and-hold strategy
AnalysisCriterion vsBuyAndHold = new VersusBuyAndHoldCriterion(new TotalProfitCriterion());
System.out.println("Our profit vs buy-and-hold profit: " + vsBuyAndHold.calculate(series, tradingRecord));
// Your turn!
}
use of org.ta4j.core.indicators.helpers.ClosePriceIndicator in project ta4j by ta4j.
the class BuyAndSellSignalsToChart 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);
/*
Building chart datasets
*/
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(buildChartTimeSeries(series, new ClosePriceIndicator(series), "Bitstamp Bitcoin (BTC)"));
/*
Creating the chart
*/
JFreeChart chart = ChartFactory.createTimeSeriesChart(// title
"Bitstamp BTC", // x-axis label
"Date", // y-axis label
"Price", // data
dataset, // 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"));
/*
Running the strategy and adding the buy and sell signals to plot
*/
addBuySellSignals(series, strategy, plot);
/*
Displaying the chart
*/
displayChart(chart);
}
Aggregations