Python笔记-最大回测计算

x33g5p2x  于2022-02-07 转载在 Python  
字(2.7k)|赞(0)|评价(0)|浏览(227)

例子

初始资金是1块钱的话,资金连续一周的变化如下:

周一:1.01

周二:1.02

周三:0.98

周四:1.0

周五:0.97

那么我们要计算的样本为:[1, 1.01, 1.02, 0.98, 1.0, 0.97]

如果计算截至到周二的最大回撤,那么我们要拿到周二的值:1.02,再拿周二之前最大的值,即,周一的1.01,再计算回撤:(1.02/1.01-1)*100=0.99%,这时间 0.99% >0 ,所以是不存在回撤的。

如果计算截至到周三的最大回撤,那么我们要拿到周三的值:0.98,再拿周三之前最大的值,即,周二的1.02,再计算回撤:(0.98/1.02-1)*100=-3.9215%,这时间 -3.9215% < 0 ,所以是存在回撤的。

如果计算截至到周五的最大回撤,那么我们要拿到周五的值:0.97,再拿周五之前最大的值,即,周二的1.02,再计算回撤:(0.97/1.02-1)*100=-4.9019%,这时间 -4.9019% < 0 ,所以是存在回撤的。

这时间再比较 Min(-4.9019%, -3.9215%),得出最大回撤为 -4.9019%。

这里用图来表示下最大回测

代码

使用Python完成上述例子:

代码:

#price = [1.0, 1.01, 1.05, 1.1, 1.11, 1.07, 1.03, 1.03, 1.01, 1.02,1.04, 1.05, 1.07, 1.06,1.05, 1.06, 1.07, 1.09, 1.12, 1.18, 1.15, 1.15, 1.18, 1.16, 1.19, 1.17, 1.17, 1.18,1.19, 1.23]
price = [1, 1.01, 1.02, 0.98, 1.0, 0.97]
maxValueList = []
reList = []

if __name__ == '__main__':

    print("样本:" + str(price))

    maxValueList.append(price[0])
    for i in range(1, len(price)):

        maxValue = price[i]

        #获取最大值
        for j in range(0, i):

            if price[j] > maxValue:
                maxValue = price[j]

            pass
        maxValueList.append(maxValue)
        revalue = (price[i]/maxValueList[i - 1] - 1) * 100

        print("当前数组下标 " + str(i) + " 回测值 : " + str(revalue) + "%")
        #获取存在的回撤
        reList.append(revalue)

        pass

    #算最大回撤
    maxRe = 0
    for i in range(0, len(reList)):
        maxRe = reList[i]
        for j in range(0, i):
            if maxRe > reList[j]:
                maxRe = reList[j]

    print("最大回测 : " + str(maxRe) + "%")

    pass

运行截图:

D:\python\content\python.exe D:/PythonProject/demo/main.py
样本:[1, 1.01, 1.02, 0.98, 1.0, 0.97]
当前数组下标 1 回测值 : 1.0000000000000009%
当前数组下标 2 回测值 : 0.990099009900991%
当前数组下标 3 回测值 : -3.9215686274509887%
当前数组下标 4 回测值 : -1.9607843137254943%
当前数组下标 5 回测值 : -4.90196078431373%
最大回测 : -4.90196078431373%

Process finished with exit code 0

换上面注释后的数据:

D:\python\content\python.exe D:/PythonProject/demo/main.py
样本:[1.0, 1.01, 1.05, 1.1, 1.11, 1.07, 1.03, 1.03, 1.01, 1.02, 1.04, 1.05, 1.07, 1.06, 1.05, 1.06, 1.07, 1.09, 1.12, 1.18, 1.15, 1.15, 1.18, 1.16, 1.19, 1.17, 1.17, 1.18, 1.19, 1.23]
当前数组下标 1 回测值 : 1.0000000000000009%
当前数组下标 2 回测值 : 3.960396039603964%
当前数组下标 3 回测值 : 4.761904761904767%
当前数组下标 4 回测值 : 0.9090909090909038%
当前数组下标 5 回测值 : -3.603603603603611%
当前数组下标 6 回测值 : -7.207207207207212%
当前数组下标 7 回测值 : -7.207207207207212%
当前数组下标 8 回测值 : -9.009009009009016%
当前数组下标 9 回测值 : -8.108108108108114%
当前数组下标 10 回测值 : -6.3063063063063085%
当前数组下标 11 回测值 : -5.405405405405405%
当前数组下标 12 回测值 : -3.603603603603611%
当前数组下标 13 回测值 : -4.504504504504503%
当前数组下标 14 回测值 : -5.405405405405405%
当前数组下标 15 回测值 : -4.504504504504503%
当前数组下标 16 回测值 : -3.603603603603611%
当前数组下标 17 回测值 : -1.8018018018018056%
当前数组下标 18 回测值 : 0.9009009009008917%
当前数组下标 19 回测值 : 5.357142857142838%
当前数组下标 20 回测值 : -2.5423728813559365%
当前数组下标 21 回测值 : -2.5423728813559365%
当前数组下标 22 回测值 : 0.0%
当前数组下标 23 回测值 : -1.6949152542372947%
当前数组下标 24 回测值 : 0.8474576271186418%
当前数组下标 25 回测值 : -1.6806722689075682%
当前数组下标 26 回测值 : -1.6806722689075682%
当前数组下标 27 回测值 : -0.8403361344537785%
当前数组下标 28 回测值 : 0.0%
当前数组下标 29 回测值 : 3.3613445378151363%
最大回测 : -9.009009009009016%

Process finished with exit code 0

相关文章