[英]Using Context-Free Grammar To Parse Options Spread Order Strings?
I need to create a tool that reads in an options spread order in string format and spits it out in human readable format.我需要创建一个工具,该工具以字符串格式读取期权传播顺序,并以人类可读的格式将其输出。
Examples:例子:
Input:输入:
BUY +6 VERTICAL LUV 100 (Weeklys) 28 AUG 20 37.5/36.5 PUT @.49 LMT
Output:输出:
VERTICAL
BUY +6 LUV 28 AUG 20 (Weeklys) 37.5 PUT
SELL -6 LUV 28 AUG 20 (Weeklys) 36.5 PUT
.49 DEBIT LMT
Input:输入:
BUY +1 DIAGONAL AMGN 100 (Weeklys) 4 SEP 20/28 AUG 20 245/240 CALL @.07 LMT
Output:输出:
DIAGONAL
BUY +1 AMGN 4 SEP 20 (Weeklys) 245 CALL
SELL +1 AMGN 28 AUG 20 (Weeklys) 240 CALL
-.07 CREDIT LMT
On the surface a context-free grammar appears to be a good solution to express the various syntax (diagonal spreads are more complicated).从表面上看,上下文无关语法似乎是表达各种语法的好方法(对角线扩展更复杂)。 But having almost no experience with context-free grammars I am not sure how I would carry the numbers over and also how I would for instance add the SELL orders which are not explicitly mentioned in the original order string.
但是几乎没有使用上下文无关语法的经验,我不确定我将如何携带数字,以及我将如何添加原始订单字符串中未明确提及的 SELL 订单。 The SELL leg is assumed due to it being a vertical spread for example.
例如,假设卖出边是垂直价差。
Hope this makes sense even if you are not an option trader ;-) The basic idea here is that translating the original string requires a bit of intelligence and is not just a matter of generating different text.希望即使您不是期权交易者,这也有意义;-) 这里的基本思想是翻译原始字符串需要一点智能,而不仅仅是生成不同文本的问题。
Any insights and pointers would be welcome.欢迎任何见解和指示。
It's a little hard to tell from only 2 examples, but my guess is, using a context-free grammar (especially if you have almost no experience with them) is probably overkill.仅从 2 个示例中很难判断,但我的猜测是,使用上下文无关语法(特别是如果您几乎没有使用它们的经验)可能有点过头了。 The grammar itself would probably be simple enough, but you would need to either add 'actions' to transform the recognized input into the desired output, or have the parser build a syntax-tree and then write code to extract the data from the tree and generate the desired output.
语法本身可能很简单,但是您需要添加“动作”以将识别的输入转换为所需的输出,或者让解析器构建语法树,然后编写代码以从树中提取数据并生成所需的输出。
It would be simpler to use regular expressions with capturing.使用正则表达式进行捕获会更简单。 For instance, here's some python3 code that pretty much handles your 2 examples:
例如,这里有一些 python3 代码几乎可以处理您的 2 个示例:
import sys, re
for line in sys.stdin:
mo = re.fullmatch(r'BUY \+(\d+) (VERTICAL|DIAGONAL) (\S+) 100 \(Weeklys\) (\d+ \w+ \d+)(?:/(\d+ \w+ \d+))? ([\d.]+)/([\d.]+) (PUT|CALL) @(.\d+) LMT\n', line)
(n_units, vert_or_diag, name, date1, date2, price1, price2, put_or_call, limit) = mo.groups()
if vert_or_diag == 'VERTICAL':
assert date2 is None
date2 = date1
print()
print(vert_or_diag)
print(f"BUY +{n_units} {name} {date1} (Weeklys) {price1} {put_or_call}")
print(f"SELL -{n_units} {name} {date2} (Weeklys) {price2} {put_or_call}")
print(f"{limit} DEBIT LMT")
It's not perfect, because the problem isn't perfectly specified (eg, it's unclear what causes the human readable format to have a positive DEBIT vs a negative CREDIT).它并不完美,因为问题没有完美地说明(例如,不清楚是什么导致人类可读格式具有正借方与负贷方)。 And the space of inputs is no doubt larger than the regex currently handles.
并且输入的空间无疑比正则表达式当前处理的要大。
The point is just to show that, based on the examples given, regular expressions could be a compact solution to the general problem.重点只是表明,基于给定的示例,正则表达式可以是一般问题的紧凑解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.