private static void Client_SpreadReceived(object sender, Events.KrakenDataEventArgs<SpreadMessage> e) { // += (sender, e) => Console.WriteLine($"Spread received"); return; string pair = e.Pair.ToString(); Sec security = Securities.Find(sec => sec.NameInSocket == pair); DateTime time = new DateTime(1970, 1, 1).AddSeconds(Convert.ToDouble(e.DataMessage.Time)); OsEngine.Entity.MarketDepth depth = new OsEngine.Entity.MarketDepth(); depth.Time = time; depth.SecurityNameCode = security.NameInRest; OsEngine.Entity.MarketDepthLevel ask = new OsEngine.Entity.MarketDepthLevel(); ask.Price = e.DataMessage.Ask; ask.Ask = 1; OsEngine.Entity.MarketDepthLevel bid = new OsEngine.Entity.MarketDepthLevel(); bid.Price = e.DataMessage.Bid; bid.Bid = 1; depth.Asks.Add(ask); depth.Bids.Add(bid); if(MarketDepthUpdateEvent != null) { MarketDepthUpdateEvent(depth); } }
// operation of the flow of drawing glasses // работа потока прорисовывающего стаканы /// <summary> /// draw a glass /// прорисовать стакан /// </summary> private void TryPaintMarketDepth() { if (_hostGlass == null) { return; } MarketDepth depth = _currentMaretDepth; _currentMaretDepth = null; if (depth != null) { PaintMarketDepth(depth); } if (_bid != 0 && _ask != 0) { PaintBidAsk(_bid, _ask); _bid = 0; _ask = 0; } }
// создание свечек из Стакана public void SetNewMarketDepth(MarketDepth marketDepth) { if (_isStarted == false) { return; } if (_isStoped) { return; } if (SeriesCreateDataType != CandleSeriesCreateDataType.MarketDepth) { return; } if (marketDepth.Bids == null || marketDepth.Bids.Count == 0 || marketDepth.Bids[0].Price == 0) { return; } if (marketDepth.Asks == null || marketDepth.Asks.Count == 0 || marketDepth.Asks[0].Price == 0) { return; } decimal price = marketDepth.Bids[0].Price + (marketDepth.Asks[0].Price - marketDepth.Bids[0].Price) / 2; UpDateCandle(marketDepth.Time, price, 1, true, Side.UnKnown); }
// создание свечек из Стакана public void SetNewMarketDepth(MarketDepth marketDepth) { if (_isStarted == false) { return; } if (_isStoped) { return; } if (_timeFrameBuilder.CandleMarketDataType != CandleMarketDataType.MarketDepth) { return; } if (marketDepth.Bids == null || marketDepth.Bids.Count == 0 || marketDepth.Bids[0].Price == 0) { return; } if (marketDepth.Asks == null || marketDepth.Asks.Count == 0 || marketDepth.Asks[0].Price == 0) { return; } decimal price = marketDepth.Bids[0].Price + (marketDepth.Asks[0].Price - marketDepth.Bids[0].Price) / 2; UpDateCandle(marketDepth.Time, price, 1, true, Side.None); }
/// <summary> /// draw a glass /// прорисовать стакан /// </summary> private void PaintMarketDepth(MarketDepth depth) { try { _lastMarketDepth = depth; if (_hostGlass == null || depth == null) { return; } if (_glassBox.InvokeRequired) { _glassBox.Invoke(new Action <MarketDepth>(PaintMarketDepth), depth); return; } if (depth.Bids[0].Bid == 0 || depth.Asks[0].Ask == 0) { return; } decimal maxVol = 0; decimal allBid = 0; decimal allAsk = 0; for (int i = 0; depth.Bids != null && i < 25; i++) { if (i < depth.Bids.Count) { _glassBox.Rows[25 + i].Cells[2].Value = depth.Bids[i].Price.ToStringWithNoEndZero(); _glassBox.Rows[25 + i].Cells[3].Value = depth.Bids[i].Bid.ToStringWithNoEndZero(); if (depth.Bids[i].Bid > maxVol) { maxVol = depth.Bids[i].Bid; } allAsk += depth.Bids[i].Bid; } else if (_glassBox.Rows[25 + i].Cells[2].Value != null) { _glassBox.Rows[25 + i].Cells[0].Value = null; _glassBox.Rows[25 + i].Cells[1].Value = null; _glassBox.Rows[25 + i].Cells[2].Value = null; _glassBox.Rows[25 + i].Cells[3].Value = null; } } for (int i = 0; depth.Asks != null && i < 25; i++) { if (i < depth.Asks.Count) { _glassBox.Rows[24 - i].Cells[2].Value = depth.Asks[i].Price.ToStringWithNoEndZero(); _glassBox.Rows[24 - i].Cells[3].Value = depth.Asks[i].Ask.ToStringWithNoEndZero(); if (depth.Asks[i].Ask > maxVol) { maxVol = depth.Asks[i].Ask; } allBid += depth.Asks[i].Ask; } else if (_glassBox.Rows[24 - i].Cells[2].Value != null) { _glassBox.Rows[24 - i].Cells[2].Value = null; _glassBox.Rows[24 - i].Cells[3].Value = null; _glassBox.Rows[24 - i].Cells[0].Value = null; _glassBox.Rows[24 - i].Cells[1].Value = null; } } // volume in sticks for ask // объём в палках для аска for (int i = 0; depth.Bids != null && i < 25 && i < depth.Bids.Count; i++) { int percentFromMax = Convert.ToInt32(depth.Bids[i].Bid / maxVol * 50); if (percentFromMax == 0) { percentFromMax = 1; } StringBuilder builder = new StringBuilder(percentFromMax); for (int i2 = 0; i2 < percentFromMax; i2++) { builder.Append('|'); } _glassBox.Rows[25 + i].Cells[1].Value = builder; } // volume in bid sticks // объём в палках для бида for (int i = 0; depth.Asks != null && i < 25 && i < depth.Asks.Count; i++) { int percentFromMax = Convert.ToInt32(depth.Asks[i].Ask / maxVol * 50); if (percentFromMax == 0) { percentFromMax = 1; } StringBuilder builder = new StringBuilder(percentFromMax); for (int i2 = 0; i2 < percentFromMax; i2++) { builder.Append('|'); } _glassBox.Rows[24 - i].Cells[1].Value = builder; } decimal maxSeries; if (allAsk > allBid) { maxSeries = allAsk; } else { maxSeries = allBid; } // volume cumulative for ask // объём комулятивный для аска decimal summ = 0; for (int i = 0; depth.Bids != null && i < 25 && i < depth.Bids.Count; i++) { summ += depth.Bids[i].Bid; int percentFromMax = Convert.ToInt32(summ / maxSeries * 50); if (percentFromMax == 0) { percentFromMax = 1; } StringBuilder builder = new StringBuilder(percentFromMax); for (int i2 = 0; i2 < percentFromMax; i2++) { builder.Append('|'); } _glassBox.Rows[25 + i].Cells[0].Value = builder; } // volume is cumulative for bids // объём комулятивный для бида summ = 0; for (int i = 0; depth.Asks != null && i < 25 && i < depth.Asks.Count; i++) { summ += depth.Asks[i].Ask; int percentFromMax = Convert.ToInt32(summ / maxSeries * 50); if (percentFromMax == 0) { percentFromMax = 1; } StringBuilder builder = new StringBuilder(percentFromMax); for (int i2 = 0; i2 < percentFromMax; i2++) { builder.Append('|'); } _glassBox.Rows[24 - i].Cells[0].Value = builder; } // _glassBox.Refresh(); } catch (Exception error) { //SendNewLogMessage(error.ToString(), LogMessageType.Error); } }
/// <summary> /// to send a glass to draw /// отправить стакан на прорисовку /// </summary> public void ProcessMarketDepth(MarketDepth depth) { _currentMaretDepth = depth; }
private void MarketDepthSpreadAnaliser_MarketDepthUpdateEvent(MarketDepth obj) { if (Tabs.Count < 2) { return; } foreach (var tab in Tabs) { if (!tab.IsConnected) { return; } if (tab.MarketDepth == null) { return; } if (tab.MarketDepth.Asks == null || tab.MarketDepth.Asks.Count == 0) { return; } if (tab.MarketDepth.Bids == null || tab.MarketDepth.Bids.Count == 0) { return; } if (tab.PriceBestBid == 0 || tab.PriceBestAsk == 0) { return; } if (tab.CandlesAll == null || tab.CandlesAll.Count == 0) { return; } //если сильный спрэд то возможно ошибка в данных if ((tab.PriceBestAsk - tab.PriceBestBid) / tab.PriceBestBid > 0.005m) { return; } if ((tab.PriceBestAsk - tab.CandlesAll[tab.CandlesAll.Count - 1].Close) / tab.CandlesAll[tab.CandlesAll.Count - 1].Close > MaxSpread) { return; } if ((tab.CandlesAll[tab.CandlesAll.Count - 1].Close - tab.PriceBestBid) / tab.PriceBestBid > MaxSpread) { return; } } lock (_locker) { //CalcIndex(); //CalcChanges(); decimal NewSpread = CalcSpread(); NewSpread = Math.Round(NewSpread, 2); if (Spread != NewSpread) { if (SpreadChangeEvent != null) { SpreadChangeEvent(NewSpread); } Spread = NewSpread; } decimal NewProfit = 0; if (!CanChange) { NewProfit = GetProfitPersent(); NewProfit = Math.Round(NewProfit, 2); if (Profit != NewProfit) { if (SpreadChangeEvent != null) { ProfitChangeEvent(NewProfit); } Profit = NewProfit; } } } }