【问题标题】:Python Infinite While Loop Not ConsistentPython Infinite While 循环不一致
【发布时间】:2020-08-20 04:13:30
【问题描述】:

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

Traceback(最近一次调用最后一次): 文件“C:/Users/dell/PycharmProjects/pythonProject/main.py”,第 27 行,在 符号 = str(符号) 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 的变量,它隐藏了 str() 类型转换函数,因此第二次通过 str 是一个元组,无法调用.在这一行重命名你的变量:str = "Beginning Balance:", balance, ...

标签: python excel infinite-loop mt4


【解决方案1】:

问题在于您使用
str = "Beginning Balance:", balance, "Current Bid:", current_bid, "Average Price:", averagePrice, "Current Profit:", round((current_bid - float(averagePrice)) * units0, 2)
,它将函数 str 替换为您在此处分配的变量。尝试替换此变量的名称,它应该可以正常工作。

【讨论】:

    猜你喜欢
    • 2018-08-16
    • 2023-03-10
    • 2014-09-22
    • 1970-01-01
    • 2018-02-23
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多