Example #1
0
        public void ThreadB()
        {
            try
            {
                //string topic = args[0] == "All" ? "" : args[0];
                //string topic = "";
                //Console.WriteLine("Subscriber started for Topic : {0}", _topic);

                using (var subSocket = new SubscriberSocket())
                {
                    subSocket.Options.ReceiveHighWatermark = 0;
                    publisherIP   = ConfigurationManager.AppSettings["publisherIP"];
                    publisherPort = ConfigurationManager.AppSettings["publisherPort"];
                    subSocket.Connect("tcp://" + publisherIP + ":" + publisherPort);
                    //subSocket.SubscribeToAnyTopic();
                    if (parentSD.dictFilters.Count == 0)
                    {
                        subSocket.SubscribeToAnyTopic();
                    }
                    else
                    {
                        foreach (var filters in parentSD.dictFilters)
                        {
                            //Console.WriteLine("Subscribing Socket for Symbol : " + filters.Key);
                            subSocket.Subscribe(filters.Key);
                        }
                    }

                    //Console.WriteLine("Subscriber socket connecting...");
                    while (true)
                    {
                        try
                        {
                            if (ScannerBox.openedMainForm == false)
                            {
                                break;
                            }
                            string messageReceived;

                            System.Threading.Thread.Sleep(5);

                            if (subSocket.TryReceiveFrameString(out messageReceived))
                            {
                                //Writing the messages received in a log file
                                long   millis      = (long)((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds);
                                string feedMessage = messageReceived + "," + millis;
                                //File.AppendAllText("D:/FeedLog.txt", messageReceived + "," + millis + "\n");

                                //string messageReceived = subSocket.ReceiveFrameString();
                                string[] origMessageSplit = messageReceived.Split(';');
                                string[] arr = origMessageSplit[0].Split('|');

                                //string tokenno, ltp, ltq, bidprice, bidsize, askprice, asksize, feedtime
                                //token,ltp,ltq,b_amt,b_qty,a_amt,a_qty,lft,ltt,totalVol,open,high,low
                                string tokenno  = arr[0];
                                double ltp      = Convert.ToDouble(arr[1]) / 100.0;
                                string ltq      = arr[2];
                                double bidprice = Convert.ToDouble(arr[3]) / 100.0;
                                string bidsize  = arr[4];
                                double askprice = Convert.ToDouble(arr[5]) / 100.0;
                                string asksize  = arr[6];

                                DateTime dt1      = epoch.AddMilliseconds(Convert.ToInt64(arr[7]));
                                string   feedtime = dt1.ToString("dd/MM/yyyy HH:mm:ss");

                                string ltt       = arr[8];
                                string tradetime = "";
                                if (ltt.Equals("0") == false)
                                {
                                    DateTime dt2 = epoch.AddMilliseconds(Convert.ToInt64(ltt));
                                    tradetime = dt2.ToString("dd/MM/yyyy HH:mm:ss.ff");
                                }

                                string volume = arr[9];
                                string open   = "0";
                                string high   = "0";
                                string low    = "0";

                                if (arr.Length >= 12)
                                {
                                    open = arr[10];
                                    high = arr[11];
                                    low  = arr[12];
                                }

                                //DateTime dt = DateTime.Now;
                                //string feedtime = dt.ToString("HH:mm:ss");

                                Feed feed = new Feed(tokenno, feedtime, Convert.ToString(ltp), ltq, bidsize, Convert.ToString(bidprice), Convert.ToString(askprice), asksize, tradetime, volume, open, high, low);

                                ScannerBox.qfeed.Enqueue(feed);
                                millis      = (long)((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds);
                                feedMessage = feedMessage + "," + millis;
                                ScannerBox.qOrgfeed.Enqueue(feedMessage);
                                //Timestamp2 and Log into file.
                                string key = feed.tokenno;
                                dictFeedDetails[key] = feed;

                                if (dictTrades.ContainsKey(tokenno) == false)
                                {
                                    dictTrades[tokenno] = new Dictionary <string, TradeDetails>();
                                }

                                Dictionary <string, TradeDetails> dictTradeDetails = dictTrades[tokenno];
                                if (dictTradeDetails.ContainsKey(ltt) == false)
                                {
                                    dictTradeDetails[ltt] = new TradeDetails(tradetime, ltp, ltq);
                                }

                                if (origMessageSplit.Length > 1)
                                {
                                    dictFeedLevels[key] = origMessageSplit[1];
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                    }
                }
            }
            finally
            {
                NetMQConfig.Cleanup();
            }
        }
Example #2
0
        public void AddListItemMethod()
        {
            //String myItem;
            if (ScannerBox.qfeed.Count > 0)
            {
                //calculate microseconds
                Feed feed = ScannerBox.qfeed.Dequeue();
                //calculate microseconds
                try
                {
                    string symbol  = "";
                    string expiry  = "";
                    string strike  = "";
                    string callput = "";

                    long   millis      = (long)((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds);
                    string feedMessage = ScannerBox.qOrgfeed.Dequeue();
                    feedMessage = feedMessage + "," + millis;

                    if (ScannerDashboard.dictSecurityMaster.ContainsKey(feed.tokenno))
                    {
                        SecurityMaster secMaster = ScannerDashboard.dictSecurityMaster[feed.tokenno];
                        symbol = secMaster.Symbol;
                        if (secMaster.Instrument != "EQ") // its a future or option stock,then we will get expirydate
                        {
                            expiry = secMaster.ExpiryDate;
                        }
                        if (secMaster.Instrument == "OPTIDX" || secMaster.Instrument == "OPTSTK")
                        {
                            strike  = secMaster.StrikePrice;
                            callput = secMaster.OptType;
                        }
                    }

                    millis      = (long)((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds);
                    feedMessage = feedMessage + "," + millis;

                    var exisiting = dtFeed.Rows.Find(new Object[] { feed.tokenno });
                    if (exisiting != null)
                    {
                        exisiting.ItemArray = new object[] { feed.tokenno, feed.feedtime, symbol, expiry, strike, callput, feed.ltp, feed.ltq, feed.bidsize, feed.bidprice, feed.askprice, feed.asksize, Convert.ToString(Math.Round((Convert.ToDouble(feed.askprice) - Convert.ToDouble(feed.bidprice)), 2)) }
                    }
                    ;
                    else
                    {
                        if (ScannerDashboard.dictSecurityMaster.ContainsKey(feed.tokenno))
                        {
                            dtFeed.Rows.Add(new Object[] { feed.tokenno, feed.feedtime, symbol, expiry, strike, callput, feed.ltp, feed.ltq, feed.bidsize, feed.bidprice, feed.askprice, feed.asksize, Convert.ToString(Math.Round((Convert.ToDouble(feed.askprice) - Convert.ToDouble(feed.bidprice)), 2)) });
                        }
                    }

                    millis      = (long)((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds);
                    feedMessage = feedMessage + "," + millis;

                    //calculate microseconds
                    //Log into file.
                    //feed, time1, time2, time3, time4, time5
                    File.AppendAllText("D:/FeedLog.txt", feedMessage + "\n");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
Example #3
0
        public void ThreadB()
        {
            try
            {
                //string topic = args[0] == "All" ? "" : args[0];
                //string topic = "";
                //Console.WriteLine("Subscriber started for Topic : {0}", _topic);

                using (var subSocket = new SubscriberSocket())
                {
                    subSocket.Options.ReceiveHighWatermark = 1000;
                    publisherIP   = ConfigurationManager.AppSettings["publisherIP"];
                    publisherPort = ConfigurationManager.AppSettings["publisherPort"];
                    subSocket.Connect("tcp://" + publisherIP + ":" + publisherPort);
                    //subSocket.SubscribeToAnyTopic();
                    if (parentSD.dictFilters.Count == 0)
                    {
                        subSocket.SubscribeToAnyTopic();
                    }
                    else
                    {
                        foreach (var filters in parentSD.dictFilters)
                        {
                            //Console.WriteLine("Subscribing Socket for Symbol : " + filters.Key);
                            subSocket.Subscribe(filters.Key);
                        }
                    }

                    //Console.WriteLine("Subscriber socket connecting...");
                    while (true)
                    {
                        try
                        {
                            if (ScannerBox.openedMainForm == false)
                            {
                                break;
                            }
                            string messageReceived;
                            if (subSocket.TryReceiveFrameString(out messageReceived))
                            {
                                //string messageReceived = subSocket.ReceiveFrameString();
                                string[] arr = messageReceived.Split(',');
                                //SELECT Symbol, feedtime, expiry, OptType, StrikePrice, Exch, PCloseRate, LastRate, TotalQty, Series, MLot, ScripNo, OpenRate, HighRate, LowRate, AvgRate

                                Feed feed = new Feed(arr[0], arr[1], arr[2], arr[3], arr[4], arr[5], arr[6], arr[7], arr[8], arr[9], arr[12], arr[13], arr[14], arr[15]);
                                bool flagSymbolCondition = true;
                                bool flagExpiryCondition = true;
                                bool flagExchCondition   = true;
                                bool flagStrikeCondition = true;

                                bool flagClosePriceCondition = true;
                                bool flagLtpCondition        = true;
                                bool flagQuantityCondition   = true;

                                bool foundKey = false;

                                List <SymbolFilter> listSymbolFilter;
                                if (parentSD.dictFilters.TryGetValue(feed.symbol.Trim(), out listSymbolFilter))
                                {
                                    foreach (var symbolfilter in listSymbolFilter)
                                    {
                                        if (symbolfilter.symbol != null && string.Compare(feed.symbol, symbolfilter.symbol) != 0)
                                        {
                                            flagSymbolCondition = false;
                                        }
                                        else
                                        {
                                            flagSymbolCondition = true;
                                        }

                                        if (symbolfilter.expiry != null && symbolfilter.expiry != "")
                                        {
                                            // MessageBox.Show(feed.expiry + " not equal " + symbolfilter.expiry);
                                            //Console.WriteLine(feed.expiry + " and " + symbolfilter.expiry);
                                            //DateTime dt1 = DateTime.ParseExact(symbolfilter.expiry, "dd-MM-yyyy", CultureInfo.InvariantCulture);

                                            if (string.Compare(feed.expiry, symbolfilter.expiry) != 0)
                                            {
                                                flagExpiryCondition = false;
                                            }
                                            else
                                            {
                                                flagExpiryCondition = true;
                                            }
                                        }

                                        if (symbolfilter.exch != null && symbolfilter.exch != "" && string.Compare(feed.exch, symbolfilter.exch) != 0)
                                        {
                                            flagExchCondition = false;
                                        }
                                        else
                                        {
                                            flagExchCondition = true;
                                        }

                                        if (flagExchCondition == true)
                                        {
                                            if (symbolfilter.strike != null && symbolfilter.strike != "" && string.Compare(feed.strike, symbolfilter.strike) != 0)
                                            {
                                                flagStrikeCondition = false;
                                            }
                                            else
                                            {
                                                flagStrikeCondition = true;
                                            }
                                        }

                                        double closePrice = Convert.ToDouble(feed.closePrice);
                                        double ltp        = Convert.ToDouble(feed.ltp);
                                        int    quantity   = Convert.ToInt32(feed.quantity);

                                        if (symbolfilter.closePrice != 0 && closePrice < symbolfilter.closePrice)
                                        {
                                            flagClosePriceCondition = false;
                                        }
                                        else
                                        {
                                            flagClosePriceCondition = true;
                                        }

                                        if (symbolfilter.ltp != 0 && ltp < symbolfilter.ltp)
                                        {
                                            flagLtpCondition = false;
                                        }
                                        else
                                        {
                                            flagLtpCondition = true;
                                        }

                                        if (symbolfilter.quantity != 0 && quantity < symbolfilter.quantity)
                                        {
                                            flagQuantityCondition = false;
                                        }
                                        else
                                        {
                                            flagQuantityCondition = true;
                                        }

                                        if (flagSymbolCondition && flagExpiryCondition && flagExchCondition &&
                                            flagStrikeCondition && flagClosePriceCondition &&
                                            flagLtpCondition && flagQuantityCondition)
                                        {
                                            ScannerBox.qfeed.Enqueue(feed);

                                            string key   = feed.symbol + "," + feed.expiry + "," + feed.strike + "," + feed.callput + "," + feed.exch;
                                            string value = feed.feedtime + "," + feed.closePrice + "," + feed.ltp + "," + feed.quantity + "," + feed.openRate + "," + feed.highRate + "," + feed.lowRate;

                                            foundKey             = true;
                                            dictFeedDetails[key] = value;
                                        }
                                    }
                                }

                                //check if it needs to be removed from the hashmap and the data table
                                if (foundKey == false)
                                {
                                    var exisiting = ScannerBox.dtFeed.Rows.Find(new Object[] { feed.symbol, feed.expiry, feed.strike, feed.callput, feed.exch });
                                    if (exisiting != null)
                                    {
                                        exisiting.Delete();
                                    }
                                }
                                //Console.WriteLine(messageReceived);
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                    }
                }
            }
            finally
            {
                NetMQConfig.Cleanup();
            }
        }