使用LINQ选择选项链的子集

时间:2015-07-26 19:04:54

标签: c# linq

我有一个选项链,我想基于底层价格进行分组。更确切地说,我想得到看跌期权和放大器。通过执行价格调整价格刚刚低于,接近和略高于标的价格,并且每个看跌期权和看涨期权相同。

这里是json中的裸选项数据:

{
"underlying_price": 130.96001,
"puts": [
    {
        "cid": "901428591898527",
        "strike": "125.00",
        "expiry": "Sep 18, 2015"
    },
    {
        "cid": "990945460064288",
        "strike": "130.00",
        "expiry": "Sep 18, 2015"
    },
    {
        "cid": "119741665156957",
        "strike": "135.00",
        "expiry": "Sep 18, 2015"
    },
    {
        "cid": "712197852541730",
        "strike": "140.00",
        "expiry": "Sep 18, 2015"
    },
    {
        "cid": "193052493845513",
        "strike": "145.00",
        "expiry": "Sep 18, 2015"
    },
    {
        "cid": "30896117048646",
        "strike": "150.00",
        "expiry": "Sep 18, 2015"
    }
],
"calls": [
    {
        "cid": "1103304608596226",
        "strike": "125.00",
        "expiry": "Sep 18, 2015"
    },
    {
        "cid": "28683704337826",
        "strike": "130.00",
        "expiry": "Sep 18, 2015"
    },
    {
        "cid": "293690467884457",
        "strike": "135.00",
        "expiry": "Sep 18, 2015"
    },
    {
        "cid": "791049407206135",
        "strike": "140.00",
        "expiry": "Sep 18, 2015"
    },
    {
        "cid": "932015841628223",
        "strike": "145.00",
        "expiry": "Sep 18, 2015"
    },
    {
        "cid": "501320075715761",
        "strike": "150.00",
        "expiry": "Sep 18, 2015"
    }
]

}

这已转换为OptionData类的对象:

public class OptionData 
{
        public Option[] puts { get; set; }
        public Option[] calls { get; set; }
        public string underlying_price { get; set; }
 }

 public class Option
 {
         public string cid { get; set; }
         public string strike { get; set; }
         public string expiry { get; set; }
 }

 public class OptionPair
 {
         public decimal Strike { get; set; }
         public DateTime Expiry { get; set; }
         public Option Call { get; set; }
         public Option Put { get; set; }
}

如本示例所示,基础价格约为130美元,因此我想提取看跌期权和收益率。在不知道增量的情况下以编程方式调用罢工(125美元,130美元和135美元)(在这种情况下为5美元,在其他选项链中可能会有所不同)。

所以我有两个问题:

  1. 如何使用LINQ或Select扩展程序执行上述操作? (可考虑使用Last,First,Skip,Take,Join等)

  2. 我如何配对每个put& amp;调用(相同的警示)作为OptionPair的对象(更喜欢使用LINQ语法)?

  3. 有什么想法吗?感谢。

    修改

    这里的代码对我有用。对于增量,我计算了两次打击的差异,并将0.99加在一起作为截止值:

    Single margin = Convert.ToSingle(puts[1].strike) - Convert.ToSingle(puts[0].strike);
            var pairs = calls.Join(puts, call => call.strike,  put => put.strike, (call, put) =>
                        new OptionPair
                        {
                            Call = call,
                            Put = put,
                            Expiry = DateTime.Parse(call.expiry),
                            Strike = Convert.ToSingle(call.strike), 
                        }).Where(pair => Math.Abs(pair.Strike - price) <= margin + 0.99);
    

1 个答案:

答案 0 :(得分:0)

这就是你需要的吗?

decimal margin = 5.99m; // set it programatically, I made it 5.99 because of the underlying_price value in your JSON
decimal underlying_price = Decimal.Parse(data.underlying_price);
var pairs = data.puts.Join(data.calls, put => put.strike, call => call.strike, (put, call) =>
    new OptionPair
    {
        Call = call,
        Put = put,
        Expiry = DateTime.Parse(call.expiry), // or from put, this is kind of ugly - the dates theoretically can be different
        Strike = Decimal.Parse(call.strike), // or rom call
    }).Where(pair => Math.Abs(pair.Strike - underlying_price) < margin);