本文整理汇总了Java中eu.verdelhan.ta4j.Decimal类的典型用法代码示例。如果您正苦于以下问题:Java Decimal类的具体用法?Java Decimal怎么用?Java Decimal使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Decimal类属于eu.verdelhan.ta4j包,在下文中一共展示了Decimal类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: BuySignalIndicator
import eu.verdelhan.ta4j.Decimal; //导入依赖的package包/类
public BuySignalIndicator(TimeSeries series, int timeframe, Decimal momentum) {
super(series);
this.momentum = momentum;
ClosePriceIndicator closePriceIndicator = new ClosePriceIndicator(series);
EMAIndicator emaIndicator = new EMAIndicator(closePriceIndicator, timeframe);
ParabolicSarIndicator sarIndicator = new ParabolicSarIndicator(series, timeframe);
this.adxIndicator = new AverageDirectionalMovementIndicator(series, timeframe);
// wait for stable turn from bearish to bullish market
this.swapIndicator = new SwapIndicator(closePriceIndicator, sarIndicator);
// consider prices above ema to be in upswing
this.upSwingIndicator = new UpSwingIndicator(closePriceIndicator, emaIndicator);
}
示例2: buildStrategy
import eu.verdelhan.ta4j.Decimal; //导入依赖的package包/类
/**
* @param series a time series
* @return a CCI correction strategy
*/
public static Strategy buildStrategy(TimeSeries series) {
if (series == null) {
throw new IllegalArgumentException("Series cannot be null");
}
CCIIndicator longCci = new CCIIndicator(series, 200);
CCIIndicator shortCci = new CCIIndicator(series, 5);
Decimal plus100 = Decimal.HUNDRED;
Decimal minus100 = Decimal.valueOf(-100);
Rule entryRule = new OverIndicatorRule(longCci, plus100) // Bull trend
.and(new UnderIndicatorRule(shortCci, minus100)); // Signal
Rule exitRule = new UnderIndicatorRule(longCci, minus100) // Bear trend
.and(new OverIndicatorRule(shortCci, plus100)); // Signal
Strategy strategy = new Strategy(entryRule, exitRule);
strategy.setUnstablePeriod(5);
return strategy;
}
示例3: buildStrategy
import eu.verdelhan.ta4j.Decimal; //导入依赖的package包/类
/**
* @param series a time series
* @return a global extrema strategy
*/
public static Strategy buildStrategy(TimeSeries series) {
if (series == null) {
throw new IllegalArgumentException("Series cannot be null");
}
ClosePriceIndicator closePrices = new ClosePriceIndicator(series);
// Getting the max price over the past week
MaxPriceIndicator maxPrices = new MaxPriceIndicator(series);
HighestValueIndicator weekMaxPrice = new HighestValueIndicator(maxPrices, NB_TICKS_PER_WEEK);
// Getting the min price over the past week
MinPriceIndicator minPrices = new MinPriceIndicator(series);
LowestValueIndicator weekMinPrice = new LowestValueIndicator(minPrices, NB_TICKS_PER_WEEK);
// Going long if the close price goes below the min price
MultiplierIndicator downWeek = new MultiplierIndicator(weekMinPrice, Decimal.valueOf("1.004"));
Rule buyingRule = new UnderIndicatorRule(closePrices, downWeek);
// Going short if the close price goes above the max price
MultiplierIndicator upWeek = new MultiplierIndicator(weekMaxPrice, Decimal.valueOf("0.996"));
Rule sellingRule = new OverIndicatorRule(closePrices, upWeek);
return new Strategy(buyingRule, sellingRule);
}
示例4: main
import eu.verdelhan.ta4j.Decimal; //导入依赖的package包/类
public static void main(String[] args) {
// load data as TimeSeries
Loader loader = new Loader();
TimeSeries series = loader.getMinuteTimeSeries("ta4j-strategies\\src\\main\\data\\fb_minutes.csv", "Facebook");
// create and initialize a strategy
SimpleRangeScalper simpleRangeScalper = new SimpleRangeScalper();
simpleRangeScalper.initStrategy(series);
// run strategy on time series and analyse results
StrategyAnalyser analyser = new StrategyAnalyser();
analyser.printAllResults(simpleRangeScalper);
// change parameters of the strategy and run again
simpleRangeScalper.setParams(20, Decimal.valueOf(0.5));
analyser.printAllResults(simpleRangeScalper);
}
示例5: calculate
import eu.verdelhan.ta4j.Decimal; //导入依赖的package包/类
@Override
protected Decimal calculate(int index) {
Decimal sum1 = Decimal.ZERO;
Decimal sum2 = Decimal.ZERO;
for (int i = Math.max(0, index - timeFrame + 1); i <= index; i++) {
Decimal close = indicator.getValue(i);
Decimal prevClose = previousPriceIndicator.getValue(i);
Decimal d = close.minus(prevClose);
if (d.compareTo(Decimal.ZERO) >= 0) {
sum1 = sum1.plus(d);
} else {
sum2 = sum2.plus(d.multipliedBy(NEGATIVE_ONE));
}
}
return sum1.minus(sum2).dividedBy(sum1.plus(sum2)).multipliedBy
(Decimal.HUNDRED);
}
示例6: CCICorrectionStrategy
import eu.verdelhan.ta4j.Decimal; //导入依赖的package包/类
public CCICorrectionStrategy(TimeSeries series, ClosePriceIndicator closePriceIndicator) {
super(series, CCICorrectionStrategy.class.getSimpleName(), closePriceIndicator);
if (series == null) {
throw new IllegalArgumentException("Series cannot be null");
}
CCIIndicator longCci = new CCIIndicator(series, 200);
CCIIndicator shortCci = new CCIIndicator(series, 5);
Decimal plus100 = Decimal.HUNDRED;
Decimal minus100 = Decimal.valueOf(-100);
Rule entryRule = new OverIndicatorRule(longCci, plus100) // Bull trend
.and(new UnderIndicatorRule(shortCci, minus100)); // Signal
Rule exitRule = new UnderIndicatorRule(longCci, minus100) // Bear trend
.and(new OverIndicatorRule(shortCci, plus100)); // Signal
this.strategy = new Strategy(entryRule, exitRule);
}
示例7: calculateRegressionLine
import eu.verdelhan.ta4j.Decimal; //导入依赖的package包/类
/**
* Calculates the regression line.
* @param startIndex the start index (inclusive) in the time series
* @param endIndex the end index (inclusive) in the time series
*/
private void calculateRegressionLine(int startIndex, int endIndex) {
// First pass: compute xBar and yBar
Decimal sumX = Decimal.ZERO;
Decimal sumY = Decimal.ZERO;
for (int i = startIndex; i <= endIndex; i++) {
sumX = sumX.plus(Decimal.valueOf(i));
sumY = sumY.plus(indicator.getValue(i));
}
Decimal nbObservations = Decimal.valueOf(endIndex - startIndex + 1);
Decimal xBar = sumX.dividedBy(nbObservations);
Decimal yBar = sumY.dividedBy(nbObservations);
// Second pass: compute slope and intercept
Decimal xxBar = Decimal.ZERO;
Decimal xyBar = Decimal.ZERO;
for (int i = startIndex; i <= endIndex; i++) {
Decimal dX = Decimal.valueOf(i).minus(xBar);
Decimal dY = indicator.getValue(i).minus(yBar);
xxBar = xxBar.plus(dX.multipliedBy(dX));
xyBar = xyBar.plus(dX.multipliedBy(dY));
}
slope = xyBar.dividedBy(xxBar);
intercept = yBar.minus(slope.multipliedBy(xBar));
}
示例8: isSatisfied
import eu.verdelhan.ta4j.Decimal; //导入依赖的package包/类
@Override
public boolean isSatisfied(int index, TradingRecord tradingRecord) {
boolean satisfied = false;
// No trading history or no trade opened, no gain
if (tradingRecord != null) {
Trade currentTrade = tradingRecord.getCurrentTrade();
if (currentTrade.isOpened()) {
Decimal entryPrice = currentTrade.getEntry().getPrice();
Decimal currentPrice = closePrice.getValue(index);
Decimal threshold = entryPrice.multipliedBy(gainRatioThreshold);
if (currentTrade.getEntry().isBuy()) {
satisfied = currentPrice.isGreaterThanOrEqual(threshold);
} else {
satisfied = currentPrice.isLessThanOrEqual(threshold);
}
}
}
traceIsSatisfied(index, satisfied);
return satisfied;
}
示例9: calculate
import eu.verdelhan.ta4j.Decimal; //导入依赖的package包/类
/**
* Calculates the cash flow for a single trade.
* @param trade a single trade
*/
private void calculate(Trade trade) {
final int entryIndex = trade.getEntry().getIndex();
int begin = entryIndex + 1;
if (begin > values.size()) {
Decimal lastValue = values.get(values.size() - 1);
values.addAll(Collections.nCopies(begin - values.size(), lastValue));
}
int end = trade.getExit().getIndex();
for (int i = Math.max(begin, 1); i <= end; i++) {
Decimal ratio;
if (trade.getEntry().isBuy()) {
ratio = timeSeries.getTick(i).getClosePrice().dividedBy(timeSeries.getTick(entryIndex).getClosePrice());
} else {
ratio = timeSeries.getTick(entryIndex).getClosePrice().dividedBy(timeSeries.getTick(i).getClosePrice());
}
values.add(values.get(entryIndex).multipliedBy(ratio));
}
}
示例10: calculateMaximumDrawdown
import eu.verdelhan.ta4j.Decimal; //导入依赖的package包/类
/**
* Calculates the maximum drawdown from a cash flow over a series.
* @param series the time series
* @param cashFlow the cash flow
* @return the maximum drawdown from a cash flow over a series
*/
private Decimal calculateMaximumDrawdown(TimeSeries series, CashFlow cashFlow) {
Decimal maximumDrawdown = Decimal.ZERO;
Decimal maxPeak = Decimal.ZERO;
if (!series.isEmpty()) {
// The series is not empty
for (int i = series.getBeginIndex(); i <= series.getEndIndex(); i++) {
Decimal value = cashFlow.getValue(i);
if (value.isGreaterThan(maxPeak)) {
maxPeak = value;
}
Decimal drawdown = maxPeak.minus(value).dividedBy(maxPeak);
if (drawdown.isGreaterThan(maximumDrawdown)) {
maximumDrawdown = drawdown;
}
}
}
return maximumDrawdown;
}
示例11: calculate
import eu.verdelhan.ta4j.Decimal; //导入依赖的package包/类
@Override
public double calculate(TimeSeries series, TradingRecord tradingRecord) {
int numberOfProfitable = 0;
for (Trade trade : tradingRecord.getTrades()) {
int entryIndex = trade.getEntry().getIndex();
int exitIndex = trade.getExit().getIndex();
Decimal result;
if (trade.getEntry().isBuy()) {
// buy-then-sell trade
result = series.getTick(exitIndex).getClosePrice().dividedBy(series.getTick(entryIndex).getClosePrice());
} else {
// sell-then-buy trade
result = series.getTick(entryIndex).getClosePrice().dividedBy(series.getTick(exitIndex).getClosePrice());
}
if (result.isGreaterThan(Decimal.ONE)) {
numberOfProfitable++;
}
}
return ((double) numberOfProfitable) / tradingRecord.getTradeCount();
}
示例12: isSatisfied
import eu.verdelhan.ta4j.Decimal; //导入依赖的package包/类
@Test
public void isSatisfied() {
final Decimal tradedAmount = Decimal.ONE;
// 5% stop-loss
rule = new StopLossRule(closePrice, Decimal.valueOf("5"));
assertFalse(rule.isSatisfied(0, null));
assertFalse(rule.isSatisfied(1, tradingRecord));
// Enter at 114
tradingRecord.enter(2, Decimal.valueOf("114"), tradedAmount);
assertFalse(rule.isSatisfied(2, tradingRecord));
assertFalse(rule.isSatisfied(3, tradingRecord));
assertTrue(rule.isSatisfied(4, tradingRecord));
// Exit
tradingRecord.exit(5);
// Enter at 128
tradingRecord.enter(5, Decimal.valueOf("128"), tradedAmount);
assertFalse(rule.isSatisfied(5, tradingRecord));
assertTrue(rule.isSatisfied(6, tradingRecord));
assertTrue(rule.isSatisfied(7, tradingRecord));
}
示例13: isSatisfied
import eu.verdelhan.ta4j.Decimal; //导入依赖的package包/类
@Test
public void isSatisfied() {
final Decimal tradedAmount = Decimal.ONE;
// 30% stop-gain
rule = new StopGainRule(closePrice, Decimal.valueOf("30"));
assertFalse(rule.isSatisfied(0, null));
assertFalse(rule.isSatisfied(1, tradingRecord));
// Enter at 108
tradingRecord.enter(2, Decimal.valueOf("108"), tradedAmount);
assertFalse(rule.isSatisfied(2, tradingRecord));
assertFalse(rule.isSatisfied(3, tradingRecord));
assertTrue(rule.isSatisfied(4, tradingRecord));
// Exit
tradingRecord.exit(5);
// Enter at 118
tradingRecord.enter(5, Decimal.valueOf("118"), tradedAmount);
assertFalse(rule.isSatisfied(5, tradingRecord));
assertTrue(rule.isSatisfied(6, tradingRecord));
assertTrue(rule.isSatisfied(7, tradingRecord));
}
示例14: testStrategies
import eu.verdelhan.ta4j.Decimal; //导入依赖的package包/类
@Test
public void testStrategies() {
PeriodicalGrowthRateIndicator gri = new PeriodicalGrowthRateIndicator(this.closePrice,5);
// Rules
Rule buyingRule = new CrossedUpIndicatorRule(gri, Decimal.ZERO);
Rule sellingRule = new CrossedDownIndicatorRule(gri, Decimal.ZERO);
Strategy strategy = new BaseStrategy(buyingRule, sellingRule);
// Check trades
int result = seriesManager.run(strategy).getTradeCount();
int expResult = 3;
assertEquals(expResult, result);
}
示例15: calculate
import eu.verdelhan.ta4j.Decimal; //导入依赖的package包/类
@Override
protected Boolean calculate(int index) {
if (index < 1) {
// Harami is a 2-candle pattern
return false;
}
Tick prevTick = series.getTick(index-1);
Tick currTick = series.getTick(index);
if (prevTick.isBearish() && currTick.isBullish()) {
final Decimal prevOpenPrice = prevTick.getOpenPrice();
final Decimal prevClosePrice = prevTick.getClosePrice();
final Decimal currOpenPrice = currTick.getOpenPrice();
final Decimal currClosePrice = currTick.getClosePrice();
return currOpenPrice.isLessThan(prevOpenPrice) && currOpenPrice.isGreaterThan(prevClosePrice)
&& currClosePrice.isLessThan(prevOpenPrice) && currClosePrice.isGreaterThan(prevClosePrice);
}
return false;
}