Esempio n. 1
0
        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);
        }
Esempio n. 2
0
 private List <Candlestick> RetrieveS5Candles(OrderBookDao.Entity orderBook)
 {
     using (DBUtils.OpenThreadConnection()) {
         DateTime firstDateTime = orderBook.DateTime;
         DateTime startDateTime = new DateTime(
             firstDateTime.Year, firstDateTime.Month, firstDateTime.Day, firstDateTime.Hour, firstDateTime.Minute, 0, DateTimeKind.Local);
         return(new CandlesticksGetter()
         {
             Start = startDateTime,
             End = startDateTime.AddMinutes(20),
             Granularity = "S5",
             Instrument = selectedInstrument.Name
         }.Execute().ToList());
     }
 }
Esempio n. 3
0
        private static OrderBookPricePointDao.Entity GetRatePricePoint(OrderBookDao.Entity orderBook, float rate)
        {
            float min = float.MaxValue;

            OrderBookPricePointDao.Entity closedPricePoint = null;
            foreach (var pricePoint in orderBook.GetPricePointsOrderByPrice())
            {
                float d = Math.Abs(rate - pricePoint.Price);
                if (d < min)
                {
                    min = d;
                    closedPricePoint = pricePoint;
                }
            }
            return(closedPricePoint);
        }
Esempio n. 4
0
        private void LoadChart(Chart chart, int index)
        {
            var list = orderBooks[selectedInstrument.Name];

            OrderBookDao.Entity orderBook         = list[index];
            OrderBookDao.Entity previousOrderBook = (index + 1 < list.Count()) ? list[index + 1] : null;
            LoadChart(chart, orderBook.DateTime, orderBook, previousOrderBook, index == 0);



            /*				splitContainer1.Panel2.HorizontalScroll.Value = splitContainer1.Panel1.HorizontalScroll.Value
             *                                      = (int) chart.ChartAreas[0].AxisX.ValueToPixelPosition(orderBook.Rate);*/

//				Console.WriteLine("ratePixel"+chart.ChartAreas[0].AxisX.ValueToPixelPosition(orderBook.Rate));
//				Console.WriteLine("splitContainer1.Panel2.HorizontalScroll.Value" + splitContainer1.Panel2.HorizontalScroll.Value);
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
 private void 近傍ポジション変化_Click(object sender, EventArgs e)
 {
     RunTask(sender, report => {
         report.Version         = 3;
         report.IsForceOverride = true;
         report.Comment         = "";
         report.SetHeader("date_time", "pre_pl", "cur_pl", "pre_ps", "cur_ps", "low", "high", "close");
         using (DBUtils.OpenThreadConnection()) {
             OrderBookDao.Entity preOrderBook = null;
             foreach (var orderBook in GetOrderBooks())
             {
                 if (preOrderBook != null)
                 {
                     var candle = Candlestick.Aggregate(new CandlesticksGetter()
                     {
                         Granularity = "M10",
                         Start       = orderBook.NormalizedDateTime.AddMinutes(-20),
                         Count       = 2
                     }.Execute());
                     if (orderBook.NormalizedDateTime == new DateTime(2016, 2, 22, 10, 40, 0, DateTimeKind.Local))
                     {
                         int a = 1;
                     }
                     float prePl = 0, prePs = 0;
                     float curPl = 0, curPs = 0;
                     foreach (var pp in GetRangePricePoint(orderBook, candle.Low, candle.High))
                     {
                         curPl += pp.Pl;
                         curPs += pp.Ps;
                     }
                     foreach (var pp in GetRangePricePoint(preOrderBook, candle.Low, candle.High))
                     {
                         prePl += pp.Pl;
                         prePs += pp.Ps;
                     }
                     report.WriteLine(orderBook.DateTime, prePl, curPl, prePs, curPs, candle.Low, candle.High, candle.Close);
                 }
                 preOrderBook = orderBook;
             }
         }
     });
 }
Esempio n. 7
0
        private static IEnumerable <OrderBookPricePointDao.Entity> GetRangePricePoint(OrderBookDao.Entity orderBook, float min, float max)
        {
            float start = GetRoundPrice(min);
            float end   = GetRoundPrice(max);

            foreach (var pricePoint in orderBook.GetPricePointsOrderByPrice())
            {
                if (start - 0.001f <= pricePoint.Price && pricePoint.Price <= end + 0.001f)
                {
                    yield return(pricePoint);
                }
            }
        }
Esempio n. 8
0
        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));
        }