private void ButtonAllCancel_Click(object sender, RoutedEventArgs e) { myLogger.Info("Cancel All Request's button was pressed"); try { MarketUserData MUD = new MarketUserData(); MarketUserData AllReq = MUD.SendQueryUserRequest(); if (AllReq.requests.Count != 0) { foreach (int id in AllReq.requests) { System.Threading.Thread.Sleep(300); // delay the function on 0.8 sec to prevent ban from the server int IdSent = Convert.ToInt32(id); MarketItemQuery MIQ = new MarketItemQuery(); MarketItemQuery output2 = MIQ.SendQueryBuySellRequest(IdSent); CancelBuySellRequest CBSR = new CancelBuySellRequest(); bool output = CBSR.SendCancelBuySellRequest(IdSent); if (output) { EnterHistory("Cancel", output2.commodity, output2.amount, output2.price, IdSent, false); } } } MessageBox.Show("All requests was delete"); } catch (Exception e7) { Console.WriteLine(e7.ToString()); } }
}//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
}//collect info cancel request private static void CollectInfoQueryRequst(string a) { switch (a) { case "1": //buysellquery int id; Console.WriteLine("Please enter the ID request\n"); do { id = Myconvert(Console.ReadLine()); //force the user to give legal id (only nums) }while (id == -1); MarketItemQuery data1 = client.SendQueryBuySellRequest(id); //call to Logic layer func Console.WriteLine(data1.ToString()); //print data on a certain deal break; case "2": //user query MarketUserData data2 = client.SendQueryUserRequest(); Console.WriteLine(data2.ToString()); break; case "3": //market query int commodity; Console.WriteLine("Please enter the Commodity\n"); do { commodity = Myconvert(Console.ReadLine()); //force the user to give legal commidity (only nums) }while (commodity == -1); MarketCommodityOffer data3 = client.SendQueryMarketRequest(commodity); //call to Logic layer func Console.WriteLine(data3.ToString()); break; } //switch return; } //collect info query
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); } }
public void initial() { market = new RequestManager(); rq = new RequestAgent(); userInfo = (MarketUserData)market.SendQueryUserRequest(); sql = new SQLmanager(); ama = new AutonomousMarketAgent(); }
private void ButtonMState_Click(object sender, RoutedEventArgs e) { myLogger.Info("Market State button was pressed"); MarketUserData MUD = new MarketUserData(); MarketUserData output = MUD.SendQueryUserRequest(); MessageBox.Show(output.ToStringA()); myLogger.Info("the request to know about user's market state is done"); }
private void PresentData() { global::Program.MarketClient marketClient = new global::Program.MarketClient(); MarketUserData marketUserData = (MarketUserData)marketClient.SendQueryUserRequest(); try { // Set all to visible this.DataGridCommodities.Visibility = Visibility.Visible; this.DataGridRequests.Visibility = Visibility.Visible; this.LabelCommodities.Visibility = Visibility.Visible; this.LabelRequests.Visibility = Visibility.Visible; this.LabelFunds.Visibility = Visibility.Visible; // Set the commodities section this.DataGridCommodities.ItemsSource = marketUserData.commodities; if (DataGridCommodities.Columns.Count > 1) { this.DataGridCommodities.Columns[0].Header = "Commodity"; this.DataGridCommodities.Columns[1].Header = "Quantity"; } // Set the requests section this.LabelRequests.Content = "Requests"; if (marketUserData.requests.Length > 0) { this.DataGridRequests.Visibility = Visibility.Visible; this.DataGridRequests.ItemsSource = marketUserData.requests .Select((x, index) => new { Number = index + 1, Request = x }) .ToList(); } else { this.DataGridRequests.Visibility = Visibility.Hidden; this.LabelRequests.Content += "\n\n No requests."; } this.LabelHeader.Content = "Refreshed: " + DateTime.Now.ToLongTimeString(); // Set the funds section this.LabelFunds.Content = "Funds: " + marketUserData.funds + " $"; } catch { // Set all to hidden this.DataGridCommodities.Visibility = Visibility.Hidden; this.DataGridRequests.Visibility = Visibility.Hidden; this.LabelCommodities.Visibility = Visibility.Hidden; this.LabelRequests.Visibility = Visibility.Hidden; this.LabelFunds.Visibility = Visibility.Hidden; this.LabelHeader.Content = "No connection to server. Refreshed: " + DateTime.Now.ToLongTimeString(); } }
public IMarketUserData SendQueryUserRequest() { SimpleHTTPClient client = new SimpleHTTPClient(); string token = SimpleCtyptoLibrary.CreateToken("user52", key); var item = new MarketItems.QueryUserRequest(); item.type = "queryUser"; mainLog.Info("Send Query User Request to the server. information: " + item.ToString()); MarketUserData output = client.SendPostRequest <QueryUserRequest, MarketUserData>("http://ise172.ise.bgu.ac.il", "user52", token, item); mainLog.Info("return answere from the server after Send Query User Request: " + output); return(output); }
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
}//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
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
private void onTimedEvent(Object source, ElapsedEventArgs e) { sql.close(); sql.connect(); MarketUserData MYUSER = new MarketUserData(); MarketUserData myUser = MYUSER.SendQueryUserRequest(); AllMarketCommodityOffer MACO = new AllMarketCommodityOffer(); List <ComInfo> cominfo = MACO.SendQueryAllMarketRequest(); SqlDataReader rdr = sql.sendCommand("SELECT top 100 commodity, AVG(price) as 'average' FROM (SELECT commodity, price, timestamp FROM items where buyer = 20) commodity group by commodity order by commodity"); int counterS = 0; string Scom = ""; while (counterS < 10 && rdr.Read()) { Scom = "" + rdr["commodity"]; if (Convert.ToInt32(Scom) > 3 && myUser.commodities[Scom] > 1) { SellRequest SR = new SellRequest(); int price = Math.Max(Convert.ToInt32(rdr["average"]) + Convert.ToInt32(rdr["average"]) * 20 / 100, cominfo[Convert.ToInt32(rdr["commodity"])].info.bid); int amount = Convert.ToInt32(myUser.commodities[Scom] / 20) + 1; int commodity = Convert.ToInt32(rdr["commodity"]); int response = SR.SendSellRequest(price, commodity, amount); if (response != -1) { DateTime date = DateTime.Now; string sdate = date.ToString(); HistoryItem h = new HistoryItem("Sell", response, Convert.ToInt32(myUser.commodities[Scom] / 20) + 1, price, Convert.ToInt32(rdr["commodity"]), true, sdate); // MessageBox.Show("SellDone");//for test App.Current.Dispatcher.Invoke((Action) delegate { mainWin.History.Insert(0, h); }); string path = "..\\History.txt"; using (StreamWriter wr = File.AppendText(path)) { wr.WriteLine("Request"); wr.WriteLine("sell"); wr.WriteLine(response); wr.WriteLine(Convert.ToInt32(myUser.commodities[Scom] / 20) + 1); wr.WriteLine(price); wr.WriteLine(Convert.ToInt32(rdr["commodity"])); wr.WriteLine("true"); wr.WriteLine(sdate.ToString()); } } counterS++; } } rdr.Close(); SqlDataReader rdr2 = sql.sendCommand("SELECT top 100 commodity, AVG(price) as 'average' FROM (SELECT commodity, price, timestamp FROM items where seller = 20) commodity group by commodity order by commodity"); int counterB = 0; while (counterB < 10 && rdr2.Read()) { Scom = "" + rdr2["commodity"]; if (Convert.ToInt32(Scom) > 3 && myUser.funds > 5000) { BuyRequest SB = new BuyRequest(); int price = Math.Min(Convert.ToInt32(rdr2["average"]) - Convert.ToInt32(rdr2["average"]) * 20 / 100, cominfo[Convert.ToInt32(rdr2["commodity"])].info.ask); int response = SB.sendBuyRequest(price, Convert.ToInt32(rdr2["commodity"]), Convert.ToInt32(myUser.commodities[Scom] / 20) + 1); if (response != -1) { DateTime date = DateTime.Now; string sdate = date.ToString(); HistoryItem h = new HistoryItem("Buy", response, Convert.ToInt32(myUser.commodities[Scom] / 20) + 1, price, Convert.ToInt32(rdr2["commodity"]), true, sdate); //MessageBox.Show("BuyDone");//for test App.Current.Dispatcher.Invoke((Action) delegate { mainWin.History.Insert(0, h); }); string path = "..\\History.txt"; using (StreamWriter wr = File.AppendText(path)) { wr.WriteLine("Request"); wr.WriteLine("buy"); wr.WriteLine(response); wr.WriteLine(Convert.ToInt32(myUser.commodities[Scom] / 10) + 1); wr.WriteLine(price); wr.WriteLine(Convert.ToInt32(rdr2["commodity"])); wr.WriteLine("true"); wr.WriteLine(sdate.ToString()); } } } counterB++; } rdr2.Close(); sql.close(); }
private void ShowColumnChart() { SqlData sql = new SqlData(); sql.connect(); //Graph of avarage of each commodity double[] valueAv = sql.averangeOnly(); List <KeyValuePair <string, double> > Avg = new List <KeyValuePair <string, double> >(); Avg.Add(new KeyValuePair <string, double>("C-0", valueAv[0])); Avg.Add(new KeyValuePair <string, double>("C-1", valueAv[1])); Avg.Add(new KeyValuePair <string, double>("C-2", valueAv[2])); Avg.Add(new KeyValuePair <string, double>("C-3", valueAv[3])); Avg.Add(new KeyValuePair <string, double>("C-4", valueAv[4])); Avg.Add(new KeyValuePair <string, double>("C-5", valueAv[5])); Avg.Add(new KeyValuePair <string, double>("C-6", valueAv[6])); Avg.Add(new KeyValuePair <string, double>("C-7", valueAv[7])); Avg.Add(new KeyValuePair <string, double>("C-8", valueAv[8])); Avg.Add(new KeyValuePair <string, double>("C-9", valueAv[9])); //Graph of Hot commodties double[] valueHo = sql.hotCommodities(); List <KeyValuePair <string, double> > HotCo = new List <KeyValuePair <string, double> >(); HotCo.Add(new KeyValuePair <string, double>("C-0", valueHo[0])); HotCo.Add(new KeyValuePair <string, double>("C-1", valueHo[1])); HotCo.Add(new KeyValuePair <string, double>("C-2", valueHo[2])); HotCo.Add(new KeyValuePair <string, double>("C-3", valueHo[3])); HotCo.Add(new KeyValuePair <string, double>("C-4", valueHo[4])); HotCo.Add(new KeyValuePair <string, double>("C-5", valueHo[5])); HotCo.Add(new KeyValuePair <string, double>("C-6", valueHo[6])); HotCo.Add(new KeyValuePair <string, double>("C-7", valueHo[7])); HotCo.Add(new KeyValuePair <string, double>("C-8", valueHo[8])); HotCo.Add(new KeyValuePair <string, double>("C-9", valueHo[9])); //Graph of each commodity with her ask price and bid price AllMarketCommodityOffer MACO = new AllMarketCommodityOffer(); List <ComInfo> AskBid = MACO.SendQueryAllMarketRequest(); List <KeyValuePair <string, double> > bidColumn = new List <KeyValuePair <string, double> >(); List <KeyValuePair <string, double> > askColumn = new List <KeyValuePair <string, double> >(); for (int i = 0; i < 10; i++) { bidColumn.Add(new KeyValuePair <string, double>("C-" + AskBid[i].Id + " ", AskBid[i].info.bid)); askColumn.Add(new KeyValuePair <string, double>("C-" + AskBid[i].Id + " ", AskBid[i].info.ask)); } this.AskBidGraph = new List <List <KeyValuePair <string, double> > >(); AskBidGraph.Add(bidColumn); AskBidGraph.Add(askColumn); //Graph of each commodity with her amount MarketUserData MUD = new MarketUserData(); MarketUserData ComAmount = MUD.SendQueryUserRequest(); List <KeyValuePair <string, double> > COfUser = new List <KeyValuePair <string, double> >(); COfUser.Add(new KeyValuePair <string, double>("C-0", ComAmount.commodities["0"])); COfUser.Add(new KeyValuePair <string, double>("C-1", ComAmount.commodities["1"])); COfUser.Add(new KeyValuePair <string, double>("C-2", ComAmount.commodities["2"])); COfUser.Add(new KeyValuePair <string, double>("C-3", ComAmount.commodities["3"])); COfUser.Add(new KeyValuePair <string, double>("C-4", ComAmount.commodities["4"])); COfUser.Add(new KeyValuePair <string, double>("C-5", ComAmount.commodities["5"])); COfUser.Add(new KeyValuePair <string, double>("C-6", ComAmount.commodities["6"])); COfUser.Add(new KeyValuePair <string, double>("C-7", ComAmount.commodities["7"])); COfUser.Add(new KeyValuePair <string, double>("C-8", ComAmount.commodities["8"])); COfUser.Add(new KeyValuePair <string, double>("C-9", ComAmount.commodities["9"])); //Setting data for column chart columnChartG.DataContext = AskBidGraph; //Setting data for pie chart columnChartA.DataContext = Avg; //Setting data for pie chart Hot.DataContext = HotCo; //Setting data for column chart Cou.DataContext = COfUser; }
public void TestSendQueryUserRequest() { MarketUserData info = (MarketUserData)market.SendQueryUserRequest(); Assert.IsNotNull(info); }
public void OnTimedEvent(Object source, ElapsedEventArgs e) { int actionCount = 0; while (actionCount < 19) { MarketUserData userInfo = (MarketUserData)market.SendQueryUserRequest(); actionCount++; foreach (KeyValuePair <string, int> entry in userInfo.commodities) { int commodityAverage = setAverage(Convert.ToInt32(entry.Key)); if (actionCount < 19) { MarketCommodityOffer offer = (MarketCommodityOffer)market.SendQueryMarketRequest(Convert.ToInt32(entry.Key)); actionCount++; if (isUpOrDown(Convert.ToInt32(entry.Key))) //check if the price of the last deals is on ascent or on descent { double currAverage = offer.ask / commodityAverage; //we are lokking for the relation between the current ask to the average to decide if we want to buy if (currAverage < 2.5 && actionCount < 19) { float spendable = userInfo.funds / 10; int count = 1; while ((count + 1) * offer.ask <= spendable) { if (currAverage < 1.5 && (count + 2) * offer.ask <= spendable) { count += 2; if (currAverage < 0.8 && (count + 2) * offer.ask <= spendable) { count += 2; } } } if (offer.ask * count <= spendable) { actionCount++; market.SendBuyRequest(offer.ask, Convert.ToInt32(entry.Key), count); } } } else { double currAverage = offer.bid / commodityAverage;////we are lokking for the relation between the current bid to the average to decide if we want to sell int commodityLeft = entry.Value; if (currAverage > 0.5 && commodityLeft > 0 && actionCount < 19) { int count = 1; commodityLeft--; if (currAverage > 1 && (commodityLeft >= count + 1)) { count++; commodityLeft--; if (currAverage > 1.5 && (commodityLeft >= count + 1)) { commodityLeft--; while (commodityLeft > 0) { count++; commodityLeft--; } } } market.SendSellRequest(offer.bid, Convert.ToInt32(entry.Key), count); actionCount++; } } } } } }