下面列出了com.alibaba.fastjson.JSONArray#getBigDecimal ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 解析订单详情
* [
* 333168795, ID integer Trade database id
* "tLTCUSD", PAIR string Pair (BTCUSD, …)
* 1547796266000, MTS_CREATE integer Execution timestamp
* 21597693840, ORDER_ID integer Order id
* -0.4, EXEC_AMOUNT float Positive means buy, negative means sell
* 31.962, EXEC_PRICE float Execution price
* null, _PLACEHOLDER,
* null, _PLACEHOLDER,
* -1, MAKER int 1 if true, -1 if false
* -0.0255696, FEE float Fee
* "USD" FEE_CURRENCY string Fee currency
* ]
* @param result 元素数据
* @return 订单详情列表
*/
public static List<OrderDetail> parseOrderDetails(String result) {
JSONArray r = JSON.parseArray(result);
List<OrderDetail> details = new ArrayList<>(r.size());
for (int i = 0; i < r.size(); i++) {
JSONArray t = r.getJSONArray(i);
String data = r.getString(i);
Long time = t.getLong(2);
String orderId = t.getString(3);
String detailId = t.getString(0);
BigDecimal price = t.getBigDecimal(5);
BigDecimal amount = t.getBigDecimal(4).abs();
BigDecimal fee = t.getBigDecimal(9).abs();
String feeCurrency = t.getString(10);
details.add(new OrderDetail(data, time, orderId, detailId, price, amount, fee, feeCurrency));
}
return details;
}
/**
* 根据序号解析Kline
*
* @param result 原始文件
* @param r array
* @param time 时间
* @return Kline
*/
public static Kline parseKlineByIndex(String result, JSONArray r, Long time, int first) {
BigDecimal open = r.getBigDecimal(first);
BigDecimal high = r.getBigDecimal(first + 1);
BigDecimal low = r.getBigDecimal(first + 2);
BigDecimal close = r.getBigDecimal(first + 3);
BigDecimal volume = r.getBigDecimal(first + 4);
return new Kline(result, time, open, high, low, close, volume);
}
static Precision of(JSONArray r) {
return new Precision(
Util.decode(r.getString(0)),
r.getString(1),
r.getInteger(2),
r.getInteger(3),
r.getBigDecimal(4),
r.getBigDecimal(5),
r.getBigDecimal(6),
r.getBigDecimal(7),
r.getBigDecimal(8),
r.getBigDecimal(9));
}
static Order of(JSONArray r) {
return new Order(
Util.decode(r.getString(0)),
r.getLong(1),
r.getString(2),
Util.of(r.getString(3), Side::valueOf),
Util.of(r.getString(4), Type::valueOf),
Util.of(r.getString(5), State::valueOf),
r.getBigDecimal(6),
r.getBigDecimal(7),
r.getBigDecimal(8),
r.getBigDecimal(9));
}
static OrderDetail of(JSONArray r) {
return new OrderDetail
(Util.decode(r.getString(0)),
r.getLong(1),
r.getString(2),
r.getString(3),
r.getBigDecimal(4),
r.getBigDecimal(5),
r.getBigDecimal(6),
r.getString(7),
Util.of(r.getString(8), Side::valueOf));
}
static Balance of(JSONArray r) {
return new Balance(
Util.decode(r.getString(0)),
r.getString(1),
r.getBigDecimal(2),
r.getBigDecimal(3));
}
static Trade of(JSONArray r) {
return new Trade(
Util.decode(r.getString(0)),
r.getLong(1),
r.getString(2),
Util.of(r.getString(3), Side::valueOf),
r.getBigDecimal(4),
r.getBigDecimal(5));
}
static Kline of(JSONArray r) {
return new Kline(
Util.decode(r.getString(0)),
r.getLong(1),
r.getBigDecimal(2),
r.getBigDecimal(3),
r.getBigDecimal(4),
r.getBigDecimal(5),
r.getBigDecimal(6));
}
/**
* [
* 21597693840, ID int64 Order ID
* null, GID int Group ID
* 26665933660, CID int Client Order ID
* "tLTCUSD", SYMBOL string Pair (tBTCUSD, …)
* 1547796266000, MTS_CREATE int Millisecond timestamp of creation
* 1547796266000, MTS_UPDATE int Millisecond timestamp of update
* 0, AMOUNT float Remaining amount.
* -0.4, AMOUNT_ORIG float Original amount, positive means buy, negative means sell.
* "EXCHANGE LIMIT", TYPE string The type of the order: LIMIT, MARKET, STOP, TRAILING STOP, EXCHANGE MARKET, EXCHANGE LIMIT, EXCHANGE STOP, EXCHANGE TRAILING STOP, FOK, EXCHANGE FOK.
* null, TYPE_PREV string Previous order type
* null, _PLACEHOLDER,
* null, _PLACEHOLDER,
* "0", FLAGS int Upcoming Params Object (stay tuned)
* "EXECUTED @ 31.962(-0.4)", ORDER_STATUS string Order Status: ACTIVE, EXECUTED, PARTIALLY FILLED, CANCELED
* null, _PLACEHOLDER,
* null, _PLACEHOLDER,
* 30, PRICE float Price
* 31.962, PRICE_AVG float Average price
* 0, PRICE_TRAILING float The trailing price
* 0, PRICE_AUX_LIMIT float Auxiliary Limit price (for STOP LIMIT)
* null, _PLACEHOLDER,
* null, _PLACEHOLDER,
* null, _PLACEHOLDER,
* 0, NOTIFY int 1 if Notify flag is active, 0 if not
* 0, HIDDEN int 1 if Hidden, 0 if not hidden
* null, PLACED_ID int If another order caused this order to be placed (OCO) this will be that other order's ID
* null,
* null,
* "API>BFX",
* null,
* null,
* null
* ]
* @param result 原始数据
* @return 订单列表
*/
public static List<Order> parseOrders(String result) {
JSONArray r = JSON.parseArray(result);
List<Order> orders = new ArrayList<>(r.size());
for (int i = 0; i < r.size(); i++) {
JSONArray t = r.getJSONArray(i);
String data = r.getString(i);
Long time = t.getLong(4);
String id = t.getString(0);
Side side = null;
Double s = t.getDouble(7);
if (0 < s) {
side = Side.BUY;
} else if (s < 0) {
side = Side.SELL;
}
Type type = null;
String orderType = t.getString(8);
if ("EXCHANGE LIMIT".equals(orderType)) {
type = Type.LIMIT;
} else if ("EXCHANGE MARKET".equals(orderType)) {
type = Type.MARKET;
} else if ("EXCHANGE FOK".equals(orderType)) {
type = Type.FILL_OR_KILL;
}
State state = null;
String orderState = t.getString(13);
if (orderState.contains("ACTIVE")) {
state = State.SUBMIT;
} else if (orderState.contains("EXECUTED")) {
state = State.FILLED;
} else if (orderState.contains("PARTIALLY FILLED")) {
state = State.UNDONE;
} else if (orderState.contains("CANCELED")) {
state = State.CANCEL;
}
BigDecimal price = t.getBigDecimal(16);
BigDecimal amount = t.getBigDecimal(7).abs();
BigDecimal deal = amount.subtract(t.getBigDecimal(6).abs());
BigDecimal average = t.getBigDecimal(17);
if (state == State.SUBMIT && greater(deal, BigDecimal.ZERO)) {
state = State.PARTIAL;
}
if (state == State.CANCEL && greater(deal, BigDecimal.ZERO)) {
state = State.UNDONE;
}
orders.add(new Order(data, time, id, side, type, state, price, amount, deal, average));
}
return orders;
}
static Row of(JSONArray r) {
return new Row(
Util.decode(r.getString(0)),
r.getBigDecimal(1),
r.getBigDecimal(2));
}
/**
* 解析档位
*
* @param result 元素数据
* @param row 数组
* @return 档位
*/
public static Row parseRowByIndex(String result, JSONArray row) {
BigDecimal price = row.getBigDecimal(0);
BigDecimal amount = row.getBigDecimal(1);
return new Row(result, price, amount);
}