/// <summary> /// 转让 /// </summary> /// <param name="_vol">数量</param> /// <param name="_price">价格</param> /// <returns></returns> protected bool Sell(long _vol, double _price, bool beIsMust = false) { try { if (position == null) { position = SYRequest.QureyPosition(userId, coinSymbol); if (position == null) { Print("转卖仓位不足,无法进行转卖。"); return(false); } } if (position.balance <= 0) { position = SYRequest.QureyPosition(userId, coinSymbol); if (position.balance == 0) { Print("转卖仓位不足,无法进行转卖。"); return(false); } } if (_vol > position.balance) { Print("转卖数量大于实际持有量,已按实际拥有数量专卖。"); _vol = (long)position.balance; } double limit = _price; if (limit < lowLimitPrice) { limit = lowLimitPrice; } if (limit > upLimitPrice) { limit = upLimitPrice; } string orderId = ProduceOrderID.GetOrderID(EnumBuySellType.转让); //订单ID request_OrderLimit ol = SYRequest.QureySell(userId, coinSymbol, _vol + "", limit + "", orderId); if (ol != null) { position.balance -= _vol; Print("订单【转卖】成功!商品编号:" + coinSymbol + ", 数量:" + _vol + ", 价格:" + limit + ", 订单号:" + orderId); if (beIsMust == false) { orderLimitSells.Add(ol); } return(true); } return(false); } catch { return(false); } }
/// <summary> /// 加载交易信息 /// </summary> public sealed override bool Load() { try { #region 成员属性赋值 //用户ID userId = 100; //用户名 userName = ""; //密码 password = ""; //设置 开仓倍数 (必成交) mustMore = 1; //设置 开仓倍数 (非成交) OtherMore = 1; //真实盘口深度 maxRealQuoteLen = 10; //伪盘口深度 maxQuoteLen = maxRealQuoteLen + 1; //产品编号 coinSymbol = "XDFYX"; //交易对 symbol = coinSymbol + "/CNY"; //最小变动单位 minPrice = 0.01; //线程休眠等级 timerGrade = 1; // mustToBuy = 30; // mustToSell = 30; // otherToBuy = 80; // otherToSell = 80; maxOrderCount = 60; //设置线程的休眠等级 SetTimer(timerGrade); #endregion #region 交易价格信息处理 //查询当日交易的价格区间 coinSymbolMarket = SYRequest.QureyCoinSymbolMarket(coinSymbol); if (coinSymbolMarket == null) { Print("获取当日涨跌停信息失败,策略启动失败!"); return(false); } if (coinSymbolMarket.beginPrice == null || coinSymbolMarket.maxPrice == null || coinSymbolMarket.minPrice == null) { Print("获取当日涨跌停信息有误,策略启动失败!"); return(false); } // 开盘价 openPrice = (double)decimal.Round((decimal)coinSymbolMarket.beginPrice, 2); // 最高限价 upLimitPrice = (double)decimal.Round((decimal)coinSymbolMarket.maxPrice, 2); // 最低限价 lowLimitPrice = (double)decimal.Round((decimal)coinSymbolMarket.minPrice, 2); Print("获取当日涨跌停信息成功!"); #endregion #region 更新用户持仓信息 //查询所有持仓 position = SYRequest.QureyPosition(userId, coinSymbol); if (position == null || position.balance == 0) { Print("该账户不存在仓位信息!"); } else { Print("获取到所有仓位信息信息!"); } #endregion #region 盘口信息更新 //查询盘口信息 Quote q = SYRequest.QureyQuote(symbol); realQuoteBuy = q.bid.items.ToArray(); realQuoteSell = q.ask.items.ToArray(); int len = maxQuoteLen; quoteBuy = new QuoteItem[len]; quoteSell = new QuoteItem[len]; for (int i = 0; i < len; i++) { quoteBuy[i] = new QuoteItem(); quoteSell[i] = new QuoteItem(); } productFakeQuote(); #endregion #region 建立行情连接 socketWorker = new SocketWorker(); if (!socketWorker.Connect()) { Print("建立行情 socket 连接失败,策略启动失败!"); return(false); } socketWorker.Init(this, symbol); Print("建立行情 socket 连接成功!"); #endregion return(true); } catch (Exception ex) { OnError(EnumExceptionCode.其他异常, ex.ToString()); return(false); } }
/// <summary> /// 加载交易信息 /// </summary> public sealed override bool Load() { try { // 加载配置信息 LoadConfig(); #region 成员属性赋值 //真实盘口深度 maxRealQuoteLen = 10; //伪盘口深度 maxQuoteLen = maxRealQuoteLen + 1; //交易对 symbol = coinSymbol + "/CNY"; //设置线程的休眠等级 SetTimer(timerGrade); string mi = minPrice.ToString(); int index = mi.IndexOf('.'); if (index < mi.Length - 1) { string flen = mi.Substring(index + 1); floatLen = flen.Length; } #endregion // 获取当日涨跌停信息 if (getPriceLimitToday() == false) { return(false); } #region 更新用户持仓信息 //查询所有持仓 position = SYRequest.QureyPosition(userId, coinSymbol); if (position == null || position.balance == 0) { Print("该账户不存在仓位信息!"); } else { Print("获取到所有仓位信息信息!"); } #endregion #region 盘口信息更新 //查询盘口信息 Quote q = SYRequest.QureyQuote(symbol); realQuoteBuy = q.bid.items.ToArray(); realQuoteSell = q.ask.items.ToArray(); int len = maxQuoteLen; quoteBuy = new QuoteItem[len]; quoteSell = new QuoteItem[len]; for (int i = 0; i < len; i++) { quoteBuy[i] = new QuoteItem(); quoteSell[i] = new QuoteItem(); } productFakeQuote(); #endregion return(true); } catch (Exception ex) { OnError(EnumExceptionCode.交易异常, "加载配置信息异常即将交易停止!\r\n" + ex.ToString()); return(false); } }