// 現在の板情報を取得・表示 static async Task ShowBoard() { Console.WriteLine(""); Console.WriteLine("========================================"); Console.WriteLine("board"); Console.WriteLine("========================================"); // 板情報の表示 (売り板、買い板を10件ずつ) Board board = await client.GetBoard(); foreach (var ask in Enumerable.Reverse(board.Asks.Take(10))) { Console.WriteLine(ask.ToString()); } Console.WriteLine("------------------"); foreach (var bid in board.Bids.Take(10)) { Console.WriteLine(bid.ToString()); } // 中間価格 Console.WriteLine(""); Console.WriteLine("MiddlePrice = " + board.MiddlePrice); Console.WriteLine(""); }
// 相場の変動が大きく変動したときに売買を行うボット static async Task BotLogicBoard() { // 1分毎にチェック double lastPrice = 0; while (true) { // 板情報を取得 var board = await client.GetBoard(); double currentPrice = board.MiddlePrice; // 現在の中間価格 Console.WriteLine("current = " + currentPrice); // 前回の相場と比較する if (lastPrice != 0) { double gap = board.MiddlePrice - lastPrice; // 1分間で100円値下がりしていたら買い時とみて買う if (gap <= -100) { Console.WriteLine("buy"); await client.Buy(0.001); } // 1分間で100円値上がりしていたら売り時とみて売る else if (gap >= 100) { Console.WriteLine("sell"); await client.Sell(0.001); } } lastPrice = currentPrice; // 1分待機 Console.WriteLine("wait"); await Task.Delay(60 * 1000); } }
async Task getOrderBook() { List <string> orderBookA = null; List <string> orderBookB = null; List <string> orderBook = null; amounts = null; bool first = true; listBox1.Items.Add("Connecting to BitFlyer server ..."); while (true) { if (!first) { if (listBox1.DataSource.Equals(orderBookA)) { orderBook = orderBookB; } else { orderBook = orderBookA; } orderBook.Clear(); amounts.Clear(); } try { board = await client.GetBoard(); } catch (Exception ex) { listBox1.BeginUpdate(); listBox1.DataSource = null; listBox1.Items.Add("Connecting to BitFlyer server ..."); listBox1.Items.Add(ex); listBox1.EndUpdate(); Console.WriteLine(ex); first = true; await Task.Delay(1000); continue; } if (first) { SMAX = Math.Max(board.Asks.Count, board.Bids.Count) + 50; orderBookA = new List <string>(SMAX * 2 + 1); orderBookB = new List <string>(SMAX * 2 + 1); orderBook = orderBookA; amounts = new List <double>(SMAX * 2 + 1); } int n = SMAX - board.Asks.Count; for (int i = 0; i < n; i++) { String index = (SMAX - i).ToString(); orderBook.Add(ss[4 - index.Length] + index + sc); amounts.Add(0.1); } for (int i = (0 < n ? board.Asks.Count : SMAX) - 1; 0 <= i; i--) { String index = (i + 1).ToString(); String[] ask = board.Asks[i].Size.ToString().Split('.'); String pr = board.Asks[i].Price.ToString(); orderBook.Add(ss[4 - index.Length] + index + sc + ss[5 - ask[0].Length] + ask[0] + (ask.Length == 2 ? ("." + ask[1] + ss[8 - ask[1].Length]) : ss[9]) + ss[9 - pr.Length] + pr); amounts.Add(board.Asks[i].Size); } String p = board.MiddlePrice.ToString(); orderBook.Add(s19 + ss[9 - p.Length] + p + ss[6] + "spread: " + Math.Round(100 * (board.Asks[0].Price - board.Bids[0].Price) / board.MiddlePrice, 5).ToString() + " %"); amounts.Add(0); n = SMAX - board.Bids.Count; for (int i = 0; i < (0 < n ? board.Bids.Count : SMAX); i++) { String index = (i + 1).ToString(); String[] bid = board.Bids[i].Size.ToString().Split('.'); String pr = board.Bids[i].Price.ToString(); orderBook.Add(s19 + ss[9 - pr.Length] + pr + ss[2] + ss[5 - bid[0].Length] + bid[0] + (bid.Length == 2 ? ("." + bid[1] + ss[8 - bid[1].Length]) : ss[9]) + ss[2] + sc + ss[4 - index.Length] + index); amounts.Add(-1 * board.Bids[i].Size); } for (int i = 0; i < n; i++) { String index = (board.Bids.Count + i + 1).ToString(); orderBook.Add(s46 + sc + ss[4 - index.Length] + index); amounts.Add(-0.1); } int foc = listBox1.SelectedIndex; int pos = listBox1.TopIndex; if (!mask) { listBox1.BeginUpdate(); listBox1.DataSource = orderBook; listBox1.EndUpdate(); } if (listBox1.DataSource == null) { await Task.Delay(1000); continue; } listBox1.SelectedIndex = foc; listBox1.TopIndex = pos; if (first) { listBox1.TopIndex = SMAX - 20; first = false; } await Task.Delay(1000); } }