FpML to QuantLib 外滙美式選擇權估值
Non-Copylefted 2019 Alvin Cho. Provided ‘AS IS’. 7 May 2019.
Any quantitative method used in this file may not 100% correct for some reasons. Please use after you fully understand them.
版權沒有, 歡迎使用, 沒有任何保證. 裡面使用的估值方法可以因為任何理由不正確或是不適用你的情況, 請完全理解再使用.
FpML 解析
from lxml import etree
利用 etree.parse 將 xml 檔案載入並 parse. 這個文檔是 FpML 5.11 Comfirmation View 裡的 fx-ex10-amer-opt.zml. 將 FpML.org 下載的最新版規格壓後就可以找到. 此 FpML 文檔的詳細說明請參考 FpML 範例詳解: 外滙美式選擇權 fx-ex10-amer-opt.xml
fp=etree.parse('FpML/confirmation/products/fx-derivatives/fx-ex10-amer-opt.xml')
Confirmation View 的 namespace 一律是這個, 在任何 xpath 搜尋都需要指定 namespace.
在 FpML 裡面 namespace 用得不是很複雜, 同一個 view 裡的都用一樣
ns={
"ns":"http://www.fpml.org/FpML-5/confirmation"}
asset=fp.xpath('//ns:trade/*',namespaces=ns)[1].tag
找到 tag 為 trade 的 node, 裡面有交易的細節. 第二個子節點就是這筆交易的產品. 這個文檔是做 fxOption
asset
'{http://www.fpml.org/FpML-5/confirmation}fxOption'
exercise=fp.xpath('//ns:fxOption/ns:americanExercise | //ns:fxOption/ns:europeanExercise',namespaces=ns)[0].tag
判斷這個外滙選擇權是美式還是歐式. 此產品是美式, 也就是在到期日之前都可以行權.
exercise
'{http://www.fpml.org/FpML-5/confirmation}americanExercise'
commencementDate=fp.xpath('//ns:americanExercise/ns:commencementDate//ns:unadjustedDate/text()',namespaces=ns)[0]
權利開始日期, 這個日期對估值不是很重要.
commencementDate
'2001-12-04'
expiryDate=fp.xpath('//ns:expiryDate/text()',namespaces=ns)[0]
其他要素一一提出
expiryDate
'2002-06-04'
businessCenter=fp.xpath('//ns:expiryTime/ns:businessCenter/text()',namespaces=ns)[0]
businessCenter
'USNY'
putCurrency=fp.xpath