通达信公式改写成python代码

funcat这个包有一部分功能,但大部分通达信函数是不支持的,
甚至有一些通达信用多维向量计算的方式,funcat只做成一维数据的循环
我自己对此有需求,所以实现了一部分代码
(2022.6更新,
当前已经有最新的MYTT包,所以文章内容已经过时,
地址:https://github.com/mpquant/MyTT/blob/main/MyTT.py

def REF(tp1, n):  
    i = 0 
    ZB_l = []
    y = 0
    while i < n: 
        y=list(tp1)[i]   
        ZB_l.append(y) 
        i=i+1
    while i < len(tp1):  
        y=list(tp1)[i-n]
        ZB_l.append(y)  
        i = i + 1          
#    ZB_s = pd.Series(ZB_l)  
    return ZB_l#[-1]s

def zig(k,x):
    #x = 0.055
#    k = CLOSE.tolist()
    #d = df["trade_date"]
#    d = df['date'].tolist()
    # 循环前的变量初始化
    # 端点 候选点 扫描点 端点列表 拐点线列表 趋势状态
    peer_i = 0
    candidate_i = None
    scan_i = 0
    peers = [0]
#    z = np.zeros(len(k))
    state = ZIG_STATE_START
    while True:
        #print(peers)
        scan_i += 1
        if scan_i == len(k) - 1:
            # 扫描到尾部
            if candidate_i is None:
                peer_i = scan_i
                peers.append(peer_i)
            else:
                if state == ZIG_STATE_RISE:
                    if k[scan_i] >= k[candidate_i]:
                        peer_i = scan_i
                        peers.append(peer_i)
                    else:
                        peer_i = candidate_i
                        peers.append(peer_i)
                        peer_i = scan_i
                        peers.append(peer_i)
                elif state == ZIG_STATE_FALL:
                    if k[scan_i] <= k[candidate_i]:
                        peer_i = scan_i
                        peers.append(peer_i)
                    else:
                        peer_i = candidate_i
                        peers.append(peer_i)
                        peer_i = scan_i
                        peers.append(peer_i)
            break
 
        if state == ZIG_STATE_START:
            if k[scan_i] >= k[peer_i] * (1 + x):
                candidate_i = scan_i
                state = ZIG_STATE_RISE
            elif k[scan_i] <= k[peer_i] * (1 - x):
                candidate_i = scan_i
                state = ZIG_STATE_FALL
        elif state == ZIG_STATE_RISE:
            if k[scan_i] >= k[candidate_i]:
                candidate_i = scan_i
            elif k[scan_i] <= k[candidate_i]*(1-x):
                peer_i = candidate_i
                peers.append(peer_i)
                state = ZIG_STATE_FALL
                candidate_i = scan_i
        elif state == ZIG_STATE_FALL:
            if k[scan_i] <= k[candidate_i]:
                candidate_i = scan_i
            elif k[scan_i] >= k[candidate_i]*(1+x):
                peer_i = candidate_i
                peers.append(peer_i)
                state = ZIG_STATE_RISE
                candidate_i = scan_i
    
    #线性插值, 计算出zig的值            
#    for i in range(len(peers) - 1):
#        peer_start_i = peers[i]
#        peer_end_i = peers[i+1]
#        start_value = k[peer_start_i]
#        end_value = k[peer_end_i]
#        a = (end_value - start_value)/(peer_end_i - peer_start_i)# 斜率
#        for j in range(peer_end_i - peer_start_i +1):
#            z[j + peer_start_i] = start_value + a*j#z是zig线
    return [k[i] for i in peers]#,[d[i] for i in peers]
    
def LLV(s, n):
    return s.rolling(n).min()

def EMA(s, n):
    return s.rolling(n).ema()

def HHV(s, n):
    if type(n)==int:
        return s.rolling(n).max()
    else:#当n不是单个值而是一个序列
        result = [0]*len(s)
        n=list(n)
        for i in range(1,len(s)):
            s_temp = s[0:i]
            result[i] = s_temp[-int(n[i]):].max()#过去n天
        return result


def CROSS(cond1, cond2):
    '''x1上穿x2'''
    return np.where(eval(cond1)>eval(cond2),1,0).tolist()

def barslast(df):
    lst=list(df)
    if sum(lst)>0: 
        first_=lst.index(1)#出现1的所有位置
        bar_slast=[]
        for i in range(first_):
            bar_slast.append(np.nan)
        for i in range(first_,len(lst)):#出现1后往后计数
            if lst[i]==1:
                count_=0
                bar_slast.append(0)
            else:
                count_+=1
                bar_slast.append(count_)
        return bar_slast
        
# @nb.jit
def COUNT(cond, n):
    # TODO lazy compute
    series = cond
    size = len(cond) - n
    try:
        result = np.full(size, 0, dtype=np.int)
    except :
        pass
    for i in range(size - 1, 0, -1):
        s = series[-n:]
        result[i] = len(s[s == True])
        series = series[:-1]
    return result

# @nb.jit
def COUNT_(cond, n):
    if type(n)!=int:
        #两列序列一一 正序对应
        result = [0]*len(cond)
        cond=list(cond)
        n=list(n)
        for i in range(1,len(cond)):
            cond_temp = cond[0:i]
            cond_n_ture=cond_temp[-n[i]:]#过去n天
            result[i] = cond_n_ture[cond_n_ture == True]
        return np.array(result)
  • 6
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

车忻青

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值