private void FireExecuteButton(object sender, EventArgs args) { int signal_qty = 0; double limit_prc = 0.0, stop_prc = 0.0; string side_code, order_type; DraftOrderData.Types.SideCode side_code_type; DraftOrderData.Types.OrderType draft_order_type; if (sender == buyButton) { side_code = "BUY"; side_code_type = DraftOrderData.Types.SideCode.BUY; } else { side_code = "SELL"; side_code_type = DraftOrderData.Types.SideCode.SELL; } if (orderTypeComboBox.Text == "LIMIT") { order_type = "LIMIT"; draft_order_type = DraftOrderData.Types.OrderType.LIMIT; if ("".Equals(limitPriceTextBox.Text)) { MessageBox.Show("LIMIT order type requires limit price."); return; } else { try { limit_prc = Convert.ToDouble(limitPriceTextBox.Text); if (limit_prc <= 0.0) { MessageBox.Show("Invalid limit price: " + limitPriceTextBox.Text); return; } } catch (Exception limitError) { log.Error("Signal qty error.", limitError); MessageBox.Show("Invalid limit price: " + limitPriceTextBox.Text); return; } } } else if (orderTypeComboBox.Text == "STOP") { order_type = "STOP"; draft_order_type = DraftOrderData.Types.OrderType.STOP; if ("".Equals(stopPriceTextBox.Text)) { MessageBox.Show("STOP order type requires stop price."); return; } else { try { stop_prc = Convert.ToDouble(stopPriceTextBox.Text); if (stop_prc <= 0.0) { MessageBox.Show("Invalid stop price: " + stopPriceTextBox.Text); return; } } catch (Exception stopError) { log.Error("Signal qty error.", stopError); MessageBox.Show("Invalid stop price: '" + stopPriceTextBox.Text + "'"); return; } } } else if (orderTypeComboBox.Text == "STOP LIMIT") { order_type = "STOP_LIMIT"; draft_order_type = DraftOrderData.Types.OrderType.STOP_LIMIT; if ("".Equals(limitPriceTextBox.Text)) { MessageBox.Show("STOP LIMIT order type requires limit price."); return; } else { try { limit_prc = Convert.ToDouble(limitPriceTextBox.Text); if (limit_prc <= 0.0) { MessageBox.Show("Invalid limit price: " + limitPriceTextBox.Text); return; } } catch (Exception limitError) { log.Error("Signal qty error.", limitError); MessageBox.Show("Invalid limit price: " + limitPriceTextBox.Text); return; } } if ("".Equals(stopPriceTextBox.Text)) { MessageBox.Show("STOP LIMIT order type requires stop price."); return; } else { try { stop_prc = Convert.ToDouble(stopPriceTextBox.Text); if (stop_prc <= 0.0) { MessageBox.Show("Invalid stop price: " + stopPriceTextBox.Text); return; } } catch (Exception stopError) { log.Error("Signal qty error.", stopError); MessageBox.Show("Invalid stop price: '" + stopPriceTextBox.Text + "'"); return; } } } else if (orderTypeComboBox.Text == "MKT ON CLOSE") { order_type = "MARKET_ON_CLOSE"; draft_order_type = DraftOrderData.Types.OrderType.MARKET_ON_CLOSE; } else { order_type = "MARKET"; draft_order_type = DraftOrderData.Types.OrderType.MARKET; } if (tabControl.SelectedIndex == 0) { // // Validate all strategy quantities // foreach (GTLTreeNode strategy in strategies.Nodes) { try { signal_qty = Convert.ToInt32(strategy.SubItems[1].Text); } catch (Exception qtyError) { log.Error("Signal qty error.", qtyError); MessageBox.Show("Invalid signal size: " + strategy.SubItems[1].Text); return; } if (signal_qty < 0) { MessageBox.Show(strategy.SubItems[0].Text + " has invalid signal size: " + strategy.SubItems[1].Text); return; } } // // All imputs are ok, create the signals // foreach (GTLTreeNode strategy in strategies.Nodes) { signal_qty = Convert.ToInt32(strategy.SubItems[1].Text); if (signal_qty > 0) { SignalData signalData = SignalData.CreateBuilder() .SetInvestmentSystemId(selectedInvestmentSystem.Id) .SetSignalId((string)strategy.Tag) .SetInstrumentId(selectedInstrument.Id) .SetSideCode(side_code) .SetSignalQty(strategy.SubItems[1].Text) .SetOrderType(order_type) .SetLimitPrc(Convert.ToString(limit_prc)) .SetStopPrc(Convert.ToString(stop_prc)) .Build(); if (releaseToFixCheckBox.Checked) { ClientAdapterResponse response = controller.SendRpc( new com.quantmodel.common.network.message.ExecuteSignal(signalData)); } else { ClientAdapterResponse response = controller.SendRpc( new com.quantmodel.common.network.message.CreateDraftOrder(signalData)); if (response.Type == ClientAdapterResponse.Types.ResponseType.ACKNOWLEDGEMENT) { DealingEngineResponse dealing_response = response.DealingResponse; if (dealing_response.Type == DealingEngineResponse.Types.ResponseType.ACKNOWLEDGEMENT) { foreach (DraftOrderMessage draft_order_msg in dealing_response.DraftOrderList) { ClientAdapterResponse create_order_response = controller.SendRpc( new com.quantmodel.common.network.message.CreateOrder(draft_order_msg)); } } } } } } } else { // // Validate all account quantities // foreach (GTLTreeNode account in accounts.Nodes) { try { signal_qty = Convert.ToInt32(account.SubItems[1].Text); } catch (Exception qtyError) { log.Error("Allocation qty error.", qtyError); MessageBox.Show("Invalid allocation quantity: " + account.SubItems[1].Text); return; } if (signal_qty < 0) { MessageBox.Show(account.SubItems[0].Text + " has invalid allocation quantity: " + account.SubItems[1].Text); return; } } // // All imputs are ok, create the draft orders // foreach (GTLTreeNode account in accounts.Nodes) { signal_qty = Convert.ToInt32(account.SubItems[1].Text); if (signal_qty > 0) { DraftOrderData draftOrder = DraftOrderData.CreateBuilder() .SetInvestmentSystemId("CLIENT") .SetInstrumentId(selectedInstrument.Id) .SetSideCode(side_code_type) .SetTif(DraftOrderData.Types.TimeInForce.DAY) .SetOrderType(draft_order_type) .SetOrderQty(Convert.ToString(signal_qty)) .SetLimitPrc(Convert.ToString(limit_prc)) .SetStopPrc(Convert.ToString(stop_prc)) .Build(); DraftAllocationData draftAllocation = DraftAllocationData.CreateBuilder() .SetAccountId((string)account.Tag) .SetAllocationQty(Convert.ToString(signal_qty)) .Build(); ClientAdapterResponse create_order_response = controller.SendRpc( new com.quantmodel.common.network.message.CreateOrder(draftOrder, draftAllocation)); if (create_order_response.Type == ClientAdapterResponse.Types.ResponseType.ACKNOWLEDGEMENT) { DealingEngineResponse dealing_order_response = create_order_response.DealingResponse; if (dealing_order_response.Type == DealingEngineResponse.Types.ResponseType.ACKNOWLEDGEMENT && releaseToFixCheckBox.Checked) { // // Release the order // foreach (OrderMessage order_msg in dealing_order_response.OrderList) { ClientAdapterResponse release_order_response = controller.SendRpc( new com.quantmodel.common.network.message.ReleaseOrder(order_msg.OrderData.OrderId)); if (release_order_response.Type == ClientAdapterResponse.Types.ResponseType.ACKNOWLEDGEMENT) { DealingEngineResponse dealing_release_response = release_order_response.DealingResponse; if (dealing_release_response.Type != DealingEngineResponse.Types.ResponseType.ACKNOWLEDGEMENT) { } } } } } } } } }
private void CloseAccountPosition(AccountPosition account_position) { foreach (InstrumentPosition instrument_position in account_position.InstrumentPositions) { DraftOrderData.Types.SideCode sideCode; if (instrument_position.Exposure == InstrumentPosition.PositionExposure.LONG) { sideCode = DraftOrderData.Types.SideCode.SELL; } else if (instrument_position.Exposure == InstrumentPosition.PositionExposure.SHORT) { sideCode = DraftOrderData.Types.SideCode.BUY; } else { // // No need to close a flat position. // continue; } DraftOrderData draftOrder = DraftOrderData.CreateBuilder() .SetInvestmentSystemId(instrument_position.InvestmentSystemId) .SetInstrumentId(instrument_position.InstrumentId) .SetSideCode(sideCode) .SetTif(DraftOrderData.Types.TimeInForce.DAY) .SetOrderType(DraftOrderData.Types.OrderType.MARKET) .SetOrderQty(Convert.ToString(instrument_position.OpenPosition)) .SetLimitPrc("0") .SetStopPrc("0") .Build(); DraftAllocationData draftAllocation = DraftAllocationData.CreateBuilder() .SetAccountId(account_position.AccountId) .SetAllocationQty(Convert.ToString(instrument_position.OpenPosition)) .Build(); ClientAdapterResponse create_order_response = controller.SendRpc(new CreateOrder(draftOrder, draftAllocation)); if (create_order_response.Type == ClientAdapterResponse.Types.ResponseType.ACKNOWLEDGEMENT) { DealingEngineResponse dealing_order_response = create_order_response.DealingResponse; if (dealing_order_response.Type == DealingEngineResponse.Types.ResponseType.ACKNOWLEDGEMENT) { // // Release the market order // foreach (OrderMessage order_msg in dealing_order_response.OrderList) { ClientAdapterResponse release_order_response = controller.SendRpc(new ReleaseOrder(order_msg.OrderData.OrderId)); if (release_order_response.Type == ClientAdapterResponse.Types.ResponseType.ACKNOWLEDGEMENT) { DealingEngineResponse dealing_release_response = release_order_response.DealingResponse; if (dealing_release_response.Type != DealingEngineResponse.Types.ResponseType.ACKNOWLEDGEMENT) { } } } } } } }