简体   繁体   English

Python 无限 While 循环不一致

[英]Python Infinite While Loop Not Consistent

The code I have wrote in Python for an infinite loop works fine the first time.我在 Python 中为无限循环编写的代码第一次可以正常工作。 However, it gives the following message upon the second run:但是,它在第二次运行时给出以下消息:

Traceback (most recent call last): File "C:/Users/dell/PycharmProjects/pythonProject/main.py", line 27, in symbol = str(symbol) TypeError: 'tuple' object is not callable回溯(最后一次调用):文件“C:/Users/dell/PycharmProjects/pythonProject/main.py”,第 27 行,符号 = str(symbol) TypeError: 'tuple' object is not callable

Any ideas why I am not getting this message after second run?任何想法为什么我在第二次运行后没有收到此消息?

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)

The problem is your using问题是你使用
str = "Beginning Balance:", balance, "Current Bid:", current_bid, "Average Price:", averagePrice, "Current Profit:", round((current_bid - float(averagePrice)) * units0, 2)
,which replaces the function str with the variable you are assigning here. ,它将 function str 替换为您在此处分配的变量。 try replacing the name of this variable and it should work fine.尝试替换此变量的名称,它应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM