我对编程非常陌生,下面的while循环在Python中遇到了一个奇怪的问题。当我在循环下面手动运行代码时(即从“现在开始...”开始到“ t + = 1”),我得到了想要的输出:
The time is now: 13:31:01
Your current position is -1
This is iteration: 1
The time is now: 13:32:01
Your current position is -1
This is iteration: 2
但是,当我运行完整循环时,我会在一分钟内得到两次,三次或多次打印(请参见while循环代码下方的输出)。 您是否看到此循环的问题?好像“ t + = 1”的增量在每个循环中运行了几次(即,每分钟打印几遍)。我不明白。
感谢您的帮助!
Blockquote
t=1
while t < 2000:
time_now = (time.strftime('%H:%M:%S', time.localtime(int(time.time()))))
if time_now[6:8] == str('00'):
sleep(1)
elif time_now[6:8] == str('01'):
sleep(0)
else:
x = 61 - int(time_now[6:8])
sleep(x)
time_now = (time.strftime('%H:%M:%S', time.localtime(int(time.time()))))
print("The time is now: " + time_now)
#+1 is the middle bar
totalBars = leftBars + rightBars + 1
swing_candles = client.Trade.Trade_getBucketed(symbol=symbol, binSize="1m", count=totalBars, reverse=True).result()[0]
last_highs = []
last_lows = []
i=0
while i <= (len(swing_candles)-1):
last_highs.append(swing_candles[i]["high"])
last_lows.append(swing_candles[i]["low"])
i += 1
#get the highest high and the lowest low
highest_high = max(last_highs)
lowest_low = min(last_lows)
#check if there are existing positions & orders
positions_quantity = client.Position.Position_get().result()[0][0]["currentQty"]
#check existing orders
buy_orders_quantity = []
sell_orders_quantity = []
orders_quantity = client.Order.Order_getOrders(filter=json.dumps({"open": True})).result()[0]
h=0
while h <= len(orders_quantity)-1:
if orders_quantity[h]["side"] == "Sell":
sell_orders_quantity.append(orders_quantity[h])
elif orders_quantity[h]["side"] == "Buy":
buy_orders_quantity.append(orders_quantity[h])
h += 1
if highest_high == last_highs[rightBars] and positions_quantity == 0:
if buy_orders_quantity == []:
client.Order.Order_new(symbol = symbol, orderQty = orderQty*1, side = "Buy", ordType = 'Stop', stopPx = highest_high, execInst ='LastPrice' ).result()
elif buy_orders_quantity != []:
orderID = buy_orders_quantity[0]["orderID"]
client.Order.Order_amend(orderID=orderID, orderQty=orderQty*1, stopPx = highest_high).result()
else:
pass
elif highest_high == last_highs[rightBars] and positions_quantity > 0:
#dont place any additional long
pass
elif highest_high == last_highs[rightBars] and positions_quantity < 0:
if buy_orders_quantity != []:
orderID = buy_orders_quantity[0]["orderID"]
client.Order.Order_amend(orderID=orderID, orderQty=orderQty*2, stopPx = highest_high).result()
else:
client.Order.Order_new(symbol = symbol, orderQty = (orderQty)*2, side = "Buy", ordType = 'Stop', stopPx = highest_high, execInst ='LastPrice' ).result()
elif lowest_low == last_lows[rightBars] and positions_quantity == 0:
if sell_orders_quantity == []:
client.Order.Order_new(symbol = symbol, orderQty = (orderQty)*-1, side = "Sell", ordType = 'Stop', stopPx = lowest_low, execInst ='LastPrice' ).result()
elif sell_orders_quantity != []:
orderID = sell_orders_quantity[0]["orderID"]
client.Order.Order_amend(orderID=orderID, orderQty=orderQty*-1, stopPx = lowest_low ).result()
else:
pass
elif lowest_low == last_lows[rightBars] and positions_quantity < 0:
#dont place any additional shorts
pass
elif lowest_low == last_lows[rightBars] and positions_quantity > 0:
if sell_orders_quantity != []:
orderID = sell_orders_quantity[0]["orderID"]
client.Order.Order_amend(orderID=orderID, orderQty=orderQty*-2, stopPx = lowest_low).result()
else:
#if there is no order, place new order with double amount
client.Order.Order_new(symbol = symbol, orderQty = (orderQty)*-2, side = "Sell", ordType = 'Stop', stopPx = lowest_low, execInst ='LastPrice' ).result()
positions_quantity = client.Position.Position_get().result()[0][0]["currentQty"]
print("Your current position is " + str(positions_quantity))
print("This is iteration: " + str(t))
t += 1
这是我在上面的while循环中运行的输出(但是每分钟应该只打印一次,但是从迭代2开始,同一分钟(循环)有几张打印):
The time is now: 13:39:01
Your current position is -1
This is iteration: 1
The time is now: 13:39:01
Your current position is -1
This is iteration: 2
The time is now: 13:40:01
Your current position is -1
This is iteration: 3
The time is now: 13:40:01
Your current position is -1
This is iteration: 4
The time is now: 13:40:01
Your current position is -1
This is iteration: 5
The time is now: 13:40:01
Your current position is -1
This is iteration: 6
The time is now: 13:40:01
Your current position is -1
This is iteration: 7
The time is now: 13:41:01
Your current position is -1
This is iteration: 8
The time is now: 13:41:01
Your current position is -1
This is iteration: 9
The time is now: 13:41:01
Your current position is -1
This is iteration: 10
The time is now: 13:41:01
Your current position is -1
This is iteration: 11
The time is now: 13:41:01
Your current position is -1
This is iteration: 12
答案 0 :(得分:1)
假设“运行完整循环”(方案A)比“手动运行时代码”(方案B)时要快,那么似乎发生的情况是,time_now
中的秒数为“ 01”,方案A足够快,可以完成一些迭代,time_now
中的秒数保持在“ 01”,而方案B则没有。
如果您在time_now
中的秒数为“ 01”时睡了至少1秒钟,那应该可以避免这种情况,因为它会更改time_now
中的秒数。