/// <summary> /// >=1 means added /// 0 means last element updated /// -1 means nothing changed /// </summary> /// <param name="t"></param> /// <param name="o"></param> /// <param name="h"></param> /// <param name="l"></param> /// <param name="c"></param> /// <param name="v"></param> /// <param name="isTriggerDataAdded"></param> /// <returns></returns> public int AddUpdate(long t, double o, double h, double l, double c, double v, bool isTriggerDataAdded = false) { if (this.Count == 0 || t > this.LastTime) { Open.Add(o); High.Add(h); Low.Add(l); Close.Add(c); Volume.Add(v); Time.Add(t); if (isTriggerDataAdded) { OnDataAddedOrUpdated?.Invoke(this, this, 1); } return(1); } else if (t == this.LastTime) { Open[this.Count - 1] = o; High[this.Count - 1] = h; Low[this.Count - 1] = l; Close[this.Count - 1] = c; Volume[this.Count - 1] = v; if (isTriggerDataAdded) { OnDataAddedOrUpdated?.Invoke(this, this, 0); } return(0); } return(-1); }
public int AddUpdate(string symbol, int interval, long t, double o, double h, double l, double c, double v, bool isTriggerDataAdded = false) { if (this.Symbol != symbol || this.Interval < interval || this.Interval % interval != 0) { return(0); } var time = t / this.Interval * this.Interval; if (this.Count == 0 || time > this.LastTime) { Open.Add(o); High.Add(h); Low.Add(l); Close.Add(c); Volume.Add(v); Time.Add(time); if (isTriggerDataAdded) { OnDataAddedOrUpdated?.Invoke(this, this, 1); } return(1); } else if (time == this.LastTime) { High[this.Count - 1] = Math.Max(h, High[this.Count - 1]); Low[this.Count - 1] = Math.Min(Low[this.Count - 1], l); Close[this.Count - 1] = c; if (interval == this.Interval) { Volume[this.Count - 1] = v; } else { Volume[this.Count - 1] += v; } if (isTriggerDataAdded) { OnDataAddedOrUpdated?.Invoke(this, this, 0); } return(0); } return(-1); }
private void Candles_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { var candle = (BinanceCandle)e.NewItems[0]; var wideness = 0.27m; var s1 = candle.Candle.Low * (100 + wideness) / 100; var l1 = candle.Candle.Low * (100 - wideness) / 100; var l2 = candle.Candle.Low * (100 - wideness * 2) / 100; var l3 = candle.Candle.Low * (100 - wideness * 3) / 100; var l4 = candle.Candle.Low * (100 - wideness * 4) / 100; Console.WriteLine("Low : {0}", candle.Candle.Low); Console.WriteLine("L1 : {0}", l1); Console.WriteLine("L2 : {0}", l2); Console.WriteLine("L3 : {0}", l3); Console.WriteLine("L4 : {0}", l4); Console.WriteLine("S1 : {0}", s1); string[] row = { candle.Name, candle.CollectedDate.ToLongTimeString(), candle.Candle.Open.ToString(), candle.Candle.Close.ToString(), candle.Candle.Low.ToString(), candle.Candle.High.ToString(), candle.Candle.Volume.ToString() }; var listViewItem = new ListViewItem(row); if (candle.Candle.Open < candle.Candle.Close) { listViewItem.ForeColor = System.Drawing.Color.Green; } else { listViewItem.ForeColor = System.Drawing.Color.Green; } FormUtils.AddListItem(listView1, listViewItem); if (candle != null) { candlesvalues.Add(new OhlcPoint { Close = double.Parse(candle.Candle.Close.ToString()), Open = double.Parse(candle.Candle.Open.ToString()), High = double.Parse(candle.Candle.High.ToString()), Low = double.Parse(candle.Candle.Low.ToString()) }); High.Add(new ObservableValue(double.Parse(candle.Candle.High.ToString()))); Low.Add(new ObservableValue(double.Parse(candle.Candle.Low.ToString()))); //Thread Safe Caller for Carte 1 //lets only use the last 60 values - To remove by Util function ! if (candlesvalues.Count > 60) { candlesvalues.RemoveAt(0); } if (High.Count > 60) { High.RemoveAt(0); } if (Low.Count > 60) { Low.RemoveAt(0); } if (Buyer.Count > 60) { Buyer.RemoveAt(0); } if (Seller.Count > 60) { Seller.RemoveAt(0); } if (Close.Count > 60) { Close.RemoveAt(0); } solidGauge1.Invoke((MethodInvoker) delegate { solidGauge1.Value = candle.Properties.Where(y => y.Key.ToString().Contains("TradeCount")).First().Value; solidGauge1.To = Global.Shared.IncomingBinance.BData.Select(y => y.Data.TradeCount).Max(); }); solidGauge2.Invoke((MethodInvoker) delegate { solidGauge2.Value = Double.Parse(candle.Properties.Where(y => y.Key.ToString().Contains("Volume")).First().Value.ToString()); solidGauge2.To = Double.Parse(Global.Shared.IncomingBinance.BData.Select(y => y.Data.Volume).Max().ToString()); }); Double TakerVolume = Double.Parse(candle.Properties.Where(y => y.Key.ToString().Contains("TakerBuyBaseAssetVolume")).First().Value.ToString()); Double TotalVolume = Double.Parse(candle.Properties.Where(y => y.Key.ToString().Contains("Volume")).First().Value.ToString()); solidGauge3.Invoke((MethodInvoker) delegate { solidGauge3.Value = TakerVolume; solidGauge3.To = TotalVolume; }); solidGauge4.Invoke((MethodInvoker) delegate { solidGauge4.Value = TotalVolume - TakerVolume; solidGauge4.To = TotalVolume; }); Buyer.Add(new ObservableValue(double.Parse(TakerVolume.ToString()))); Seller.Add(new ObservableValue(double.Parse((TotalVolume - TakerVolume).ToString()))); cartesianChart2.Invoke((MethodInvoker) delegate { cartesianChart2.Series[0].Values = Buyer; cartesianChart2.Series[1].Values = Seller; //cartesianChart2.Series[2].Values = Close; }); LastUID = candle.UID; } }
protected override void OnBarUpdate() { DateTime dtBegin = DateTime.MaxValue; switch (IntervalType) { case EnumIntervalType.Sec: dtBegin = MinBar.D.Date.AddHours(MinBar.D.Hour).AddMinutes(MinBar.D.Minute).AddSeconds(MinBar.D.Second / Interval * Interval); break; case EnumIntervalType.Min: dtBegin = MinBar.D.Date.AddHours(MinBar.D.Hour).AddMinutes(MinBar.D.Minute / Interval * Interval); break; case EnumIntervalType.Hour: dtBegin = MinBar.D.Date.AddHours(MinBar.D.Hour / Interval * Interval); break; case EnumIntervalType.Day: dtBegin = DateTime.ParseExact(MinBar.TradingDay.ToString(), "yyyyMMdd", null); break; case EnumIntervalType.Week: dtBegin = DateTime.ParseExact(MinBar.TradingDay.ToString(), "yyyyMMdd", null); dtBegin = dtBegin.Date.AddDays(1 - (byte)dtBegin.DayOfWeek); break; case EnumIntervalType.Month: dtBegin = DateTime.ParseExact(MinBar.TradingDay.ToString(), "yyyyMMdd", null); dtBegin = new DateTime(dtBegin.Year, dtBegin.Month, 1); break; case EnumIntervalType.Year: dtBegin = DateTime.ParseExact(MinBar.TradingDay.ToString(), "yyyyMMdd", null); dtBegin = new DateTime(dtBegin.Year, 1, 1); break; default: throw new Exception("参数错误"); } if (bar == null) //首次调用 { bar = new Bar { D = dtBegin, I = MinBar.I, V = MinBar.V, // kOld.preVol == 0 ? 0 : _tick.Volume - kOld.preVol; }; bar.H = MinBar.H; bar.L = MinBar.L; bar.O = MinBar.O; bar.C = MinBar.C; newbar = true; } else { if (bar.D == dtBegin) //在当前K线范围内 { newbar = false; bar.H = Math.Max(bar.H, MinBar.H); bar.L = Math.Min(bar.L, MinBar.L); bar.C = MinBar.C; bar.V = bar.V + MinBar.V; bar.I = MinBar.I; } else if (dtBegin > bar.D) { newbar = true; bar.D = dtBegin; bar.V = MinBar.V; bar.I = MinBar.I; bar.O = MinBar.O; bar.H = MinBar.H; bar.L = MinBar.L; bar.C = MinBar.C; } } if (newbar) { Date.Add(double.Parse(bar.D.ToString("yyyyMMdd"))); Time.Add(double.Parse(bar.D.ToString("0.HHmmss"))); Open.Add(bar.O); High.Add(bar.H); Low.Add(bar.L); Close.Add(bar.C); Volume.Add(bar.V); OpenInterest.Add(bar.I); } else { High[0] = bar.H; Low[0] = bar.L; Close[0] = bar.C; Volume[0] = bar.V; OpenInterest[0] = bar.I; } }
protected override void OnBarUpdate() { DateTime dtBegin = DateTime.MaxValue; switch (IntervalType) { case IntervalType.Sec: dtBegin = MinBar.Time.Date.AddHours(MinBar.Time.Hour).AddMinutes(MinBar.Time.Minute).AddSeconds(MinBar.Time.Second / Interval * Interval); break; case IntervalType.Min: dtBegin = MinBar.Time.Date.AddHours(MinBar.Time.Hour).AddMinutes(MinBar.Time.Minute / Interval * Interval); break; case IntervalType.Hour: dtBegin = MinBar.Time.Date.AddHours(MinBar.Time.Hour / Interval * Interval); break; case IntervalType.Day: dtBegin = DateTime.ParseExact(MinBar.TradingDay.ToString(), "yyyyMMdd", null); break; case IntervalType.Week: dtBegin = DateTime.ParseExact(MinBar.TradingDay.ToString(), "yyyyMMdd", null); dtBegin = dtBegin.Date.AddDays(1 - (byte)dtBegin.DayOfWeek); break; case IntervalType.Month: dtBegin = DateTime.ParseExact(MinBar.TradingDay.ToString(), "yyyyMMdd", null); dtBegin = new DateTime(dtBegin.Year, dtBegin.Month, 1); break; case IntervalType.Year: dtBegin = DateTime.ParseExact(MinBar.TradingDay.ToString(), "yyyyMMdd", null); dtBegin = new DateTime(dtBegin.Year, 1, 1); break; default: throw new Exception("参数错误"); } if (bar == null) //首次调用 { bar = new Bar { Time = dtBegin, Volume = MinBar.Volume, // kOld.preVol == 0 ? 0 : _tick.Volume - kOld.preVol; }; bar.High = MinBar.High; bar.Low = MinBar.Low; bar.Open = MinBar.Open; bar.Close = MinBar.Close; newbar = true; } else { if (bar.Time == dtBegin) //在当前K线范围内 { newbar = false; bar.High = Math.Max(bar.High, MinBar.High); bar.Low = Math.Min(bar.Low, MinBar.Low); bar.Close = MinBar.Close; bar.Volume = bar.Volume + MinBar.Volume; } else if (dtBegin > bar.Time) { newbar = true; bar.Time = dtBegin; bar.Volume = MinBar.Volume; bar.Open = MinBar.Open; bar.High = MinBar.High; bar.Low = MinBar.Low; bar.Close = MinBar.Close; } } if (newbar) { Date.Add(double.Parse(bar.Time.ToString("yyyyMMdd"))); Time.Add(double.Parse(bar.Time.ToString("0.HHmmss"))); Open.Add(bar.Open); High.Add(bar.High); Low.Add(bar.Low); Close.Add(bar.Close); Volume.Add(bar.Volume); } else { High[0] = bar.High; Low[0] = bar.Low; Close[0] = bar.Close; Volume[0] = bar.Volume; } }