Exemplo n.º 1
0
        private void DumpBrokerageRecommendation(ActiveTradersNewsElement element, StringBuilder builder)
        {
            builder.Append("<tr><td>");
            builder.Append("<a href=http://finance.yahoo.com/q?s=");
            builder.Append(element.Symbol);
            builder.Append(">");
            builder.Append(element.Symbol);
            builder.Append("</a><td>");

            // Remove time
            string text = element.Text.Substring(element.Text.IndexOf("EDT") + 4);

            // Remove the changed text itself because it is redundant
            text = text.Replace("upgraded ", string.Empty).Replace("downgraded ", string.Empty).Replace("initiated ", string.Empty);

            // Some entries have extra detail that we don't want. This usually comes after a " - " sequence.
            int hyphenIndex = text.IndexOf(" - ");

            if (hyphenIndex != -1)
            {
                text = text.Substring(0, hyphenIndex);
            }

            // If there is anything saying "Correction:" then remove it
            text = text.Replace("Correction:", string.Empty);
            text = text.Replace("Correct:", string.Empty);

            builder.Append(text);
            builder.Append("</td></tr>\r\n");
        }
Exemplo n.º 2
0
        private void DumpEarningsSummary(ActiveTradersNewsElement element, StringBuilder builder, bool isEvenRow)
        {
            builder.Append("<tr");
            if (isEvenRow)
            {
                builder.Append(" style=\"background-color: #deeffc;\"");
            }
            builder.Append("><td style=\"vertical-align: top;\">");
            builder.Append("<a href=http://finance.yahoo.com/q?s=");
            builder.Append(element.Symbol);
            builder.Append(">");
            builder.Append(element.Symbol);
            builder.Append("</a><td>");

            // Remove time
            string text = element.Text.Substring(element.Text.IndexOf("EDT") + 4);

            builder.Append(text);
            builder.Append("</td></tr>\r\n");
        }
Exemplo n.º 3
0
        private void AddNewNewsElement(ActiveTradersNewsElement newNewsElement, bool notifyClients)
        {
            // Has a new day begun?
            bool newDay = false;

            // First atomically update the collection of news elements we have
            SortedSet <ActiveTradersNewsElement> newsElementsClone = new SortedSet <ActiveTradersNewsElement>(new ActiveTradersNewsElement.Comparer());

            // Clone the existing collection
            newsElementsClone.UnionWith(NewsElements);

            // If the new item has a number smaller than the current maximum, then a new day has begun. As such, clear all the data and start from
            // scratch. Also, let clients know about this wonderful phenomenon in history of human beings.
            if (newsElementsClone.Count > 0 && newNewsElement.ElementId < newsElementsClone.Min.ElementId)
            {
                newDay = true;
                newsElementsClone.Clear();
            }

            // Add the new item
            newsElementsClone.Add(newNewsElement);

            // Update the current items atomically
            Interlocked.Exchange(ref NewsElements, newsElementsClone);

            // Then notify the clients out there about the new item
            if (notifyClients && Clients != null)
            {
                if (newDay)
                {
                    Clients.resetState();
                }

                Clients.addNewsElement(newNewsElement);
            }
        }
Exemplo n.º 4
0
        private void GrabNewsElementsForProduction()
        {
            do
            {
                try
                {
                    // Create a TCP connection to FlyOnTheWall servers
                    using (TcpClient tcpClient = new TcpClient(FlyOnTheWallServerAddress, FlyOnTheWallServerPort))
                    {
                        NetworkStream stream = tcpClient.GetStream();

                        // Send the initial request which must have HTTP like headers
                        byte[] initialRequest =
                            Encoding.ASCII.GetBytes(
                                "GET /SERVICE/STORY?NUM=0&u=stockwinners&p=stockwinners HTTP/1.0\r\n\r\n");
                        stream.Write(initialRequest, 0, initialRequest.Length);

                        // Used to track the data received so far (unprocessed data)
                        string dataReceived = string.Empty;

                        // Start receiving the data and loop indefinitely. NetworkStream.Read will block until data is available.
                        int bytesRead = 0;
                        do
                        {
                            // Read data in chunks of 1KB each.
                            byte[] data = new byte[1024];
                            bytesRead = stream.Read(data, 0, data.Length);

                            dataReceived = dataReceived + Encoding.ASCII.GetString(data, 0, bytesRead);

                            // Messages sent from the server are delimited by \n
                            for (int splitterIndex = dataReceived.IndexOf('\n');
                                 splitterIndex != -1;
                                 splitterIndex = dataReceived.IndexOf('\n'))
                            {
                                // Parse from the start of the accumulated data until the first \n found
                                ActiveTradersNewsElement newsElement =
                                    ParseActiveTradersElement(dataReceived.Substring(startIndex: 0,
                                                                                     length: splitterIndex));

                                if (newsElement != null)
                                {
                                    // Only notify clients if we received a single update. This helps us distinguish from the cases where we are starting up the server
                                    // in which cases we would want to collect all existing messages without sending a notification to clients for each message.
                                    this.AddNewNewsElement(newsElement, notifyClients: !stream.DataAvailable);
                                }

                                // Remove the processed part of the massage from it (and also remove the \n) and continue processing
                                // the rest
                                dataReceived = dataReceived.Substring(splitterIndex + 1,
                                                                      dataReceived.Length - splitterIndex - 1);
                            }

                            if (!stream.DataAvailable)
                            {
                                // Wait 10 seconds for the next news element
                                Thread.Sleep(10000);
                            }

                            if (ActiveTradersHub.Reset)
                            {
                                ActiveTradersHub.Reset = false;
                                break;
                            }
                        } while (true);
                    }
                }
                catch (SocketException exp)
                {
                    Thread.Sleep(10000);
                }
            } while (true);
        }
Exemplo n.º 5
0
        private IEnumerable <ActiveTradersNewsElement> ChooseElementsRandomly(IEnumerable <ActiveTradersNewsElement> items, int numberOfElements)
        {
            // Map entries based on symbol
            Dictionary <string, List <ActiveTradersNewsElement> > elements = new Dictionary <string, List <ActiveTradersNewsElement> >();
            Random random = new Random();

            foreach (var item in items)
            {
                List <ActiveTradersNewsElement> entriesForSymbol = null;

                if (elements.TryGetValue(item.Symbol, out entriesForSymbol))
                {
                    entriesForSymbol.Add(item);
                }
                else
                {
                    elements.Add(item.Symbol, new List <ActiveTradersNewsElement>()
                    {
                        item
                    });
                }
            }

            // For symbols that have more than one entry, choose one randomly
            foreach (string symbol in elements.Keys)
            {
                // If symbol is empty, then the news element is general, so leave it alone
                if (string.IsNullOrEmpty(symbol))
                {
                    continue;
                }

                List <ActiveTradersNewsElement> entriesForSymbol = elements[symbol];

                if (entriesForSymbol.Count > 1)
                {
                    ActiveTradersNewsElement singleElement = entriesForSymbol[random.Next(0, entriesForSymbol.Count)];

                    entriesForSymbol.Clear();
                    entriesForSymbol.Add(singleElement);
                }
            }

            // Flatten the entries
            List <ActiveTradersNewsElement> eligibleEntries = new List <ActiveTradersNewsElement>();

            foreach (List <ActiveTradersNewsElement> entryForSymbol in elements.Values)
            {
                eligibleEntries.AddRange(entryForSymbol);
            }

            // Is the total less than 9?
            if (eligibleEntries.Count > numberOfElements)
            {
                // Choose 9 elements randomly
                List <int> selectedIndices = new List <int>();
                while (selectedIndices.Count < numberOfElements)
                {
                    int randomIndex = random.Next(0, eligibleEntries.Count);

                    if (!selectedIndices.Contains(randomIndex))
                    {
                        selectedIndices.Add(randomIndex);
                    }
                }

                List <ActiveTradersNewsElement> chosenElements = new List <ActiveTradersNewsElement>(selectedIndices.Count);

                foreach (int index in selectedIndices)
                {
                    chosenElements.Add(eligibleEntries[index]);
                }

                // Swap the full list with the set of randomly selected elements
                eligibleEntries = chosenElements;
            }

            return(eligibleEntries);
        }