private void beginTradingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Create three stocks and add them to the market
            Subject = new RealTimedata();
            Subject.clearCompanies();
            // In this lab assignment we will add three companies only using the following format:
            // Company symbol , Company name , Open price

            this.watchToolStripMenuItem.Visible        = true;
            this.ordersToolStripMenuItem.Visible       = true;
            this.beginTradingToolStripMenuItem.Enabled = false;
            this.stopTradingToolStripMenuItem.Enabled  = true;
            this.marketToolStripMenuItem.Text          = "&Join<<Connected>>";

            this.joinServer();
            this.ListCompanies();

            MarketDepthSubMenu(this.marketByOrderToolStripMenuItem1);
            MarketDepthSubMenu(this.marketByPriceToolStripMenuItem1);



            //var companies = JsonConvert.DeserializeObject(response);

            Console.WriteLine(response);
        }
Beispiel #2
0
 public Company(String symbol, String _name, double openPrice, RealTimedata handledBy)
 {
     this._symbol       = symbol;
     this._openPrice    = openPrice;
     this._closePrice   = 0;
     this._currentPrice = 0;
     this.market        = handledBy;
     this._name         = _name;
 }
        public PlaceBidOrder(Object _subject)
        {
            Subject = (RealTimedata)_subject;

            InitializeComponent();
            //this.comboBox1.Items.Add("");
            foreach (Company company in Subject.getCompanies())
            {
                this.comboBox1.Items.Add(company.Name);
            }
            comboBox1.SelectedIndex = 0;
        }
        public PlaceSellOrder(Object _subject, TcpClient client, ref int CSeq, int SessionNum)
        {
            Subject         = (RealTimedata)_subject;
            this.client     = client;
            this.CSeq       = CSeq;
            this.sessionNum = SessionNum;

            InitializeComponent();

            foreach (Company company in Subject.getCompanies())
            {
                this.comboBox1.Items.Add(company.Name);
            }
            comboBox1.SelectedIndex = 0;
        }
Beispiel #5
0
        private void beginTradingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Create three stocks and add them to the market
            Subject = new RealTimedata();

            control.openConnection(_userName, _serverIP, _serverPort);
            string res = control.Register(_userName, _clientPort);

            var split = res.Split('/');

            MessageBox.Show(res);
            // Add all companies to the list for the first time
            Subject.addCompany(split[0], split[1], double.Parse(split[2]));
            Subject.addCompany(split[3], split[4], double.Parse(split[5]));
            Subject.addCompany(split[6], split[7], double.Parse(split[8]));
            // In this lab assignment we will add three companies only using the following format:
            // Company symbol , Company name , Open price
            //Subject.addCompany("MSFT", "Microsoft Corporation", 46.13);
            //Subject.addCompany("AAPL", "Apple Inc.", 105.22);
            //Subject.addCompany("FB", "Facebook, Inc.", 80.67);

            this.watchToolStripMenuItem.Visible        = true;
            this.ordersToolStripMenuItem.Visible       = true;
            this.beginTradingToolStripMenuItem.Enabled = false;

            //Create a Tcp Listener
            IPAddress localAddr = IPAddress.Parse(_clientIP);

            server = new TcpListener(localAddr, _clientPort);

            server.Start();

            //Openconnection(_userName, _serverIP, _serverPort, _clientIP, _clientPort);

            this.stopTradingToolStripMenuItem.Enabled = true;
            this.marketToolStripMenuItem.Text         = "&Join <<Connected>>";

            MarketDepthSubMenu(this.marketByOrderToolStripMenuItem1);
            MarketDepthSubMenu(this.marketByPriceToolStripMenuItem1);
        }
Beispiel #6
0
        private void beginTradingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Create three stocks and add them to the market
            Subject = new RealTimedata();

            // In this lab assignment we will add three companies only using the following format:
            // Company symbol , Company name , Open price
            Subject.addCompany("MSFT", "Microsoft Corporation", 46.13);
            Subject.addCompany("AAPL", "Apple Inc.", 105.22);
            Subject.addCompany("FB", "Facebook, Inc.", 80.67);

            this.watchToolStripMenuItem.Visible        = true;
            this.ordersToolStripMenuItem.Visible       = true;
            this.beginTradingToolStripMenuItem.Enabled = false;
            this.marketToolStripMenuItem.Text          = "&Join <<Connected>>";

            clientSocket.Connect(txtServerIP.Text, Convert.ToInt32(txtServerPort.Text));
            System.Net.Sockets.NetworkStream netStream = clientSocket.GetStream();
            Byte[] sendRegister = Encoding.UTF8.GetBytes("REGISTER/" + txtUsername.Text + "/" + txtClientIP.Text + "/" + txtClientPort.Text);
            netStream.Write(sendRegister, 0, sendRegister.Length);

            MarketDepthSubMenu(this.marketByOrderToolStripMenuItem1);
            MarketDepthSubMenu(this.marketByPriceToolStripMenuItem1);
        }
Beispiel #7
0
        private void beginTradingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //connect to server
            //if its already opened print error
            if (client == null)
            {
                client = new TcpClient();
                //IPAddress ipAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
                //IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, Convert.ToInt32(this.clientPortToolStripMenuItem.Text));
                //client = new TcpClient(ipLocalEndPoint);
                try
                {//try to connect
                    client.Connect(this.serverIPToolStripMenuItem.Text, Convert.ToInt32(this.serverPortToolStripMenuItem.Text));
                }
                catch (Exception err)//catch error
                {
                    client = null;
                    Console.Write(err.Message);
                }
            }
            //send register
            CSeq = rnd.Next();
            string        msg      = "register SME/TCP-1.0\nID: " + this.userNameToolStripMenuItem.Text.ToString() + " CSeq: " + CSeq++ + " Notification Port: ";
            NetworkStream nwstream = client.GetStream();

            byte[] tosend = ASCIIEncoding.ASCII.GetBytes(msg);
            nwstream.Write(tosend, 0, tosend.Length);

            //read msg
            byte[] read      = new byte[client.ReceiveBufferSize];
            int    bytesread = nwstream.Read(read, 0, client.ReceiveBufferSize);
            string decode    = Encoding.ASCII.GetString(read, 0, bytesread);

            string[] message = decode.Split(' ');
            sessionNum = Convert.ToInt32(message[5]);

            //byte[] read1 = new byte[client.ReceiveBufferSize];
            //int bytesread1 = nwstream.Read(read1, 0, client.ReceiveBufferSize);
            //string decode1 = Encoding.ASCII.GetString(read1, 0, bytesread1);
            //string[] message2 = decode1.Split(' ');

            //sanaty check
            if (message[1] != "OK")
            {
                client.Close();
            }
            else    //no error
            {
                this.beginTradingToolStripMenuItem.Enabled = false;
                this.stopTradingToolStripMenuItem.Enabled  = true;

                string companymsg = "listCompanies SME/TCP-1.0\nCSeq: " + CSeq++ + " Session: " + sessionNum;
                tosend = ASCIIEncoding.ASCII.GetBytes(companymsg);
                nwstream.Write(tosend, 0, tosend.Length);


                bytesread = nwstream.Read(read, 0, client.ReceiveBufferSize);
                decode    = Encoding.ASCII.GetString(read, 0, bytesread);
                message   = decode.Split(new string[] { "Data: " }, StringSplitOptions.None);

                // Create three stocks and add them to the market
                Subject = new RealTimedata(message[1], client, ref CSeq, sessionNum);



                this.watchToolStripMenuItem.Visible        = true;
                this.ordersToolStripMenuItem.Visible       = true;
                this.beginTradingToolStripMenuItem.Enabled = false;
                this.marketToolStripMenuItem.Text          = "Join <<Connected>>";
                this.userNameToolStripMenuItem.Enabled     = false;
                this.clientIPToolStripMenuItem.Enabled     = false;
                this.clientPortToolStripMenuItem.Enabled   = false;
                this.serverIPToolStripMenuItem.Enabled     = false;
                this.serverPortToolStripMenuItem.Enabled   = false;


                MarketDepthSubMenu(this.marketByOrderToolStripMenuItem1);
                MarketDepthSubMenu(this.marketByPriceToolStripMenuItem1);


                //get sellorder, buyorder from the company

                companymsg = "listBuyOrders SME/TCP-1.0\nCSeq: " + CSeq++ + " Session: " + sessionNum;
                tosend     = ASCIIEncoding.ASCII.GetBytes(companymsg);
                nwstream.Write(tosend, 0, tosend.Length);

                bytesread = nwstream.Read(read, 0, client.ReceiveBufferSize);
                decode    = Encoding.ASCII.GetString(read, 0, bytesread);
                message   = decode.Split(new string[] { "Data: " }, StringSplitOptions.None);

                JObject        jObject = JObject.Parse(message[1]);
                IList <JToken> BMSFT   = jObject["MSFT"].Children().ToList();
                IList <JToken> BFB     = jObject["FB"].Children().ToList();
                IList <JToken> BAAPL   = jObject["AAPL"].Children().ToList();


                foreach (Company company in Subject.getCompanies())
                {
                    if (company.Symbol == "MSFT")
                    {
                        foreach (JToken result in BMSFT)
                        {
                            int      q  = Convert.ToInt32(((string)result["size"]));
                            double   p  = Convert.ToDouble(((string)result["price"]));
                            DateTime dt = Convert.ToDateTime(((string)result["closedPrice"]));
                            company.addBuyOrder(p, q, dt);
                        }
                    }
                    if (company.Symbol == "FB")
                    {
                        foreach (JToken result in BFB)
                        {
                            int      q  = Convert.ToInt32(((string)result["size"]));
                            double   p  = Convert.ToDouble(((string)result["price"]));
                            DateTime dt = Convert.ToDateTime(((string)result["closedPrice"]));
                            company.addBuyOrder(p, q, dt);
                        }
                    }
                    if (company.Symbol == "AAPL")
                    {
                        foreach (JToken result in BAAPL)
                        {
                            int      q  = Convert.ToInt32(((string)result["size"]));
                            double   p  = Convert.ToDouble(((string)result["price"]));
                            DateTime dt = Convert.ToDateTime(((string)result["closedPrice"]));
                            company.addBuyOrder(p, q, dt);
                        }
                    }
                }



                companymsg = "listSellOrders SME/TCP-1.0\nCSeq: " + CSeq++ + " Session: " + sessionNum;
                tosend     = ASCIIEncoding.ASCII.GetBytes(companymsg);
                nwstream.Write(tosend, 0, tosend.Length);

                bytesread = nwstream.Read(read, 0, client.ReceiveBufferSize);
                decode    = Encoding.ASCII.GetString(read, 0, bytesread);
                message   = decode.Split(new string[] { "Data: " }, StringSplitOptions.None);

                jObject = JObject.Parse(message[1]);
                IList <JToken> SMSFT = jObject["MSFT"].Children().ToList();
                IList <JToken> SFB   = jObject["FB"].Children().ToList();
                IList <JToken> SAAPL = jObject["AAPL"].Children().ToList();


                foreach (Company company in Subject.getCompanies())
                {
                    if (company.Symbol == "MSFT")
                    {
                        foreach (JToken result in SMSFT)
                        {
                            int      q  = Convert.ToInt32(((string)result["size"]));
                            double   p  = Convert.ToDouble(((string)result["price"]));
                            DateTime dt = Convert.ToDateTime(((string)result["closedPrice"]));
                            company.addSellOrder(p, q, dt);
                        }
                    }
                    if (company.Symbol == "FB")
                    {
                        foreach (JToken result in SFB)
                        {
                            int      q  = Convert.ToInt32(((string)result["size"]));
                            double   p  = Convert.ToDouble(((string)result["price"]));
                            DateTime dt = Convert.ToDateTime(((string)result["closedPrice"]));
                            company.addSellOrder(p, q, dt);
                        }
                    }
                    if (company.Symbol == "AAPL")
                    {
                        foreach (JToken result in SAAPL)
                        {
                            int      q  = Convert.ToInt32(((string)result["size"]));
                            double   p  = Convert.ToDouble(((string)result["price"]));
                            DateTime dt = Convert.ToDateTime(((string)result["closedPrice"]));
                            company.addSellOrder(p, q, dt);
                        }
                    }
                }

                // thread out to recieve update from other user
                Thread listener = new Thread(new ThreadStart(listentoServer));
                //listener.IsBackground = true;
                listener.Start();
            }
        }
        private void beginTradingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StartNetworkManager();
            //new Thread(new ThreadStart(StartNetworkManager)).Start();
            // Create three stocks and add them to the market
            Subject = new RealTimedata(tcpClient, clientID.Text, session);

            // In this lab assignment we will add three companies only using the following format:
            // Company symbol , Company name , Open price

            //SEND DATA REQUEST
            SMERequest smeRequest = new SMERequest("SME/TCP-1.0", "LIST COMPANIES", 700, name, session);
            string     request    = JsonConvert.SerializeObject(smeRequest, Formatting.Indented);

            Console.WriteLine(request);
            Byte[] data = System.Text.Encoding.ASCII.GetBytes(request);
            ioStream.Write(data, 0, data.Length);
            ioStream.Flush();
            //GET RESPONSE AND UPDATE DATA
            var           _data    = new byte[256];
            StringBuilder response = new StringBuilder();

            do
            {
                var numBytesRead = ioStream.Read(_data, 0, _data.Length);
                response.AppendFormat("{0}", Encoding.ASCII.GetString(_data, 0, numBytesRead));
            } while (ioStream.DataAvailable);
            string fullMessage = response.ToString();
            //IN CASE OF DATA IS LONGER THAN DATA LENGTH
            // String to store the response ASCII representation.
            JObject json      = JObject.Parse(fullMessage);
            JObject Data      = JObject.Parse((string)json["Data"]);
            JArray  companies = (JArray)Data["stockCompanies"];

            foreach (JToken company in companies)
            {
                Company newCompany = Subject.addCompany((string)company["symbol"], (string)company["name"], (double)(company["openPrice"]));
                newCompany.lastSale = (double)company["currentPrice"];
                JArray buyOrders    = (JArray)company["buyOrders"];
                JArray sellOrders   = (JArray)company["sellOrders"];
                JArray transactions = (JArray)company["transactions"];
                foreach (JToken order in buyOrders)
                {
                    newCompany.BuyOrders.Add(new BuyOrder((double)order["price"], (int)order["size"]));
                }
                foreach (JToken order in sellOrders)
                {
                    newCompany.SellOrders.Add(new SellOrder((double)order["price"], (int)order["size"]));
                }
                foreach (JToken order in transactions)
                {
                    newCompany.Transactions.Add(new SellOrder((double)order["price"], (int)order["size"]));
                }
            }

            this.watchToolStripMenuItem.Visible        = true;
            this.ordersToolStripMenuItem.Visible       = true;
            this.beginTradingToolStripMenuItem.Enabled = false;
            this.marketToolStripMenuItem.Text          = "&Market <<Open>>";

            MarketDepthSubMenu(this.marketByOrderToolStripMenuItem1);
            MarketDepthSubMenu(this.marketByPriceToolStripMenuItem1);
        }