public ApiOrderResult DoTrade(TradeType tradeType, double?price, double?amount) { try { ApiAccountInfo accountInfo = GetAccountinfo(); ApiOrderResult result = new ApiOrderResult(); switch (tradeType) { case TradeType.BuyMarket: result = this.Trade(Convert.ToDouble(accountInfo.info.funds.free.cny), null, tradeType, accountInfo); break; case TradeType.Buy: result = this.Trade(price, Convert.ToDecimal(accountInfo.info.funds.free.cny) / Convert.ToDecimal(price), tradeType, accountInfo); break; case TradeType.SellMarket: result = this.Trade(null, Convert.ToDecimal(accountInfo.info.funds.free.ltc), tradeType, accountInfo); break; case TradeType.Sell: result = this.Trade(price, Convert.ToDecimal(accountInfo.info.funds.free.ltc), tradeType, accountInfo); break; } return(result); } catch (Exception ex) { throw ex; } }
public ApiOrderResult Trade(double?price, decimal?amount, TradeType tradeType, ApiAccountInfo accountInfo) { try { string hashInput = string.Empty; var apisettings = GetApiSettings(); string action = string.Empty; switch (tradeType) { case TradeType.Buy: action = "buy"; hashInput = string.Format("amount={2}&partner={3}&rate={1}&symbol=ltc_cny&type={0}{4}", action, price, amount, apisettings.partnerId, apisettings.secretKey); break; case TradeType.BuyMarket: action = "buy_market"; hashInput = string.Format("partner={2}&rate={1}&symbol=ltc_cny&type={0}{3}", action, price, apisettings.partnerId, apisettings.secretKey); break; case TradeType.Sell: action = "sell"; hashInput = string.Format("amount={2}&partner={3}&rate={1}&symbol=ltc_cny&type={0}{4}", action, price, amount, apisettings.partnerId, apisettings.secretKey); break; case TradeType.SellMarket: action = "sell_market"; hashInput = string.Format("amount={1}&partner={2}&symbol=ltc_cny&type={0}{3}", action, amount, apisettings.partnerId, apisettings.secretKey); break; } string sig = Crypto.CalculateMD5Hash(hashInput); string result; using (var wb = new FlipperWebClient()) { var data = new NameValueCollection(); data["amount"] = amount.ToString(); data["partner"] = apisettings.partnerId; data["rate"] = price.ToString(); data["symbol"] = "ltc_cny"; data["type"] = action; data["sign"] = sig.ToUpper(); var response = wb.UploadValues(apisettings.tradeUri, "POST", data); result = System.Text.Encoding.Default.GetString(response); } return((ApiOrderResult)JsonConvert.DeserializeObject <ApiOrderResult>(result)); } catch (Exception ex) { throw ex; } }