THS高低点赋值{0,1]

short = 12
long = 26
m = 9
close_list=[]
high_list=[]
low_list=[]
diff_list=[]
dea_list=[]
GDCross = 0
GDCross_list=[]
CurveP = 0
CurveP_list=[]
CurveLine_list=[]
CurveLine2_list=[]
CurveLineNum_list=[]
for i in range(0, total):
    #画出MACD
    close=get("CLOSE",i)
    close_list.append(close)
    
    low=get("LOW",i)
    low_list.append(low)
    high=get("HIGH",i)
    high_list.append(high)
    CurveP = 0
    CurveP_list.append(CurveP)
    CurveLine_list.append(CurveP)
    
    s=EMA(close_list,short, i)
    l=EMA(close_list,long, i)
    diff=round(s - l, 3)

    diff_list.append(diff)
    dea=EMA(diff_list,m, i)    
    dea_list.append(dea)    

    macd=2*(diff - dea)
    save("MACD", macd,i)
    save("DIFF", diff,i)
    save("DEA", dea,i)
#画出储存好数据的线
#draw.curve("DIFF", 15)            #画出"DIFF"折线
#draw.curve("DEA", 7)              #画出"DEA"折线
#draw.color_stick("MACD")          #画出"MACD"红涨绿跌图
for i in range(0, total):
    if( diff_list[i]<0 and dea_list[i]<0 
       and diff_list[i-1] < dea_list[i-1] and diff_list[i] > dea_list[i]):
        GDCross = 1
        GDCross_list.append(GDCross)
    elif ( diff_list[i]>0 and dea_list[i]>0 
       and diff_list[i-1] > dea_list[i-1] and diff_list[i] < dea_list[i]):
        GDCross = 2
        GDCross_list.append(GDCross)
    else:
        GDCross = 0
        GDCross_list.append(GDCross)
    #save("GDCross", GDCross,i)
    #draw.curve("GDCross", 15)
lastStatus = 1; statusCount = 0; Status = 0; ###1代表'GoldC',2 代表'DeadC'
j = 0; k = 0;
for i in range(0, total): ###判断第一个是Gold还是Dead
    if(GDCross_list[i] == 1):
        lastStatus = 1
        break;
    elif(GDCross_list[i] == 2):
        lastStatus = 2
        break;          
for i in range(0, total):
    if(i == 0):  ###第一项处理
        numMax= 0;   numMin= 0;
        valMax = get("HIGH",i);  valMin = get("LOW",i);
        CurveP_list[i] = 1;
        CurveLine_list[i] = 1;
    else : ####后边的处理
        if(GDCross_list[i]!=0 and GDCross_list[i] != lastStatus):   ##计算变换次数               
            lastStatus = GDCross_list[i]
            statusCount = statusCount+1;
            Status = 1;
            CurveP_list[i] = 0; 
        if(GDCross_list[i] == lastStatus and statusCount%2 ==0):   ##开始循环。如果为偶数则如下
            k = i; 
            stockDF_list = low_list[j:k]             
            if(min(stockDF_list)<valMin):
                if(Status == 0):  ###如果状态为0,即没有变化,才把上一个变为0.否则不再更改此值
                    CurveP_list[numMin] = 0;
                valMin = min(stockDF_list);                
                numMin = stockDF_list.index(min(stockDF_list))+j;     ####这时最小值是[j:k]之间,j为起点
                #print(j,k,numMin,valMin)
                CurveP_list[numMin] = valMin;   ###求得极值时的位置,并使命名'CurveP'为1 
                CurveLine_list[numMin] = valMin;
                valMin = low_list[i]; ###最小值改为金叉时的值了     
                valMax = high_list[i]; ###最大值改为金叉时的值了
                j = i;
                Status = 0; ###状态回零
            print(i,numMin,valMin)
        elif(GDCross_list[i] == lastStatus and statusCount%2 !=0):###如果为奇数则如下
            k = i; 
            #stockDF_list = high_list[j:k]
            if(max(high_list[j:k])>valMax):
                if(Status == 0):    ###如果状态为0,即没有变化,才把上一个变为0.否则不再更改此值
                    CurveP_list[numMax] = 0;
                valMax = max(high_list[j:k]);                
                numMax = high_list[j:k].index(max(high_list[j:k]))+j;  ####这时最小值是[j:k]之间,j为起点
                #print(j,k,numMax,valMax)
                CurveP_list[numMax] = valMax;   ####求得极值时的位置,并使命名'CurveP'为1 
                CurveLine_list[numMax] = valMax;
                valMin = low_list[i]; ###最小值改为金叉时的值了   
                valMax = high_list[i]; ###最大值改为金叉时的值了
                j = i;
                Status = 0; ###状态回零
            print(i,"hihg",numMax,valMax)   
for i in range(0, total):
    if(CurveP_list[i]!= 0):
        CurveLine2_list.append(CurveP_list[i])
        CurveLineNum_list.append(i)
        
for i in range(1,len(CurveLineNum_list)):
    draw.line(CurveLine2_list[i-1],CurveLineNum_list[i-1],CurveLine2_list[i],CurveLineNum_list[i],"blue")     

posted @ 2023-02-07 11:25  光量子  阅读(82)  评论(0编辑  收藏  举报