仮想通貨の自動売買プログラムを作る過程を紹介しています。
さて、成行注文も入れれるようになり、それが確約した値段も調べられるようになったので、今回はボタン一つで以下の一連の処理をすべてまとめて実行するようにしていきます。
- 成行注文を入れる (枚数は変更できるようにする)
- 成行注文が確約した値段を確認する
- 確約した値段 + x で指値注文を入れる (x は後で変えられるようにする)
- 指値注文が通るのを待つ。完了したら再度 1. から繰り返す。
この処理用のボタンと利確する価格を設定するUI を作る
今回の一連の処理に必要な UI を追加していきます。ひとまずこんな感じで作成。
あとはこの発注ボタンを押すと、「成り行き注文⇒値段調べる⇒値段+x で指値を入れる⇒指値がとったら最初から繰り返す」となるように実装する
自動売買プログラムに C# コードを書き込む
最初の成行注文を入れるところとその確約価格を調べるところのコードはここまでの作業でできているので、ほぼコピペするだけ。確約価格が取れたらそれに今回のテキストボックスの値を足して指値注文を入れる。
指値注文を入れるコードは成行注文とほぼ同じ。送り込む BODY の内容の MARKET と記載されている箇所を LIMIT に変更するだけ。あと、値段指定も忘れずに。
これが指値注文を入れる関数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
public async Task<string> MakeLimitOrder(string sSide, double iPrice, double iSize) { var method = "POST"; var path = "/v1/me/sendchildorder"; var query = ""; var body = @"{ ""product_code"": ""FX_BTC_JPY"", ""child_order_type"": ""LIMIT"", ""side"": """ + sSide + @""", ""price"":" + iPrice + @", ""size"": " + iSize + @", ""minute_to_expire"": 43200, ""time_in_force"": ""GTC"" }"; using (var client = new HttpClient()) using (var request = new HttpRequestMessage(new HttpMethod(method), path + query)) using (var content = new StringContent(body)) { client.BaseAddress = endpointUri; content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); request.Content = content; var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString(); var data = timestamp + method + path + query + body; var hash = SignWithHMACSHA256(data, apiSecret); request.Headers.Add("ACCESS-KEY", apiKey); request.Headers.Add("ACCESS-TIMESTAMP", timestamp); request.Headers.Add("ACCESS-SIGN", hash); var message = await client.SendAsync(request); var response = await message.Content.ReadAsStringAsync(); if (response == "[]") { Console.WriteLine("Error: MakeMarketOrder() returning ERROR_NO_RESPONSE"); return "ERROR_NO_RESPONSE"; } return response; } } |
注文 ID から取得できる child_order_state の値から指値注文の状態を確認できる。(以下 bitFlyer Api documentation より)
ACTIVE
: オープンな注文の一覧を取得します。COMPLETED
: 全額が取引完了した注文の一覧を取得します。CANCELED
: お客様がキャンセルした注文です。EXPIRED
: 有効期限に到達したため取り消された注文の一覧を取得します。REJECTED
: 失敗した注文です。
指値を入れたら、状態を確認して、Active の場合にはまだ注文が通っていないのでしばらくして再確認。COMPLETED になったらまた成行注文を入れるところからスタート。それ以外の場合にはエラーを返してループを終了。
作ったボタンをクリックしたらこれを実行するようにコードを追加。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
private void buttonSpecialOrderBuyBuy_Click(object sender, EventArgs e) { Task Job1 = RunThisWhenButtonSpecialOrderBuyBuyIsClicked(); } public async Task RunThisWhenButtonSpecialOrderBuyBuyIsClicked() { bool bContinueBuyBuy=true; while (bContinueBuyBuy) { //1) 成行注文を入れる string ResponseMakeMarketOrder = await MakeMarketOrder("BUY", (double)numericUpDownMarketOrder.Value); var DesirializedResponse = JsonConvert.DeserializeObject<JsonSendChildOrder>(ResponseMakeMarketOrder); var ChildOrderAcceptanceID = DesirializedResponse.child_order_acceptance_id; textBox1.AppendText("MakeMarketOrder succeeded with ID: " + ChildOrderAcceptanceID + System.Environment.NewLine); //2) 成り行き注文が通るのを待つ string ResponseGetOrderInformationWithID = await GetOrderInformationWithID(ChildOrderAcceptanceID); while (ResponseGetOrderInformationWithID == "ERROR_NO_RESPONSE") { textBox1.AppendText(" Waiting for order to complete. Re-check in 10sec." + System.Environment.NewLine); await Task.Delay(10000); ResponseGetOrderInformationWithID = await GetOrderInformationWithID(ChildOrderAcceptanceID); } //3) レスポンス (JsonGetChildOrders) から平均価格、枚数、状態を取り出す。 var DesirializedResponse2 = JsonConvert.DeserializeObject<JsonGetChildOrders>(ResponseGetOrderInformationWithID.Substring(1, ResponseGetOrderInformationWithID.Length - 2)); var ChildOrderAveragePrice = DesirializedResponse2.average_price; var ChildOrderSize = DesirializedResponse2.size; var ChildOrderState = DesirializedResponse2.child_order_state; textBox1.AppendText(" GetOrderInformationWithID succeeded for ID: " + ChildOrderAcceptanceID + System.Environment.NewLine); textBox1.AppendText(" ChildOrderAveragePrice: " + ChildOrderAveragePrice + System.Environment.NewLine); textBox1.AppendText(" ChildOrderSize: " + ChildOrderSize + System.Environment.NewLine); textBox1.AppendText(" ChildOrderState: " + ChildOrderState + System.Environment.NewLine); //4) 確約した成行注文の値段 + x を指値注文を入れる double LimitOrderPrice = ChildOrderAveragePrice + double.Parse(textBoxSpecialOrderBuyBuy.Text); string ResponseMakeLimitOrder = await MakeLimitOrder("SELL", LimitOrderPrice, (double)numericUpDownMarketOrder.Value); var DesirializedResponse3 = JsonConvert.DeserializeObject<JsonSendChildOrder>(ResponseMakeLimitOrder); var ChildOrderAcceptanceID2 = DesirializedResponse3.child_order_acceptance_id; textBox1.AppendText("MakeLimitOrder succeeded with ID: " + ChildOrderAcceptanceID2 + System.Environment.NewLine); //5) 指値注文が通るのを待つ string ResponseGetOrderInformationWithID2 = await GetOrderInformationWithID(ChildOrderAcceptanceID2); while (ResponseGetOrderInformationWithID2 == "ERROR_NO_RESPONSE") { textBox1.AppendText(" Waiting for order to complete. Re-check in 10sec." + System.Environment.NewLine); await Task.Delay(10000); ResponseGetOrderInformationWithID2 = await GetOrderInformationWithID(ChildOrderAcceptanceID2); } //6) レスポンス (JsonGetChildOrders) から child_order_state を取得。 var DesirializedResponse4 = JsonConvert.DeserializeObject<JsonGetChildOrders>(ResponseGetOrderInformationWithID2.Substring(1, ResponseGetOrderInformationWithID2.Length - 2)); var ChildOrderType2 = DesirializedResponse4.child_order_type; var ChildOrderPrice2 = DesirializedResponse4.price; var ChildOrderSize2 = DesirializedResponse4.size; var ChildOrderState2 = DesirializedResponse4.child_order_state; textBox1.AppendText(" GetOrderInformationWithID succeeded for ID: " + ChildOrderAcceptanceID2 + System.Environment.NewLine); textBox1.AppendText(" ChildOrderType: " + ChildOrderType2 + System.Environment.NewLine); textBox1.AppendText(" ChildOrderPrice: " + ChildOrderPrice2 + System.Environment.NewLine); textBox1.AppendText(" ChildOrderSize: " + ChildOrderSize2 + System.Environment.NewLine); textBox1.AppendText(" ChildOrderState: " + ChildOrderState2 + System.Environment.NewLine); //7) child_order_state が ACTIVE の間は状態確認を毎分繰り返す while (ChildOrderState2 == "ACTIVE") { textBox1.AppendText("Limit Order ID " + ChildOrderAcceptanceID2 + " is still ACTIVE. recheck in 60s." + ChildOrderState2 + System.Environment.NewLine); await Task.Delay(60000); ResponseGetOrderInformationWithID2 = await GetOrderInformationWithID(ChildOrderAcceptanceID2); while (ResponseGetOrderInformationWithID2 == "ERROR_NO_RESPONSE") { textBox1.AppendText(" Waiting for order to complete. Re-check in 10sec." + System.Environment.NewLine); await Task.Delay(10000); ResponseGetOrderInformationWithID2 = await GetOrderInformationWithID(ChildOrderAcceptanceID2); } DesirializedResponse4 = JsonConvert.DeserializeObject<JsonGetChildOrders>(ResponseGetOrderInformationWithID2.Substring(1, ResponseGetOrderInformationWithID2.Length - 2)); ChildOrderState2 = DesirializedResponse4.child_order_state; } //8) 確約したら bContinueBuyBuy=true で 1) から再スタート // それ以外は何かがおかしくなったのでループから抜け出して終了。 if (ChildOrderState2 == "COMPLETED") { bContinueBuyBuy = true; //re-run the whole process again textBox1.AppendText("TRANSACTION COMPLETED. RERUNNING THE PROCESS." + System.Environment.NewLine); } else { bContinueBuyBuy = false; //ABORT textBox1.AppendText("SOMETHING WENT WRONG:" + System.Environment.NewLine); textBox1.AppendText(" child_order_state: " + ChildOrderState2 + System.Environment.NewLine); } } } |
これで出来上がり。ボタンを押したら最高値まできっちりと利確していってくれるはず!
自動売買プログラムの作成状況
今の状態のプログラムの見た目がこれ。
コードはこんな状態。長くなってきたので全部を張り付けるのはそろそろやめる予定。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net.Http; using System.Net.Http.Headers; using Newtonsoft.Json; using System.Security.Cryptography; namespace BtcFxStep1 { public partial class Form1 : Form { static readonly Uri endpointUri = new Uri("https://api.bitflyer.jp"); static readonly string apiKey = "xxxxxxxxxxxxxxxxxxxxxx"; static readonly string apiSecret = "xxxxxxxxxxxxxxxxxxxxxx"; //指値注文を入れる // Param: // sSide: "BUY" か "SELL" // iPrice: 価格 // iSize: 枚数 public async Task<string> MakeLimitOrder(string sSide, double iPrice, double iSize) { var method = "POST"; var path = "/v1/me/sendchildorder"; var query = ""; var body = @"{ ""product_code"": ""FX_BTC_JPY"", ""child_order_type"": ""LIMIT"", ""side"": """ + sSide + @""", ""price"":" + iPrice + @", ""size"": " + iSize + @", ""minute_to_expire"": 43200, ""time_in_force"": ""GTC"" }"; using (var client = new HttpClient()) using (var request = new HttpRequestMessage(new HttpMethod(method), path + query)) using (var content = new StringContent(body)) { client.BaseAddress = endpointUri; content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); request.Content = content; var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString(); var data = timestamp + method + path + query + body; var hash = SignWithHMACSHA256(data, apiSecret); request.Headers.Add("ACCESS-KEY", apiKey); request.Headers.Add("ACCESS-TIMESTAMP", timestamp); request.Headers.Add("ACCESS-SIGN", hash); var message = await client.SendAsync(request); var response = await message.Content.ReadAsStringAsync(); if (response == "[]") { Console.WriteLine("Error: MakeMarketOrder() returning ERROR_NO_RESPONSE"); return "ERROR_NO_RESPONSE"; } return response; } } //成行注文を入れる // Param: // sSide: "BUY" か "SELL" // iSize: 枚数 public async Task<string> MakeMarketOrder(string sSide, double iSize) { var method = "POST"; var path = "/v1/me/sendchildorder"; var query = ""; var body = @"{ ""product_code"": ""FX_BTC_JPY"", ""child_order_type"": ""MARKET"", ""side"": """ + sSide + @""", ""price"": 0, ""size"": " + numericUpDownMarketOrder.Value + @", ""minute_to_expire"": 43200, ""time_in_force"": ""GTC"" }"; using (var client = new HttpClient()) using (var request = new HttpRequestMessage(new HttpMethod(method), path + query)) using (var content = new StringContent(body)) { client.BaseAddress = endpointUri; content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); request.Content = content; var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString(); var data = timestamp + method + path + query + body; var hash = SignWithHMACSHA256(data, apiSecret); request.Headers.Add("ACCESS-KEY", apiKey); request.Headers.Add("ACCESS-TIMESTAMP", timestamp); request.Headers.Add("ACCESS-SIGN", hash); var message = await client.SendAsync(request); var response = await message.Content.ReadAsStringAsync(); if (response == "[]") { Console.WriteLine("Error: MakeMarketOrder() returning ERROR_NO_RESPONSE"); return "ERROR_NO_RESPONSE"; } return response; } } public async Task<string> GetOrderInformationWithID(string ChildOrderAcceptanceID) { var method = "GET"; var path = "/v1/me/getchildorders"; var query = "?product_code=FX_BTC_JPY&child_order_acceptance_id=" + ChildOrderAcceptanceID; using (var client = new HttpClient()) using (var request = new HttpRequestMessage(new HttpMethod(method), path + query)) { client.BaseAddress = endpointUri; var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString(); var data = timestamp + method + path + query; var hash = SignWithHMACSHA256(data, apiSecret); request.Headers.Add("ACCESS-KEY", apiKey); request.Headers.Add("ACCESS-TIMESTAMP", timestamp); request.Headers.Add("ACCESS-SIGN", hash); var message = await client.SendAsync(request); var response = await message.Content.ReadAsStringAsync(); if (response == "[]") { Console.WriteLine("GetOrderInformationWithID():OUT returning ERROR_NO_RESPONSE"); return "ERROR_NO_RESPONSE"; } return response; } } static string SignWithHMACSHA256(string data, string secret) { using (var encoder = new HMACSHA256(Encoding.UTF8.GetBytes(secret))) { var hash = encoder.ComputeHash(Encoding.UTF8.GetBytes(data)); return ToHexString(hash); } } static string ToHexString(byte[] bytes) { var sb = new StringBuilder(bytes.Length * 2); foreach (var b in bytes) { sb.Append(b.ToString("x2")); } return sb.ToString(); } ///v1/me/getchildorders public class JsonGetChildOrders { public double id { get; set; } public string child_order_id { get; set; } public string product_code { get; set; } public string side { get; set; } public string child_order_type { get; set; } public double price { get; set; } public double average_price { get; set; } public double size { get; set; } public string child_order_state { get; set; } public DateTime expire_date { get; set; } public DateTime child_order_date { get; set; } public string child_order_acceptance_id { get; set; } public double outstanding_size { get; set; } public double cancel_size { get; set; } public double executed_size { get; set; } public double total_commission { get; set; } } ///v1/me/sendchildorder で受け取るレスポンスの型を定義 public class JsonSendChildOrder { public string child_order_acceptance_id; } ///v1/ticker で受け取るレスポンスの型を定義 // GetCurrentBtcFxPrice() で使う public class JsonTicker { public string product_code { get; set; } public DateTime timestamp { get; set; } public int tick_id { get; set; } public double best_bid { get; set; } public double best_ask { get; set; } public double best_bid_size { get; set; } public double best_ask_size { get; set; } public double total_bid_depth { get; set; } public double total_ask_depth { get; set; } public double ltp { get; set; } public double volume { get; set; } public double volume_by_product { get; set; } } //採取取引価格を TextBoxPrice に出力する public async Task GetCurrentBtcFxPrice() { while (true) { var method = "GET"; var path = "/v1/ticker"; var query = "?product_code=FX_BTC_JPY"; using (var client = new HttpClient()) using (var request = new HttpRequestMessage(new HttpMethod(method), path + query)) { client.BaseAddress = endpointUri; var message = await client.SendAsync(request); var response = await message.Content.ReadAsStringAsync(); var DesirializedResponse = JsonConvert.DeserializeObject<JsonTicker>(response); var CurrentPrice = String.Format("{0:#,0}", DesirializedResponse.ltp); textBoxPrice.Text = CurrentPrice; } await Task.Delay(1000); } } public Form1() { InitializeComponent(); Task JobUpdatePrice = GetCurrentBtcFxPrice(); } private void button1_Click(object sender, EventArgs e) { Task JobButton1 = RunThisWhenButton1IsClicked(); } public async Task RunThisWhenButton1IsClicked() { } private void buttonMarketOrder_Click(object sender, EventArgs e) { Task JobMakeMarketOrder = RunThisWhenButtonMarketOrderIsClicked(); } public async Task RunThisWhenButtonMarketOrderIsClicked() { textBox1.AppendText("MakeMarketOrder() Start." + System.Environment.NewLine); string ResponseMakeMarketOrder = await MakeMarketOrder("BUY", (double)numericUpDownMarketOrder.Value); //レスポンスの型:JsonSendChildOrder //{ "child_order_acceptance_id":"JRF20180314-102635-135926"} var DesirializedResponse = JsonConvert.DeserializeObject<JsonSendChildOrder>(ResponseMakeMarketOrder); var ChildOrderAcceptanceID = DesirializedResponse.child_order_acceptance_id; textBox1.AppendText("MakeMarketOrder() succeeded with ID: " + ChildOrderAcceptanceID + System.Environment.NewLine); } private void buttonSpecialOrderBuyBuy_Click(object sender, EventArgs e) { Task Job1 = RunThisWhenButtonSpecialOrderBuyBuyIsClicked(); } public async Task RunThisWhenButtonSpecialOrderBuyBuyIsClicked() { bool bContinueBuyBuy=true; while (bContinueBuyBuy) { //1) 成行注文 //成行注文を入れてレスポンス (JsonSendChildOrder) から注文 ID ("JRF20180314-102635-135926") を取り出す string ResponseMakeMarketOrder = await MakeMarketOrder("BUY", (double)numericUpDownMarketOrder.Value); var DesirializedResponse = JsonConvert.DeserializeObject<JsonSendChildOrder>(ResponseMakeMarketOrder); var ChildOrderAcceptanceID = DesirializedResponse.child_order_acceptance_id; textBox1.AppendText("MakeMarketOrder succeeded with ID: " + ChildOrderAcceptanceID + System.Environment.NewLine); //2) 確約した成行注文の値段を確認 //注文 ID の情報を取得。早すぎるとエラーになるのでその場合には再チャレンジ string ResponseGetOrderInformationWithID = await GetOrderInformationWithID(ChildOrderAcceptanceID); while (ResponseGetOrderInformationWithID == "ERROR_NO_RESPONSE") { textBox1.AppendText(" Waiting for order to complete. Re-check in 10sec." + System.Environment.NewLine); await Task.Delay(10000); ResponseGetOrderInformationWithID = await GetOrderInformationWithID(ChildOrderAcceptanceID); } //レスポンス (JsonGetChildOrders) から平均価格、枚数、状態を取り出す。 var DesirializedResponse2 = JsonConvert.DeserializeObject<JsonGetChildOrders>(ResponseGetOrderInformationWithID.Substring(1, ResponseGetOrderInformationWithID.Length - 2)); var ChildOrderAveragePrice = DesirializedResponse2.average_price; var ChildOrderSize = DesirializedResponse2.size; var ChildOrderState = DesirializedResponse2.child_order_state; textBox1.AppendText(" GetOrderInformationWithID succeeded for ID: " + ChildOrderAcceptanceID + System.Environment.NewLine); textBox1.AppendText(" ChildOrderAveragePrice: " + ChildOrderAveragePrice + System.Environment.NewLine); textBox1.AppendText(" ChildOrderSize: " + ChildOrderSize + System.Environment.NewLine); textBox1.AppendText(" ChildOrderState: " + ChildOrderState + System.Environment.NewLine); //3) 確約した成行注文の値段 + x を指値注文を入れる //average_price + x で指値注文を入れる。 double LimitOrderPrice = ChildOrderAveragePrice + double.Parse(textBoxSpecialOrderBuyBuy.Text); string ResponseMakeLimitOrder = await MakeLimitOrder("SELL", LimitOrderPrice, (double)numericUpDownMarketOrder.Value); var DesirializedResponse3 = JsonConvert.DeserializeObject<JsonSendChildOrder>(ResponseMakeLimitOrder); var ChildOrderAcceptanceID2 = DesirializedResponse3.child_order_acceptance_id; textBox1.AppendText("MakeLimitOrder succeeded with ID: " + ChildOrderAcceptanceID2 + System.Environment.NewLine); //注文 ID から状態を取得 string ResponseGetOrderInformationWithID2 = await GetOrderInformationWithID(ChildOrderAcceptanceID2); while (ResponseGetOrderInformationWithID2 == "ERROR_NO_RESPONSE") { textBox1.AppendText(" Waiting for order to complete. Re-check in 10sec." + System.Environment.NewLine); await Task.Delay(10000); ResponseGetOrderInformationWithID2 = await GetOrderInformationWithID(ChildOrderAcceptanceID2); } //レスポンス (JsonGetChildOrders) から child_order_state を取得。 var DesirializedResponse4 = JsonConvert.DeserializeObject<JsonGetChildOrders>(ResponseGetOrderInformationWithID2.Substring(1, ResponseGetOrderInformationWithID2.Length - 2)); var ChildOrderType2 = DesirializedResponse4.child_order_type; var ChildOrderPrice2 = DesirializedResponse4.price; var ChildOrderSize2 = DesirializedResponse4.size; var ChildOrderState2 = DesirializedResponse4.child_order_state; textBox1.AppendText(" GetOrderInformationWithID succeeded for ID: " + ChildOrderAcceptanceID2 + System.Environment.NewLine); textBox1.AppendText(" ChildOrderType: " + ChildOrderType2 + System.Environment.NewLine); textBox1.AppendText(" ChildOrderPrice: " + ChildOrderPrice2 + System.Environment.NewLine); textBox1.AppendText(" ChildOrderSize: " + ChildOrderSize2 + System.Environment.NewLine); textBox1.AppendText(" ChildOrderState: " + ChildOrderState2 + System.Environment.NewLine); while (ChildOrderState2 == "ACTIVE") { textBox1.AppendText("Limit Order ID " + ChildOrderAcceptanceID2 + " is still ACTIVE. recheck in 60s." + ChildOrderState2 + System.Environment.NewLine); await Task.Delay(60000); ResponseGetOrderInformationWithID2 = await GetOrderInformationWithID(ChildOrderAcceptanceID2); while (ResponseGetOrderInformationWithID2 == "ERROR_NO_RESPONSE") { textBox1.AppendText(" Waiting for order to complete. Re-check in 10sec." + System.Environment.NewLine); await Task.Delay(10000); ResponseGetOrderInformationWithID2 = await GetOrderInformationWithID(ChildOrderAcceptanceID2); } DesirializedResponse4 = JsonConvert.DeserializeObject<JsonGetChildOrders>(ResponseGetOrderInformationWithID2.Substring(1, ResponseGetOrderInformationWithID2.Length - 2)); ChildOrderState2 = DesirializedResponse4.child_order_state; } if (ChildOrderState2 == "COMPLETED") { bContinueBuyBuy = true; //re-run the whole process again textBox1.AppendText("TRANSACTION COMPLETED. RERUNNING THE PROCESS." + System.Environment.NewLine); } else { bContinueBuyBuy = false; //ABORT textBox1.AppendText("SOMETHING WENT WRONG:" + System.Environment.NewLine); textBox1.AppendText(" child_order_state: " + ChildOrderState2 + System.Environment.NewLine); } } } private void buttonCheckOrderFromID_Click(object sender, EventArgs e) { Task Job1 = RunThisWhenButtonCheckOrderFromIDIsClicked(); } public async Task RunThisWhenButtonCheckOrderFromIDIsClicked() { //注文 ID の情報を取得。早すぎるとエラーになるのでその場合には再チャレンジ string ResponseGetOrderInformationWithID = await GetOrderInformationWithID(textBoxCheckOrderFromID.Text); for (int i=0; (ResponseGetOrderInformationWithID == "ERROR_NO_RESPONSE") && (i<100); i++) { textBox1.AppendText("Waiting for response. Re-check in 10sec."); await Task.Delay(10000); ResponseGetOrderInformationWithID = await GetOrderInformationWithID(textBoxCheckOrderFromID.Text); } //レスポンス (JsonGetChildOrders) を表示。 var DesirializedResponse = JsonConvert.DeserializeObject<JsonGetChildOrders>(ResponseGetOrderInformationWithID.Substring(1, ResponseGetOrderInformationWithID.Length - 2)); textBox1.AppendText("GetOrderInformationWithID succeeded for ID: " + textBoxCheckOrderFromID.Text + System.Environment.NewLine); textBox1.AppendText(" id: " + DesirializedResponse.id + System.Environment.NewLine); textBox1.AppendText(" child_order_id: " + DesirializedResponse .child_order_id+ System.Environment.NewLine); textBox1.AppendText(" product_code: " + DesirializedResponse.product_code + System.Environment.NewLine); textBox1.AppendText(" side: " + DesirializedResponse.side + System.Environment.NewLine); textBox1.AppendText(" child_order_type: " + DesirializedResponse.child_order_type + System.Environment.NewLine); textBox1.AppendText(" price: " + DesirializedResponse.price + System.Environment.NewLine); textBox1.AppendText(" average_price: " + DesirializedResponse.average_price + System.Environment.NewLine); textBox1.AppendText(" size: " + DesirializedResponse.size + System.Environment.NewLine); textBox1.AppendText(" child_order_state: " + DesirializedResponse.child_order_state + System.Environment.NewLine); textBox1.AppendText(" expire_date: " + DesirializedResponse.expire_date + System.Environment.NewLine); textBox1.AppendText(" child_order_date: " + DesirializedResponse.child_order_date + System.Environment.NewLine); textBox1.AppendText(" child_order_acceptance_id: " + DesirializedResponse.child_order_acceptance_id + System.Environment.NewLine); textBox1.AppendText(" outstanding_size: " + DesirializedResponse.outstanding_size + System.Environment.NewLine); textBox1.AppendText(" cancel_size: " + DesirializedResponse.cancel_size + System.Environment.NewLine); textBox1.AppendText(" executed_size: " + DesirializedResponse.executed_size + System.Environment.NewLine); textBox1.AppendText(" total_commission: " + DesirializedResponse.total_commission + System.Environment.NewLine); } } } |
これで値上がり中は確実に利確していけるはず。
目的とした実装はこれでいいが、流しっぱなしにすると取引状況が追いにくい。後から見て取引履歴がちゃんとわかるようにしたり、放置を前提としてちょっと見た目をきれいにしたい。