自适应网格
This commit is contained in:
parent
12bf7e893d
commit
a9b3bbb095
24
src/main/java/com/sample/trend/config/CommonConstant.java
Normal file
24
src/main/java/com/sample/trend/config/CommonConstant.java
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package com.sample.trend.config;
|
||||||
|
|
||||||
|
import vip.uuquant.exhangeapi.core.ExchangeFactory;
|
||||||
|
import vip.uuquant.exhangeapi.core.HttpClient;
|
||||||
|
import vip.uuquant.exhangeapi.core.WebSocketManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 交易所客户端全局单例
|
||||||
|
*/
|
||||||
|
public class CommonConstant {
|
||||||
|
|
||||||
|
public static final HttpClient httpClient = HttpClient.builder().build();
|
||||||
|
|
||||||
|
public static final WebSocketManager webSocketManager = new WebSocketManager(httpClient);
|
||||||
|
|
||||||
|
public static final ExchangeFactory exchangeFactory = new ExchangeFactory();
|
||||||
|
|
||||||
|
static {
|
||||||
|
webSocketManager.setLogEnabled(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private CommonConstant() {
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,59 @@
|
|||||||
|
package com.sample.trend.strategy.util;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson2.JSONArray;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.math.RoundingMode;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ATR(Average True Range)计算工具
|
||||||
|
*/
|
||||||
|
public final class AtrCalculator {
|
||||||
|
|
||||||
|
private AtrCalculator() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据 K 线数据计算 ATR。
|
||||||
|
* K 线格式:[timestamp, open, high, low, close, ...]
|
||||||
|
*
|
||||||
|
* @param klines K 线数组,按时间升序
|
||||||
|
* @param period ATR 周期
|
||||||
|
* @return ATR 值,数据不足时返回 null
|
||||||
|
*/
|
||||||
|
public static BigDecimal calculate(JSONArray klines, int period) {
|
||||||
|
if (klines == null || klines.size() < period + 1) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<BigDecimal> trueRanges = new ArrayList<>();
|
||||||
|
for (int i = 1; i < klines.size(); i++) {
|
||||||
|
JSONArray current = klines.getJSONArray(i);
|
||||||
|
JSONArray previous = klines.getJSONArray(i - 1);
|
||||||
|
|
||||||
|
BigDecimal high = current.getBigDecimal(2);
|
||||||
|
BigDecimal low = current.getBigDecimal(3);
|
||||||
|
BigDecimal prevClose = previous.getBigDecimal(4);
|
||||||
|
|
||||||
|
BigDecimal range1 = high.subtract(low);
|
||||||
|
BigDecimal range2 = high.subtract(prevClose).abs();
|
||||||
|
BigDecimal range3 = low.subtract(prevClose).abs();
|
||||||
|
|
||||||
|
BigDecimal tr = range1.max(range2).max(range3);
|
||||||
|
trueRanges.add(tr);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (trueRanges.size() < period) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
BigDecimal sum = BigDecimal.ZERO;
|
||||||
|
int start = trueRanges.size() - period;
|
||||||
|
for (int i = start; i < trueRanges.size(); i++) {
|
||||||
|
sum = sum.add(trueRanges.get(i));
|
||||||
|
}
|
||||||
|
return sum.divide(BigDecimal.valueOf(period), 8, RoundingMode.HALF_UP);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user