Beispiel #1
0
        public bool ExecuteOrder(IAccountHandlerInterface AccountHandler, ITradeHandlerInterface TradeHandler, out IOrder StopLossOrder)
        {
            StopLossOrder = this;
            if (EvaluateTriggerOnOrder())
            {
                try
                {
                    IAccountInterface oA = AccountHandler.GetAccount(m_oAccountIdentifier);
                    switch (m_oOrderType)
                    {
                    case OrderType.Buy:
                        oA.AddHoldingToAccount(m_oQuantity, m_oSymbol, TradeHandler);
                        StopLossOrder = this;
                        return(true);

                    case OrderType.Sell:
                        oA.SellHoldingFromAccount(m_oQuantity, m_oSymbol, TradeHandler);
                        return(true);

                    case OrderType.StopLoss:
                        oA.AddHoldingToAccount(m_oQuantity, m_oSymbol, TradeHandler);
                        StopLossOrder = CreateStopLossOrder(ImperaturGlobal.Quotes.Where(q => q.Symbol.Equals(m_oSymbol)).First().LastTradePrice.Amount);
                        return(true);

                    default:
                        break;
                    }
                }
                catch (Exception ex)
                {
                    m_oLastErrorMessage = "OrderExecute error: " + ex.Message;
                    return(false);
                }
            }
            return(false);
        }
        private void button_BuySecurity_Click(object sender, EventArgs e)
        {
            int  QuantityToBuy;
            bool isNumeric = int.TryParse(textBox_Quantity.Text.Trim(), out QuantityToBuy);

            if (m_oAccountData != null && isNumeric && comboBox_Symbols.SelectedItem != null && comboBox_Symbols.SelectedItem.ToString().Length > 0)
            {
                if (ImperaturGlobal.ExchangeStatus != ExchangeStatus.Open)
                {
                    DialogResult oDr = MessageBox.Show("Exchange is closed, would you like to create an order for the next businessday?", "Exchange closed", MessageBoxButtons.YesNo);
                    if (oDr.Equals(DialogResult.No))
                    {
                        return;
                    }
                    else
                    {
                        Instrument oI         = ImperaturGlobal.Instruments.Where(i => i.Symbol.Equals(comboBox_Symbols.SelectedItem.ToString())).First();
                        IMoney     TradePrice = ImperaturGlobal.Quotes.Where(q => q.Symbol.Equals(comboBox_Symbols.SelectedItem.ToString())).First().LastTradePrice;
                        m_oOrderQueueHandler.AddOrder(
                            ImperaturGlobal.Kernel.Get <IOrder>(
                                new Ninject.Parameters.ConstructorArgument("Symbol", oI.Symbol),
                                new Ninject.Parameters.ConstructorArgument("Trigger", new List <ITrigger> {
                            ImperaturGlobal.Kernel.Get <ITrigger>(
                                new Ninject.Parameters.ConstructorArgument("m_oOperator", TriggerOperator.EqualOrless),
                                new Ninject.Parameters.ConstructorArgument("m_oValueType", TriggerValueType.TradePrice),
                                new Ninject.Parameters.ConstructorArgument("m_oTradePriceValue", TradePrice.Amount),
                                new Ninject.Parameters.ConstructorArgument("m_oPercentageValue", 0m)
                                )
                        }),
                                new Ninject.Parameters.ConstructorArgument("AccountIdentifier", m_oAccountData.Identifier),
                                new Ninject.Parameters.ConstructorArgument("Quantity", QuantityToBuy),
                                new Ninject.Parameters.ConstructorArgument("OrderType", OrderType.StopLoss),
                                new Ninject.Parameters.ConstructorArgument("ValidToDate", DateTime.Now.AddDays(3)),
                                new Ninject.Parameters.ConstructorArgument("StopLossValidDays", 30),
                                new Ninject.Parameters.ConstructorArgument("StopLossAmount", 0m),
                                new Ninject.Parameters.ConstructorArgument("StopLossPercentage", 0.8m)
                                ));
                        return;
                    }
                }

                string sMessage = string.Format("Are you sure you want to buy {0} of {1} for {2}?",
                                                QuantityToBuy,
                                                comboBox_Symbols.SelectedItem.ToString(),
                                                ImperaturGlobal.Quotes.Where(q => q.Symbol.Equals(comboBox_Symbols.SelectedItem.ToString())).First().LastTradePrice.Multiply(Convert.ToDecimal(QuantityToBuy)).ToString());
                DialogResult dialogResult = MessageBox.Show(sMessage, "Buy stock?", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    if (!m_oAccountData.AddHoldingToAccount(
                            QuantityToBuy,
                            comboBox_Symbols.SelectedItem.ToString(),
                            m_oTradeHandler
                            ))
                    {
                        MessageBox.Show(string.Format("Couldn't complete transaction! {0}", m_oAccountData.GetLastErrorMessage));
                    }
                    else
                    {
                        //refresh current account and list!
                        RefreshData();
                    }
                }
                else if (dialogResult == DialogResult.No)
                {
                    //do something else
                }
            }
        }