Ta4j是用于技术分析的开源Java库。它提供了创建,评估和执行交易策略的基本组件。
关于技术分析:
Ta4j在Maven上可用。您可以在eclipse中创建一个Maven项目,并将ta4j依赖项添加到pom.xml文件中。
<dependency>
<groupId>org.ta4j</groupId>
<artifactId>ta4j-core</artifactId>
<version>0.13</version>
</dependency>
另一种方法可以是克隆此git存储库,也可以简单地下载此库并将源代码添加到现有的Eclipse项目中。
在此快速示例中,我们将对价格时间序列上的交易策略进行回测。
在开始时,我们只需要一个时间序列。
// Creating a time series (from any provider: CSV, web service, etc.)
BarSeries series = new BaseBarSeriesBuilder().withName("AXP_Stock").build();
创建a之后,TimeSeries
我们可以将OHLC数据和体积添加到系列中:
// adding open, high, low, close and volume data to the series
series.addBar(ZonedDateTime.now(), 105.42, 112.99, 104.01, 111.42, 1337);
请参阅“条形和条形”部分,以了解时间序列并了解如何构造一个。
我们可以计算该柱系列的指标,以便通过研究过去的市场数据来预测价格的方向。
// Getting the close price of the ticks
Num 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:
System.out.println(firstClosePrice.isEqual(closePrice.getValue(0))); // equal to firstClosePrice
// Getting the simple moving average (SMA) of the close price over the last 5 ticks
SMAIndicator shortSma = new SMAIndicator(closePrice, 5);
// Here is the 5-ticks-SMA value at the 42nd index
System.out.println("5-ticks-SMA value at the 42nd index: " + shortSma.getValue(42).doubleValue());
// Getting a longer SMA (e.g. over the 30 last ticks)
SMAIndicator longSma = new SMAIndicator(closePrice, 30);
Ta4j包含130多个技术指标。
然后,我们必须建立我们的交易策略。它由两个交易规则组成:一个用于进入,另一个用于退出。
// Buying rules
// We want to buy:
// - if the 5-ticks SMA crosses over 30-ticks SMA
// - or if the price goes below a defined price (e.g $800.00)
Rule buyingRule = new CrossedUpIndicatorRule(shortSma, longSma)
.or(new CrossedDownIndicatorRule(closePrice, 800d));
// Selling rules
// We want to sell:
// - if the 5-ticks SMA crosses under 30-ticks SMA
// - or if the price loses more than 3%
// - or if the price earns more than 2%
Rule sellingRule = new CrossedDownIndicatorRule(shortSma, longSma)
.or(new StopLossRule(closePrice, 3.0))
.or(new StopGainRule(closePrice, 2.0));
Strategy strategy = new BaseStrategy(buyingRule, sellingRule);
Ta4j带有一组基本的交易规则/策略,可以使用布尔运算符进行组合。
回测步骤非常简单:
// Running our juicy trading strategy...
BarSeriesManager manager = new BarSeriesManager(series);
TradingRecord tradingRecord = manager.run(strategy);
System.out.println("Number of trades for our strategy: " + tradingRecord.getTradeCount());
这是我们可以分析回测结果的方法:
// 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));
可以根据一组分析标准轻松地比较交易策略。
Ta4j还可以用于具有更复杂策略的实时交易。查看其余的文档和示例。
免责声明:好库网所展示的信息由买卖双方自行提供,其真实性、准确性和合法性由信息发布人负责。好库网不提供任何保证,并不承担任何法律责任。