public static void test_More_Buy(object o) { Example exp = o as Example; int i = exp.cancelCount; int cnt = 0; double milSum = 0; double secSum = 0; while (i-- > 0) { cnt++; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // 开始监视代码运行时间 var resObj = SYRequest.QureySell(exp.userId, exp.coinSymbol, "1", "102.50"); stopwatch.Stop(); // 停止监视 TimeSpan timespan = stopwatch.Elapsed; // 获取当前实例测量得出的总时间 double hours = timespan.TotalHours; // 总小时 double minutes = timespan.TotalMinutes; // 总分钟 double seconds = timespan.TotalSeconds; // 总秒数 double milliseconds = timespan.TotalMilliseconds; // 总毫秒数 Console.WriteLine("第一" + cnt + "次请求,总毫秒数:" + milliseconds); milSum += milliseconds; secSum += seconds; } string time = DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"); File.AppendAllText(".\\TradeTest\\" + exp.fileName, "线程" + exp.thread_number + "结束时间:" + time + "\r\n"); }
/// <summary> /// 购买 /// </summary> /// <param name="_vol">数量</param> /// <param name="_price">价格</param> /// <returns></returns> protected bool Buy(int _vol, double _price, bool beIsMust = false) { try { double limit = _price; if (limit > upLimitPrice) { limit = upLimitPrice; } request_OrderLimit ol = SYRequest.QureyBuy(userId, coinSymbol, _vol + "", limit + ""); if (ol != null) { Print("订单【买入】成功!商品编号:" + coinSymbol + ", 数量:" + _vol + ", 价格:" + limit); if (beIsMust == false) { orderLimitBuys.Add(ol); } return(true); } return(false); } catch { return(false); } }
public void test_More_Buy() { int maxValue = 1000; fileName = "10个线程_卖吃买.txt"; List <Order> orders = SYRequest.OureyAllOrder(userId, coinSymbol); foreach (var order in orders) { if (SYRequest.QureyCancelOrder(userId, order.orderId)) { Console.WriteLine("撤单成功!"); } } object obj = SYRequest.QureyBuy(userId, coinSymbol, "" + maxValue, "102.60"); string time = DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"); File.AppendAllText(".\\TradeTest\\" + fileName, maxValue + "手买单已经准备完成。\r\n"); File.AppendAllText(".\\TradeTest\\" + fileName, "开始吃盘口,分" + maxValue + "笔吃每次吃一手。\r\n"); File.AppendAllText(".\\TradeTest\\" + fileName, "起始时间:" + time + "\r\n"); cancelCount = maxValue / 10; for (int i = 1; i <= 10; i++) { th_s = new Thread(new ParameterizedThreadStart(test_More_Buy)); th_s.Start(this); } }
/// <summary> /// 购买 /// </summary> /// <param name="_vol">数量</param> /// <param name="_price">价格</param> /// <returns></returns> protected bool Buy(int _vol, double _price, bool beIsMust = false) { try { double limit = _price; if (limit > upLimitPrice) { limit = upLimitPrice; } if (limit < lowLimitPrice) { limit = lowLimitPrice; } string orderId = ProduceOrderID.GetOrderID(EnumBuySellType.购买); //订单ID request_OrderLimit ol = SYRequest.QureyBuy(userId, coinSymbol, _vol + "", limit + "", orderId); if (ol != null) { Print("订单【买入】成功!商品编号:" + coinSymbol + ", 数量:" + _vol + ", 价格:" + limit + ", 订单号:" + orderId); if (beIsMust == false) { orderLimitBuys.Add(ol); } return(true); } return(false); } catch { return(false); } }
protected bool getPriceLimitToday() { //查询当日交易的价格区间 coinSymbolMarket = SYRequest.QureyCoinSymbolMarket(coinSymbol); if (coinSymbolMarket == null) { Print("获取当日涨跌停信息失败。"); return(false); } if (coinSymbolMarket.beginPrice == null || coinSymbolMarket.maxPrice == null || coinSymbolMarket.minPrice == null) { Print("获取当日涨跌停信息有误。"); return(false); } int len = 3; if (floatLen > 0) { len = floatLen; } // 开盘价 openPrice = (double)decimal.Round((decimal)coinSymbolMarket.beginPrice, len); // 最高限价 upLimitPrice = (double)decimal.Round((decimal)coinSymbolMarket.maxPrice, len); // 最低限价 lowLimitPrice = (double)decimal.Round((decimal)coinSymbolMarket.minPrice, len); Print("获取当日涨跌停信息成功!"); return(true); }
protected bool CancelOrder(string _orderId) { if (SYRequest.QureyCancelOrder(userId, _orderId)) { Print("撤单成功!订单编号:" + _orderId); return(true); } return(false); }
private void MainForm_Load(object sender, EventArgs e) { SqLiteHelper sqlHelper = new SqLiteHelper(); sqlHelper.SqliteOpen(); try { //查询所有可以交易品种 symbols = SYRequest.QureyAllSymbol(); if (symbols == null) { MessageBox.Show("查询当前可用交易品种失败!"); windowStop = true; Close(); return; } //读取整张表 SQLiteDataReader reader = sqlHelper.ReadFullTable("user"); if (reader != null) { List <string> item = new List <string>(); while (reader.Read()) { int id = reader.GetInt32(reader.GetOrdinal("id")); string username = reader.GetString(reader.GetOrdinal("username")); string password = reader.GetString(reader.GetOrdinal("password")); int uid = reader.GetInt32(reader.GetOrdinal("uid")); string coinSymbol = reader.GetString(reader.GetOrdinal("coinSymbol")); string reamrks = reader.GetString(reader.GetOrdinal("remarks")); ListViewItem lvt = new ListViewItem(); lvt.Text = id + ""; lvt.SubItems.Add(username); lvt.SubItems.Add("********"); lvt.SubItems.Add(uid + ""); lvt.SubItems.Add(coinSymbol); lvt.SubItems.Add(getRunStatus(uid).ToString()); lvt.SubItems.Add(reamrks); strategyList.Items.Add(lvt); } } sqlHelper.SqliteClose(); //设置顶时任务 System.Timers.Timer timer = new System.Timers.Timer(); timer.Enabled = true; timer.Interval = 10000;//执行间隔时间,单位为毫秒 timer.Start(); timer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimer); } catch { sqlHelper.SqliteClose(); } }
/// <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); } }
public void cancelAllOrder() { List <Order> orders = SYRequest.OureyAllOrder(userId, coinSymbol); foreach (var order in orders) { if (SYRequest.QureyCancelOrder(userId, order.orderId)) { Console.WriteLine("撤单成功!"); } } }
private void btn_Flush_Click(object sender, EventArgs e) { try { //查询所有可以交易品种 symbols = SYRequest.QureyAllSymbol(); if (symbols == null) { MessageBox.Show("刷新失败!"); return; } SqLiteHelper sqlHelper = new SqLiteHelper(); strategyList.Items.Clear(); sqlHelper.SqliteOpen(); //读取整张表 SQLiteDataReader reader = sqlHelper.ReadFullTable("user"); if (reader != null) { List <string> item = new List <string>(); while (reader.Read()) { int id = reader.GetInt32(reader.GetOrdinal("id")); string username = reader.GetString(reader.GetOrdinal("username")); string password = reader.GetString(reader.GetOrdinal("password")); int uid = reader.GetInt32(reader.GetOrdinal("uid")); string coinSymbol = reader.GetString(reader.GetOrdinal("coinSymbol")); string reamrks = reader.GetString(reader.GetOrdinal("remarks")); ListViewItem lvt = new ListViewItem(); lvt.Text = id + ""; lvt.SubItems.Add(username); lvt.SubItems.Add("********"); lvt.SubItems.Add(uid + ""); lvt.SubItems.Add(coinSymbol); lvt.SubItems.Add(getRunStatus(uid).ToString()); lvt.SubItems.Add(reamrks); strategyList.Items.Add(lvt); } } sqlHelper.SqliteClose(); MessageBox.Show("刷新成功!"); } catch { MessageBox.Show("刷新失败!"); } }
public void test01_sell() { fileName = "1个线程_买吃卖.txt"; List <Order> orders = SYRequest.OureyAllOrder(userId, coinSymbol); foreach (var order in orders) { if (SYRequest.QureyCancelOrder(userId, order.orderId)) { Console.WriteLine("撤单成功!"); } } object obj = SYRequest.QureySell(userId, coinSymbol, "1000", "102.60"); string time = DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"); File.AppendAllText(".\\TradeTest\\" + fileName, "1000手买单已经准备完成。\r\n"); File.AppendAllText(".\\TradeTest\\" + fileName, "开始吃盘口,分1000笔吃每次吃一手。\r\n"); File.AppendAllText(".\\TradeTest\\" + fileName, "起始时间:" + time + "\r\n"); cancelCount = 1000; th_s = new Thread(new ParameterizedThreadStart(FunTest01_sell)); th_s.Start(this); }
/// <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); } }
protected List <Order> getAllOrder(string _coinSymbol) { return(SYRequest.OureyAllOrder(userId, coinSymbol)); }
/// <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); } }