缩短长 Python 指标

shorten long Python indicator

这是我编写的代码,用于帮助我定义策略。它运行良好,但我觉得代码太长了,我想知道是否有一种有效的方法可以对 for 循环执行相同的操作。 该代码搜索一组烛台,其实体包含在另一个先前的柱中。用我笨拙的方法,我只能设计最少 2 支蜡烛,最多 7 支蜡烛。是否可以使用 for 循环来获取满足此条件的尽可能多的蜡烛? 提前致谢。

def eATR(df1,n=14):
    """This calculates the Average True Range of of a dataframe of the open,
    high, low, and close data of an instrument"""

    df = df1[['Open', 'High', 'Low', 'Close',]]
    # True Range
    df['TR'] = 0
    for i in range(len(df)):
        try:
            df.iloc[i, 4] = max(df.iat[i,1] - df.iat[i,2],
                         abs(df.iat[i,1] - df.iat[i-1,3]),
                         abs(df.iat[i,2] - df.iat[i-1,3]))
        except ValueError:
            pass

    # eATR
    
    df['eATR']= df['TR'].ewm(span=n, adjust=False).mean()
    df.drop(['TR'], axis = 1, inplace=True)     
    return df['eATR']

def contraction(data):
    df = data.copy()

    e_ATR = eATR(df)

    bodyHi = df[['Open', 'Close']].apply(max,axis=1)
    bodyLo = df[['Open', 'Close']].apply(min,axis=1)
    topWick = abs(df['High'] - bodyHi)
    botWick = abs(bodyLo - df['Low'])
    bodyLen = abs(bodyHi - bodyLo)
    canLen = df['High'] - df['Low']

    per = 1.5
    
    contraction = np.where((df['High'].shift(7) >= bodyHi.shift(6))
                          & (df['High'].shift(7) >= bodyHi.shift(5))
                          & (df['High'].shift(7) >= bodyHi.shift(4))
                          & (df['High'].shift(7) >= bodyHi.shift(3))
                          & (df['High'].shift(7) >= bodyHi.shift(2))
                          & (df['High'].shift(7) >= bodyHi.shift(1))
                          & (df['High'].shift(7) >= bodyHi.shift(0))
                          
                          & (df['Low'].shift(7) <= bodyLo.shift(6))
                          & (df['Low'].shift(7) <= bodyLo.shift(5))
                          & (df['Low'].shift(7) <= bodyLo.shift(4))
                          & (df['Low'].shift(7) <= bodyLo.shift(3))
                          & (df['Low'].shift(7) <= bodyLo.shift(2))
                          & (df['Low'].shift(7) <= bodyLo.shift(1))
                          & (df['Low'].shift(7) <= bodyLo.shift(0))
                          & (canLen.shift(0) <= canLen.shift(7) * per)
                          & (canLen.shift(1) <= canLen.shift(7) * per)
                          & (canLen.shift(2) <= canLen.shift(7) * per)
                          & (canLen.shift(3) <= canLen.shift(7) * per)
                          & (canLen.shift(4) <= canLen.shift(7) * per)
                          & (canLen.shift(5) <= canLen.shift(7) * per)
                          & (canLen.shift(6) <= canLen.shift(7) * per)
                          
                          | (df['High'].shift(6) >= bodyHi.shift(5))
                          & (df['High'].shift(6) >= bodyHi.shift(4))
                          & (df['High'].shift(6) >= bodyHi.shift(3))
                          & (df['High'].shift(6) >= bodyHi.shift(2))
                          & (df['High'].shift(6) >= bodyHi.shift(1))                        
                          & (df['High'].shift(6) >= bodyHi.shift(0))
                         
                          & (df['Low'].shift(6) <= bodyLo.shift(5))
                          & (df['Low'].shift(6) <= bodyLo.shift(4))
                          & (df['Low'].shift(6) <= bodyLo.shift(3))
                          & (df['Low'].shift(6) <= bodyLo.shift(2))
                          & (df['Low'].shift(6) <= bodyLo.shift(1))
                          & (df['Low'].shift(6) <= bodyLo.shift(0))
                          & (canLen.shift(0)  <= canLen.shift(6) * per)
                          & (canLen.shift(1)  <= canLen.shift(6) * per)
                          & (canLen.shift(2)  <= canLen.shift(6) * per)
                          & (canLen.shift(3)  <= canLen.shift(6) * per)
                          & (canLen.shift(4)  <= canLen.shift(6) * per)
                          & (canLen.shift(5)  <= canLen.shift(6) * per)
                         
                          | (df['High'].shift(5) >= bodyHi.shift(4))
                          & (df['High'].shift(5) >= bodyHi.shift(3))
                          & (df['High'].shift(5) >= bodyHi.shift(2))
                          & (df['High'].shift(5) >= bodyHi.shift(1))
                          & (df['High'].shift(5) >= bodyHi.shift(0))
                          & (df['Low'].shift(5) <= bodyLo.shift(4))
                          & (df['Low'].shift(5) <= bodyLo.shift(3))
                          & (df['Low'].shift(5) <= bodyLo.shift(2))
                          & (df['Low'].shift(5) <= bodyLo.shift(1))
                          & (df['Low'].shift(5) <= bodyLo.shift(0))                         
                          
                          & (canLen.shift(0) <= canLen.shift(5) * per)
                          & (canLen.shift(1) <= canLen.shift(5) * per)
                          & (canLen.shift(2) <= canLen.shift(5) * per)
                          & (canLen.shift(3) <= canLen.shift(5) * per)
                          & (canLen.shift(4) <= canLen.shift(5) * per)
                         
                          | (df['High'].shift(4) >= bodyHi.shift(3))
                          & (df['High'].shift(4) >= bodyHi.shift(2))
                          & (df['High'].shift(4) >= bodyHi.shift(1))
                          & (df['High'].shift(4)  >= bodyHi.shift(0))
                          & (df['Low'].shift(4) <= bodyLo.shift(3))
                          & (df['Low'].shift(4) <= bodyLo.shift(2))
                          & (df['Low'].shift(4) <= bodyLo.shift(1)) 
                          & (df['Low'].shift(4) <= bodyLo.shift(0))                    
                                                  
                          & (canLen.shift(0) <= canLen.shift(4) * per)
                          & (canLen.shift(1) <= canLen.shift(4) * per)
                          & (canLen.shift(2) <= canLen.shift(4) * per)
                          & (canLen.shift(3) <= canLen.shift(4) * per)
                          
                          | (df['High'].shift(3) >= bodyHi.shift(2))
                          & (df['High'].shift(3) >= bodyHi.shift(1))
                          & (df['High'].shift(3) >= bodyHi.shift(0))
                          & (df['Low'].shift(3) <= bodyLo.shift(2))
                          & (df['Low'].shift(3) <= bodyLo.shift(1)) 
                          & (df['Low'].shift(3) <= bodyLo.shift(0))                    
                                                  
                          & (canLen.shift(0) <= canLen.shift(3) * per)
                          & (canLen.shift(1) <= canLen.shift(3) * per)
                          & (canLen.shift(1) <= canLen.shift(3) * per)
                          
                          | (df['High'].shift(2) >= bodyHi.shift(1))
                          & (df['High'].shift(2) >= bodyHi.shift(0))
                          & (df['Low'].shift(2) <= bodyLo.shift(1)) 
                          & (df['Low'].shift(2) <= bodyLo.shift(0))                    
                                                  
                          & (canLen.shift(0) <= canLen.shift(2) * per)
                          & (canLen.shift(1) <= canLen.shift(2) * per), 1, 0)
    
     

 
    return contraction

这是满足上述条件时在我的图表上用 'burlywood' 着色的输出。

感谢期待!

chart

虽然我不太明白你在做什么,但这里有一些我认为你正在尝试做的 pythonic 伪代码:

def fits_in(a,b,per=1.5):
    return (a.hi <= b.hi) and (a.lo >= b.lo) and (a.size * per <= b.size)

def anyfit (candles):
    for i in range(1,len(candle_list)):
        for j in range(i):
            if fits_in(candles[i], candles[j]):
               return True
    return False

candle_list = []   # <- fill this list in from somewhere
 
print(anyfit(candle_list))
         

代码的目的是帮助我识别一些符合特定标准的蜡烛。我的实现太长了,实现的想法对我来说看起来不像 pythonic;当我学习 qBasic 时,我会做更多的事情。 @RufusVS 你给了我一个关于如何去做的提示,这是我的最终代码,而不是我之前发布的代码。

def contract(data, per = 1.5):
df = data[['open', 'high', 'low', 'close']].copy()

df['bodyHi'] = df[['open', 'close']].apply(max,axis=1)
df['bodyLo'] = df[['open', 'close']].apply(min,axis=1)
df['canLen'] = df['high'] - df['low']
df['eATR'] = eATR(df)
df['contraction'] = 0
for i in range(len(df)):
    if df['canLen'].iloc[i] < (2 * df['eATR'].iloc[i]):
        for j in range(i+1, len(df)):
            if (df['bodyHi'].iloc[j] < df['high'].iloc[i]) and (df['bodyLo'].iloc[j] > df['low'].iloc[i]):
                df['contraction'].iloc[j] = 1
            else:
                break
    
    
    
return df['contraction'].to_frame()

再次感谢。我花了一段时间才开始,但我最终做到了。我想我应该让你知道。