Python无限While循环不一致

Python无限While循环不一致,python,excel,infinite-loop,mt4,Python,Excel,Infinite Loop,Mt4,我用Python为无限循环编写的代码第一次运行良好。但是,在第二次运行时,它会给出以下消息: 回溯(最近一次呼叫最后一次): 文件“C:/Users/dell/PycharmProjects/pythonProject/main.py”,第27行,在 symbol=str(symbol) TypeError:“tuple”对象不可调用 你知道为什么我在第二次跑步后没有收到这个信息吗 from xlrd import open_workbook import win32com.client as

我用Python为无限循环编写的代码第一次运行良好。但是,在第二次运行时,它会给出以下消息:

回溯(最近一次呼叫最后一次): 文件“C:/Users/dell/PycharmProjects/pythonProject/main.py”,第27行,在 symbol=str(symbol) TypeError:“tuple”对象不可调用

你知道为什么我在第二次跑步后没有收到这个信息吗

from xlrd import open_workbook
import win32com.client as win32
from oandapyV20.contrib.requests import MarketOrderRequest
from oandapyV20.contrib.requests import TakeProfitDetails, StopLossDetails
import oandapyV20.endpoints.orders as orders
import oandapyV20
from oandapyV20 import API
import oandapyV20.endpoints.accounts as accounts
import oandapyV20.endpoints.pricing as pricing
import oandapyV20.endpoints.positions as positions
import easygui
import tkinter as tk
import time

while True:
    time.sleep(5)
    excel = win32.gencache.EnsureDispatch('Excel.Application')
    for wb in excel.Workbooks:
        if wb.Name == 'forex2.xlsx':
            wb.Save()

    wb = open_workbook('C:/Users/dell/Documents/forex2.xlsx')

    xl_sheet = wb.sheet_by_index(0)
    marginrate = xl_sheet.cell(1, 2)
    symbol = xl_sheet.cell(1, 1)
    symbol = str(symbol)
    marginrate = str(marginrate)
    symbol = symbol.replace("text:", "")
    marginrate = 20
    symbol = symbol.replace("'", "")
    print("Symbol:", symbol)
    print("Margin Rate:", marginrate)

    access_token = "XXXX"
    accountID = "XXXX"
    client = API(access_token=access_token)

    r = accounts.AccountDetails(accountID)
    client.request(r)
    dict = r.response

    params = {"instruments": symbol}
    r2 = pricing.PricingInfo(accountID=accountID, params=params)
    rv2 = client.request(r2)
    a = list(rv2.items())[1][1][0]
    ask = float(a['closeoutAsk'])
    print("Starting Ask:", ask)

    a = list(dict.items())[0][1]
    marginUsed = float(list(a.items())[25][1])
    marginAvailable = float(list(a.items())[26][1])
    balance = float(list(a.items())[9][1])
    print("Margin Available:", marginAvailable)
    print("Balance:", balance)
    print("Margin Used + Margin Available:", balance)

    STOP_LOSS = .001
    TAKE_PROFIT = 100000
    units0 = round((marginrate * marginAvailable) / ask * .95)
    print("Order Units:", units0)

    mktOrder = MarketOrderRequest(
        instrument=symbol,
        units=units0,
        takeProfitOnFill=TakeProfitDetails(price=TAKE_PROFIT).data,
        stopLossOnFill=StopLossDetails(price=STOP_LOSS).data)

    r = orders.OrderCreate(accountID, data=mktOrder.data)

    try:
        rv = client.request(r)
    except oandapyV20.exceptions.V20Error as err:
        print("")
        print("UNITS_INVALID")
    else:
        print("")

    excel = win32.gencache.EnsureDispatch('Excel.Application')

    for wb in excel.Workbooks:
        if wb.Name == 'forex2.xlsx':
            wb.Save()
    book = open_workbook('C:/Users/dell/Documents/forex2.xlsx')

    r = positions.PositionList(accountID=accountID)
    client.request(r)
    dict = r.response

    a = list(dict.items())[0][1]

    for i, element in enumerate(a):
        long = a[i]
        long2 = long['long']
        symbol = long['instrument']
        try:
            averagePrice = long2['averagePrice']
        except:
            pass
        else:
            window = tk.Tk()
            frame_a = tk.Frame()
            label_a = tk.Label(master=frame_a, text="Hello")
            label_a.pack()
            frame_a.pack()

            for sheet in book.sheets():
                for rowidx in range(sheet.nrows):
                    row = sheet.row(rowidx)
                    for colidx, cell in enumerate(row):
                        if cell.value == symbol:
                            row = rowidx + 3
                            current_bid = sheet.cell(1, row)
                            current_bid = str(current_bid)
                            current_bid = float(current_bid.replace("number:", ""))
                            str = "Beginning Balance:", balance, "Current Bid:", current_bid, "Average Price:", averagePrice, "Current Profit:", round(
                                (current_bid - float(averagePrice)) * units0, 2)
                            print(str)

                            data = {"longUnits": "ALL"}

                            r = positions.PositionClose(accountID=accountID, instrument=symbol, data=data)
                            client.request(r)

问题在于您使用的是

str=“期初余额:”,余额,“当前出价:”,当前出价,“平均价格:”,平均价格,“当前利润:”,四舍五入((当前出价-浮动(平均价格))*units0,2)

,它将函数str替换为您在此处指定的变量。尝试替换此变量的名称,它应该可以正常工作。

在代码末尾,您创建了一个名为
str
的变量,该变量隐藏了
str()
类型转换函数,因此第二次通过
str
是一个元组,无法调用。重命名此行中的变量:
str=“开始余额:”,余额,…