簡體   English   中英

打破 python 循環

[英]Break python loop

當條件為真時,我需要停止循環,我正在通過 pandas dataframe 循環,我看到問題可能來自范圍(1,len(newdatafr)):因為此列表的所有元素都必須被填充'

for ind in range(1,len(newdatafr)):
    if newdatafr['h'][ind] > tp_2 and newdatafr['l'][ind] > sl_1:
        sl_1 = tp_2  
        print("scale up")
    elif newdatafr['h'][ind] < tp_2 and newdatafr['h'][ind] > tp_1 and newdatafr['l'][ind] > sl_1:
        sl_1 = tp_1 ## BE

    elif newdatafr['l'][ind] < sl_1: 
        print("Sortie du trade. Ouverture :"+str(open_1)+" Sortie :"+str(sl_1) )
        break

我怎么能用其他方式寫這個? 謝謝

如果我理解正確,則每個 if 語句都需要休息一下:

for ind in range(1,len(newdatafr)):
    if newdatafr['h'][ind] > tp_2 and newdatafr['l'][ind] > sl_1:
        sl_1 = tp_2  
        print("scale up")
        break
    elif newdatafr['h'][ind] < tp_2 and newdatafr['h'][ind] > tp_1 and newdatafr['l'][ind] > sl_1:
        sl_1 = tp_1 ## BE
        break
    elif newdatafr['l'][ind] < sl_1: 
        print("Sortie du trade. Ouverture :"+str(open_1)+" Sortie :"+str(sl_1) )
        break

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM