在字典列表中搜索大于给定值的值?
Search list of dicts for values greater than given value?
所以我有一个具有不同行使价、买入价和 implied_volatility 的指令列表,用于 4 种不同的潜在期权交易。
search_option = [{'strike_price': '1', 'bid_price': '0.25', 'implied_volatility': '0.94' },
{'strike_price': '3.5', 'bid_price': '0.20', 'implied_volatility': '0.88'},
{'strike_price': '2', 'bid_price': '0.05', 'implied_volatility': None},
{'strike_price': '3.5', 'bid_price': '0.31', 'implied_volatility': '0.25'}]
在这里,代码搜索具有最高 implied_volatility 的选项并给我一个输出。
highest_IV, highest_idx = 0, None
for idx, option in enumerate(search_option):
if option['implied_volatility'] and highest_IV < float(option['implied_volatility']):
highest_IV = float(option['implied_volatility'])
highest_idx = idx
if highest_idx is not None:
print("Strike Price: {strike_price}, Bid: {bid_price}, IV: {implied_volatility}".format(**search_option[highest_idx]))
然后我将它们分别保存为变量以启动交易。
order_strike_price = search_option[highest_idx].get('strike_price')
order_bid_price = search_option[highest_idx].get('bid_price')
....
....
我真的不再需要 'highest implied volatility' 代码了。
我现在的任务是,如何搜索行使价 > 3 且买入价 < 0.25 的期权?
然后我需要将匹配字典的所有键(strike、bid、implied_volatility)保存为单独的变量,就像我在上面所做的那样。
试试这个:
search_option = [{'strike_price': '1', 'bid_price': '0.25', 'implied_volatility': '0.94' },
{'strike_price': '3.5', 'bid_price': '0.20', 'implied_volatility': '0.88'},
{'strike_price': '2', 'bid_price': '0.05', 'implied_volatility': None},
{'strike_price': '3.5', 'bid_price': '0.31', 'implied_volatility': '0.25'}]
sort = [i for i in search_option if float(i["strike_price"]) > 3 and float(i["bid_price"]) < 0.25]
print(sort)
输出:
[{'strike_price': '3.5', 'bid_price': '0.20', 'implied_volatility': '0.88'}]
所以我有一个具有不同行使价、买入价和 implied_volatility 的指令列表,用于 4 种不同的潜在期权交易。
search_option = [{'strike_price': '1', 'bid_price': '0.25', 'implied_volatility': '0.94' },
{'strike_price': '3.5', 'bid_price': '0.20', 'implied_volatility': '0.88'},
{'strike_price': '2', 'bid_price': '0.05', 'implied_volatility': None},
{'strike_price': '3.5', 'bid_price': '0.31', 'implied_volatility': '0.25'}]
在这里,代码搜索具有最高 implied_volatility 的选项并给我一个输出。
highest_IV, highest_idx = 0, None
for idx, option in enumerate(search_option):
if option['implied_volatility'] and highest_IV < float(option['implied_volatility']):
highest_IV = float(option['implied_volatility'])
highest_idx = idx
if highest_idx is not None:
print("Strike Price: {strike_price}, Bid: {bid_price}, IV: {implied_volatility}".format(**search_option[highest_idx]))
然后我将它们分别保存为变量以启动交易。
order_strike_price = search_option[highest_idx].get('strike_price')
order_bid_price = search_option[highest_idx].get('bid_price')
....
....
我真的不再需要 'highest implied volatility' 代码了。
我现在的任务是,如何搜索行使价 > 3 且买入价 < 0.25 的期权?
然后我需要将匹配字典的所有键(strike、bid、implied_volatility)保存为单独的变量,就像我在上面所做的那样。
试试这个:
search_option = [{'strike_price': '1', 'bid_price': '0.25', 'implied_volatility': '0.94' },
{'strike_price': '3.5', 'bid_price': '0.20', 'implied_volatility': '0.88'},
{'strike_price': '2', 'bid_price': '0.05', 'implied_volatility': None},
{'strike_price': '3.5', 'bid_price': '0.31', 'implied_volatility': '0.25'}]
sort = [i for i in search_option if float(i["strike_price"]) > 3 and float(i["bid_price"]) < 0.25]
print(sort)
输出:
[{'strike_price': '3.5', 'bid_price': '0.20', 'implied_volatility': '0.88'}]