在Python中访问特定属性时遇到问题

时间:2019-09-10 20:39:37

标签: python python-3.x bitmex

模块:https://github.com/yanagisawa-kentaro-777/pybitmex/blob/master/pybitmex/bitmex.py

我正在使用ws_open_order_objects_of_account(),使用它,我可以访问:

open_orders = bitmex.ws_open_order_objects_of_account()
for open_bid in open_orders.bids:
    print(open_bid.price)

但是我想要open_bid.orderID,但是我已经尝试过open_bid['orderID'],这是无法下标的。我正在阅读仅返回价格的功能吗?

2 个答案:

答案 0 :(得分:3)

遇到这种情况时,我建议您使用dir(open_bid)type(open_bid)之类的Python自省工具来查找您要查看的内容!

基于对源代码的快速阅读,我怀疑您正在查看

的实例
class OpenOrder:

    def __init__(self, order_id, client_order_id, side, quantity, price, timestamp):
        self.order_id = order_id
        self.client_order_id = client_order_id
        self.side = side
        self.quantity = quantity
        self.price = price
        self.timestamp = timestamp

    def __str__(self):
        return "Side: {}; Quantity: {:d}; Price: {:.1f}; OrderID: {}; ClOrdID: {}; Timestamp: {}; ".format(
            self.side, self.quantity, self.price, self.order_id, self.client_order_id,
            self.timestamp.strftime("%Y%m%d_%H%M%S")
        )

所以您可能需要open_bid.order_id

https://github.com/yanagisawa-kentaro-777/pybitmex/blob/08e6c4e7ae7bbadd5208ec01fd8d361c3a0ce992/pybitmex/models.py#L33

有关反省Python中发生的情况的更多信息:

答案 1 :(得分:0)

查看该函数的文档字符串:

        """
        [{'orderID': '57180f5f-d16a-62d6-ff8d-d1430637a8d9',
        'clOrdID': '', 'clOrdLinkID': '',
        'account': XXXXX, 'symbol': 'XBTUSD', 'side': 'Sell',
        'simpleOrderQty': None,
        'orderQty': 30, 'price': 3968,
        'displayQty': None, 'stopPx': None, 'pegOffsetValue': None,
        'pegPriceType': '', 'currency': 'USD', 'settlCurrency': 'XBt',
        'ordType': 'Limit', 'timeInForce': 'GoodTillCancel',
        'execInst': 'ParticipateDoNotInitiate', 'contingencyType': '',
        'exDestination': 'XBME', 'ordStatus': 'New', 'triggered': '',
        'workingIndicator': True, 'ordRejReason': '', 'simpleLeavesQty': None,
        'leavesQty': 30, 'simpleCumQty': None, 'cumQty': 0, 'avgPx': None,
        'multiLegReportingType': 'SingleSecurity', 'text': 'Submission from www.bitmex.com',
        'transactTime': '2019-03-25T07:10:34.290Z', 'timestamp': '2019-03-25T07:10:34.290Z'}]
        """

这表示它返回词典列表,而不是对象。您无需访问任何bids属性。

open_orders = bitmex.ws_open_order_objects_of_account()
for order in open_orders:
    print(order['price'])