How to print data from API call into a CSV file

て烟熏妆下的殇ゞ 提交于 2021-01-29 09:23:12

问题


I'm using the Alpaca trading API and want to export data from a function call into a CSV file.

When I run a call like this:

    closed_orders = api.list_orders(
    status='closed',
    limit=2,
    nested=True  # show nested multi-leg orders
)
print(closed_orders)

I get back:

[Order({   'asset_class': 'us_equity',
    'asset_id': '8a9-43b6-9b36-662f01e8fadd',
    'canceled_at': None,
    'client_order_id': 'e38a-b51c-349314bc6e9e',
    'created_at': '2020-06-05T16:16:53.307491Z',
    'expired_at': None,
    'extended_hours': False,
    'failed_at': None,
    'filled_at': '2020-06-05T16:16:53.329Z',
    'filled_avg_price': '7.8701',
    'filled_qty': '45',
    'id': '8-4888-9c7c-97bf8c2a3a16',
    'legs': None,
    'limit_price': '7.87',
    'order_class': '',
    'order_type': 'limit',
    'qty': '45',
    'replaced_at': None,
    'replaced_by': None,
    'replaces': None,
    'side': 'sell',
    'status': 'filled',
    'stop_price': None,
    'submitted_at': '2020-06-05T16:16:53.293859Z',
    'symbol': 'CARS',
    'time_in_force': 'day',
    'type': 'limit',
    'updated_at': '2020-06-08T11:21:51.411547Z'}), Order({   'asset_class': 'us_equity',
    'asset_id': '1aef-42f4-9975-750dbcb3e67d',
    'canceled_at': None,
    'client_order_id': '2bde-4572-a5d0-bfc32c2bf31a',
    'created_at': '2020-06-05T16:16:37.508176Z',
    'expired_at': None,
    'extended_hours': False,
    'failed_at': None,
    'filled_at': '2020-06-05T16:16:37.531Z',
    'filled_avg_price': '10.8501',
    'filled_qty': '26',
    'id': '4256-472c-a5de-6ca9d6a21422',
    'legs': None,
    'limit_price': '10.85',
    'order_class': '',
    'order_type': 'limit',
    'qty': '26',
    'replaced_at': None,
    'replaced_by': None,
    'replaces': None,
    'side': 'sell',
    'status': 'filled',
    'stop_price': None,
    'submitted_at': '2020-06-05T16:16:37.494389Z',
    'symbol': 'IGT',
    'time_in_force': 'day',
    'type': 'limit',
    'updated_at': '2020-06-08T11:21:51.424963Z'})]

How do I grab this data and print it into a CSV? I tried to write something like this but I get the error "Order" object has no keys. I assumed I'd be able to loop through the API response and write according to the headers; how do I break down/flatten the API response accordingly?

with open('historical_orders.csv', 'w', newline='') as csvfile:
    fieldnames = [
        'asset_id',
        'canceled_at',
        'client_order_id',
        'created_at',
        'expired_at',
        'extended_hours',
        'failed_at',
        'filled_at',
        'filled_avg_price',
        'filled_qty',
        'id',
        'legs',
        'limit_price',
        'order_class',
        'order_type',
        'qty',
        'replaced_at',
        'replaced_by',
        'replaces',
        'side',
        'status',
        'stop_price',
        'submitted_at',
        'symbol',
        'time_in_force',
        'type',
        'updated_at'
    ]
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()

    for c in closed_orders:
        writer.writerow(c)

回答1:


You can access the __dict__ attribute of the Orders class to get the headers and rows for the CSV file.

with open('historical_orders.csv', 'w', newline='') as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=closed_orders[0].__dict__['_raw'].keys())
    writer.writeheader()
    for order in closed_orders:
        writer.writerow(order.__dict__['_raw'])


来源:https://stackoverflow.com/questions/62397498/how-to-print-data-from-api-call-into-a-csv-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!