分享

考夫曼自适应均线(python源代码)

 禁忌石 2022-11-06 发布于浙江
#!/usr/bin/env python# -*- coding: utf-8 -*-import pandas as pdimport numpy as npdef KAMA(series:pd.Series, cyc:int=10, fastest:int=2, slowest:int=30)->np.float64: '''考夫曼自适应均线指标函数 series: 接受 pandas 的 Series 格式的数值 cyc: int 指定数据的计算周期 fastest: int 快线 slowest: int 慢线 输出值: np.float64 例程:kama = KAMA(series.close, cyc=10, fastest=2, slowest=30)''' length = series.shape[0] if length >= cyc: direction = (series.shift(cyc - 1) - series).abs() #价格方向 volatility = series.diff(1).abs().rolling(cyc - 1).sum() #波动幅度 efficiency_ratio = direction / volatility #效率系数 fastest_ratio, slowest_ratio = 2 / (fastest + 1), 2 / (slowest + 1) #快线/慢线系数 smooth = (efficiency_ratio * (fastest_ratio - slowest_ratio) + slowest_ratio) ** 2 #平滑系数 ama_array = np.zeros(length) first_value = True for i in range(length): if smooth[i] != smooth[i]: ama_array[i] = np.nan else: if first_value: ama_array[i] = series[i] first_value = False else: ama_array[i] = ama_array[i - 1] + smooth[i] * (series[i] - ama_array[i - 1]) return ama_array return Exception('错误警告:考夫曼自适应均线指标函数中传入的数据量不足!')

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多