/// <summary> Функция сохранения котировок </summary> /// <param name="sec"></param> public void SaveCharts(int timeFrame = 1, int limitSave = 20, int stepWait = 5) { string dir = GetDirCharts(); var frame = this.CollectionTimeFrames.FirstOrDefault(t => t.TimeFrame == timeFrame); if (frame.IsNull()) { return; } int limit = 20; //Кол-ва сохраняемых свечек за раз while (limit > 0) { frame.LockCollection(); var candle = frame.MainCollection.ToArray().FirstOrDefault(c => !c._write && c._lastUpdate < DateTime.Now.AddMinutes(-5));// this.IndexWriteCandle); frame.UnlockCollection(); if (!candle.Empty()) { string filename = this.Security.Code + "." + this.Security.Class.Code + "_" + timeFrame + "_" + candle.Time.ToShortDateString() + ".charts"; filename = dir + filename; if (this.SaveCandleInfile(filename, candle) != -1) { candle._write = true; this.LastIndexCandle = candle; } } limit--; Thread.Sleep(2); } }
/// <summary> /// /// </summary> private int SaveCandleInfile(string filename, CandleLib.CandleData candle) { FileLib.WFile file = new FileLib.WFile(filename); char split = '\t'; string text = candle.Time.ToString() + split + candle.FirstId.ToString() + split + candle.Open.ToString().Replace(',', '.') + split + candle.Close.ToString().Replace(',', '.') + split + //3 candle.High.ToString().Replace(',', '.') + split + candle.Low.ToString().Replace(',', '.') + split + candle.LastId.ToString() + split + //6 candle.LastTime.ToString() + split; //7 if (candle.HorVolumes.HVolCollection.Count > 0) { candle.HorVolumes.HVolCollection.CollectionArray.ForEach <ChartVol>((vol) => { if (vol.VolBuy > 0) { text += "b:" + vol.Price.ToString().Replace(',', '.') + ":" + vol.VolBuy.ToString() + split; } if (vol.VolSell > 0) { text += "s:" + vol.Price.ToString().Replace(',', '.') + ":" + vol.VolSell.ToString() + split; } }); } LastSaveCandle = candle; if (file.Append(text) == -1) { return(-1); } return(0); }
/// <summary> Расчет горизонтальных объемов (для ускорения) </summary> /// <param name="collection"></param> public void CalculationHVol(IEnumerable <CandleLib.CandleData> collection) { if (collection.IsNull()) { return; } this.LisForHVol = new List <CandleLib.CandleData>(); CandleLib.CandleData mainCandle = new CandleLib.CandleData(DateTime.Now); int indx = 0; foreach (var el in collection.ToArray()) { if (indx > 0) { mainCandle.High = mainCandle.High < el.High ? el.High : mainCandle.High; mainCandle.Low = mainCandle.Low > el.Low ? el.Low : mainCandle.Low; if (el.HorVolumes.HVolCollection.Count > 0) { foreach (var hv in el.HorVolumes.HVolCollection.CollectionArray) { mainCandle.HorVolumes.AddBuy(hv.Price, hv.VolBuy); mainCandle.HorVolumes.AddSell(hv.Price, hv.VolSell); } } } indx++; } this.LisForHVol.Add(new CandleLib.CandleData(DateTime.Now)); //Первая свечка изменяемая this.LisForHVol.Add(mainCandle); //Последующие расчитываются один раз }
/// <summary> Рисует одну свечку </summary> /// <param name="canvas"></param> /// <param name="rectPaint"></param> /// <param name="candleData"></param> /// <param name="index"></param> /// <param name="maxPrice"></param> /// <param name="minPrice"></param> private void PaintOneCandle(Graphics canvas, Rectangle rectPaint, CandleLib.CandleData candleData, int index, decimal maxPrice, decimal minPrice) { int tailY1 = GraphicShape.GetCoordinate(rectPaint.Height, maxPrice, minPrice, candleData.High); int tailY2 = GraphicShape.GetCoordinate(rectPaint.Height, maxPrice, minPrice, candleData.Low); int tailX1 = (int)((rectPaint.Width - this.WidthOneCandle * index) + this.WidthOneCandle / 2); int bodyX = (int)(rectPaint.Width - this.WidthOneCandle * index); int bodyY = GraphicShape.GetCoordinate(rectPaint.Height, maxPrice, minPrice, candleData.Open > candleData.Close ? candleData.Open : candleData.Close); int bodyWidth = this.WidthOneCandle - MarginCandle; //- чтобы свечки не слипались int bodyHeight = GraphicShape.GetCoordinate(rectPaint.Height, maxPrice, minPrice, candleData.Open < candleData.Close ? candleData.Open : candleData.Close); bodyHeight = bodyHeight - bodyY; bodyHeight = bodyHeight == 0 ? bodyHeight + 1 : bodyHeight; GraphicShape.PaintLine(canvas, new Point(tailX1, tailY1), new Point(tailX1, tailY2), Color.Black, 2); GraphicShape.PaintRectangle(canvas, bodyX, bodyY, bodyWidth, bodyHeight, Color.Black, candleData.Open > candleData.Close ? Color.LightCoral : Color.LightGreen); if (OnPaintCandle != null) { OnPaintCandle(new DataCandle() { PaintRect = rectPaint, Candle = candleData, TailCoord = new TailCoord() { High = new Point(tailX1, tailY1), Low = new Point(tailX1, tailY2) }, Body = new Rectangle() { X = bodyX, Y = bodyY, Width = bodyWidth, Height = bodyHeight }, Index = index }); } }