首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >威廉·分形技术指标执行情况

威廉·分形技术指标执行情况
EN

Code Review用户
提问于 2021-04-18 20:37:51
回答 1查看 5.9K关注 0票数 3

我对Python、数据分析和熊猫都很陌生,因为我对这些组件有点熟悉,并试图复制一种交易策略,我正在创建一个函数,给我Wiliam Fractal技术指示符,它本质上是一个滞后指标,可以应用于当前分析过的数据行,而不是实际发生的位置,但不需要向前看就不会有不切实际的回溯测试结果(同时也避免在实际发生之前的2行返回并总是寻找信号)。

指示符是用此约束来源 \begin{aligned} \text{Bearish Fractal} =\ &\text{High} ( N ) > \text{High} ( N - 2 ) \text{ and} \\ &\text{High} ( N ) > \text{High} ( N - 1 ) \text{ and} \\ &\text{High} ( N ) > \text{High} ( N + 1 ) \text{ and} \\ &\text{High} ( N ) > \text{High} ( N + 2 ) \\ \end{aligned}定义的。

\begin{aligned} \text{Bullish Fractal} =\ &\text{Low} ( N ) < \text{Low} ( N - 2 ) \text{ and} \\ &\text{Low} ( N ) < \text{Low} ( N - 1 ) \text{ and} \\ &\text{Low} ( N ) < \text{Low} ( N + 1 ) \text{ and} \\ &\text{Low} ( N ) < \text{Low} ( N + 2 ) \\ \end{aligned}​,我用以下代码实现了它:

代码语言:javascript
复制
def wilFractal(dataframe):
        df = dataframe.copy()
        df['bear_fractal'] = (
                dataframe['high'].shift(4).lt(dataframe['high'].shift(2)) &
                dataframe['high'].shift(3).lt(dataframe['high'].shift(2)) &
                dataframe['high'].shift(1).lt(dataframe['high'].shift(2)) &
                dataframe['high'].lt(dataframe['high'].shift(2))
        )
    
        df['bull_fractal'] = (
                dataframe['low'].shift(4).gt(dataframe['low'].shift(2)) &
                dataframe['low'].shift(3).gt(dataframe['low'].shift(2)) &
                dataframe['low'].shift(1).gt(dataframe['low'].shift(2)) &
                dataframe['low'].gt(dataframe['high'].shift(2))
        )
    
        return df['bear_fractal'], df['bull_fractal']

有什么建议吗?

使用编辑:本例中的代码用于以这种方式向密码交易机器人发送购买信号:

代码语言:javascript
复制
def populate_buy_trend(self, dataframe, metadata) 
    dataframe.loc[
        dataframe['bull_fractal'],
        'buy'] = 1

    return dataframe
EN

回答 1

Code Review用户

回答已采纳

发布于 2021-04-19 10:16:22

Update:有一篇关于参数化移位周期的帖子(现在已删除),因此我在下面的shift()rolling()版本中都添加了一个C2参数。

shift()版本

  1. 与其单独创建一个copy()来分配结果列,不如仅使用独立的系列来节省一些开销。
  2. 与测试shift(2)4,3,1,0不同,更自然的是测试未移位的vs -2,-1,1,2
  3. 与链接& & &不同,与np.logical_and.reduce()一起使用理解更快。
  4. 与其硬编码移位数,不如将其参数化。尽管是定义此指标的期限为2.,但我们可以添加一个period参数(默认值为2),并根据该值生成所有periods
代码语言:javascript
复制
from typing import Tuple

def will_frac(df: pd.DataFrame, period: int = 2) -> Tuple[pd.Series, pd.Series]:
    """Indicate bearish and bullish fractal patterns using shifted Series.

    :param df: OHLC data
    :param period: number of lower (or higher) points on each side of a high (or low)
    :return: tuple of boolean Series (bearish, bullish) where True marks a fractal pattern
    """

    periods = [p for p in range(-period, period + 1) if p != 0] # default [-2, -1, 1, 2]

    highs = [df['high'] > df['high'].shift(p) for p in periods]
    bears = pd.Series(np.logical_and.reduce(highs), index=df.index)

    lows = [df['low'] < df['low'].shift(p) for p in periods]
    bulls = pd.Series(np.logical_and.reduce(lows), index=df.index)

    return bears, bulls

rolling()版本

或者,使用rolling()检查滑动窗口中的中心值是最大值(熊市)还是min (公牛):

代码语言:javascript
复制
from typing import Tuple

def will_frac_roll(df: pd.DataFrame, period: int = 2) -> Tuple[pd.Series, pd.Series]:
    """Indicate bearish and bullish fractal patterns using rolling windows.

    :param df: OHLC data
    :param period: number of lower (or higher) points on each side of a high (or low)
    :return: tuple of boolean Series (bearish, bullish) where True marks a fractal pattern
    """

    window = 2 * period + 1 # default 5

    bears = df['high'].rolling(window, center=True).apply(lambda x: x[period] == max(x), raw=True)
    bulls = df['low'].rolling(window, center=True).apply(lambda x: x[period] == min(x), raw=True)

    return bears, bulls

时间

rolling()版本更简洁、更容易理解,但是shift()版本的扩展性要好得多(内存允许):

票数 11
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/259703

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档