306 lines
15 KiB
C#
306 lines
15 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using System.Diagnostics;
|
|
using System.Reflection;
|
|
|
|
namespace SpreaderFrontEnd {
|
|
// UI control that represents a spreader leg. This is hidden within the MainSpreaderForm
|
|
public partial class LegControl : UserControl {
|
|
private SpreaderLegContract _leg_contract = new SpreaderLegContract();
|
|
private IEnumerable<string> _securities = new List<string>();
|
|
public LegControl() {
|
|
InitializeComponent();
|
|
}
|
|
public void initialize(IEnumerable<string> securities, SpreaderLegContract? leg_contract) {
|
|
_securities = securities;
|
|
_leg_contract = leg_contract ?? new SpreaderLegContract();
|
|
_populate_ui();
|
|
}
|
|
|
|
#region Data -> UI
|
|
|
|
private void _populate_ui() {
|
|
_populate_basic();
|
|
_populate_quote();
|
|
_populate_prehedge();
|
|
_populate_posthedge();
|
|
_initialize_enablement();
|
|
_assign_events();
|
|
}
|
|
|
|
void _assign_events() {
|
|
foreach (System.Reflection.FieldInfo fi in GetType().GetFields(BindingFlags.NonPublic
|
|
| BindingFlags.Public
|
|
| BindingFlags.Instance)) {
|
|
if (fi.FieldType == typeof(TextBox)) {
|
|
object? val = fi.GetValue(this);
|
|
if (val != null) {
|
|
TextBox? textbox = val as TextBox;
|
|
if (textbox != null) {
|
|
textbox.TextChanged += Textbox_TextChanged;
|
|
textbox.EnabledChanged += Textbox_EnabledChanged;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Textbox_EnabledChanged(object? sender, EventArgs e) {
|
|
}
|
|
|
|
private void Textbox_TextChanged(object? sender, EventArgs e) {
|
|
if (sender != null && sender is TextBox) {
|
|
TextBox? tb = sender as TextBox;
|
|
if (tb == null)
|
|
return;
|
|
if ("Double".Equals(tb.Tag)) {
|
|
double res;
|
|
bool parsed = Double.TryParse(tb.Text, out res);
|
|
tb.ForeColor = parsed ? Color.Black : Color.Red;
|
|
} else {
|
|
UInt32 res;
|
|
bool parsed = UInt32.TryParse(tb.Text, out res);
|
|
tb.ForeColor = parsed ? Color.Black : Color.Red;
|
|
}
|
|
}
|
|
}
|
|
|
|
void _populate_basic() {
|
|
// security
|
|
_combobox_security.Items.Clear();
|
|
List<string> securities_plus_empty = new List<string>(_securities);
|
|
securities_plus_empty.Add(String.Empty);
|
|
_combobox_security.Items.AddRange(securities_plus_empty.ToArray());
|
|
_combobox_security.Text = _leg_contract.security;
|
|
// qty etc
|
|
_textbox_leg_qty.Text = _leg_contract.qty.ToString();
|
|
_textbox_leg_price_mult.Text = _leg_contract.price_mult.ToString();
|
|
_checkbox_leg_enablequoting.Checked = _leg_contract.enable_quoting;
|
|
_textbox_leg_leanqtyfactor.Text = _leg_contract.lean_qty_factor.ToString();
|
|
_combobox_mdtype.Text = _leg_contract.md_type.ToString();
|
|
}
|
|
|
|
void _populate_quote() {
|
|
Debug.Assert(_leg_contract.quote_params != null);
|
|
var qp = _leg_contract.quote_params;
|
|
_textbox_quote_qho_length.Text = qp.qho_length.ToString();
|
|
// show qty
|
|
_checkbox_quote_showqty.Checked = qp.show_qty != null;
|
|
if (qp.show_qty != null) {
|
|
_checkbox_quote_showqty.Checked = true;
|
|
_textbox_quote_showqty_qty.Text = qp.show_qty.show_qty.ToString();
|
|
}
|
|
// throttle
|
|
_checkbox_quote_throttle.Checked = qp.throttle != null;
|
|
if (qp.throttle != null) {
|
|
_textbox_quote_throttle_insideticks.Text = qp.throttle.inside_ticks.ToString();
|
|
_textbox_quote_throttle_outsideticks.Text = qp.throttle.outside_ticks.ToString();
|
|
}
|
|
// conditional
|
|
_checkbox_quote_conditional.Checked = qp.conditional != null;
|
|
if (qp.conditional != null) {
|
|
_textbox_quote_conditional_ticks.Text = qp.conditional.ticks.ToString();
|
|
}
|
|
// inside market dist.
|
|
_checkbox_quote_insidemarketdistance.Checked = qp.inside_market_distance != null;
|
|
if (qp.inside_market_distance != null) {
|
|
_textbox_quote_insidemarketdistance_towarddistance.Text = qp.inside_market_distance.ToString();
|
|
}
|
|
}
|
|
|
|
void _populate_prehedge() {
|
|
var ph = _leg_contract.pre_hedge_params;
|
|
// payless
|
|
_checkbox_prehedge_payless.Checked = ph.payless != null;
|
|
if (ph.payless != null) {
|
|
_textbox_prehedge_payless_threshold.Text = ph.payless.threshold.ToString();
|
|
_textbox_prehedge_payless_paylessticks.Text = ph.payless.payless_ticks.ToString();
|
|
}
|
|
// payup
|
|
_checkbox_prehedge_payup.Checked = ph.payup != null;
|
|
if (ph.payup != null) {
|
|
_textbox_prehedge_payup_threshold.Text = ph.payup.threshold.ToString();
|
|
_textbox_prehedge_payup_payupticks.Text = ph.payup.payup_ticks.ToString();
|
|
}
|
|
}
|
|
|
|
void _populate_posthedge() {
|
|
var ph = _leg_contract.post_hedge_params;
|
|
_checkbox_posthedge_ticksawaycross.Checked = ph.ticks_away_cross != null;
|
|
// ticks away cross
|
|
if (ph.ticks_away_cross != null) {
|
|
_textbox_posthedge_ticksawaycross_threshold.Text = ph.ticks_away_cross.threshold.ToString();
|
|
_textbox_posthedge_ticksawaycross_crossticks.Text = ph.ticks_away_cross.cross_ticks.ToString();
|
|
}
|
|
//time based cross
|
|
_checkbox_posthedge_timebasedcross.Checked = ph.time_based_cross != null;
|
|
if (ph.time_based_cross != null) {
|
|
_textbox_posthedge_timesbasedcross_timems.Text = ph.time_based_cross.time_ms.ToString();
|
|
_textbox_posthedge_timesbasedcross_crossticks.Text = ph.time_based_cross.cross_ticks.ToString();
|
|
}
|
|
// size based cross
|
|
_checkbox_posthedge_sizebasedcross.Checked = ph.size_based_cross != null;
|
|
if (ph.size_based_cross != null) {
|
|
_textbox_posthedge_sizebasedcross_threshold.Text = ph.size_based_cross.threshold.ToString();
|
|
_textbox_posthedge_sizebasedcross_crossticks.Text = ph.size_based_cross.ToString();
|
|
}
|
|
}
|
|
|
|
// forces checkbox events so that the text boxes connected to it are enabled/disabled
|
|
private void _initialize_enablement() {
|
|
_checkbox_quote_showqty_CheckedChanged(_checkbox_quote_showqty, new EventArgs());
|
|
_checkbox_quote_throttle_CheckedChanged(_checkbox_quote_showqty, new EventArgs());
|
|
_checkbox_quote_conditional_CheckedChanged(_checkbox_quote_showqty, new EventArgs());
|
|
_checkbox_quote_insidemarketdistance_CheckedChanged(_checkbox_quote_showqty, new EventArgs());
|
|
_checkbox_prehedge_payless_CheckedChanged(_checkbox_quote_showqty, new EventArgs());
|
|
_checkbox_prehedge_payup_CheckedChanged(_checkbox_quote_showqty, new EventArgs());
|
|
_checkbox_posthedge_ticksawaycross_CheckedChanged(_checkbox_quote_showqty, new EventArgs());
|
|
_checkbox_posthedge_timebasedcross_CheckedChanged(_checkbox_quote_showqty, new EventArgs());
|
|
_checkbox_posthedge_sizebasedcross_CheckedChanged(_checkbox_quote_showqty, new EventArgs());
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region UI -> Data
|
|
|
|
public ParsedUIData<SpreaderLegContract> get_data() {
|
|
SpreaderLegContract rv = new SpreaderLegContract();
|
|
try {
|
|
// top part
|
|
rv.security = _combobox_security.Text;
|
|
rv.qty = Int32.Parse(_textbox_leg_qty.Text);
|
|
rv.price_mult = Double.Parse(_textbox_leg_price_mult.Text);
|
|
rv.enable_quoting = _checkbox_leg_enablequoting.Checked;
|
|
rv.lean_qty_factor = Double.Parse(_textbox_leg_leanqtyfactor.Text);
|
|
rv.md_type = SpreaderLegContract.MDType.Merged;
|
|
// Quote
|
|
rv.quote_params = new SpreaderLegContract.QuoteParams();
|
|
rv.quote_params.qho_length = String.IsNullOrEmpty(_textbox_quote_qho_length.Text) ? 0 :
|
|
UInt32.Parse(_textbox_quote_qho_length.Text);
|
|
rv.quote_params.show_qty = null;
|
|
if (_checkbox_quote_showqty.Checked) {
|
|
rv.quote_params.show_qty = new SpreaderLegContract.QuoteParams.ShowQty();
|
|
rv.quote_params.show_qty.show_qty = UInt32.Parse(_textbox_quote_showqty_qty.Text);
|
|
}
|
|
rv.quote_params.throttle = null;
|
|
if (_checkbox_quote_throttle.Checked) {
|
|
rv.quote_params.throttle = new SpreaderLegContract.QuoteParams.Throttle();
|
|
rv.quote_params.throttle.inside_ticks = UInt32.Parse(_textbox_quote_throttle_insideticks.Text);
|
|
rv.quote_params.throttle.outside_ticks = UInt32.Parse(_textbox_quote_throttle_outsideticks.Text);
|
|
}
|
|
rv.quote_params.conditional = null;
|
|
if (_checkbox_quote_conditional.Checked) {
|
|
rv.quote_params.conditional = new SpreaderLegContract.QuoteParams.Conditional();
|
|
rv.quote_params.conditional.ticks = Int32.Parse(_textbox_quote_conditional_ticks.Text);
|
|
}
|
|
rv.quote_params.inside_market_distance = null;
|
|
if (_checkbox_quote_insidemarketdistance.Checked) {
|
|
rv.quote_params.inside_market_distance = new SpreaderLegContract.QuoteParams.InsideMarketDistance();
|
|
rv.quote_params.inside_market_distance.toward_distance =
|
|
UInt32.Parse(_textbox_quote_insidemarketdistance_towarddistance.Text);
|
|
}
|
|
//Pre-Hedge
|
|
rv.pre_hedge_params = new SpreaderLegContract.PreHedgeParams();
|
|
rv.pre_hedge_params.payless = null;
|
|
if (_checkbox_prehedge_payless.Checked) {
|
|
rv.pre_hedge_params.payless = new SpreaderLegContract.PreHedgeParams.PayLess();
|
|
rv.pre_hedge_params.payless.threshold = UInt32.Parse(_textbox_prehedge_payless_threshold.Text);
|
|
rv.pre_hedge_params.payless.payless_ticks =
|
|
UInt32.Parse(_textbox_prehedge_payless_paylessticks.Text);
|
|
}
|
|
rv.pre_hedge_params.payup = null;
|
|
if (_checkbox_prehedge_payup.Checked) {
|
|
rv.pre_hedge_params.payup = new SpreaderLegContract.PreHedgeParams.PayUp();
|
|
rv.pre_hedge_params.payup.threshold = UInt32.Parse(_textbox_prehedge_payup_threshold.Text);
|
|
rv.pre_hedge_params.payup.payup_ticks = UInt32.Parse(_textbox_prehedge_payup_payupticks.Text);
|
|
}
|
|
//Post-Hedge
|
|
rv.post_hedge_params = new SpreaderLegContract.PostHedgeParams();
|
|
rv.post_hedge_params.ticks_away_cross = null;
|
|
if (_checkbox_posthedge_ticksawaycross.Checked) {
|
|
rv.post_hedge_params.ticks_away_cross = new SpreaderLegContract.PostHedgeParams.TicksAwayCross();
|
|
rv.post_hedge_params.ticks_away_cross.threshold = UInt32.Parse(_textbox_posthedge_ticksawaycross_threshold.Text);
|
|
rv.post_hedge_params.ticks_away_cross.cross_ticks = UInt32.Parse(_textbox_posthedge_ticksawaycross_crossticks.Text);
|
|
}
|
|
rv.post_hedge_params.time_based_cross = null;
|
|
if (_checkbox_posthedge_timebasedcross.Checked) {
|
|
rv.post_hedge_params.time_based_cross = new SpreaderLegContract.PostHedgeParams.TimeBasedCross();
|
|
rv.post_hedge_params.time_based_cross.time_ms =
|
|
UInt32.Parse(_textbox_posthedge_timesbasedcross_timems.Text);
|
|
rv.post_hedge_params.time_based_cross.cross_ticks =
|
|
UInt32.Parse(_textbox_posthedge_timesbasedcross_crossticks.Text);
|
|
}
|
|
rv.post_hedge_params.size_based_cross = null;
|
|
if (_checkbox_posthedge_sizebasedcross.Checked) {
|
|
rv.post_hedge_params.size_based_cross = new SpreaderLegContract.PostHedgeParams.SizedBasedCross();
|
|
rv.post_hedge_params.size_based_cross.threshold = UInt32.Parse(_textbox_posthedge_sizebasedcross_threshold.Text);
|
|
rv.post_hedge_params.size_based_cross.cross_ticks = UInt32.Parse(_textbox_posthedge_sizebasedcross_crossticks.Text);
|
|
}
|
|
}
|
|
catch(Exception ex) {
|
|
return new ParsedUIData<SpreaderLegContract>(null, true, ex.Message);
|
|
}
|
|
return new ParsedUIData<SpreaderLegContract>(rv, false, String.Empty);
|
|
}
|
|
|
|
#endregion
|
|
|
|
public SpreaderLegContract get_contract() {
|
|
return null;
|
|
}
|
|
|
|
#region Checkbox Check Change Events
|
|
|
|
private void _checkbox_quote_showqty_CheckedChanged(object sender, EventArgs e) {
|
|
_textbox_quote_showqty_qty.Enabled = _checkbox_quote_showqty.Checked;
|
|
}
|
|
|
|
private void _checkbox_quote_throttle_CheckedChanged(object sender, EventArgs e) {
|
|
_textbox_quote_throttle_insideticks.Enabled = _checkbox_quote_throttle.Checked;
|
|
_textbox_quote_throttle_outsideticks.Enabled = _checkbox_quote_throttle.Checked;
|
|
}
|
|
|
|
private void _checkbox_quote_conditional_CheckedChanged(object sender, EventArgs e) {
|
|
_textbox_quote_conditional_ticks.Enabled = _checkbox_quote_conditional.Checked;
|
|
}
|
|
|
|
private void _checkbox_quote_insidemarketdistance_CheckedChanged(object sender, EventArgs e) {
|
|
_textbox_quote_insidemarketdistance_towarddistance.Enabled = _checkbox_quote_insidemarketdistance.Checked;
|
|
}
|
|
|
|
private void _checkbox_prehedge_payless_CheckedChanged(object sender, EventArgs e) {
|
|
_textbox_prehedge_payless_threshold.Enabled = _checkbox_prehedge_payless.Checked;
|
|
_textbox_prehedge_payless_paylessticks.Enabled = _checkbox_prehedge_payless.Checked;
|
|
}
|
|
|
|
private void _checkbox_prehedge_payup_CheckedChanged(object sender, EventArgs e) {
|
|
_textbox_prehedge_payup_threshold.Enabled = _checkbox_prehedge_payup.Checked;
|
|
_textbox_prehedge_payup_payupticks.Enabled = _checkbox_prehedge_payup.Checked;
|
|
}
|
|
|
|
private void _checkbox_posthedge_ticksawaycross_CheckedChanged(object sender, EventArgs e) {
|
|
_textbox_posthedge_ticksawaycross_threshold.Enabled = _checkbox_posthedge_ticksawaycross.Checked;
|
|
_textbox_posthedge_ticksawaycross_crossticks.Enabled = _checkbox_posthedge_ticksawaycross.Checked;
|
|
}
|
|
|
|
private void _checkbox_posthedge_timebasedcross_CheckedChanged(object sender, EventArgs e) {
|
|
_textbox_posthedge_timesbasedcross_timems.Enabled = _checkbox_posthedge_timebasedcross.Checked;
|
|
_textbox_posthedge_timesbasedcross_crossticks.Enabled = _checkbox_posthedge_timebasedcross.Checked;
|
|
}
|
|
|
|
private void _checkbox_posthedge_sizebasedcross_CheckedChanged(object sender, EventArgs e) {
|
|
_textbox_posthedge_sizebasedcross_threshold.Enabled = _checkbox_posthedge_sizebasedcross.Checked;
|
|
_textbox_posthedge_sizebasedcross_crossticks.Enabled = _checkbox_posthedge_sizebasedcross.Checked;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|