samedi 13 juillet 2019

Python- If statement to handle queue output not correctly handling all outputs

I receive data updates via a queue, in order to determine whether the update is an updated order(s) or updated market price(s) I need to run an if statement, which looks to see which custom class is being received. This works fine, I can see the correct classes are being received and the if statement (or at least this part) works correctly.

Where I am struggling is that I have 2 functions, one that updates the orders and puts them in a format to feed into a second function, which then formats the market data (with orders) and outputs in a readable format.

The functions themselves work correctly, in that if I plug in the expected data, I get the correct outputs...however I am struggling to get my head around receiving the data correctly, as this appears to be causing the problem.

while True:
    combined_books = market_queue.get()
    order_books = []
    market_books = []
    for y in combined_books:
        print(y)
        if isinstance(y, MarketBook):
            market_books.append(y)
            time_stamp = datetime.now()
            print(time_stamp, "Running for: ", time_stamp - start_time,
                  "======================================================")
            update_market_book(market_books, mb, uo, market_catalogues, mkt_runners, profitloss, trading, False,
                               start_time, eo)
        elif isinstance(y, CurrentOrders):
            order_books.append(y)
            uo = update_orders(order_books, mkt_runners, profitloss, trading, eo, mb)
            time_stamp = datetime.now()
            print(time_stamp, "Running for: ", time_stamp - start_time,
                  "======================================================")
            update_market_book(market_books, mb, uo, market_catalogues, mkt_runners, profitloss, trading, False,
                               start_time, eo)
        else:
            print('Its all gone horribly wrong: ', y)


So, what happens is as follows:

1) If I receive a market data update, the update_market_book function is triggered and this works correctly.

2) If I receive an order update, this is correctly identified, but doesn't update the market_book (i.e. of the 2 functions, update_orders fires, but then there is no market_books to populate the first variable of the update_market_books function and hence it simply shows a blank).

3) When the next market data update comes through, the previous orders show correctly.

So in essence, what I'm not understanding, is why in this section:

        elif isinstance(y, CurrentOrders):
            order_books.append(y)
            uo = update_orders(order_books, mkt_runners, profitloss, trading, eo, mb)
            time_stamp = datetime.now()
            print(time_stamp, "Running for: ", time_stamp - start_time,
                  "======================================================")
            update_market_book(market_books, mb, uo, market_catalogues, mkt_runners, profitloss, trading, False,
                               start_time, eo)

market_books is blank, when it has already been populated by a previous update and/or perhaps more importantly, how can I ensure that it is populated with the last updated market_books that (correctly) triggered the first part of the if statement.

Hopefully that makes sense and is probably really simple to a Python expert, maybe its the time of day, but I just can't seem to figure out the logic here, so any help would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire