package com.iquant.util;
import com.iquant.common.DateUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
public class CountKRIUtil {
/**
* 计算整体风险指标数据
* @param output
* @return
*/
public static Map<String, Object> countKRI(String output) {
Map<String, Object> data = new HashMap<String, Object>();
KriUtil kriUtil = new KriUtil();
JSONObject outputJson = JSONObject.fromObject(output);
JSONArray strategyReturnRateJson = JSONArray.fromObject(outputJson
.getString("account"));
JSONArray benchmarkReturnRateJson = JSONArray.fromObject(outputJson
.getString("benchmark"));
JSONArray worthJson = JSONArray.fromObject(outputJson
.getString("worth"));
JSONArray orderJson = JSONArray.fromObject(outputJson
.getString("order"));
// JSONArray buyAdjustJson = JSONArray.fromObject(outputJson
// .getString("buyAdjust"));
JSONArray logJson = JSONArray.fromObject(outputJson
.getString("log"));
List<Double> strategyReturnRateArray = new ArrayList<Double>();
for (int i = 0; i < strategyReturnRateJson.size(); i++) {
String[] str = strategyReturnRateJson.getString(i).split(":");
strategyReturnRateArray.add(Double.parseDouble(str[1]) / 100);
}
List<Double> strategyWorth = new ArrayList<Double>();
List<Double> strategyGain = new ArrayList<Double>();
for (int i = 0; i < worthJson.size(); i++) {
String[] str = worthJson.getString(i).split(":");
String[] str2 = str[1].split(",");
strategyWorth.add(Double.parseDouble(str2[0]));
if (i > 0)
strategyGain.add(strategyWorth.get(i)
/ strategyWorth.get(i - 1) - 1);
}
List<Double> benchmarkWorth = new ArrayList<Double>();
List<Double> benchmarkGain = new ArrayList<Double>();
for (int i = 0; i < benchmarkReturnRateJson.size(); i++) {
String[] str = benchmarkReturnRateJson.getString(i).split(":");
String[] str2 = str[1].split(",");
if (i == 0) {
benchmarkReturnRateJson.set(i, str[0] + ":" + str2[0]);
benchmarkWorth.add(Double.parseDouble(str2[2]));
} else {
String[] lastStr = benchmarkReturnRateJson.getString(i - 1)
.split(":");
String[] lastStr2 = lastStr[1].split(",");
double newReturnRate = ((1 + Double.parseDouble(lastStr2[0]) / 100) * (1 + Double.parseDouble(str2[0]) / 100)
- 1) * 100;
benchmarkReturnRateJson.set(i, str[0] + ":" + newReturnRate);
benchmarkWorth.add(Double.parseDouble(str2[2]));
benchmarkGain.add(benchmarkWorth.get(i)
/ benchmarkWorth.get(i - 1) - 1);
}
}
double buy = 0.0;
double sell = 0.0;
// order[] 数组
for (int i = 0; i < orderJson.size(); i++) {
// order的对象
JSONObject dataObject = JSONObject.fromObject(orderJson.get(i));
for (Iterator it2 = dataObject.keys(); it2.hasNext();) {
// 时间日期的key值
String key2 = (String) it2.next();
// 日期下的时间值
JSONObject dataObject2 = JSONObject.fromObject(dataObject
.getString(key2));
// 遍历日期下的所有分钟
for (Iterator it3 = dataObject2.keys(); it3.hasNext();) {
// 分钟的key值
String key3 = (String) it3.next();
// String[] str = dataObject2.getString(key3).split(",");
// double tradePrice = Double.parseDouble(str[2]);
// if (tradePrice > 0)
// buy += tradePrice;
// else
// sell += tradePrice;
JSONObject dataObject3 = dataObject2.getJSONObject(key3);
for(Iterator it4 = dataObject3.keys(); it4.hasNext();){
String key4 = (String) it4.next();
String[] str = dataObject3.getString(key4).split(",");
double tradePrice = Double.parseDouble(str[2]);
if (tradePrice > 0)
buy += tradePrice;
else
sell += tradePrice;
}
}
}
}
double strategyReturnRate = 0.0;
if(strategyWorth != null && strategyWorth.size() > 0){
strategyReturnRate = kriUtil.annualizedReturnRate(
strategyWorth.get(0),
strategyWorth.get(strategyWorth.size() - 1),
strategyWorth.size());
}
double benchmarkReturnRate = 0.0;
if(benchmarkWorth != null && benchmarkWorth.size() > 0){
benchmarkReturnRate = kriUtil.annualizedReturnRate(
benchmarkWorth.get(0),
benchmarkWorth.get(benchmarkWorth.size() - 1),
benchmarkWorth.size());
}
double beta = kriUtil.beta(strategyGain, benchmarkGain);
double alpha = kriUtil.alpha(beta, strategyReturnRate,
benchmarkReturnRate);
double volatility = kriUtil.annualizedVolatility(strategyGain);
double sharpeRatio = kriUtil
.sharpeRatio(volatility, strategyReturnRate);
double informationRatio = kriUtil.annualizedInformationRatio(
strategyReturnRate, benchmarkReturnRate, strategyGain,
benchmarkGain);
double maxDrawdown = kriUtil.maxDrawdown(strategyWorth);
double turnoverRate = kriUtil.turnoverRate(buy, sell, strategyWorth);
String logStr = logJson.toString();
// String logStr = "";
// for (int i = 0; i < logJson.size(); i++) {
// String temp = logJson.getString(i);
// String[] str1 = temp.split(":");
// if(str1.length > 1){
// String[] str2 = str1[1].split(",");
// if (Integer.parseInt(str2[1]) > 0) {
// logStr += str1[0] + " 09:30:00 - INFO - 原始下单数量为" + str2[1]
// + ",买入股票数量必须是100的倍数,调整为" + str2[2] + "<br>";
// logStr += str1[0] + " 09:30:00 - INFO - Buying " + str2[0]
// + "<br>";
// } else if (Integer.parseInt(str2[1]) < 0) {
// logStr += str1[0] + " 09:30:00 - INFO - Selling " + str2[0]
// + "<br>";
// }
// }
// }
data.put("strategy", strategyReturnRateJson);
data.put("benchmark", benchmarkReturnRateJson);
data.put("strategyReturnRate", strategyReturnRate);
data.put("benchmarkReturnRate", benchmarkReturnRate);
data.put("alpha", alpha);
data.put("beta", beta);
data.put("sharpeRatio", sharpeRatio);
data.put("volatility", volatility);
data.put("informationRatio", informationRatio);
data.put("maxDrawdown", maxDrawdown);
data.put("turnoverRate", turnoverRate);
data.put("log", logStr);
return data;
}
/**
* 计算风险指标按月详细数据
* @param output
* @return
* @throws Exception
*/
public static Map<String, Object> countKRIDetail(String output) throws Exception {
Map<String, Object> data = new HashMap<String, Object>();
KriUtil kriUtil = new KriUtil();
DecimalFormat precentFormat = new DecimalFormat("0.00%");
SimpleDateFormat sformat = new SimpleDateFormat("yyyy-MM");
JSONObject outputJson = JSONObject.fromObject(output);
JSONArray strategyReturnRateJson = JSONArray.fromObject(outputJson
.getString("account"));
JSONArray benchmarkReturnRateJson = JSONArray.fromObject(outputJson
.getString("benchmark"));
JSONArray worthJson = JSONArray.fromObject(outputJson
.getString("worth"));
JSONArray orderJson = JSONArray.fromObject(outputJson
.getString("order"));
// JSONArray buyAdjustJson = JSONArray.fromObject(outputJson
// .getString("buyAdjust"));
JSONArray logJson = JSONArray.fromObject(outputJson
.getString("log"));
String start = outputJson.getString("start");
String end = outputJson.getString("end");
/*
* 将原始数据分割成每月一份放入对应MAP中
*/
Map<Date, List<Double>> strategyReturnRateMap = new HashMap<Date, List<Double>>();
List<Double> strategyReturnRateTemp = new ArrayList<Double>();
int strategyReturnRateMonth = 0;
for (int i = 0; i < strategyReturnRateJson.size(); i++) {
String[] str = strategyReturnRateJson.getString(i).split(":");
Date date = DateUtil.getString2Date(str[0], "yyyy-MM-dd");
if (i == 0) {
strategyReturnRateMonth = DateUtil.getDateMonth(date);
strategyReturnRateTemp.add(Double.parseDouble(str[1]) / 100);
} else {
if (DateUtil.getDateMonth(date) == strategyReturnRateMonth) {
strategyReturnRateTemp
.add
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
iquant-master xmljava系统源码-iquant一个关于量化回测的开源系统.zip (599个子文件)
bootstrap.css 144KB
bootstrap.min.css 120KB
jquery-ui.css 31KB
bootstrap-theme.css 26KB
docs.css 24KB
bootstrap-theme.min.css 23KB
docs.min.css 22KB
layer.css 14KB
bootstrap-datetimepicker.css 12KB
bootstrap-datetimepicker.min.css 11KB
celue.css 8KB
data.css 6KB
webnote.css 5KB
game.css 4KB
index.css 4KB
bootstrap-common.css 3KB
model_new.css 3KB
community.css 3KB
carousel.css 2KB
login.css 2KB
dashboard.css 2KB
patch.css 413B
glyphicons-halflings-regular.eot 20KB
loading-0.gif 6KB
loading-2.gif 2KB
loading-1.gif 701B
.gitignore 818B
CountKRIUtil.java 25KB
NetWorkProtocol.java 16KB
DateUtil.java 12KB
LabJsonController.java 11KB
Base64Helper.java 9KB
JsonUtil.java 9KB
StockController.java 8KB
TelWebSocketHandler.java 7KB
KriUtil.java 6KB
IndexController.java 6KB
LabController.java 4KB
FoldeUtil.java 4KB
RuntimeExec.java 4KB
RemoteShellTool.java 4KB
TelSocketSessionUtils.java 4KB
TelWebSocketHandshakeInterceptor.java 3KB
MyHandler.java 3KB
UserStrategy.java 3KB
CacheDirective.java 2KB
LabServiceImpl.java 2KB
VelocityToolboxToView.java 2KB
User.java 2KB
HelpController.java 1KB
WebHelper.java 1KB
LoginHandlerInterceptor.java 1KB
UserServiceImpl.java 1KB
HandshakeInterceptor.java 1KB
WebSocketConfig.java 1KB
TestHandlerInterceptor.java 1KB
LoginServiceImpl.java 942B
UserStrategyMapper.java 817B
LabService.java 814B
TestMyBatis.java 740B
UserStrategyQuery.java 658B
WebsocketEndPoint.java 650B
UserService.java 604B
UserQuery.java 598B
Application.java 580B
UserMapper.java 571B
BaseQuery.java 466B
LoginService.java 216B
CONSTANT.java 160B
item02.jpg 759KB
item01.jpg 617KB
user07.jpg 176KB
user03.jpg 129KB
user01.jpg 128KB
login_bg.jpg 123KB
user05.jpg 118KB
user04.jpg 34KB
user06.jpg 31KB
user02.jpg 29KB
worker-xquery.js 2.67MB
ace.js 630KB
mode-php.js 607KB
jquery-ui-1.10.3.custom.js 426KB
worker-javascript.js 337KB
mode-jsoniq.js 334KB
mode-xquery.js 331KB
worker-html.js 330KB
worker-css.js 290KB
highstock-all.js 281KB
jquery-1.11.3.js 278KB
worker-php.js 231KB
jquery-ui-1.10.3.custom.min.js 223KB
worker-coffee.js 220KB
keybinding-vim.js 200KB
highmaps.js 179KB
highcharts.js 166KB
mode-html_elixir.js 135KB
mode-html_ruby.js 130KB
mode-ejs.js 129KB
mode-luapage.js 126KB
共 599 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
1530023_m0_67912929
- 粉丝: 3508
- 资源: 4674
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功