private void updateContractInfo(object sender, EventArgs e) { if (this.comboBoxOrderType.Text == "期权") { var cur_contract = this.dataset.Tables["options_contracts"].Rows.Find(this.comboBoxContractCode.Text); underlying_code = cur_contract.Field <string>("标的代码"); strike = cur_contract.Field <decimal>("执行价"); var exp_date = cur_contract.Field <DateTime>("到期日"); var dtm = this.dataset.GetDTM(exp_date); T = dtm / 256d; vol = cur_contract.Field <double>("波动率"); option_type = cur_contract.Field <string>("认购认沽")[0]; var spot_price = double.Parse(this.redis_db.HashGet(underlying_code, "LastPrice").ToString()); this.textBoxUnderlyingPrice.Text = spot_price.ToString("N2"); var option_price = OptionsCalculator.GetBlsPrice(spot_price, (double)strike, T, vol, this.r, option_type); this.numericUpDownPrice.Value = Math.Max(this.comboBoxLongShort.Text == "买入" ? decimal.Ceiling(new decimal(option_price) * 100m) / 100m : decimal.Floor(new decimal(option_price) * 100m) / 100m, 0.0001m); } else if (this.comboBoxOrderType.Text == "期货") { var spot_price = double.Parse(this.redis_db.HashGet(this.comboBoxContractCode.Text, "LastPrice").ToString()); this.numericUpDownPrice.Value = new decimal(spot_price); } }
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 updatePrice() { if (this.comboBoxOrderType.Text == "期权" && this.underlying_code != null && this.underlying_code != "") { var spot_price = double.Parse(this.redis_db.HashGet(underlying_code, "LastPrice").ToString()); if (this.checkBoxMarketPriceFuture.Checked) { this.textBoxUnderlyingPrice.Text = spot_price.ToString("N2"); } var option_price = OptionsCalculator.GetBlsPrice(spot_price, (double)strike, T, vol, this.r, option_type); if (this.checkBoxMarketPriceOption.Checked) { this.numericUpDownPrice.Value = this.comboBoxLongShort.Text == "买入" ? decimal.Ceiling(new decimal(option_price) * 100m) / 100m : decimal.Floor(new decimal(option_price) * 100m) / 100m; } } else if (this.comboBoxOrderType.Text == "期货" && this.underlying_code != null && this.underlying_code != "") { var spot_price = double.Parse(this.redis_db.HashGet(underlying_code, "LastPrice").ToString()); if (this.checkBoxMarketPriceOption.Checked) { this.numericUpDownPrice.Value = new decimal(spot_price); } } }
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(); } } }