Example #1
0
        //Calculates values for entire portfolio (top label)
        private void CalculatePortfolioValue()
        {
            double cashUsed = 0.0;
            double totalPnL = 0.0;

            foreach (DataGridViewRow row in m_PortfolioGrid.Rows)
            {
                Orders.Status status = Orders.StatusFromString(Convert.ToString(row.Cells["Status"].Value));
                if (!(((status != Orders.Status.CancelReceived) & (status != Orders.Status.Rejected)) &
                      (status != Orders.Status.Expired)))
                {
                    continue;
                }

                double entryPrice = Format.FromUsCurrency(row.Cells["Entry"].Value);
                double dollarGain = Format.FromUsCurrency(row.Cells["DollarGain"].Value);
                int    qty        = Format.FromLocalInteger(row.Cells["Qty"].Value);

                cashUsed += entryPrice * qty;
                totalPnL += dollarGain;

                //cashUsed += Convert.ToDouble(row.Cells["Entry"].Value) * Convert.ToDouble(row.Cells["Qty"].Value);
                //totalPnL += double.Parse(row.Cells["DollarGain"].Value.ToString(), NumberStyles.Currency);
            }

            string PortfolioVal   = "Portfolio value: " + Format.ToUsCurrency(m_StartingBalance + totalPnL);
            string TotalPL        = "Total P&L: " + Format.ToUsCurrency(totalPnL);
            string AccountBalance = "Account balance (cash): " + Format.ToUsCurrency(m_StartingBalance - cashUsed);
            string Trades         = "Trades: " + m_PortfolioGrid.Rows.Count;
        }
Example #2
0
        private void CalculateGains()
        {
            foreach (DataGridViewRow row in m_PortfolioGrid.Rows)
            {
                Orders.Status status = Orders.StatusFromString(Convert.ToString(row.Cells["Status"].Value));

                if (!(((status != Orders.Status.CancelReceived) & (status != Orders.Status.Rejected)) &
                      (status != Orders.Status.Expired)))
                {
                    continue;
                }

                double pctGain    = 0.0;
                double valGain    = 0.0;
                double LastPrice  = Format.FromUsCurrency(row.Cells["Last"].Value);
                double EntryPrice = Format.FromUsCurrency(row.Cells["Entry"].Value);
                int    Quantity   = (int)Math.Round((double)Format.FromLocalInteger(row.Cells["Qty"].Value));

                if (row.Cells["Type"].Value.ToString() == "Long") //Long position
                {
                    if ((LastPrice != 0.0) & (EntryPrice != 0.0))
                    {
                        valGain = LastPrice - EntryPrice;
                        pctGain = Math.Round((LastPrice / EntryPrice) - 1.0, 4) * 100.0;
                    }
                }
                else if (row.Cells["Type"].Value.ToString() == "Short") //Short position
                {
                    if ((LastPrice != 0.0) & (EntryPrice != 0.0))
                    {
                        valGain = EntryPrice - LastPrice;
                        pctGain = Math.Round((EntryPrice / LastPrice) - 1.0, 4) * 100.0;
                    }
                }
                else
                {
                    valGain = 0.0;
                    pctGain = 0.0;
                }

                if (valGain > 0.0)
                {
                    row.Cells["DollarGain"].Style.ForeColor  = Color.Green;
                    row.Cells["PercentGain"].Style.ForeColor = Color.Green;
                }
                else if (valGain < 0.0)
                {
                    row.Cells["DollarGain"].Style.ForeColor  = Color.Red;
                    row.Cells["PercentGain"].Style.ForeColor = Color.Red;
                }
                else
                {
                    row.Cells["DollarGain"].Style.ForeColor  = Color.DarkBlue;
                    row.Cells["PercentGain"].Style.ForeColor = Color.DarkBlue;
                }
                row.Cells["DollarGain"].Value  = Format.ToUsCurrency(valGain * Quantity);
                row.Cells["PercentGain"].Value = Convert.ToString(Math.Round(pctGain, 2)) + "%";
            }
        }
Example #3
0
        private void GetLastPrices()
        {
            foreach (DataGridViewRow row in m_PortfolioGrid.Rows)
            {
                string symbol = Convert.ToString(row.Cells["Symbol"].Value);

                //Update the price only if we're not out of the position
                Orders.Status status = Orders.StatusFromString(Convert.ToString(row.Cells["Status"].Value));
                if ((status != Orders.Status.Out) & (status != Orders.Status.CancelReceived))
                {
                    row.Cells["Last"].Value = Format.ToUsCurrency(GetLastPrice(symbol));
                }
            }
        }
Example #4
0
        //Sends an alert to the web service for the mobile app
        private void SendExecutionAlertToServer(string orderID, Orders.Status OrderStatus, string symbol, DateTime ExecTime,
                                                Orders.Side Side, int Quantity, double EntryPrice, Order.OrderType OrderType,
                                                Order.Expiration Expires, double Limit)
        {
            string key  = "ALERT|" + Convert.ToString(ExecTime) + "|ORDER|" + symbol + "|" + orderID;
            string data = orderID + "|" + Orders.StatusToString(OrderStatus) + "|" + symbol + "|" +
                          Convert.ToString(ExecTime) + "|" + Orders.SideToString(Side) + "|" +
                          Convert.ToString(Quantity) + "|" + Convert.ToString(EntryPrice) + "|" +
                          Order.OrderTypeToString(OrderType) + "|" + Order.ExpirationToString(Expires) + "|" +
                          Convert.ToString(Limit);

            try
            {
                svc.SetUserData(frmMain2.ClientId, frmMain2.ClientPassword, frmMain2.LicenseKey, key, data);
            }
            catch (Exception)
            {
                return;
            }
        }
Example #5
0
        //#############################
        //WARNING! Example code only!
        //Your order entry API is responsible for returning order status events to update the portfolio!
        //#############################

        //Call this routine when your order entry API returns a fill, cancel, reject, etc. message
        //If the orderID is not found, it is created
        //Orderid, status, time, symbol, type, qty, entry, last, $ gain, % gain
        public void ExecuteOrder(string orderID, Orders.Status OrderStatus, string symbol,
                                 DateTime ExecTime, Orders.Side Side, int Quantity, double EntryPrice,
                                 Order.OrderType OrderType, Order.Expiration Expires, double Limit)
        {
            //Details for the order including order type, expiration and limit price (if not market)
            string details;

            if (OrderType != Order.OrderType.Market)
            {
                details = Order.OrderTypeToString(OrderType) + " (" + Convert.ToString(Limit) + ") " +
                          Order.ExpirationToString(Expires);
            }
            else
            {
                details = Order.OrderTypeToString(OrderType) + " " + Order.ExpirationToString(Expires);
            }
            if ((DateTime.Compare(ExecTime, DateTime.MinValue) != 0) |
                (DateTime.Compare(ExecTime, Convert.ToDateTime("12:00:00 AM")) == 0))
            {
                ExecTime = DateTime.Now; //Time must be valid
            }

            //Try to find the order to update it
            for (int n = 0; n <= m_PortfolioGrid.Rows.Count - 1; n++)
            {
                if (m_PortfolioGrid.Rows[n].Cells["orderID"].Value.ToString().ToLower() != orderID.ToLower())
                {
                    continue;
                }

                if (OrderStatus != Orders.Status.Unknown)
                {
                    m_PortfolioGrid.Rows[n].Cells["Status"].Value = Orders.StatusToString(OrderStatus);
                }
                if (OrderType != Order.OrderType.Unknown)
                {
                    m_PortfolioGrid.Rows[n].Cells["Details"].Value = details;
                }
                if (symbol != "")
                {
                    m_PortfolioGrid.Rows[n].Cells["symbol"].Value = symbol;
                }
                if (Side != Orders.Side.Unknown)
                {
                    m_PortfolioGrid.Rows[n].Cells["Type"].Value = Orders.SideToString(Side);
                }
                if (Quantity != 0)
                {
                    m_PortfolioGrid.Rows[n].Cells["Qty"].Value = Quantity;
                }
                if (EntryPrice != 0.0)
                {
                    m_PortfolioGrid.Rows[n].Cells["Entry"].Value = EntryPrice;
                }

                //Send an alert
                SendExecutionAlertToServer(orderID, OrderStatus, symbol, ExecTime, Side, Quantity, EntryPrice, OrderType,
                                           Expires, Limit);

                //Speak the order change 'TODO: optional
                string text = Orders.StatusToString(OrderStatus);
                //m_frmMain.Speak("an order has been updated to," + text + ",for,symbol,[" + symbol + "]");

                return;
            }

            //Couldn't find the order so add it
            try
            {
                m_PortfolioGrid.Rows.Add(new object[]
                {
                    orderID, Orders.StatusToString(OrderStatus), details, ExecTime, symbol,
                    Orders.SideToString(Side), Quantity, EntryPrice
                });

                //Send an alert
                SendExecutionAlertToServer(orderID, OrderStatus, symbol, ExecTime, Side, Quantity, EntryPrice, OrderType,
                                           Expires, Limit);

                ////Speak the order 'TODO: optional
                //switch (Side)
                //{
                //    case Orders.Side.LongSide:
                //        m_frmMain.Speak("a buy order has been submitted,[" + Convert.ToString(Quantity) + "],shares of,symbol,[" +
                //                        symbol + "]");
                //        break;
                //    case Orders.Side.ShortSide:
                //        m_frmMain.Speak("a sell order has been submitted,[" + Convert.ToString(Quantity) +
                //                        "]symbol,shares of,symbol,[" + symbol + "]");
                //        break;
                //}
            }
            catch (Exception)
            {
                //Form was closing when adding an order to the grid
            }

            UpdatePortolioScreen(); //Refresh the data grid view

            // Send an update to twitter, if requested
            //if (frmMain2.GInstance.TweetTrades)
            //{
            //    string trade = Side.ToString().Replace("Side", "") + " " + symbol + " @ " + EntryPrice; // TODO: change message as desired
            //    frmMain2.GInstance.SendTweet(trade);
            //}
        }