public static Task <StockPriceCollection> Get(string code, DateTime begin, DateTime end, string interval, IProgress <int> progress = null) { var collection = new StockPriceCollection(); collection.Request(code, new DateTime(begin.Year, begin.Month, begin.Day), new DateTime(end.Year, end.Month, end.Day), collection, interval, progress); return(collection._source.Task); }
private void Request(string code, DateTime begin, DateTime end, StockPriceCollection collection, string interval, IProgress <int> progress, int seq = 0) { _code = code; _begin = begin; _end = end; _progress = progress; _interval = interval; OpenApi.SetInputValue("종목코드", code); if (IsDaily()) { OpenApi.SetInputValue("기준일자", _end.ToString("yyyyMMdd")); } else { OpenApi.SetInputValue("틱범위", interval); } OpenApi.SetInputValue("수정주가구분", "0"); OpenApi.CommRqData("차트구하기", IsDaily() ? "opt10081" : "opt10080", collection.PriceCallback, seq); }
private async void button_Test_Click(object sender, EventArgs e) { var stock1 = comboBox_Stock1.SelectedItem as Stock; var stock2 = comboBox_Stock2.SelectedItem as Stock; if (stock1 == null || stock2 == null) { return; } string interval = checkBox_UseMinute.Checked ? comboBox_Interval.SelectedItem.ToString() : ""; int gap; int.TryParse(interval, out gap); var begin = dateTimePicker_Begin.Value; var end = dateTimePicker_End.Value; int target = CalculateTarget(interval, begin, end); var progress = new IntProgress(progressBar, target); var collection1 = await StockPriceCollection.Get(stock1.Code, begin, end, interval, progress); progressBar.Value = 0; Debug.Info("{0}", collection1.Items.Count); var collection2 = await StockPriceCollection.Get(stock2.Code, begin, end, interval, progress); Debug.Info("{0}", collection2.Items.Count); progressBar.Value = 100; float margin = textBox_Margin.Text.ToFloat(); margin /= 100; margin += 1.0f; var items1 = collection1.Items; var items2 = collection2.Items; if (items1.Count <= 0 || items2.Count <= 0) { Debug.Warn("No data"); return; } int i = 0; int j = 0; while (items1[i].Time != items2[j].Time) { if (items1[i].Time < items2[j].Time) { i++; } else { j++; } } bool isItem1 = items1[i].Price <= items2[j].Price; long quantity; long.TryParse(textBox_Quantity.Text, out quantity); i++; j++; long sum = 0; while (i < items1.Count && j < items2.Count) { var item1 = items1[i]; var item2 = items2[j]; if (item1.Time != item2.Time) { if (item1.Time < item2.Time) { i++; } else { j++; } continue; } i++; j++; if (item1.Time.Hour == 9 && item1.Time.Minute < 5) { continue; } if (item1.Time.Hour >= 15 && item1.Time.Minute >= 20) { continue; } var price1 = item1.Price; var price2 = item2.Price; var mine = isItem1 ? price1 : price2; var other = isItem1 ? price2 : price1; var mineValue = mine * quantity; var otherValue = other * quantity; if (otherValue * margin <= mineValue) { long diff = mineValue - otherValue; long profit = diff; profit -= (long)Math.Ceiling((mineValue + otherValue) * OpenApi.Fee); sum += profit; isItem1 = !isItem1; Debug.Info("Date: {0} - Item1: {1}, Item2: {2}, Profit: {3}", item1.Time, price1, price2, profit); } } Debug.Info("Total Profit: {0}", sum); }