private void checkPrice(Object source, ElapsedEventArgs e) { MarketClientConnection pc = new MarketClientConnection(); MarketCommodityOffer query = (MarketCommodityOffer)pc.SendQueryMarketRequest(commodity); if (buyRequest) //buy when price of ask of commidity id is lower then extremePrice { int stockPrice = query.ask; if (stockPrice <= extremePrice) { pc.SendBuyRequest(stockPrice, commodity, amount); aTimer.Enabled = false; keepingProcces = false; return; // comment } } else { int stockOffer = query.bid; if (stockOffer >= extremePrice) { pc.SendSellRequest(stockOffer, commodity, amount); aTimer.Enabled = false; keepingProcces = false; return; } } }
private void ButtonCoffer_Click(object sender, RoutedEventArgs e) { myLogger.Info("Commodity Offer button was pressed"); string input = commodityid.Text; if (input.Length != 0) { try { int id = Convert.ToInt32(input); MarketCommodityOffer MCO = new MarketCommodityOffer(); MarketCommodityOffer output = MCO.SendQueryMarketRequest(id); MessageBox.Show(output.ToStringA()); myLogger.Info("the request about commodity :" + id + " done successfuly"); } catch (Exception e2) { MessageBox.Show("Bad commodity"); int id = Convert.ToInt32(input); myLogger.Error("the request about commodity :" + id + " failed because of BAD COMMODITY"); } } else { MessageBox.Show("full the field"); } commodityid.Text = ""; }
}//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); } }
private void Commodity(int input) { global::Program.MarketClient marketClient = new global::Program.MarketClient(); try { MarketCommodityOffer respCommodityOffer = (MarketCommodityOffer)marketClient.SendQueryMarketRequest(input); this.textBoxOutput.Text = respCommodityOffer.ToString(); this.textBoxOutput.Visibility = Visibility.Visible; } catch { MessageBoxResult popup = MessageBox.Show("Could not fetch info about commodity #" + input, "Confirmation"); } }
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; } } } }
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++; } } } } } }