如何将 API 调用中的数据打印到 CSV 文件中 [英] How to print data from API call into a CSV file
问题描述
我正在使用 Alpaca 交易 API,并希望将函数调用中的数据导出到 CSV 文件中.
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)
我回来了:
[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'})]
如何获取这些数据并将其打印为 CSV 文件?我试图写这样的东西,但我收到错误订单"对象没有键.我假设我能够遍历 API 响应并根据标头进行写入;我如何相应地分解/扁平化 API 响应?
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)
推荐答案
您可以访问 Orders
类的 __dict__
属性以获取 CSV 的标题和行文件.
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'])
这篇关于如何将 API 调用中的数据打印到 CSV 文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!