2、写了三种方式做比较,Level1是参考了一些资料做的模版;Level2在前者基础上做了循环点的修改,将净值中所有下降沿的顶点和终点成对筛选出来,存为一个集合。经运行测试,Level1和Level2两者消耗时间相当;Level3将净值中所有下降沿的顶点值和终点值筛选出来,分别存为集合。当最大回撤出现在数据靠后位置时,减少了搜寻最低点的次数,此时Level3有一定优势。
3、主要代码如下:class ValueSample
{
private static ArrayList<Double> valueCollection = new ArrayList<>();// 净值集合
private static ArrayList<Double> maxCollection = new ArrayList<>();// 下降趋势顶部索引集合
private static ArrayList<Double> minCollection = new ArrayList<>();// 上升趋势底部索引集合
private static ArrayList<Integer> IndexCollection = new ArrayList<>();// 趋势点索引集合
public static void main(String[] args)
{
//.....此处省略导入净值到valueCollection的过程。
// 最大回撤,Level1
long start = System.nanoTime();
System.out.println("L1最大回撤=" + getMaxDrawdownLevel1());
long end = System.nanoTime();
System.out.println("L1用时:" + (end - start));
Thread.sleep(3000);//暂停3s
// 最大回撤,Level2
start = System.nanoTime();
indexInitialize();
System.out.println("L2最大回撤=" + getMaxDrawdownLevel2());
end = System.nanoTime();
System.out.println("L2用时:" + (end - start));
Thread.sleep(3000);
// 最大回撤,Level3
start = System.nanoTime();
valueInitialize();
System.out.println("L3最大回撤=" + getMaxDrawdownLevel3());
end = System.nanoTime();
System.out.println("L3用时:" + (end - start));
}
private static double getMaxDrawdownLevel1()// 最大回撤L1
{
double diff = 0;
double max = valueCollection.get(0);
for (int temp = 1; temp < valueCollection.size(); temp++)
{
if (valueCollection.get(temp) - max < diff)
diff = valueCollection.get(temp) - max;
if (valueCollection.get(temp) > max)
max = valueCollection.get(temp);
}
System.out.println("最大回撤率max=" + (diff / max) * 100);
return diff;
}
private static double getMaxDrawdownLevel2()// 最大回撤L2
{
double diff = 0;
double max = valueCollection.get(IndexCollection.get(0));
for (int baseIndex = 1; baseIndex < IndexCollection.size(); baseIndex++)
{
if (valueCollection.get(IndexCollection.get(baseIndex)) - max < diff)
diff = valueCollection.get(IndexCollection.get(baseIndex)) - max;
if (valueCollection.get(IndexCollection.get(baseIndex)) > max)
max = valueCollection.get(IndexCollection.get(baseIndex));
}
System.out.println("最大回撤率max=" + (diff / max) * 100);
return diff;
}
private static double getMaxDrawdownLevel3()// 最大回撤L3
{
double max = maxCollection.get(0);
int maxIndex = 0;
for (int baseIndex = 1; baseIndex < maxCollection.size(); baseIndex++)
if (max < maxCollection.get(baseIndex))
{
max = maxCollection.get(baseIndex);
maxIndex = baseIndex;
}
double min = minCollection.get(maxIndex + 1);
for (int baseIndex = maxIndex + 2; baseIndex < minCollection.size(); baseIndex++)
if (min > minCollection.get(baseIndex))
min = minCollection.get(baseIndex);
System.out.println("最大回撤率max=" + (min / max - 1) * 100);
return min - max;
}
private static void valueInitialize()// 初始化趋势点值集合
{
for (int baseIndex = 0; baseIndex < valueCollection.size() - 1; baseIndex++)
if (valueCollection.get(baseIndex) > valueCollection.get(baseIndex + 1))
{
maxCollection.add(valueCollection.get(baseIndex));// 加入下降趋势顶部值
for (; baseIndex + 1 < valueCollection.size(); baseIndex++)
if (valueCollection.get(baseIndex) < valueCollection.get(baseIndex + 1))
{
minCollection.add(valueCollection.get(baseIndex));// 加入上升趋势底部值
break;
}
}
}
private static void indexInitialize()// 初始化趋势点索引集合
{
for (int baseIndex = 0; baseIndex < valueCollection.size() - 1; baseIndex++)
if (valueCollection.get(baseIndex) > valueCollection.get(baseIndex + 1))
{
IndexCollection.add(baseIndex);// 加入下降趋势顶部索引
for (; baseIndex + 1 < valueCollection.size(); baseIndex++)
if (valueCollection.get(baseIndex) < valueCollection.get(baseIndex + 1))
{
IndexCollection.add(baseIndex);// 成对加入上升趋势底部索引
break;
}
}
}
}
4、测试时间(单位,毫微秒):
5、数据源xml,使用Navicat导出的MySQL数据表。(因不能上传xml文件,加了txt的后缀)