基本技术指标 Python 实现(1)

简介: 基本技术指标 Python 实现

布林带

SharpCharts 计算

* Middle Band = 20-day simple moving average (SMA)
  * Upper Band = 20-day SMA + (20-day standard deviation of price x 2) 
  * Lower Band = 20-day SMA - (20-day standard deviation of price x 2)


点击这里下载此电子表格示例。")

布林带由一个中间带和两个外部带组成。 中间带是一个通常设置为 20 个周期的简单移动平均。使用简单移动平均是因为标准差公式也使用简单移动平均。标准差的回溯期与简单移动平均相同。外部带通常设置在中间带的上下 2 个标准差处。


设置可以调整以适应特定证券或交易风格的特征。 布林格建议对标准差乘数进行小幅调整。 更改移动平均线的周期数也会影响用于计算标准差的周期数。 因此,标准差乘数只需要进行小幅调整。 增加移动平均线周期将自动增加用于计算标准差的周期数,并且还需要增加标准差乘数。 使用 20 天 SMA 和 20 天标准差,标准差乘数设置为 2。 布林格建议将标准差乘数增加到 2.1 以用于 50 周期 SMA,并将标准差乘数降低到 1.9 以用于 10 周期 SMA。

代码

名称:布林带计算
描述:根据给定的DataFrame计算布林带指标
代码:
def bollinger_bands(df):
    df['Middle Band'] = df['close'].rolling(window=20).mean()
    df['Upper Band'] = df['Middle Band'] + (df['close'].rolling(window=20).std() * 2)
    df['Lower Band'] = df['Middle Band'] - (df['close'].rolling(window=20).std() * 2)
    return df

吊灯退出

计算

吊灯退出公式由三部分组成:周期高点或周期低点、平均真实范围(ATR)和一个乘数。在日线图上使用默认设置的 22 周期,吊灯退出将寻找过去 22 天的最高点或最低点。请注意,一个月有 22 个交易日。这个参数(22)也将用于计算平均真实范围。

Chandelier Exit (long) = 22-day High - ATR(22) x 3 
Chandelier Exit (short) = 22-day Low + ATR(22) x 3

如上述公式所示,长仓和短仓都有一个吊灯退出。吊灯退出(长仓)悬挂在 22 周期高点的三个 ATR 值以下。这意味着随着周期高点和 ATR 值的变化,它会上升和下降。短仓的吊灯退出放置在 22 周期低点的三个 ATR 值以上。电子表格示例展示了两者的样本计算。


代码

# 计算吊灯退出指标
def chandelier_exit(df):
    # 计算 ATR
    df['TR'] = df['high'] - df['low']
    df['TR1'] = abs(df['high'] - df['close'].shift())
    df['TR2'] = abs(df['low'] - df['close'].shift())
    df['TR'] = df[['TR', 'TR1', 'TR2']].max(axis=1)
    df['ATR'] = df['TR'].rolling(window=22).mean()
    
    # 计算吊灯退出(长仓和短仓)
    df['Chandelier_Exit_Long'] = df['high'].rolling(window=22).max() - df['ATR'] * 3
    df['Chandelier_Exit_Short'] = df['low'].rolling(window=22).min() + df['ATR'] * 3
    
    return df[['Chandelier_Exit_Long', 'Chandelier_Exit_Short']]
# 使用示例
result = chandelier_exit(df)
print(result)

一目均衡云

计算

一目均衡云中的五个图表中的四个是基于一定时间内的高低平均值。例如,第一个图表只是 9 天高点和 9 天低点的平均值。在计算机普及之前,计算这种高低平均值可能比计算 9 天移动平均线更容易。一目均衡云由五个图表组成:

Tenkan-sen (Conversion Line): (9-period high + 9-period low)/2))
The default setting is 9 periods and can be adjusted. On a daily chart, this line is the midpoint of the 9-day high-low range, 
which is almost two weeks.  
Kijun-sen (Base Line): (26-period high + 26-period low)/2))
The default setting is 26 periods and can be adjusted. On a daily chart, this line is the midpoint of the 26-day high-low range, which is almost one month).  
Senkou Span A (Leading Span A): (Conversion Line + Base Line)/2))
This is the midpoint between the Conversion Line and the Base Line. The Leading Span A forms one of the two Cloud boundaries. It is referred to as "Leading" because it is plotted 26 periods in the future and forms the faster Cloud boundary. 
Senkou Span B (Leading Span B): (52-period high + 52-period low)/2))
On the daily chart, this line is the midpoint of the 52-day high-low range, which is a little less than 3 months. The default calculation setting is 52 periods, but can be adjusted. This value is plotted 26 periods in the future and forms the slower Cloud boundary.
Chikou Span (Lagging Span): Close plotted 26 days in the past
The default setting is 26 periods, but can be adjusted. 

本教程在解释各种图表时将使用英文等效词。下表显示了道琼斯工业指数与一目均衡云图的情况。转换线(蓝色)是最快、最敏感的线。请注意,它最接近价格走势。基准线(红色)落后于更快的转换线,但在很大程度上跟随价格走势。转换线和基准线之间的关系类似于 9 日移动平均线和 26 日移动平均线之间的关系。9 日线更快,更紧密地跟随价格走势。26 日线较慢,落后于 9 日线。顺便提一下,注意到 9 和 26 是用来计算 MACD 的相同周期。


代码

名称:一目均衡云计算
描述:根据给定的开盘价、最高价、最低价和收盘价计算一目均衡云中的各个指标
代码:
def calculate_ichimoku_cloud(df):
    # Tenkan-sen (Conversion Line)
    nine_period_high = df['high'].rolling(window=9).max()
    nine_period_low = df['low'].rolling(window=9).min()
    df['tenkan_sen'] = (nine_period_high + nine_period_low) / 2
    
    # Kijun-sen (Base Line)
    twenty_six_period_high = df['high'].rolling(window=26).max()
    twenty_six_period_low = df['low'].rolling(window=26).min()
    df['kijun_sen'] = (twenty_six_period_high + twenty_six_period_low) / 2
    
    # Senkou Span A (Leading Span A)
    df['senkou_span_a'] = ((df['tenkan_sen'] + df['kijun_sen']) / 2).shift(26)
    
    # Senkou Span B (Leading Span B)
    fifty_two_period_high = df['high'].rolling(window=52).max()
    fifty_two_period_low = df['low'].rolling(window=52).min()
    df['senkou_span_b'] = ((fifty_two_period_high + fifty_two_period_low) / 2).shift(26)
    
    # Chikou Span (Lagging Span)
    df['chikou_span'] = df['close'].shift(-26)
    
    return df

考夫曼自适应移动平均线(KAMA)

计算

计算考夫曼自适应移动平均线需要几个步骤。让我们首先从佩里·考夫曼推荐的设置开始:KAMA(10,2,30)。

  • 10 是效率比率(ER)的周期数。
  • 2 是最快 EMA 常数的周期数。
  • 30 是最慢 EMA 常数的周期数。

在计算 KAMA 之前,我们需要计算效率比率(ER)和平滑常数(SC)。将公式分解成易于理解的小块有助于理解指标背后的方法论。请注意,ABS 代表绝对值。

代码

## 计算
名称:{KAMA}
描述:计算考夫曼自适应移动平均线(KAMA)
--- 代码:
def KAMA(df):
    er_window = 10
    fast_period = 2
    slow_period = 30
    
    df['change'] = df['close'] - df['close'].shift(1)
    df['volatility'] = abs(df['close'] - df['close'].shift(er_window))
    
    df['er'] = df['change'] / df['volatility']
    
    sc_fast = 2 / (fast_period + 1)
    sc_slow = 2 / (slow_period + 1)
    
    df['sc'] = (df['er'] * (sc_fast - sc_slow) + sc_slow) ** 2
    
    df['kama'] = 0.0
    
    for i in range(er_window, len(df)):
        if df['kama'][i-1] != 0:
            df['kama'][i] = df['kama'][i-1] + df['sc'][i] * (df['close'][i] - df['kama'][i-1])
        else:
            df['kama'][i] = df['close'][i]
    
    return df['kama']

凯尔特纳通道

计算

计算凯尔特纳通道有三个步骤。首先,选择指数移动平均线的长度。其次,选择真实波幅(ATR)的时间周期。第三,选择真实波幅的乘数。

Middle Line: 20-day exponential moving average 
Upper Channel Line: 20-day EMA + (2 x ATR(10))
Lower Channel Line: 20-day EMA - (2 x ATR(10))

上面的示例基于 SharpCharts 的默认设置。由于移动平均线滞后于价格,较长的移动平均线会有更多的滞后,而较短的移动平均线则会有较少的滞后。ATR 是基本的波动率设置。较短的时间框架,如 10,会产生更加波动的 ATR,随着 10 期波动性的起伏而波动。较长的时间框架,如 100,会平滑这些波动,产生更加稳定的 ATR 读数。乘数对通道宽度影响最大。简单地从 2 改为 1 会将通道宽度减半。从 2 增加到 3 会使通道宽度增加 50%。

这里有一张图表展示了三条 Keltner 通道,分别设置在中心移动平均线的 1、2 和 3 个 ATR 之外。这种特定技术多年来一直由 SpikeTrade.com 的 Kerry Lovvorn 提倡。


上图显示了默认的红色 Keltner 通道,更宽的蓝色通道和更窄的绿色通道。蓝色通道设置为中心移动平均线的三个平均真实范围值之上和之下(3 x ATR)。绿色通道使用一个 ATR 值。所有三个通道共享 20 天的 EMA,即中间的虚线。指标窗口显示了 10 期、50 期和 100 期的平均真实范围(ATR)的差异。请注意,短期 ATR(10)更加波动,范围更广。相比之下,100 期 ATR 更加平稳,范围波动较小。

代码

名称:Keltner通道
描述:根据给定的参数计算Keltner通道的三条线
def keltner_channel(df, ema_length=20, atr_length=10, multiplier=2):
    df['EMA'] = df['close'].ewm(span=ema_length, adjust=False).mean()
    df['ATR'] = df['high'].rolling(window=atr_length).max() - df['low'].rolling(window=atr_length).min()
    
    df['Upper_Channel'] = df['EMA'] + (multiplier * df['ATR'])
    df['Lower_Channel'] = df['EMA'] - (multiplier * df['ATR'])
    
    return df[['EMA', 'Upper_Channel', 'Lower_Channel']]

移动平均线 - 简单和指数

简单移动平均线计算

简单移动平均线是通过计算特定周期内某个证券的平均价格形成的。 大多数移动平均线是基于收盘价格的。 5 天简单移动平均线是收盘价格的五天总和除以五。 正如其名称所示,移动平均线是一个移动的平均值。 随着新数据的出现,旧数据被丢弃。 这导致平均值沿着时间尺度移动。 以下是一个 5 天移动平均线在三天内演变的示例。

Daily Closing Prices: 11,12,13,14,15,16,17
First day of 5-day SMA: (11 + 12 + 13 + 14 + 15) / 5 = 13
Second day of 5-day SMA: (12 + 13 + 14 + 15 + 16) / 5 = 14
Third day of 5-day SMA: (13 + 14 + 15 + 16 + 17) / 5 = 15

移动平均线的第一天简单地涵盖了过去五天。 移动平均线的第二天删除了第一个数据点(11)并添加了新的数据点(16)。 移动平均线的第三天继续删除第一个数据点(12)并添加新的数据点(17)。 在上面的示例中,价格在七天内逐渐从 11 增加到 17。 请注意,移动平均线在三天的计算期间也从 13 上升到 15。 还要注意,每个移动平均值都略低于最后的价格。 例如,第一天的移动平均值等于 13,而最后的价格是 15。 过去四天的价格较低,这导致移动平均线滞后。

代码

名称:simple_moving_average
描述:计算简单移动平均线
--- 
代码:
def simple_moving_average(df, window):
    df['SMA'] = df['close'].rolling(window=window).mean()
    return df

移动平均包络

计算

移动平均包络的计算方法很简单。首先,选择简单移动平均或指数移动平均。简单移动平均对每个数据点(价格)的权重相同。指数移动平均对最近的价格赋予更大的权重,滞后性更小。其次,选择移动平均的时间周期数。第三,设置包络的百分比。一个 20 天的移动平均线,带有 2.5%的包络,将显示以下两条线:

Upper Envelope: 20-day SMA + (20-day SMA x .025)
Lower Envelope: 20-day SMA - (20-day SMA x .025)


上图显示了 IBM 的 20 天简单移动平均线和 2.5%的包络。请注意,20 天简单移动平均线被添加到此 SharpChart 中以供参考。请注意包络如何与 20 天简单移动平均线平行移动。它们始终保持在移动平均线的上方和下方的恒定 2.5%。

代码

名称:移动平均包络
描述:根据给定的DataFrame计算移动平均包络
代码:
def moving_average_envelope(df, window=20, percentage=0.025):
    df['SMA'] = df['close'].rolling(window=window).mean()
    df['Upper Envelope'] = df['SMA'] + (df['SMA'] * percentage)
    df['Lower Envelope'] = df['SMA'] - (df['SMA'] * percentage)
    
    return df[['SMA', 'Upper Envelope', 'Lower Envelope']]

抛物线 SAR

计算

SAR 的计算涉及复杂的 if/then 变量,使其难以放入电子表格中。这些示例将提供 SAR 计算的一般概念。由于上升 SAR 和下降 SAR 的公式不同,因此更容易将计算分为两部分。第一部分涵盖上升 SAR 的计算,第二部分涵盖下降 SAR。

Rising SAR
----------
Prior SAR: The SAR value for the previous period. 
Extreme Point (EP): The highest high of the current uptrend. 
Acceleration Factor (AF): Starting at .02, AF increases by .02 each time the extreme point makes a new high. AF can reach a maximum of .20, no matter how long the uptrend extends. 
Current SAR = Prior SAR + Prior AF(Prior EP - Prior SAR)
13-Apr-10 SAR = 48.28 = 48.13 + .14(49.20 - 48.13)
The Acceleration Factor is multiplied by the difference between the Extreme Point and the prior period's SAR. This is then added to the prior period's SAR. Note however that SAR can never be above the prior two periods' lows. Should SAR be above one of those lows, use the lowest of the two for SAR. 



Falling SAR
-----------
Prior SAR: The SAR value for the previous period. 
Extreme Point (EP): The lowest low of the current downtrend. 
Acceleration Factor (AF): Starting at .02, AF increases by .02 each time the extreme point makes a new low. AF can reach a maximum of .20, no matter how long the downtrend extends. 
Current SAR = Prior SAR - Prior AF(Prior SAR - Prior EP)
9-Feb-10 SAR = 43.56 = 43.84 - .16(43.84 - 42.07)
The Acceleration Factor is multiplied by the difference between the Prior period's SAR and the Extreme Point. This is then subtracted from the prior period's SAR. Note however that SAR can never be below the prior two periods' highs. Should SAR be below one of those highs, use the highest of the two for SAR. 



代码

名称:SAR计算
描述:根据给定的开盘价、最高价、最低价和收盘价计算抛物线 SAR 指标
import pandas as pd
def calculate_SAR(df):
    df['SAR'] = 0
    df['AF'] = 0
    df['EP'] = 0
    
    for i in range(2, len(df)):
        if df['high'][i-1] > df['high'][i-2] and df['low'][i-1] > df['low'][i-2]:
            df.at[i, 'AF'] = min(df['AF'][i-1] + 0.02, 0.2)
        elif df['high'][i-1] < df['high'][i-2] and df['low'][i-1] < df['low'][i-2]:
            df.at[i, 'AF'] = max(df['AF'][i-1] - 0.02, 0.02)
        else:
            df.at[i, 'AF'] = df['AF'][i-1]
        
        if df['AF'][i] == 0.2:
            df.at[i, 'EP'] = df['high'][i]
        else:
            df.at[i, 'EP'] = df['EP'][i-1]
        
        if df['AF'][i] > 0:
            if df['AF'][i-1] > 0:
                if df['high'][i] > df['EP'][i-1]:
                    df.at[i, 'SAR'] = df['SAR'][i-1] + df['AF'][i-1] * (df['EP'][i-1] - df['SAR'][i-1])
                else:
                    df.at[i, 'SAR'] = df['EP'][i-1]
            else:
                df.at[i, 'SAR'] = df['EP'][i-1]
        else:
            if df['AF'][i-1] < 0:
                if df['low'][i] < df['EP'][i-1]:
                    df.at[i, 'SAR'] = df['SAR'][i-1] - df['AF'][i-1] * (df['SAR'][i-1] - df['EP'][i-1])
                else:
                    df.at[i, 'SAR'] = df['EP'][i-1]
            else:
                df.at[i, 'SAR'] = df['EP'][i-1]
    
    return df
# 使用示例
df = pd.DataFrame({
    'open': [50, 48, 47, 45, 46],
    'high': [52, 50, 49, 48, 47],
    'low': [48, 46, 45, 43, 44],
    'close': [49, 47, 46, 44, 45]
})
df = calculate_SAR(df)
print(df)

枢轴点

代码

名称:{title}
描述:计算股票的收益率
---
代码:
def calculate_returns(df):
    df['returns'] = df['close'].pct_change()
    return df

价格通道

计算

Upper Channel Line: 20-day high
Lower Channel Line: 20-day low
Centerline: (20-day high + 20-day low)/2 


上述公式基于日线图和 20 周期价格通道,这是 SharpCharts 中的默认设置。价格通道可用于分钟线、日线、周线或月线图表。回溯期(20)可以更短或更长。较短的回溯期,如 10 天,产生更紧密的通道线。较长的回溯期产生更宽的通道。

价格通道公式不包括最近的周期。价格通道是基于当前周期之前的价格。10 月 21 日的 20 天价格通道将基于前一天 10 月 20 日的 20 天高点和 20 天低点。如果使用最近的周期,通道突破将不可能发生。在下面的图表中,请注意价格如何突破了上方价格通道,因为高点是基于倒数第二根柱子,而不是当前柱子。


代码

# 计算价格通道指标
def calculate_price_channel(df):
    df['Upper Channel Line'] = df['high'].rolling(window=20).max()
    df['Lower Channel Line'] = df['low'].rolling(window=20).min()
    df['Centerline'] = (df['Upper Channel Line'] + df['Lower Channel Line']) / 2
    
    return df

成交量价格

计算

成交量价格计算基于图表上显示的整个时期。五个月日线图上的成交量价格是基于所有五个月的每日收盘数据。两周 30 分钟图上的成交量价格是基于两周的 30 分钟收盘数据。三年周线图上的成交量价格是基于三年的每周收盘数据。你明白了吧。成交量价格计算不会超出图表显示的历史数据范围。

There are four steps involved in the calculation. 
This example is based on closing prices and the default parameter setting (12). 
  1\. Find the high-low range for closing prices for the entire period.  
  2\. Divide this range by 12 to create 12 equal price zones.
  3\. Total the amount of volume traded within each price zone.  
  4\. Divide the volume into up volume and down volume (optional). 

请注意,当收盘价格从一个周期下跌到下一个周期时,成交量为负。当收盘价格从一个周期上涨到下一个周期时,成交量为正。


上面的示例显示了从 2010 年 4 月 12 日到 9 月 15 日对纳斯达克 100ETF 进行的成交量价格计算。在此期间,收盘价格范围从 40.32 到 47.87(47.87 - 40.32 = 7.55)。一百一十个收盘价格(每个交易日一个)从低到高排序,然后分成 12 个均匀的价格区间(7.55/12 = .6292)。


上图突出显示了前三个价格区间(40.32 至 40.95,40.96 至 41.58 和 41.59 至 42.21)。从低点(40.32)开始,我们可以添加区间大小(.6292)以创建导致高点的价格区间。只有落在这些区间内的价格才用于特定的成交量价格计算。


成交量价格条代表每个价格区间的总成交量。成交量可以分为正成交量和负成交量。请注意上图中的成交量价格条是红色和绿色的,以区分正成交量和负成交量。

代码

名称:成交量价格计算
描述:根据给定的收盘价和成交量数据,计算成交量价格
代码:
def calculate_volume_price(df):
    # Step 1: Find the high-low range for closing prices for the entire period
    price_range = df['close'].max() - df['close'].min()
    
    # Step 2: Divide the range by 12 to create 12 equal price zones
    price_zone = price_range / 12
    
    # Step 3: Total the amount of volume traded within each price zone
    df['price_zone'] = ((df['close'] - df['close'].min()) // price_zone).astype(int)
    volume_price = df.groupby('price_zone')['volume'].sum()
    
    # Step 4: Divide the volume into up volume and down volume
    df['volume_direction'] = df['close'].diff().apply(lambda x: 1 if x > 0 else -1)
    up_volume = df[df['volume_direction'] == 1].groupby('price_zone')['volume'].sum()
    down_volume = df[df['volume_direction'] == -1].groupby('price_zone')['volume'].sum()
    
    return volume_price, up_volume, down_volume

成交量加权平均价格(VWAP)

计算

VWAP 计算涉及五个步骤。首先,计算日内周期的典型价格。这是高、低和收盘价的平均值:{(H+L+C)/3)}。其次,将典型价格乘以周期的成交量。第三,创建这些值的累积总和。这也被称为累积总和。第四,创建成交量的累积总和(累积成交量)。第五,将价格-成交量的累积总和除以成交量的累积总和。

Cumulative(Volume x Typical Price)/Cumulative(Volume)


上面的示例显示了 IBM 交易的前 30 分钟的 1 分钟 VWAP。通过将累积价格-成交量除以累积成交量,产生一个根据成交量调整(加权)的价格水平。第一个 VWAP 值始终是典型价格,因为分子和分母中的成交量相等。它们在第一次计算中互相抵消。下图显示了 IBM 的 1 分钟 K 线图和 VWAP。在交易的前 30 分钟内,价格从最高的 127.36 到最低的 126.67。实际上,前 30 分钟非常波动。VWAP 的范围从 127.21 到 127.09,并且大部分时间处于这个范围的中间。


代码

名称:VWAP 
描述:计算成交量加权平均价格
def vwap(df):
    typical_price = (df['high'] + df['low'] + df['close']) / 3
    cumulative_price_volume = (typical_price * df['volume']).cumsum()
    cumulative_volume = df['volume'].cumsum()
    vwap = cumulative_price_volume / cumulative_volume
    return vwap

ZigZag

计算

ZigZag 基于图表的“类型”。基于收盘价的线状和点状图表将显示基于收盘价的 ZigZag。高低收盘价柱状图(HLC)、开盘-最高-最低-收盘价柱状图(OHLC)和蜡烛图显示了周期的高低范围,将显示基于这一高低范围的 ZigZag。基于高低范围的 ZigZag 更有可能改变方向,因为高低范围会更大,产生更大的波动。

参数框允许图表分析者设置 ZigZag 功能的灵敏度。参数框中设置为 5 的 ZigZag 将过滤掉所有小于 5%的波动。设置为 10 的 ZigZag 将过滤掉小于 10%的波动。如果一支股票从 100 的反弹低点到 109 的高点(+9%),则不会有线条,因为波动小于 10%。如果股票从 100 的低点上涨到 110 的高点(+10%),则会有一条线从 100 到 110。如果股票继续上涨到 112,这条线将延伸到 112(从 100 到 112)。直到股票从高点下跌 10%或更多,ZigZag 才会反转。从 112 的高点开始,股票必须下跌 11.2 点(或至 100.8 的低点)才能再次出现线条。下面的图表显示了一个带有 7% ZigZag 的 QQQQ 线状图。6 月初的反弹被忽略,因为小于 7%(黑色箭头)。7 月的两次回调也被忽略,因为它们远低于 7%(红色箭头)。


注意最后一个 ZigZag 线。敏锐的图表分析师会注意到,尽管 QQQQ 仅上涨了 4.13%(43.36 至 45.15),但最后一个 ZigZag 线是向上的。这只是一个临时线,因为 QQQQ 尚未达到 7%的变化阈值。需要到 46.40 才能获得 7%的增长,然后才会有一个永久的 ZigZag 线。如果 QQQQ 在此反弹中未能达到 7%的阈值,然后下跌至 43 以下,这条临时线将消失,之前的 ZigZag 线将从 8 月初的高点继续。


代码

名称:ZigZag
描述:根据给定的DataFrame计算ZigZag指标
import pandas as pd
def ZigZag(df):
    df['high_low_range'] = df['high'] - df['low']
    df['high_close_range'] = abs(df['high'] - df['close'])
    df['low_close_range'] = abs(df['low'] - df['close'])
    
    df['high_low_range_pct'] = df['high_low_range'] / df['close'] * 100
    df['high_close_range_pct'] = df['high_close_range'] / df['close'] * 100
    df['low_close_range_pct'] = df['low_close_range'] / df['close'] * 100
    
    df['zigzag'] = 0
    threshold = 5  # 设置ZigZag功能的灵敏度
    
    for i in range(1, len(df)):
        if df.loc[i, 'high_low_range_pct'] >= threshold:
            df.loc[i, 'zigzag'] = df.loc[i, 'close']
        elif df.loc[i, 'high_close_range_pct'] >= threshold:
            df.loc[i, 'zigzag'] = df.loc[i, 'close']
        elif df.loc[i, 'low_close_range_pct'] >= threshold:
            df.loc[i, 'zigzag'] = df.loc[i, 'close']
        else:
            df.loc[i, 'zigzag'] = df.loc[i-1, 'zigzag']
    
    return df
# 使用示例
# df = pd.read_csv('your_data.csv')
# df = ZigZag(df)

这个函数计算了ZigZag指标,根据给定的DataFrame中的高、低、收盘价等属性计算出ZigZag值,并根据设定的阈值来确定ZigZag的变化。

基本技术指标 Python 实现(2)https://developer.aliyun.com/article/1484225

相关文章
|
13天前
|
Python
基本技术指标 Python 实现(4)
基本技术指标 Python 实现
55 0
|
13天前
|
项目管理 Python
基本技术指标 Python 实现(3)
基本技术指标 Python 实现
59 0
|
13天前
|
Python
基本技术指标 Python 实现(2)
基本技术指标 Python 实现
48 1
|
Serverless 开发者 Python
python股票量化交易(6)---使用TA-Lib计算技术指标
python股票量化交易(6)---使用TA-Lib计算技术指标
2314 0
python股票量化交易(6)---使用TA-Lib计算技术指标
|
Python
Python用主成分分析的方法分析螺纹钢期货30多个技术指标
Python用主成分分析的方法分析螺纹钢期货30多个技术指标
81 0
python-分水岭-技术指标
python-分水岭-技术指标
136 0
|
算法 Python
python-技术指标-ema算法
python-技术指标-ema算法
182 0
|
1天前
|
网络协议 算法 网络架构
Python网络编程之udp编程、黏包以及解决方案、tcpserver
Python网络编程之udp编程、黏包以及解决方案、tcpserver
|
1天前
|
机器学习/深度学习 数据挖掘 算法框架/工具
Python:编程的艺术与魅力
Python:编程的艺术与魅力
10 3
|
4天前
|
机器学习/深度学习 数据挖掘 API
pymc,一个灵活的的 Python 概率编程库!
pymc,一个灵活的的 Python 概率编程库!
12 1