Exemple #1
0
 private void timerTicked(object sender, EventArgs e)
 {
     if (!(strike_valid && maturity_valid && rate_valid && volatility_valid && underlying_valid && yield_valid && buysell_valid && callput_valid && quantity_valid))
     {
         timer_quote.Stop();
         textBoxPrice.Text = "";
         textBoxDelta.Text = "";
         return;
     }
     if (double.TryParse(redis_conn.HashGet(underlying_code, "LastPrice"), out underlying_price))
     {
         double spread_value = checkBoxSpreadQuote.Checked?((call_put == 'c' ? 1 : -1) * (buy_sell == 'b' ? 1 : -1) * (double)numericUpDownSpreadValue.Value):0;
         textBoxPrice.Text         = OptionsCalculator.GetBlsPrice(underlying_price + spread_value, strike, T, volatility, r, call_put).ToString("N2");
         textBoxDelta.Text         = OptionsCalculator.GetBlsDelta(underlying_price, strike, T, volatility, r, call_put).ToString("N2");
         textBoxValue.Text         = (decimal.Parse(textBoxPrice.Text) * decimal.Parse(textBoxQuantity.Text)).ToString();
         textBoxTotalDelta.Text    = (decimal.Parse(textBoxDelta.Text) * decimal.Parse(textBoxQuantity.Text)).ToString();
         textBoxRemainBalance.Text = (decimal.Parse(textBoxBalance.Text) - decimal.Parse(textBoxValue.Text)).ToString();
     }
     else
     {
         textBoxPrice.Text = "";
         textBoxDelta.Text = "";
     }
 }
        private void UpdateGreeks()
        {
            var       db    = this.redis_connection.GetDatabase();
            DataTable table = Tables["risk_info"];

            foreach (var row in Tables["options_positions_summary"].AsEnumerable().ToArray())
            {
                if (row.RowState != DataRowState.Deleted)
                {
                    String   contract_code      = row["合约代码"].ToString();
                    int      client_id          = int.Parse(row["客户编号"].ToString());
                    string   position_direction = row["买卖方向"].ToString();
                    double   quantity           = double.Parse(row["数量"].ToString());
                    DataRow  contract_row       = Tables["options_contracts"].Rows.Find(contract_code);
                    String   underlying         = contract_row["标的代码"].ToString();
                    double   S0    = double.Parse(db.HashGet(underlying, "LastPrice"));
                    double   K     = double.Parse(contract_row["执行价"].ToString());
                    double   sigma = double.Parse(contract_row["波动率"].ToString());
                    DateTime date  = DateTime.Parse(contract_row["到期日"].ToString());
                    double   dtm   = GetDTM(date);
                    char     type  = char.Parse(contract_row["认购认沽"].ToString());
                    double   rate  = 0.015;
                    int      direction_multiplier = position_direction == "买入" ? -1 : 1;
                    double   delta = OptionsCalculator.GetBlsDelta(S0, K, dtm / 256d, sigma, rate, type) * quantity * direction_multiplier;
                    double   gamma = OptionsCalculator.GetBlsGamma(S0, K, dtm / 256d, sigma, rate) * quantity * direction_multiplier;
                    double   theta = OptionsCalculator.GetBlsTheta(S0, K, dtm / 256d, sigma, rate, type) * quantity / 256 * direction_multiplier * 10;
                    double   vega  = OptionsCalculator.GetBlsVega(S0, K, dtm / 256d, sigma, rate) * quantity / 100 * direction_multiplier * 10;
                    double   rho   = OptionsCalculator.GetBlsRho(S0, K, dtm / 256d, sigma, rate, type) * quantity / 100 * direction_multiplier * 10;
                    if (table.Rows.Contains(new object[] { client_id, contract_code, position_direction }))
                    {
                        DataRow row_risk = table.Rows.Find(new object[] { client_id, contract_code, position_direction });
                        row_risk["数量"]    = quantity;
                        row_risk["波动率"]   = sigma;
                        row_risk["标的现价"]  = S0;
                        row_risk["到期天数"]  = dtm;
                        row_risk["Delta"] = delta;
                        row_risk["Gamma"] = gamma;
                        row_risk["Theta"] = theta;
                        row_risk["Vega"]  = vega;
                        row_risk["Rho"]   = rho;
                    }
                    else
                    {
                        table.Rows.Add(client_id, contract_code, underlying, position_direction, S0, quantity, dtm, sigma, delta, gamma, theta, vega, rho);
                    }
                }
            }

            var position_keys = new List <object[]>();

            foreach (var row in table.AsEnumerable())
            {
                position_keys.Add(new object[] { row["客户编号"], row["合约代码"], row["买卖方向"] });
            }
            foreach (var key in position_keys)
            {
                if (!Tables["options_positions_summary"].Rows.Contains(key))
                {
                    table.Rows.Find(key).Delete();
                }
            }
            var risk_info_gross = from row in table.AsEnumerable()
                                  group row by row.Field <String>("标的代码")
                                  into grp
                                  select
                                  new
            {
                code  = grp.Key,
                price = grp.First().Field <decimal>("标的现价"),
                Delta = grp.Sum(r => r.Field <double>("Delta")),
                Gamma = grp.Sum(r => r.Field <double>("Gamma")),
                Theta = grp.Sum(r => r.Field <double>("Theta")),
                Vega  = grp.Sum(r => r.Field <double>("Vega")),
                Rho   = grp.Sum(r => r.Field <double>("Rho")),
            };
            List <String> contracts = new List <string>();

            foreach (var a in risk_info_gross)
            {
                double long_underlying_position  = 0;
                double short_underlying_position = 0;
                double.TryParse(this.Tables["futures_positions_summary"].Compute("sum(数量)", string.Format("合约代码='{0}' AND 买卖方向='{1}'", a.code, "买入")).ToString(), out long_underlying_position);
                double.TryParse(this.Tables["futures_positions_summary"].Compute("sum(数量)", string.Format("合约代码='{0}' AND 买卖方向='{1}'", a.code, "卖出")).ToString(), out short_underlying_position);

                contracts.Add(a.code);
                if (!Tables["risk_info_gross"].Rows.Contains(a.code))
                {
                    Tables["risk_info_gross"].Rows.Add(a.code, a.price, a.Delta + long_underlying_position - short_underlying_position, a.Gamma, a.Theta, a.Vega, a.Rho);
                }
                else
                {
                    var row = Tables["risk_info_gross"].Rows.Find(a.code);
                    row["标的现价"]  = a.price;
                    row["Delta"] = a.Delta + long_underlying_position - short_underlying_position;
                    row["Gamma"] = a.Gamma;
                    row["Theta"] = a.Theta;
                    row["Vega"]  = a.Vega;
                    row["Rho"]   = a.Rho;
                }
            }
            List <string> position_codes = new List <string>();

            foreach (var row in Tables["risk_info_gross"].AsEnumerable())
            {
                position_codes.Add(row.Field <string>("标的代码"));
            }
            foreach (var code in position_codes)
            {
                if (!contracts.Contains(code))
                {
                    Tables["risk_info_gross"].Rows.Find(code).Delete();
                }
            }
        }