如何解决如何在Python中创建股票期权交易日志?
我是python的新手,不确定要问这个问题的去向。我的主要目标是为所有期权交易创建交易日志,以查看每只股票,策略和时间表的净利润。我一直在尝试找到一种组织所有事务,忽略重复项并使之可排序的方法。到目前为止,我已经尝试过嵌套词典(添加所有条目时会遇到问题)和嵌套列表(我认为这样可以很好地组织它)。我将发布一个示例,说明我正在寻找的东西和尝试过的东西(底部的字典是我想要的,但不会捕获所有条目。有什么建议吗?
transaction_log = []
results = set()
results_list = []
x = 0
for option in options_portfolio:
transaction_log[options_portfolio[x].chain_symbol] = dict({options_portfolio[x].option_type: {options_portfolio[x].expiration_date: {options_portfolio[x].strike_price: {options_portfolio[x].group: options_portfolio[x].name}}}})
transaction_log[options_portfolio[x].chain_symbol] = options_portfolio[x].option_type
transaction_log[options_portfolio[x].chain_symbol][options_portfolio[x].option_type] = options_portfolio[x].expiration_date
transaction_log[options_portfolio[x].chain_symbol][options_portfolio[x].option_type][options_portfolio[x].expiration_date] = options_portfolio[x].strike_price
transaction_log[options_portfolio[x].chain_symbol][options_portfolio[x].option_type][options_portfolio[x].expiration_date][options_portfolio[x].strike_price] = options_portfolio[x].group
transaction_log[options_portfolio[x].chain_symbol][options_portfolio[x].option_type][options_portfolio[x].expiration_date][options_portfolio[x].strike_price][options_portfolio[x].group] = options_portfolio[x].name
y = 0
for option in options_portfolio:
if options_portfolio[x].order_created_at == options_portfolio[y].order_created_at:
pass
elif options_portfolio[x].order_created_at != options_portfolio[y].order_created_at:
if options_portfolio[x].chain_symbol == options_portfolio[y].chain_symbol and options_portfolio[x].expiration_date == options_portfolio[y].expiration_date and options_portfolio[x].strike_price == options_portfolio[y].strike_price and options_portfolio[x].option_type == options_portfolio[y].option_type:
results.add((f'{options_portfolio[y].name} and {options_portfolio[x].name} are the same option'))
results_list.append((options_portfolio[y].name,options_portfolio[x].name))
else:
pass
else:
pass
y += 1
x += 1
x = 0
y = 0
{'chain_symbol': 'AAPL','expiration_date': '2020-09-18','strike_price': 110.0,'option_type': 'put','side': 'buy','order_created_at': '2020-09-11T19:48:25.375384Z','direction': 'debit','order_quantity': 1.0,'order_type': 'limit','opening_strategy': 'long_put','closing_strategy': nan,'price': 2.45,'processed_quantity': 1.0,'quanity_remaining': 1.0,'is_open': True,'group': 'AAPL_2020-09-18_110.0_put','name': 'AAPL_2020-09-18_110.0_put_buy_2020-09-11T19:48:25.375384Z_1.0'}
{'chain_symbol': 'SQ','expiration_date': '2020-10-02','strike_price': 130.0,'order_created_at': '2020-09-11T17:31:23.914397Z','price': 5.7,'group': 'SQ_2020-10-02_130.0_put','name': 'SQ_2020-10-02_130.0_put_buy_2020-09-11T17:31:23.914397Z_1.0'}
{'chain_symbol': 'QQQ','strike_price': 269.0,'side': 'sell','order_created_at': '2020-09-11T17:20:54.890999Z','direction': 'credit','opening_strategy': nan,'closing_strategy': 'long_put','price': 6.14,'quanity_remaining': '-1.0','group': 'QQQ_2020-09-18_269.0_put','name': 'QQQ_2020-09-18_269.0_put_sell_2020-09-11T17:20:54.890999Z_1.0'}
{'chain_symbol': 'QQQ','order_created_at': '2020-09-11T16:45:29.188917Z','price': 5.36,'name': 'QQQ_2020-09-18_269.0_put_buy_2020-09-11T16:45:29.188917Z_1.0'}
{'chain_symbol': 'SQ','strike_price': 135.0,'order_created_at': '2020-09-11T16:43:03.694028Z','price': 4.4,'group': 'SQ_2020-09-18_135.0_put','name': 'SQ_2020-09-18_135.0_put_sell_2020-09-11T16:43:03.694028Z_1.0'}
{'chain_symbol': 'SQ','order_created_at': '2020-09-11T16:41:49.081863Z','price': 4.0,'name': 'SQ_2020-09-18_135.0_put_buy_2020-09-11T16:41:49.081863Z_1.0'}
{'chain_symbol': 'GOOG','strike_price': 1520.0,'option_type': 'call','order_created_at': '2020-09-11T16:39:35.940262Z','opening_strategy': 'long_call','price': 27.4,'group': 'GOOG_2020-09-18_1520.0_call','name': 'GOOG_2020-09-18_1520.0_call_buy_2020-09-11T16:39:35.940262Z_1.0'}
{'chain_symbol': 'GOOG','strike_price': 1540.0,'order_created_at': '2020-09-11T16:39:05.158876Z','closing_strategy': 'long_call','price': 18.1,'group': 'GOOG_2020-09-18_1540.0_call','name': 'GOOG_2020-09-18_1540.0_call_sell_2020-09-11T16:39:05.158876Z_1.0'}
{'chain_symbol': 'MSFT','strike_price': 200.0,'order_created_at': '2020-09-11T16:36:56.414844Z','order_quantity': 2.0,'price': 3.5,'processed_quantity': 2.0,'quanity_remaining': '-2.0','group': 'MSFT_2020-09-18_200.0_put','name': 'MSFT_2020-09-18_200.0_put_sell_2020-09-11T16:36:56.414844Z_2.0'}
{'chain_symbol': 'MSFT','order_created_at': '2020-09-11T16:36:23.445176Z','price': 2.9,'quanity_remaining': 2.0,'name': 'MSFT_2020-09-18_200.0_put_buy_2020-09-11T16:36:23.445176Z_2.0'}
number of unique option traded = 5
{'AAPL': {'put': {'2020-09-18': {110.0: {'AAPL_2020-09-18_110.0_put': 'AAPL_2020-09-18_110.0_put_buy_2020-09-11T19:48:25.375384Z_1.0'}}}},'SQ': {'put': {'2020-09-18': {135.0: {'SQ_2020-09-18_135.0_put': 'SQ_2020-09-18_135.0_put_buy_2020-09-11T16:41:49.081863Z_1.0'}}}},'QQQ': {'put': {'2020-09-18': {269.0: {'QQQ_2020-09-18_269.0_put': 'QQQ_2020-09-18_269.0_put_buy_2020-09-11T16:45:29.188917Z_1.0'}}}},'GOOG': {'call': {'2020-09-18': {1540.0: {'GOOG_2020-09-18_1540.0_call': 'GOOG_2020-09-18_1540.0_call_sell_2020-09-11T16:39:05.158876Z_1.0'}}}},'MSFT': {'put': {'2020-09-18': {200.0: {'MSFT_2020-09-18_200.0_put': 'MSFT_2020-09-18_200.0_put_buy_2020-09-11T16:36:23.445176Z_2.0'}}}}}
['AAPL',['SQ','put'],['QQQ','SQ',['GOOG','call'],['MSFT','put']]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。