private void buttonDeleteBook_Click(object sender, EventArgs e)
        {
            if (buttonDeleteBook.Text == ButtonDeleteBook_Normal)
            {   // We are in the normal state.
                buttonDeleteBook.Text = ButtonDeleteBook_Confirm;
            }
            else if (buttonDeleteBook.Text == ButtonDeleteBook_Confirm)
            {   // User has just confirmed the delete request.
                // Submit the delete request

                if (Log != null)
                {
                    if (m_FillHub == null)
                    {
                        Log.NewEntry(Misty.Lib.Hubs.LogLevel.Major, "FormAddFill.buttonDeleteBook_Click: FillHub is null!");
                        buttonDeleteBook.Text = ButtonDeleteBook_Normal;
                        return;
                    }
                    else if (m_CurrentInstrument == null || m_CurrentInstrument == m_EmptyInstrument)
                    {
                        Log.NewEntry(Misty.Lib.Hubs.LogLevel.Major, "FormAddFill.buttonDeleteBook_Click: CurrentInsrument is null!");
                        buttonDeleteBook.Text = ButtonDeleteBook_Normal;
                        return;
                    }
                    //else if (m_CurrentInstrument.ForeignKey == null)
                    //{
                    //    Log.NewEntry(Misty.Lib.Hubs.LogLevel.Major,"FormAddFill.buttonDeleteBook_Click: CurrentInsrument.ForeignKey is null!");
                    //    buttonDeleteBook.Text = ButtonDeleteBook_Normal;
                    //    return;
                    //}
                    Log.NewEntry(Misty.Lib.Hubs.LogLevel.Major, "FormAddFill.buttonDeleteBook_Click: Request delete for {0} in FillHub {1}.", m_CurrentInstrument, m_FillHub.Name);
                }
                Misty.Lib.OrderHubs.OrderHubRequest request = new Misty.Lib.OrderHubs.OrderHubRequest(Misty.Lib.OrderHubs.OrderHubRequest.RequestType.RequestDeleteBook);
                //TradingTechnologies.TTAPI.InstrumentKey instrKey = (TradingTechnologies.TTAPI.InstrumentKey)m_CurrentInstrument.ForeignKey;
                //request.Data = new object[1] { instrKey };
                request.Data = new object[1] {
                    m_CurrentInstrument
                };
                m_FillHub.Request(request);
                buttonDeleteBook.Text = ButtonDeleteBook_Normal;
            }
        }//
        }//

        //
        //
        //
        //
        #endregion//Constructors


        #region Private Methods
        // *****************************************************************
        // ****                     Private Methods                     ****
        // *****************************************************************
        //
        //
        //
        //
        public void ControlPanel_ButtonClicked(object sender, EventArgs eventArgs)
        {
            if (!(sender is Button))
            {
                return;
            }
            Button button = (Button)sender;

            try
            {
                if (button.Parent != m_LeftControl)
                {
                    return;                                 // at present, only buttons active on left control
                }
                //
                // Collect the state of the LEFT control
                //
                FillHub        leftFillHub;
                InstrumentName leftInstrumentName;
                List <double>  leftDoubles;
                CollectStateInfo(m_LeftControl, out leftFillHub, out leftInstrumentName, out leftDoubles);

                //
                // Collect the state of the RIGHT control
                //
                FillHub        rightFillHub        = null;
                InstrumentName rightInstrumentName = new InstrumentName();
                List <double>  rightDoubles        = new List <double>();
                if (m_IsRightControlVisible)
                {
                    CollectStateInfo(m_RightControl, out rightFillHub, out rightInstrumentName, out rightDoubles);
                }

                //
                // Process and submit request
                //
                string buttonName = button.Name;
                if (m_IsRightControlVisible)
                {   // Transfer mode: transfer between left "from account" (left) to the "to account" (right)
                    // Transfer from account:
                    Misty.Lib.OrderHubs.OrderHubRequest leftRequest = new Misty.Lib.OrderHubs.OrderHubRequest(Misty.Lib.OrderHubs.OrderHubRequest.RequestType.RequestRealPnLReset);
                    leftRequest.Data = new object[3];
                    if (!leftInstrumentName.IsEmpty)
                    {
                        leftRequest.Data[0] = leftInstrumentName;
                    }
                    else
                    {
                        System.Windows.Forms.MessageBox.Show("Transfers must be from one Instrument to another.", "Request not submitted");
                        return;                                 // Now, only transfers from specific instr to another is allowed.
                    }
                    double moneyToTransfer = 0;
                    if (buttonName.Equals("buttonSubmitRealPnL"))
                    {
                        if (!double.IsNaN(leftDoubles[0]))          // previous value
                        {
                            moneyToTransfer = leftDoubles[0];
                        }
                        leftRequest.Data[1] = 0;                    // new value is zero.
                        leftRequest.Data[2] = null;                 // leave starting-pnl alone.
                    }
                    else if (buttonName.Equals("buttonSubmitStartRealPnL"))
                    {
                        if (!double.IsNaN(leftDoubles[2]))
                        {
                            moneyToTransfer = leftDoubles[2];
                        }
                        leftRequest.Data[1] = null;
                        leftRequest.Data[2] = 0;                    // new value is zero.
                    }
                    else
                    {
                        System.Windows.Forms.MessageBox.Show(string.Format("Unknown button {0}", buttonName), "Request not submitted");
                        return;
                    }
                    // Transfer to account:
                    Misty.Lib.OrderHubs.OrderHubRequest rightRequest = new Misty.Lib.OrderHubs.OrderHubRequest(Misty.Lib.OrderHubs.OrderHubRequest.RequestType.RequestRealPnLReset);
                    rightRequest.Data = new object[3];
                    if (!rightInstrumentName.IsEmpty)
                    {
                        rightRequest.Data[0] = rightInstrumentName;
                    }
                    else
                    {
                        MessageBox.Show("Transfers must be from one Instrument to another.", "Request not submitted");
                        return;                                 // Now, only transfers from specific instr to another is allowed.
                    }
                    double currentMoney = 0;
                    if (buttonName.Equals("buttonSubmitRealPnL"))
                    {
                        if (!double.IsNaN(rightDoubles[0]))         // previous value
                        {
                            currentMoney = rightDoubles[0];
                        }
                        else
                        {
                            MessageBox.Show(" \"Transfer to\" account has bad value for current PnL.", "Request not submitted");
                            return;
                        }
                        rightRequest.Data[1] = currentMoney + moneyToTransfer; // new value is sum
                        rightRequest.Data[2] = null;                           // leave starting-pnl alone.
                    }
                    else if (buttonName.Equals("buttonSubmitStartRealPnL"))
                    {
                        if (!double.IsNaN(rightDoubles[2]))
                        {
                            currentMoney = rightDoubles[2];
                        }
                        else
                        {
                            MessageBox.Show(" \"Transfer to\" account has bad value for current PnL.", "Request not submitted");
                            return;
                        }
                        rightRequest.Data[1] = null;                           // leave value alone
                        rightRequest.Data[2] = currentMoney + moneyToTransfer; // new value
                    }
                    // Submit both requests now.
                    StringBuilder msg = new StringBuilder();
                    msg.AppendFormat("From: \r\nAccount: {0} \r\nInstrument:{1}", leftFillHub.ServiceName, leftRequest.Data[0]);
                    if (leftRequest.Data[1] != null)
                    {
                        msg.AppendFormat("\r\nDaily PnL: {0}", leftRequest.Data[1]);
                    }
                    else if (leftRequest.Data[2] != null)
                    {
                        msg.AppendFormat("\r\nStarting PnL: {0}", leftRequest.Data[2]);
                    }
                    else
                    {
                        MessageBox.Show(" \"Transfer from\" request has no values in data.", "Request not submitted");
                        return;
                    }

                    msg.Append("\r\n");
                    msg.AppendFormat("To: \r\nAccount: {0} \r\nInstrument:{1}", rightFillHub.ServiceName, rightRequest.Data[0]);
                    if (rightRequest.Data[1] != null)
                    {
                        msg.AppendFormat("\r\nDaily PnL: {0}", rightRequest.Data[1]);
                    }
                    else if (rightRequest.Data[2] != null)
                    {
                        msg.AppendFormat("\r\nStarting PnL: {0}", rightRequest.Data[2]);
                    }
                    else
                    {
                        MessageBox.Show(" \"Transfer to\" request has no values in data.", "Request not submitted");
                        return;
                    }
                    msg.Append("\r\nPlease confirm these final amounts.");
                    DialogResult result = MessageBox.Show(msg.ToString(), "Confirm transfer", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        leftFillHub.Request(leftRequest);           // Subtract the money first.
                        rightFillHub.Request(rightRequest);
                    }
                }
                else
                {   // User supplied MANUAL values for PnL.
                    if (buttonName.Equals("buttonSubmitRealPnL"))
                    {
                        Misty.Lib.OrderHubs.OrderHubRequest request = new Misty.Lib.OrderHubs.OrderHubRequest(Misty.Lib.OrderHubs.OrderHubRequest.RequestType.RequestRealPnLReset);
                        request.Data = new object[3];
                        if (!leftInstrumentName.IsEmpty)
                        {
                            request.Data[0] = leftInstrumentName;
                        }
                        else
                        {
                            request.Data[0] = null;             // means update all instruments.
                        }
                        if (!double.IsNaN(leftDoubles[1]))
                        {
                            request.Data[1] = leftDoubles[1];  // value to update all instrument pnl.
                        }
                        else
                        {
                            MessageBox.Show("Manual value is not set.", "Request not submitted");
                            return;
                        }
                        request.Data[2] = null;                 // null means leave starting PnL alone.
                        leftFillHub.Request(request);
                    }
                    else if (buttonName.Equals("buttonSubmitStartRealPnL"))
                    {
                        Misty.Lib.OrderHubs.OrderHubRequest request = new Misty.Lib.OrderHubs.OrderHubRequest(Misty.Lib.OrderHubs.OrderHubRequest.RequestType.RequestRealPnLReset);
                        request.Data = new object[3];
                        if (!leftInstrumentName.IsEmpty)
                        {
                            request.Data[0] = leftInstrumentName;
                        }
                        else
                        {
                            request.Data[0] = null;            // means update all instruments.
                        }
                        request.Data[1] = null;                // null means leave daily real pnl alone.
                        if (!double.IsNaN(leftDoubles[3]))     // update the starting pnl.
                        {
                            request.Data[2] = leftDoubles[3];
                        }
                        else
                        {
                            MessageBox.Show("Manual value is not set.", "Request not submitted");
                            return;
                        }
                        leftFillHub.Request(request);
                    }
                }
            }
            catch (Exception)
            {
            }
        }
 //
 // ****                 Button_Click()                  ****
 //
 private void Button_Click(object sender, EventArgs e)
 {
     if (sender == buttonSubmit)
     {
         if (radioButton1.Checked)
         {   // reset all hubs now
             foreach (FillHub fillHub in m_FillHubs.Values)
             {
                 Misty.Lib.OrderHubs.OrderHubRequest request = new Misty.Lib.OrderHubs.OrderHubRequest(Misty.Lib.OrderHubs.OrderHubRequest.RequestType.RequestRealPnLReset);
                 request.Data    = new object[3];
                 request.Data[0] = null;             // means update all instruments.
                 double x;
                 if (checkBoxDailyPnL.Checked && double.TryParse(textBoxRealPnL.Text, out x))
                 {
                     request.Data[1] = x;
                 }
                 else
                 {
                     request.Data[1] = null;
                 }
                 if (checkBoxStartingPnL.Checked && double.TryParse(textBoxRealStartingPnL.Text, out x))
                 {
                     request.Data[2] = x;
                 }
                 else
                 {
                     request.Data[2] = null;
                 }
                 // Submit request
                 fillHub.Request(request);
             }
         }
         else if (radioButton2.Checked)
         {
             FillHub fillHub;
             if (comboBoxFillHubs.SelectedItem != null && m_FillHubs.TryGetValue((string)comboBoxFillHubs.SelectedItem, out fillHub))
             {
                 Misty.Lib.OrderHubs.OrderHubRequest request = new Misty.Lib.OrderHubs.OrderHubRequest(Misty.Lib.OrderHubs.OrderHubRequest.RequestType.RequestRealPnLReset);
                 request.Data    = new object[3];
                 request.Data[0] = null;             // means update all instruments.
                 double x;
                 if (checkBoxDailyPnL.Checked && double.TryParse(textBoxRealPnL.Text, out x))
                 {
                     request.Data[1] = x;
                 }
                 else
                 {
                     request.Data[1] = null;
                 }
                 if (checkBoxStartingPnL.Checked && double.TryParse(textBoxRealStartingPnL.Text, out x))
                 {
                     request.Data[2] = x;
                 }
                 else
                 {
                     request.Data[2] = null;
                 }
                 // Submit request
                 fillHub.Request(request);
             }
         }
         else if (radioButton3.Checked)
         {
             FillHub fillHub;
             if (comboBoxFillHubs.SelectedItem != null && m_FillHubs.TryGetValue((string)comboBoxFillHubs.SelectedItem, out fillHub))
             {     // The user has selected a fillHub.
                 if (comboBoxInstruments.SelectedItem != null)
                 { // The user has selected an instrumentName
                     InstrumentName name = (InstrumentName)comboBoxInstruments.SelectedItem;
                     Misty.Lib.OrderHubs.OrderHubRequest request = new Misty.Lib.OrderHubs.OrderHubRequest(Misty.Lib.OrderHubs.OrderHubRequest.RequestType.RequestRealPnLReset);
                     request.Data    = new object[3];
                     request.Data[0] = name;             // means update all instruments.
                     double x;
                     if (checkBoxDailyPnL.Checked && double.TryParse(textBoxRealPnL.Text, out x))
                     {
                         request.Data[1] = x;
                     }
                     else
                     {
                         request.Data[1] = null;
                     }
                     if (checkBoxStartingPnL.Checked && double.TryParse(textBoxRealStartingPnL.Text, out x))
                     {
                         request.Data[2] = x;
                     }
                     else
                     {
                         request.Data[2] = null;
                     }
                     // Submit request
                     fillHub.Request(request);
                 }
             }
         }
     }
 }
Beispiel #4
0
        private void buttonDeleteBook_Click(object sender, EventArgs e)
        {
            if (buttonDeleteBook.Text == ButtonDeleteBook_Normal)
            {   // We are in the normal state.
                buttonDeleteBook.Text = ButtonDeleteBook_Confirm;
            }
            else if (buttonDeleteBook.Text == ButtonDeleteBook_Confirm)
            {   // User has just confirmed the delete request.
                // Submit the delete request

                if (Log != null)
                {
                    if (m_FillHub == null)
                    {
                        Log.NewEntry(Misty.Lib.Hubs.LogLevel.Major, "FormAddFill.buttonDeleteBook_Click: FillHub is null!");
                        buttonDeleteBook.Text = ButtonDeleteBook_Normal;
                        return;
                    }
                    else if (m_CurrentInstrument == null || m_CurrentInstrument == m_EmptyInstrument)
                    {
                        Log.NewEntry(Misty.Lib.Hubs.LogLevel.Major, "FormAddFill.buttonDeleteBook_Click: CurrentInsrument is null!");
                        buttonDeleteBook.Text = ButtonDeleteBook_Normal;
                        return;
                    }
                    //else if (m_CurrentInstrument.ForeignKey == null)
                    //{
                    //    Log.NewEntry(Misty.Lib.Hubs.LogLevel.Major,"FormAddFill.buttonDeleteBook_Click: CurrentInsrument.ForeignKey is null!");
                    //    buttonDeleteBook.Text = ButtonDeleteBook_Normal;
                    //    return;
                    //}
                    Log.NewEntry(Misty.Lib.Hubs.LogLevel.Major, "FormAddFill.buttonDeleteBook_Click: Request delete for {0} in FillHub {1}.", m_CurrentInstrument, m_FillHub.Name);
                }

                // Try to read the required values like realized PnL, average cost, net position.
                IFillBook book;
                if (m_FillHub.TryEnterReadBook(m_CurrentInstrument, out book))
                {
                    Log.NewEntry(LogLevel.Major, "The deleted book contains net pos:{0}, real PnL:{1}, start real PnL:{2}, avg price:{3}, currency name:{4}, currency rate:{5}.",
                                 book.NetPosition, book.RealizedDollarGains, book.RealizedStartingDollarGains, book.AveragePrice, book.CurrencyName, book.CurrencyRate);

                    double netPosition      = book.NetPosition;
                    double realizedPnL      = book.RealizedDollarGains;
                    double startRealizedPnL = book.RealizedStartingDollarGains;
                    double avgPrice         = book.AveragePrice;
                    string currencyName     = book.CurrencyName;
                    double currencyRate     = book.CurrencyRate;
                    m_FillHub.ExitReadBook(m_CurrentInstrument);
                    double settlementValue = double.NaN;

                    // Find the instrument detail for this instrument and load the cash book accordingly.
                    //TradingTechnologies.TTAPI.InstrumentDetails instrDetail;
                    //if (!m_Market.TryLookupInstrumentDetails(m_CurrentInstrument, out instrDetail))
                    //{
                    //    Log.NewEntry(LogLevel.Warning, "No instrument detail is found for {0}", m_CurrentInstrument);
                    //    return;
                    //}

                    if (netPosition != 0)
                    {
                        // Request the user to input a settlement value to calculate the PnL.
                        string input = string.Empty;
                        while (!double.TryParse(input, out settlementValue) && settlementValue != double.NaN)
                        {
                            input = Microsoft.VisualBasic.Interaction.InputBox("Please input the settlement price. Input nothing to exit.",
                                                                               "Request settlement price", avgPrice.ToString(), 450, 450);

                            if (string.IsNullOrEmpty(input))
                            {
                                return;
                            }
                        }

                        // Calculate the PnL by the settlement price.
                        realizedPnL += (settlementValue - avgPrice) * netPosition * book.DollarPerPoint;
                    }

                    // Ask the user whether he likes to transfer the PnL to the corresponding cash instrument.
                    DialogResult result = MessageBox.Show(string.Format("I am going to transfer {0} ammount of PnL in currency {1} to the cash instrument",
                                                                        startRealizedPnL + realizedPnL, currencyName), "PnL transfer to cash instrument", MessageBoxButtons.YesNo);

                    if (result == System.Windows.Forms.DialogResult.Yes)
                    {
                        // Get the cash book according to the instrument detail.
                        Product        product      = new Product("Cash", currencyName, ProductTypes.Cash, "");
                        InstrumentName newCashInstr = new InstrumentName(product, "");

                        Log.NewEntry(LogLevel.Minor, "Send request to create/update cash book.");
                        Misty.Lib.OrderHubs.OrderHubRequest cashBookCreateRequest = new Misty.Lib.OrderHubs.OrderHubRequest(Misty.Lib.OrderHubs.OrderHubRequest.RequestType.RequestCreateUpdateCashBook);
                        cashBookCreateRequest.Data = new object[5] {
                            newCashInstr,
                            currencyName,
                            currencyRate,
                            realizedPnL,
                            startRealizedPnL
                        };
                        m_FillHub.Request(cashBookCreateRequest);

                        Misty.Lib.OrderHubs.OrderHubRequest instrumentDeleteRequest = new Misty.Lib.OrderHubs.OrderHubRequest(Misty.Lib.OrderHubs.OrderHubRequest.RequestType.RequestDeleteBook);
                        //TradingTechnologies.TTAPI.InstrumentKey instrKey = (TradingTechnologies.TTAPI.InstrumentKey)m_CurrentInstrument.ForeignKey;
                        //request.Data = new object[1] { instrKey };
                        Log.NewEntry(LogLevel.Minor, "Send request to delete fill book.");
                        instrumentDeleteRequest.Data = new object[1] {
                            m_CurrentInstrument
                        };
                        m_FillHub.Request(instrumentDeleteRequest);
                        buttonDeleteBook.Text = ButtonDeleteBook_Normal;
                    }
                    else
                    {
                        buttonDeleteBook.Text = ButtonDeleteBook_Normal;
                    }
                }
                else
                {
                    Log.NewEntry(LogLevel.Warning, "FormAddFill.buttonDeleteBook_Click:No instrument key found for {0}", m_CurrentInstrument);
                    buttonDeleteBook.Text = ButtonDeleteBook_Normal;
                }

                //Misty.Lib.OrderHubs.OrderHubRequest request = new Misty.Lib.OrderHubs.OrderHubRequest(Misty.Lib.OrderHubs.OrderHubRequest.RequestType.RequestDeleteBook);
                ////TradingTechnologies.TTAPI.InstrumentKey instrKey = (TradingTechnologies.TTAPI.InstrumentKey)m_CurrentInstrument.ForeignKey;
                ////request.Data = new object[1] { instrKey };
                //request.Data = new object[1] { m_CurrentInstrument };
                //m_FillHub.Request(request);
                //buttonDeleteBook.Text = ButtonDeleteBook_Normal;
            }
        }//