Ejemplo n.º 1
0
        public void TestBuySellActions()
        {
            bool error = false;

            try
            {
                MarketClientClass client = new MarketClientClass();

                MarketBuySell buyreq1 = client.SendBuyRequest(1, 1, 3); //legal req
                Assert.IsNull(buyreq1.Error);                           //null expected
                client.SendCancelBuySellRequest(buyreq1.Id);


                MarketBuySell sellreq1 = client.SendBuyRequest(1, 1, 3); //legal req
                Assert.IsNull(sellreq1.Error);                           //null expected
                client.SendCancelBuySellRequest(sellreq1.Id);

                //no errors expected ( null)
            }

            catch (Exception)
            {
                error = true;
            }

            Assert.IsFalse(error);                       //false expected
        }
Ejemplo n.º 2
0
        }//AMAbuy

        public static void AMA_Sell(int commodity, int desiredPrice, int amount)
        {
            FLAG_isRunning = true;
            NotOverLoadServer();

            MarketClientClass client = new MarketClientClass();
            MarketUserData userData = client.SendQueryUserRequest();
            NotOverLoadServer();

            if (userData.Error != null)
            {
                FLAG_isRunning = false;
                return;
            }

            foreach (int cmdty in userData.Commodities.Keys)    //passing on all commodities
            {
                if (cmdty == commodity && userData.Commodities[cmdty] > 0)           //check if we own that commodity
                {
                    //if item is the right commodity & we own it
                    if (amount > userData.Commodities[cmdty] || amount == -1)                //we cant sell more than we have OR -1 is our sign to sell ALL
                        amount = userData.Commodities[cmdty];

                    MarketBuySell sellreq = client.SendSellRequest(desiredPrice, commodity, amount);
                    NotOverLoadServer();

                    if (sellreq.Error == null)        //the sell req is successfuly passed to the server
                        HistoryLogger.WriteHistory(sellreq.Id, "Sell", commodity, desiredPrice, amount);
                }
            }
            FLAG_isRunning = false;
            return;
        }//AMAsell
Ejemplo n.º 3
0
        public void TestAMAsell()
        {
            bool   e            = false;
            Random rnd          = new Random();
            int    rndCommodity = rnd.Next(0, 10);

            int EXPECTEDcommAmount = 0;
            int ACTUALcommAmount   = 0;


            MarketClientClass    client        = new MarketClientClass();
            MarketUserData       userData      = client.SendQueryUserRequest();
            MarketCommodityOffer commodityInfo = client.SendQueryMarketRequest(rndCommodity);

            if (userData.Error != null | commodityInfo.Error != null)   //is NOT successfuly passed to the server
            {
                return;
            }


            foreach (int cmdty in userData.Commodities.Keys)        //passing on all commodities  .
            {
                if (cmdty == rndCommodity)
                {
                    EXPECTEDcommAmount = userData.Commodities[cmdty];      //checking how many we own from rndCommodity
                }
            }


            //we are selling 1
            AMA.AMA_Sell(commodityInfo.Bid, rndCommodity, 1);

            userData = client.SendQueryUserRequest();        //refresh userData

            foreach (int cmdty in userData.Commodities.Keys) //passing on all commodities
            {
                if (cmdty == rndCommodity)
                {
                    ACTUALcommAmount = userData.Commodities[cmdty];
                }
            }

            if (EXPECTEDcommAmount == ACTUALcommAmount)
            {
                e = !e;
            }



            if (e | EXPECTEDcommAmount == 0)
            {
                Assert.AreEqual <int>(EXPECTEDcommAmount, ACTUALcommAmount);
            }

            else
            {
                Assert.AreNotEqual <int>(EXPECTEDcommAmount, ACTUALcommAmount);
            }
        }
Ejemplo n.º 4
0
        public static void AMA_Buy(int commodity, int desiredPrice, int amount)
        {
            FLAG_isRunning = true;
            notOverLoadServer();
            MarketClientClass client = new MarketClientClass();
            AllMarketRequest  all    = client.QueryAllMarketRequest();

            counter++;


            foreach (ItemAskBid item in all.MarketInfo)
            {
                if (item.Id == commodity && item.Info.Ask <= desiredPrice)
                {   //if item is the right commodity & right price
                    MarketUserData userData = client.SendQueryUserRequest();
                    counter++;

                    List <int> l = userData.Requests;

                    if (l.Count != 0)                      //there are open requests in server

                    //if USER dont have enough money, we'll cancel his open buy requests- hoping after that he'll have enough
                    {
                        for (int i = l.Count; i >= 0 & userData.Funds < (item.Info.Ask * amount); i--)   //going from end so in delete won't change index of l
                        {
                            notOverLoadServer();

                            int reqID = l[i];        //saving the ID just for simplicity

                            MarketItemQuery request = client.SendQueryBuySellRequest(l[i]);
                            counter++;
                            if (request.Type.Equals("buy"))    //Note: check with roey
                            {
                                client.SendCancelBuySellRequest(reqID);
                                HistoryLogger.WriteHistory("Cancel," + request.Commodity + "," + request.Price + "," + request.Amount + "," + reqID);
                                counter++;
                            }
                        }
                    }

                    if (userData.Funds >= item.Info.Ask * amount)
                    {
                        int ID = client.SendBuyRequest(item.Info.Ask + 1, commodity, amount).Id;
                        HistoryLogger.WriteHistory("Buy," + commodity + "," + (item.Info.Ask + 1) + "," + amount + "," + ID);
                        counter++;
                    }
                }//bigIf
            }
            FLAG_isRunning = false;
            return;
        }//AMAbuy
Ejemplo n.º 5
0
        public void TestFalsePrice()
        {
            MarketClientClass client = new MarketClientClass();

            MarketBuySell sellreq1 = client.SendSellRequest(2000000, 2, 1);  //no such price : 2,000,000

            Assert.IsNotNull(sellreq1.Error);

            MarketBuySell sellreq2 = client.SendSellRequest(-25, 5, 3);   //no such price : -25

            Assert.IsNotNull(sellreq2.Error);

            //Errors expected (not null)
        }
Ejemplo n.º 6
0
        public void TestSendBuyRequest()
        {
            MarketUserData user = new MarketClientClass().SendQueryUserRequest();

            if (user.Funds <= 0)
            {
                Assert.Fail();
                return;
            }
            MarketBuySell ans = new MarketClientClass().SendBuyRequest(1, 0, 1);

            Assert.IsNull(ans.Error);
            new MarketClientClass().SendCancelBuySellRequest(ans.Id);
        }
Ejemplo n.º 7
0
        public void TestSendSellRequest()
        {
            MarketUserData user = new MarketClientClass().SendQueryUserRequest();

            for (int i = 0; i < user.Commodities.Count; i++)
            {
                if (user.Commodities[i] > 0)
                {
                    MarketBuySell ans = new MarketClientClass().SendSellRequest(99, i, 1);
                    Assert.IsNull(ans.Error);
                    new MarketClientClass().SendCancelBuySellRequest(ans.Id);
                    break;
                }
            }
        }
Ejemplo n.º 8
0
        public void TestFalseCommodity()
        {
            MarketClientClass client = new MarketClientClass();

            MarketBuySell buyreq1 = client.SendBuyRequest(5, -2, 3);   //no such commodies : -2

            Assert.IsNotNull(buyreq1.Error);

            MarketBuySell buyreq2 = client.SendBuyRequest(1, 10, 3);    //no such commodies : 10

            Assert.IsNotNull(buyreq2.Error);

            MarketBuySell sellreq3 = client.SendSellRequest(7, 24, 3);   //no such commodies : 24

            Assert.IsNotNull(sellreq3.Error);

            //Errors expected (not null)
        }
Ejemplo n.º 9
0
        public void TestFalseAmount()
        {
            MarketClientClass client = new MarketClientClass();

            MarketBuySell sellreq1 = client.SendSellRequest(1, 2, -8);   //no such amount : -8

            Assert.IsNotNull(sellreq1.Error);


            MarketBuySell sellreq2 = client.SendSellRequest(4, 4, -3);   //no such amount : -3

            Assert.IsNotNull(sellreq2.Error);


            MarketBuySell sellreq3 = client.SendSellRequest(2, 7, -100);   //no such amount : -100

            Assert.IsNotNull(sellreq3.Error);

            //Errors expected (not null)
        }
Ejemplo n.º 10
0
        }//AMAbuy

        public static void AMA_Sell(int commodity, int desiredPrice, int amount)
        {
            FLAG_isRunning = true;
            notOverLoadServer();


            MarketClientClass client = new MarketClientClass();
            AllMarketRequest  all    = client.QueryAllMarketRequest();

            counter++;

            MarketUserData userData = client.SendQueryUserRequest();

            counter++;

            foreach (int cmdty in userData.Commodities.Keys)      //check if we own that commodity
            {
                if (cmdty == commodity & userData.Commodities[cmdty] > 0)
                {
                    //passing on commodities list, until arriving the wished one
                    foreach (ItemAskBid item in all.MarketInfo)
                    {
                        if (item.Id == commodity && item.Info.Bid >= desiredPrice)
                        {                                                            //if item is the right commodity & right price
                            if (amount > userData.Commodities[cmdty] | amount == -1) //we cant sell more than we have OR -1 is our sign to sell ALL
                            {
                                amount = userData.Commodities[cmdty];
                            }

                            //Note: ask roey about error
                            int ID = client.SendSellRequest(item.Info.Bid - 1, commodity, amount).Id;
                            HistoryLogger.WriteHistory("Sell," + commodity + "," + (item.Info.Bid - 1) + "," + amount + "," + ID);
                            counter++;
                        }
                    }
                }
            }

            FLAG_isRunning = false;
            return;
        }//AMAsell
Ejemplo n.º 11
0
        private static void OnAMAEvent(object sender, EventArgs e)
        {
            if (!FLAG_isRunning)                     //for not creating lot of AMA functions running in parallel
            {

                Trace.WriteLine("AAAAAAMMMMMMAAAAAA");
                
                using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["historyConnectionString"].ConnectionString))
                {
                    Random rnd = new Random();
                    int avgPrice=0;
                    int rndCommodity = rnd.Next(1, 10);
                    int amountToBuy = rnd.Next(4, 10);
                    int amountToSell = rnd.Next(-1, 8);           //-1 means all


                    if (amountToSell == 0)
                        amountToSell = -1;

                    myConnection.Open();
                    SqlCommand myCommand = new SqlCommand("select Avg(price) AS AveragePrice from items where commodity='" + rndCommodity + "' and timestamp>= DATEADD(mi, -30, GETUTCDATE())", myConnection);
                    SqlDataReader myDataReader = myCommand.ExecuteReader();
                    if (myDataReader.HasRows)

                    {
                        myDataReader.Read();
                        NotOverLoadServer();
                        try
                        {
                            Trace.WriteLine(myDataReader[0].ToString());
                            Trace.WriteLine(Double.Parse(myDataReader[0].ToString()));
                            Double avgPriceDouble = Double.Parse(myDataReader[0].ToString());
                            avgPrice = Convert.ToInt32(avgPriceDouble);
                        }
                        catch
                        {
                            return;
                        }
                        //want to get INFO for the commodity ASK-BID
                        MarketClientClass client = new MarketClientClass();
                        MarketCommodityOffer commodityInfo = client.SendQueryMarketRequest(rndCommodity);
                        NotOverLoadServer();

                        if (commodityInfo.Error != null)
                            return;

                        //choose alternately buy or sell. and choose randomly commodity
                        if (FLAG_buyOrSell)
                        {    //ama buy
                            if (avgPrice - 15 < 0)
                            {
                                avgPrice = rnd.Next(4, 9);
                                AMA_Buy(rndCommodity, avgPrice, amountToBuy);
                            }
                            else
                            {
                                avgPrice -= 15;
                                AMA_Buy(rndCommodity, avgPrice, amountToBuy);
                            }
                        }
                        else
                        {     //ama sell  
                            avgPrice += 4;
                            AMA_Sell(rndCommodity, avgPrice , amountToSell);
                        }
                        FLAG_buyOrSell = !FLAG_buyOrSell;
                    }
                }
            }
        }
Ejemplo n.º 12
0
        public static void AMA_Buy(int commodity, int desiredPrice, int amount)
        {
            FLAG_isRunning = true;
            NotOverLoadServer();

            MarketClientClass client = new MarketClientClass();
            MarketUserData userData = client.SendQueryUserRequest();
            NotOverLoadServer();

            if (userData.Error != null)
            {
                FLAG_isRunning = false;
                return;
            }

            if (userData.Funds >= desiredPrice * amount)    //if we have enough money- just buy and finish running.
            {
                MarketBuySell buyreq = client.SendBuyRequest(desiredPrice, commodity, amount);
                NotOverLoadServer();

                if (buyreq.Error == null)          //the buy req is successfuly passed to the server
                    HistoryLogger.WriteHistory(buyreq.Id, "Buy", commodity, desiredPrice, amount);
                FLAG_isRunning = false;
                return;
            }

            //if USER dont have enough money, we'll cancel his open buy requests- hoping after that he'll have enough
            List<int> l = userData.Requests;

            if (l.Count == 0)               //there are NO open requests in server
            {
                FLAG_isRunning = false;
                return;
            }

            for (int i = l.Count - 1; i >= 0 && userData.Funds < (desiredPrice * amount); i--)   //going from end so in delete won't change index of l
            {
                int reqID = l[i];    //saving the ID just for simplicity

                MarketItemQuery request = client.SendQueryBuySellRequest(reqID);
                NotOverLoadServer();

                if (request.Error != null)
                {
                    FLAG_isRunning = false;
                    return;
                }

                //wish to cancel only buy requests. only this kind of canceling request give back money
                //func SendCancelBuySellRequest returns bool - of the action passed successfuly
                if (request.Type.Equals("buy") && client.SendCancelBuySellRequest(reqID))
                    HistoryLogger.WriteHistory(reqID, "Cancel", request.Commodity, request.Price, request.Amount);

                NotOverLoadServer();
            }

            userData = client.SendQueryUserRequest();   //refresh data
            NotOverLoadServer();

            if (userData.Error != null)
            {
                FLAG_isRunning = false;
                return;
            }

            if (userData.Funds >= desiredPrice * amount)    //if NOW we have enough money-  buy 
            {
                MarketBuySell buyreq = client.SendBuyRequest(desiredPrice, commodity, amount);
                NotOverLoadServer();

                if (buyreq.Error == null)          //the buy req is successfuly passed to the server
                    HistoryLogger.WriteHistory(buyreq.Id, "Buy", commodity, desiredPrice, amount);
            }
            FLAG_isRunning = false;
            return;
        }//AMAbuy