简体   繁体   中英

Display updated values nested in a a loop, and iterating over a dynamic list Python

I am currently writing a small python program to watch my current trades at 1Broker.com using their API, it should list any open positions, number them using a counter, and then update the chosen stats (P/L percent and position_id) on each run through, after a sleep timer.

I was working in a dirty early draft, however decided to "optimize" and clean my code... and broke it. Here is a snippit of the erroneous section:

if self.total_open_positions != []:
        counter = int("0")
        for position in self.total_open_positions:
            counter += 1
            display =  {}
            display["Open Order"] = str(counter)
            display["ID"] = str(position["position_id"])
            display["P/L Percent"] = str(position["profit_loss_percent"])                           
            print display
            time.sleep(timer - ((time.time() - starttime) % timer))
        if self.total_positions == []:
            print "All trades closed"

The data in P/L percent and ID refuse to update on each loop through.

Thank you in advance to anyone who helps :)

By "refuse to update" I mean when the program is run, it retrieves the P/L percent as desired. However each consecutive loop prints the same P/L percentage. Meaning the value of str(position["profit_loss_percent"]) hasnt updated to the most recent data retrieved from the website. (eg. shows 3%, when the trade is now up to 6%)

在此处输入图片说明 The left image is 1st draft. However, it you look, the P/L% chages through each iteration. whereas in the right image it stays the same.

As for self.total_open_positions, it is equal to my api request: self.total_open_positions = requests.get(API_URL)

heres a snippet of the "dirty" version that at least works properly, maybe it will help show my intentions, and my noob-level skill (which is why i need help lol):

total_open_orders = open_orders["response"]
while total_open_orders == []:
    print "Checking again......"
    time.sleep(timer1 - ((time.time() - starttime) % timer1))
else:
    #When orders are found, loop through and display
    while True:
        #Wait desired time between refreshing stats
        time.sleep(timer1 - ((time.time() - starttime) % timer1))
        #Position number (oldest first)
        counter = 0
        ##Print a small seperator between refreshes
        print "#" * 20
        #Loop through list of positions and print stats for each
        for order in total_open_orders:
            #Add to counter for each position
            counter += 1
            #Display stats to user (anything in '[]' is JSON format
            try:
                print "#" * 40
                print "Open Order #: " + str(counter)
                print "ID #:         " + str(order["position_id"])
                print "Market:       " + str(order["symbol"])
                print "Entry:        " + str(order["entry_price"])
                print "Stop Loss:    " + str(order["stop_loss"])
                print "Take Profit:  " + str(order["take_profit"])
                print "P/L:  " + str(order["profit_loss_percent"])
                print "#" * 40
                print ""
            #Catch any connection errors and print for debugging
            except Exception as e:
                print e

Problem has been solved. Because self.total_open_positions was defined in a function above, it was only called once and therefore only got one set of stats. by having total_open_positions redefined within the scope of my loop, all data is properly displayed :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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