全局变量的 While 和 for 循环不能用 Python 更新了

问题描述 投票:0回答:1

单一符号的工作

  todate = zerodha.get_trade_day(datetime.now().astimezone(to_india) - timedelta(days=0))
fromdate = zerodha.get_trade_day(datetime.now().astimezone(to_india) - timedelta(days=5))

symbol = "ZINC20MAYFUT" 

instype = "MCX"     
Timeinterval = "5minute" 

tradeDir =  0 #neutral

while (True):

histdata1 = zerodha.get_history(symbol, fromdate, todate, Timeinterval, instype)
df = pd.DataFrame(histdata1)
df = heikinashi(df)
df = bollinger_bands(df,field='h_close',period=20, numsd=2)
df1 =pd.DataFrame(df, columns=['date','volume','close','h_close','middle_band', 'upper_band'])
pp = pd.DataFrame(df1.tail(3))
print(pp)

dfCToList = pp['h_close'].tolist()
dfCList = list(pp['h_close'])
dfHValues = pp['h_close'].values
dfBMValues = pp['middle_band'].values
H_last = dfHValues[2]  # tail 1
BM_last = dfBMValues[2]  # tail 1

if (H_last > BM_last and (tradeDir == 0 or tradeDir == -1)):
    print("buy")
    tradeDir = 1  # up

if (H_last < BM_last and (tradeDir == 0 or tradeDir == 1)):
    print("SELL")
    tradeDir = -1  # down

# pdb.set_trace()

问题。当条件满足时,其打印 "BUYSELL "一次又一次。我想只打印一次,当条件满足第一次时

todate = zerodha.get_trade_day(datetime.now().astimezone(to_india) - timedelta(days=0))
fromdate = zerodha.get_trade_day(datetime.now().astimezone(to_india) - timedelta(days=5))
tradeDir =  0 #neutral

def script():
    global tradeDir
    ##For historical Data##
    symbol = ["ZINC20MAYFUT" ,"CRUDEOIL20MAYFUT","GOLD20JUNFUT"]
    instype = "MCX"     
    Timeinterval = "5minute" 

for symbol in symbol:
    global tradeDir
    histdata1 = zerodha.get_history(symbol, fromdate, todate, Timeinterval, instype)
    df = pd.DataFrame(histdata1)
    df = heikinashi(df)
    df = bollinger_bands(df,field='h_close',period=20, numsd=2)
    df1 =pd.DataFrame(df, columns=['date','volume','close','h_close','middle_band', 'upper_band'])
    pp = pd.DataFrame(df1.tail(3))
    print(pp)
    dfCToList = pp['h_close'].tolist()
    dfCList = list(pp['h_close'])
    dfHValues = pp['h_close'].values
    dfBMValues = pp['middle_band'].values
    H_last = dfHValues[2]  # tail 1
    BM_last = dfBMValues[2]  # tail 1

    if (H_last > BM_last and (tradeDir == 0 or tradeDir == -1)):
        print("buy")
        tradeDir = 1  # up

    if (H_last < BM_last and (tradeDir == 0 or tradeDir == 1)):
        print("SELL")
        tradeDir = -1  # down

    # pdb.set_trace()

while True:
    try:
        script()
    except Exception as e:
        sleep(2)
        continue

当条件满足时,其打印 "BUYSELL "一次又一次。我想只打印一次,当条件满足时,第一次完整的脚本,并应连续运行。

python function for-loop while-loop global-variables
1个回答
0
投票

如果你想让代码在第一次打印出 "买入 "或 "卖出 "后停止循环,你只需要在代码中添加一个 "买入 "或 "卖出 "的字样。break 语句后的每条打印内容(在包含的 if 块)。)

© www.soinside.com 2019 - 2024. All rights reserved.