private PricePoints GetPricePoints(OrderBookDao.Entity orderBook) { if (pricePointsCache == null) { pricePointsCache = new Dictionary <string, Dictionary <OrderBookDao.Entity, PricePoints> >(); } if (pricePointsCache.ContainsKey(selectedInstrument.Name) == false) { pricePointsCache.Add(selectedInstrument.Name, new Dictionary <OrderBookDao.Entity, PricePoints>()); } var orderBookPricePoints = pricePointsCache[selectedInstrument.Name]; if (orderBookPricePoints.ContainsKey(orderBook)) { return(orderBookPricePoints[orderBook]); } PricePoints pricePoints = new PricePoints(); pricePoints.rate = orderBook.Rate; pricePoints.price_points = new Dictionary <float, PricePoint>(); foreach (var pp in orderBook.GetPricePointsOrderByPrice()) { pricePoints.price_points[pp.Price] = new PricePoint() { os = pp.Os, ol = pp.Ol, ps = pp.Ps, pl = pp.Pl }; } orderBookPricePoints[orderBook] = pricePoints; return(pricePoints); }
private void LoadChart(Chart chart, OrderBookDao.Entity orderBook) { PricePoints pricePoints = new PricePoints(); pricePoints.rate = orderBook.Rate; pricePoints.price_points = new Dictionary <float, PricePoint>(); foreach (var pp in orderBook.GetPricePointsOrderByPrice()) { pricePoints.price_points[pp.Price] = new PricePoint() { os = pp.Os, ol = pp.Ol, ps = pp.Ps, pl = pp.Pl }; } LoadChart(chart, orderBook.DateTime, pricePoints); }
private void SavePricePoints(string instrument, DateTime time, PricePoints pricePoints) { using (var transaction = connection.BeginTransaction()) { Int64 id = 0; using (var cmd = new NpgsqlCommand()) { cmd.Connection = connection; cmd.CommandText = "insert into order_book(instrument,date_time,rate) values(:instrument,:date_time,:rate) returning id"; cmd.Parameters.Add(new NpgsqlParameter("instrument", DbType.String)); cmd.Parameters.Add(new NpgsqlParameter("date_time", DbType.DateTime)); cmd.Parameters.Add(new NpgsqlParameter("rate", DbType.Single)); cmd.Parameters["instrument"].Value = instrument; cmd.Parameters["date_time"].Value = time; cmd.Parameters["rate"].Value = pricePoints.rate; id = (Int64)cmd.ExecuteScalar(); } using (var cmd = new NpgsqlCommand()) { cmd.Connection = connection; cmd.CommandText = "insert into order_book_price_point(order_book_id,price,os,ps,ol,pl) values(:order_book_id,:price,:os,:ps,:ol,:pl)"; cmd.Parameters.Add(new NpgsqlParameter("order_book_id", DbType.Int64)); cmd.Parameters.Add(new NpgsqlParameter("price", DbType.Single)); cmd.Parameters.Add(new NpgsqlParameter("os", DbType.Single)); cmd.Parameters.Add(new NpgsqlParameter("ps", DbType.Single)); cmd.Parameters.Add(new NpgsqlParameter("ol", DbType.Single)); cmd.Parameters.Add(new NpgsqlParameter("pl", DbType.Single)); foreach (var price in pricePoints.price_points.Keys.OrderBy(k => k)) { var price_point = pricePoints.price_points[price]; cmd.Parameters["order_book_id"].Value = id; cmd.Parameters["price"].Value = price; cmd.Parameters["os"].Value = price_point.os; cmd.Parameters["ps"].Value = price_point.ps; cmd.Parameters["ol"].Value = price_point.ol; cmd.Parameters["pl"].Value = price_point.pl; cmd.ExecuteNonQuery(); } } transaction.Commit(); } }
private void LoadChart(Chart chart, DateTime dateTime, PricePoints pricePoints) { chart.Series.Clear(); chart.ChartAreas.Clear(); chart.Titles.Clear(); chart.Size = new Size(10000, 400); chart.Titles.Add(new Title(dateTime.ToString())); ChartArea chartArea = new ChartArea(); chartArea.AxisX.Interval = 0.05d; chartArea.AxisX.Minimum = 100f; chartArea.AxisX.Maximum = 125f; chartArea.AxisX.LabelStyle.Format = "f2"; chartArea.AxisX.MajorGrid.Enabled = true; chartArea.AxisX.MajorGrid.Interval = 0.1d; chartArea.AxisX.MinorGrid.Enabled = true; chartArea.AxisX.MinorGrid.LineColor = Color.LightGray; chartArea.AxisX.MinorGrid.Interval = 0.05d; chartArea.AxisY.Maximum = 5.0f; chartArea.AxisY.Minimum = -1.0f; chartArea.AxisY.Interval = 1.0f; chartArea.AxisY.MajorGrid.LineColor = Color.LightGray; chart.ChartAreas.Add(chartArea); Series seriesRate = new Series(); seriesRate.ChartType = SeriesChartType.Column; var dataPoint = new DataPoint(pricePoints.rate, 5.0f); seriesRate.SetCustomProperty("PointWidth", "0.01"); // dataPoint["PointWidth"] = "1.0"; seriesRate.Points.Add(dataPoint); seriesRate.Color = Color.Orange; chart.Series.Add(seriesRate); Series seriesDeltaPos = new Series(); seriesDeltaPos.ChartType = SeriesChartType.Column; seriesDeltaPos.LegendText = "buy - sell position"; seriesDeltaPos.Color = Color.LightGreen; foreach (var price in pricePoints.price_points.Keys.OrderBy(k => k)) { var dprice = decimal.ToDouble((decimal)price); var p = pricePoints.price_points[price]; seriesDeltaPos.Points.Add(new DataPoint(dprice, p.pl - p.ps)); } chart.Series.Add(seriesDeltaPos); Series[] lines = new Series[4]; string[] titles = new string[] { "order_long", "order_short", "pos_long", "pos_short" }; Color[] colors = new Color[] { Color.Pink, Color.LightBlue, Color.Red, Color.Blue }; for (int i = 0; i < 4; i++) { Series seriesLine = new Series(); chart.Series.Add(seriesLine); seriesLine.ChartType = SeriesChartType.Line; seriesLine.LegendText = titles[i]; seriesLine.BorderWidth = 1; seriesLine.Color = colors[i]; seriesLine.MarkerStyle = MarkerStyle.Circle; seriesLine.MarkerSize = 2; lines[i] = seriesLine; } foreach (var price in pricePoints.price_points.Keys.OrderBy(k => k)) { var dprice = decimal.ToDouble((decimal)price); var p = pricePoints.price_points[price]; lines[0].Points.Add(new DataPoint(dprice, p.ol)); lines[1].Points.Add(new DataPoint(dprice, p.os)); lines[2].Points.Add(new DataPoint(dprice, p.pl)); lines[3].Points.Add(new DataPoint(dprice, p.ps)); } }
private void LoadChart(Chart chart, DateTime dateTime, OrderBookDao.Entity orderBook, OrderBookDao.Entity previousOrderBook, bool isLatestChart) { PricePoints pricePoints = GetPricePoints(orderBook); PricePoints previousPricePoints = previousOrderBook != null?GetPricePoints(previousOrderBook) : null; chart.Series.Clear(); chart.ChartAreas.Clear(); chart.Titles.Clear(); chart.Size = new Size(6000, 400); ChartArea chartArea = new ChartArea(); chartArea.AxisX.Interval = selectedInstrument.GraphInterval; chartArea.AxisX.Minimum = selectedInstrument.GraphMinPrice; chartArea.AxisX.Maximum = selectedInstrument.GraphMaxPrice; chartArea.AxisX.LabelStyle.Format = selectedInstrument.AxisLabelFormat; chartArea.AxisX.MajorGrid.Enabled = true; chartArea.AxisX.MajorGrid.Interval = selectedInstrument.GraphInterval * 2; chartArea.AxisX.MinorGrid.Enabled = true; chartArea.AxisX.MinorGrid.LineColor = Color.LightGray; chartArea.AxisX.MinorGrid.Interval = selectedInstrument.GraphInterval; /* chartArea.AxisX2.Enabled = AxisEnabled.True; * chartArea.AxisX2.Minimum = chartArea.AxisX.Minimum; * chartArea.AxisX2.Maximum = chartArea.AxisX.Maximum;*/ // chartArea.AxisX2.Interval = VOLUME_PRICE_GRANURALITY; chartArea.AxisY.Maximum = 5.0f; chartArea.AxisY.Minimum = 0f; chartArea.AxisY.Interval = 1.0f; chartArea.AxisY.MajorGrid.LineColor = Color.LightGray; chartArea.AxisY2.Enabled = AxisEnabled.True; chartArea.AxisY2.Maximum = 2.5f; chartArea.AxisY2.Minimum = -2.5f; chartArea.AxisY2.Interval = 1.0f; chart.ChartAreas.Add(chartArea); Series volumeSeries = new Series(); volumeSeries.ChartType = SeriesChartType.Column; // volumeSeries.XAxisType = AxisType.Secondary; volumeSeries.Color = Color.LightGray; volumeSeries.SetCustomProperty("PixelPointWidth", "2"); // volumeSeries.SetCustomProperty("PointWidth", "0.5"); chart.Series.Add(volumeSeries); var UISyncContext = TaskScheduler.FromCurrentSynchronizationContext(); if (isLatestChart) { if (latestS5Candles == null) { latestVolumeSeries = volumeSeries; retriveVolumeQueue.Add(() => { var candlesticks = RetrieveS5Candles(orderBook); Invoke(new Action(() => { latestS5Candles = candlesticks.ToList(); LoadVolumeSeries(volumeSeries, latestS5Candles); })); }); } // , UISyncContext); /* * lock (volumeCandlesLock) { * if (latestS5Candles != null) { * LoadVolumeSeries(latestVolumeSeries,latestS5Candles); * } * }*/ } else { retriveVolumeQueue.Add(() => { var candlesticks = RetrieveS5Candles(orderBook); Invoke(new Action(() => { LoadVolumeSeries(volumeSeries, candlesticks); })); }); /* * new TaskFactory().StartNew(() => RetrieveS5Candles(orderBook)).ContinueWith(candlesticks => { * latestS5Candles = candlesticks.Result.ToList(); * LoadVolumeSeries(volumeSeries, latestS5Candles); * }, UISyncContext);*/ /* * if (gettingVolumes == false) { * gettingVolumes = true; * RetrieveS5CandlesAsync(orderBook).ContinueWith(candlesticks => { * LoadVolumeSeries(volumeSeries, candlesticks.Result); * gettingVolumes = false; * }, UISyncContext); * }*/ } Series seriesDeltaPos = new Series(); chart.Series.Add(seriesDeltaPos); seriesDeltaPos.YAxisType = AxisType.Secondary; seriesDeltaPos.ChartType = SeriesChartType.StackedColumn; seriesDeltaPos.LegendText = "buy - sell position"; seriesDeltaPos.SetCustomProperty("PixelPointWidth", "14"); seriesDeltaPos.Color = Color.FromArgb(100, 0, 255, 0); Series seriesPreviousDeltaPos = new Series(); chart.Series.Add(seriesPreviousDeltaPos); seriesPreviousDeltaPos.YAxisType = AxisType.Secondary; seriesPreviousDeltaPos.ChartType = SeriesChartType.StackedColumn; seriesPreviousDeltaPos.SetCustomProperty("PixelPointWidth", "14"); seriesPreviousDeltaPos.Color = Color.FromArgb(100, 0, 255, 0); float totalPl = 0; float totalPs = 0; foreach (var price in pricePoints.price_points.Keys.OrderBy(k => k)) { var dprice = (double)price; var p = pricePoints.price_points[price]; var curD = p.pl - p.ps; totalPl += p.pl; totalPs += p.ps; if (previousPricePoints != null) { if (previousPricePoints.price_points.ContainsKey(price)) { var pre = previousPricePoints.price_points[price]; var preD = pre.pl - pre.ps; if (preD < 0 && curD > 0) { seriesDeltaPos.Points.Add(new DataPoint(dprice, curD) { Color = Color.FromArgb(100, 255, 0, 0) }); seriesPreviousDeltaPos.Points.Add(new DataPoint(dprice, preD) { Color = Color.FromArgb(100, 255, 0, 0) }); } else if (preD > 0 && curD < 0) { seriesDeltaPos.Points.Add(new DataPoint(dprice, curD) { Color = Color.FromArgb(100, 0, 0, 255) }); seriesPreviousDeltaPos.Points.Add(new DataPoint(dprice, preD) { Color = Color.FromArgb(100, 0, 0, 255) }); } else if (preD > 0 && curD > 0) { seriesDeltaPos.Points.Add(new DataPoint(dprice, Math.Min(curD, preD)) { Color = Color.FromArgb(100, 0, 255, 0) }); seriesPreviousDeltaPos.Points.Add(new DataPoint(dprice, Math.Abs(curD - preD)) { Color = curD > preD ? Color.FromArgb(100, 255, 0, 0) : Color.FromArgb(25, 0, 0, 255) }); } else { seriesDeltaPos.Points.Add(new DataPoint(dprice, Math.Max(curD, preD)) { Color = Color.FromArgb(100, 0, 255, 0) }); seriesPreviousDeltaPos.Points.Add(new DataPoint(dprice, -Math.Abs(curD - preD)) { Color = curD > preD ? Color.FromArgb(25, 255, 0, 0) : Color.FromArgb(100, 0, 0, 255) }); } } else { seriesDeltaPos.Points.Add(new DataPoint(dprice, curD)); seriesPreviousDeltaPos.Points.Add(new DataPoint(dprice, 0)); } } else { seriesDeltaPos.Points.Add(new DataPoint(dprice, curD)); } } Series[] lines = new Series[4]; string[] titles = new string[] { "order_long", "order_short", "pos_long", "pos_short" }; Color[] colors = new Color[] { Color.Pink, Color.LightBlue, Color.Red, Color.Blue }; for (int i = 0; i < 4; i++) { Series seriesLine = new Series(); chart.Series.Add(seriesLine); seriesLine.ChartType = SeriesChartType.Line; seriesLine.LegendText = titles[i]; seriesLine.BorderWidth = 1; seriesLine.Color = colors[i]; seriesLine.MarkerStyle = MarkerStyle.Circle; seriesLine.MarkerSize = 2; lines[i] = seriesLine; } foreach (var price in pricePoints.price_points.Keys.OrderBy(k => k)) { var dprice = decimal.ToDouble((decimal)price); var p = pricePoints.price_points[price]; lines[0].Points.Add(new DataPoint(dprice, p.ol)); lines[1].Points.Add(new DataPoint(dprice, p.os)); lines[2].Points.Add(new DataPoint(dprice, p.pl)); lines[3].Points.Add(new DataPoint(dprice, p.ps)); } Series seriesRate = new Series(); chart.Series.Add(seriesRate); seriesRate.ChartType = SeriesChartType.Line; // seriesRate.SetCustomProperty("PointWidth", "0.01"); seriesRate.Points.Add(new DataPoint(pricePoints.rate, 5.0f)); seriesRate.Points.Add(new DataPoint(pricePoints.rate, -1.0f)); seriesRate.Color = Color.Pink; seriesRate.BorderWidth = 3; if (isLatestChart) { streamPriceSeries = new Series(); chart.Series.Add(streamPriceSeries); streamPriceSeries.ChartType = SeriesChartType.Line; // streamPriceSeries.SetCustomProperty("PointWidth", "0.01"); streamPriceSeries.Points.Add(new DataPoint(latestPrice, 5.0f)); streamPriceSeries.Points.Add(new DataPoint(latestPrice, -1.0f)); streamPriceSeries.Color = Color.Red; streamPriceSeries.BorderWidth = 3; } float pld = 0; float psd = 0; if (previousPricePoints != null) { float prevTotalPl = 0; float prevTotalPs = 0; foreach (var pp in previousPricePoints.price_points) { prevTotalPl += pp.Value.pl; prevTotalPs += pp.Value.ps; } pld = totalPl - prevTotalPl; psd = totalPs - prevTotalPs; } chart.Titles.Add(String.Format("{0} short position:{1:0.###}({2:+0.###;-0.###;0.###}) long position:{3:0.###}({4:+0.###;-0.###;0.###}) ", dateTime, totalPs, psd, totalPl, pld)); }