Exemplo n.º 1
0
        public void FillUnfilledGaps()
        {
            using (MarketsDBEntities mde = new MarketsDBEntities())
            {
                foreach (var gap in mde.Gaps.Where(x => x.FillDate == null))
                {
                    var candles = mde.DailyCandles.Where(x => x.MarketId == gap.MarketId && x.Date > gap.OpenDate);
                    foreach (var candle in candles)
                    {
                        if (gap.Direction == "Up")
                        {
                            if (candle.Low <= gap.GapBottom)
                            {
                                Console.WriteLine("Gap fill on " + candle.Date.ToShortDateString());
                                gap.FillDate = candle.Date;
                                break;
                            }

                        }
                        else
                        {
                            if (candle.High >= gap.GapTop)
                            {
                                Console.WriteLine("Gap fill on " + candle.Date.ToShortDateString());
                                gap.FillDate = candle.Date;
                                break;
                            }
                        }
                    }
                }

                mde.SaveChanges();
            }
        }
Exemplo n.º 2
0
        public void UpdateMarketHistorical(DateTime sd, DateTime ed, int MarketId)
        {
            using (MarketsDBEntities mde = new MarketsDBEntities())
            {

                YahooFinance yf = new YahooFinance();

                Market m = mde.Markets.Where(x => x.MarketId == MarketId).FirstOrDefault();

                List<DailyCandle> dcs = yf.GetHistoricalData(sd, ed, m);

                foreach (DailyCandle td in dcs)
                {
                    //see if this day is in the db for this issue

                    int x = (from oa in mde.DailyCandles
                             where oa.MarketId == MarketId && oa.Date == td.Date
                             select oa).Count<DailyCandle>();

                    if (x == 0)
                    {
                        mde.DailyCandles.Add(td);
                    }

                    //if yes, no nothing for now (consider overwriting at a certain point?)

                    //if no, insert this row into the table

                }

                mde.SaveChanges();

            }
        }
Exemplo n.º 3
0
        public void CatchUpDailyCandles(int MarketId)
        {
            using (MarketsDBEntities mdb = new MarketsDBEntities())
            {

                //load the current daily candles

                var CompiledCandles = mdb.Candles.Where(x => x.MarketId == MarketId && x.PeriodId == 5);

                //load from the data

                var RawData = mdb.DailyCandles.Where(x => x.MarketId == MarketId);

                //compare lengths, if same then return

                Console.WriteLine(CompiledCandles.Count());
                Console.WriteLine(RawData.Count());

                if (CompiledCandles.Count() < RawData.Count())
                {

                    //if not same, need to add some candles
                    HashSet<DateTime> processedDates = new HashSet<DateTime>(CompiledCandles.Select(s => s.StartDate));

                    var results = RawData.Where(m => !processedDates.Contains(m.Date));

                    foreach (var dc in results)
                    {
                        Candle c = new Candle();
                        c.Close = dc.Close;
                        c.EndDate = dc.Date;
                        c.High = dc.High;
                        c.Low = dc.Low;
                        c.MarketId = dc.MarketId;
                        c.Open = dc.Open;
                        c.PeriodId = 5;
                        c.StartDate = dc.Date;
                        c.Volume = dc.Volume;

                        using (MarketsDBEntities mdbSaver = new MarketsDBEntities())
                        {

                            mdbSaver.Candles.Add(c);
                            mdbSaver.SaveChanges();
                        }

                    }
                }

            }
        }
Exemplo n.º 4
0
        //
        // GET: /Tests/
        public ActionResult Index()
        {
            MarketMachineClassLibrary.MarketUpdates mu = new MarketMachineClassLibrary.MarketUpdates();

            MarketsDBEntities mdb = new MarketsDBEntities();

            foreach (Market m in mdb.Markets.Where(x=>x.CategoryId>1))
            {

                mu.UpdateMarketHistorical(DateTime.Now.AddDays(-180), DateTime.Now, m.MarketId);
            }

            return View();
        }
Exemplo n.º 5
0
        public void DailyCandleCheck(int marketId)
        {
            using (MarketsDBEntities mde = new MarketsDBEntities())
            {
                //sum each month

                var candles = mde.DailyCandles.Where(x => x.MarketId == marketId).OrderBy(x=>x.Date).Select(y => y.Date.Year.ToString() + "/" + y.Date.Month.ToString());

                var candleCounts = candles.GroupBy(x => x).Select(x => new ItemCount { Item = x.Key, Count = x.Count()});

                foreach (var candle in candleCounts)
                {
                    Console.WriteLine(candle.Item + " " + candle.Count.ToString());
                }
            }
        }
Exemplo n.º 6
0
        public void UpdateMarkets()
        {
            MarketUpdates mu = new MarketUpdates();

            using (MarketsDBEntities mde = new MarketsDBEntities())
            {
                foreach (var m in mde.Markets) {
                    Console.WriteLine("Updating data for " + m.Name);
                mu.UpdateMarketHistorical(DateTime.Now.AddYears(-5), DateTime.Now, m.MarketId);
                }
            }
        }
Exemplo n.º 7
0
        public void GetCurrentPrices()
        {
            using (MarketsDBEntities mde = new MarketsDBEntities())
            {
                foreach (var market in mde.Markets)
                {
                    using (MarketsDBEntities mdeSaver = new MarketsDBEntities())
                    {
                        var lastCandle = mdeSaver.DailyCandles.Where(x => x.MarketId == market.MarketId).OrderByDescending(x => x.Date).FirstOrDefault();
                        market.CurrentPrice = lastCandle.Close;

                        Console.WriteLine("Updated price for " + market.Symbol);
                    }
                }

                mde.SaveChanges();

            }
        }
Exemplo n.º 8
0
        public void GenerateMonthlyCandles(int marketId)
        {
            using (MarketsDBEntities mde = new MarketsDBEntities())
            {
                var candles = mde.DailyCandles.Where(x => x.MarketId == marketId)
                    .OrderBy(x => x.Date)
                    .GroupBy(x => x.Date.Month.ToString() + "-" + x.Date.Year.ToString())
                    .Select(x => new CandleListItem { MarketId = marketId,
                        StartDate = x.Min(y => y.Date),
                        High = x.Max(y => y.High),
                        Low = x.Min(y => y.High),
                        Open = 0,
                        Close = 0,
                        Volume = x.Sum(y => y.Volume),

                    });

                foreach (CandleListItem candle in candles)
                {
                    //get open and close

                    var dailyCandles = mde.DailyCandles.Where(x => x.MarketId == marketId && x.Date.Month == candle.StartDate.Month && x.Date.Year == candle.StartDate.Year).OrderBy(x => x.Date);
                    candle.Open = dailyCandles.First().Open;
                    candle.Close = dailyCandles.OrderByDescending(x => x.Date).First().Close;

                    Candle c = new Candle();
                    c.Close = candle.Close;
                    c.EndDate = dailyCandles.OrderByDescending(x => x.Date).First().Date;
                    c.High = candle.High;
                    c.Low = candle.Low;
                    c.MarketId = marketId;
                    c.Open = candle.Open;
                    c.PeriodId = 2;
                    c.StartDate = candle.StartDate;
                    c.Volume = candle.Volume;

                    if (mde.Candles.Where(x=>x.MarketId == marketId && x.StartDate == c.StartDate && x.PeriodId == c.PeriodId).Count() == 0)
                    {
                        using (MarketsDBEntities mdeSaver = new MarketsDBEntities())
                        {
                            mdeSaver.Candles.Add(c);
                            mdeSaver.SaveChanges();
                        }
                    }

                    Console.WriteLine(candle.StartDate.ToShortDateString() + " H:" + candle.High);
                }

            }
        }
Exemplo n.º 9
0
        public void GapStats()
        {
            using (MarketsDBEntities mde = new MarketsDBEntities())
            {
                foreach (var m in mde.Markets)
                {
                    int marketId = m.MarketId;
                    //get all unfilled gaps
                    var market = mde.Markets.Find(marketId);
                    var gaps = market.Gaps.Where(x => x.MarketId == marketId && x.FillDate == null);

                    //ordered by date, which direction is the last unfilled gap?
                    var lastGap = gaps.OrderByDescending(x => x.OpenDate).FirstOrDefault();

                    if (lastGap.OpenDate > DateTime.Now.AddYears(-100))
                    {
                        market.GapsCurrentDirection = lastGap.Direction;
                        if (lastGap.Direction == "Up")
                            market.GapsFirstPrice = lastGap.GapTop;
                        else
                            market.GapsFirstPrice = lastGap.GapBottom;
                    }

                }

                mde.SaveChanges();

            }
        }
Exemplo n.º 10
0
        public void GapFinders()
        {
            MarketUpdates mu = new MarketUpdates();

            using (MarketsDBEntities mde = new MarketsDBEntities())
            {
                foreach (var m in mde.Markets)
                {
                    GapFinder(m.MarketId);
                }
            }
        }
Exemplo n.º 11
0
        public void GapFinder(int marketId)
        {
            using (MarketsDBEntities mde = new MarketsDBEntities())
            {
            //find all candles that don't have a corresponding gap
                var candles = mde.DailyCandles.Where (x => x.MarketId == marketId && x.Gaps.Count == 0);

                foreach (var candle in candles.OrderBy(x=>x.Date))
                {
                    Console.WriteLine("Finding gap for " + candle.Date.ToShortDateString());

                    //1. Find the candle one candle back date-wise

                    int trys = 0;
                    DailyCandle lastCandle = new DailyCandle();
                    DateTime lastDate = candle.Date.AddDays(-1);
                    while (trys < 6 && lastCandle.Date< DateTime.Now.AddYears(-100))
                    {
                        //Console.WriteLine(lastDate.Date.ToShortDateString());

                        if (mde.DailyCandles.Where(x => x.MarketId == marketId && x.Date == lastDate).Count() > 0)
                        {
                            Console.WriteLine("Last candle found.");
                            lastCandle = mde.DailyCandles.Where(x => x.MarketId == marketId && x.Date == lastDate).First();
                        }
                        trys++;
                        lastDate = lastDate.AddDays(-1);
                    }

                    if (lastCandle.Date< DateTime.Now.AddYears(-100))
                        Console.WriteLine("No last candle found!");

                    Console.WriteLine("Last candle: " + lastCandle.Date.ToShortDateString());

                    Gap g = new Gap();
                    g.MarketId = marketId;
                    g.DailyCandleId = candle.DailyCandleId;
                    g.OpenDate = candle.Date;
                    g.GapSize = candle.Open - lastCandle.Close;

                    if (candle.Open >= lastCandle.Close)
                    {
                        g.Direction = "Up";
                        g.GapTop = candle.Open;
                        g.GapBottom = lastCandle.Close;

                        if (candle.Low <= lastCandle.Close)
                        {
                            g.FillDate = candle.Date;
                        }
                    }
                    else
                    {
                        g.Direction = "Down";
                        g.GapTop = lastCandle.Close;
                        g.GapBottom = candle.Open;

                        if (candle.High >= lastCandle.Close)
                        {
                            g.FillDate = candle.Date;
                        }
                    }

                    g.GapExtension = candle.Open + g.GapSize;

                    using (MarketsDBEntities mdeAdder = new MarketsDBEntities())
                    {
                        mdeAdder.Gaps.Add(g);
                        mdeAdder.SaveChanges();
                    }

                }

            }
        }
Exemplo n.º 12
0
        public void FirstAndLastDates()
        {
            using (MarketsDBEntities mde = new MarketsDBEntities())
            {
                foreach (var m in mde.Markets)
                {
                    m.DateFirstCandle = m.DailyCandles.OrderBy(x => x.Date).First().Date;
                    m.DateLastCandle = m.DailyCandles.OrderByDescending(x => x.Date).First().Date;

                }

                mde.SaveChanges();
            }
        }
Exemplo n.º 13
0
        public void CreateChart(int MarketId = 1)
        {
            Telerik.Web.UI.RadChart objChart = new Telerik.Web.UI.RadChart();

            // the data we want to chart

            // setting some chart parameters

            objChart.Height = 480;
            objChart.Width = 500;
            /*objChart.Margins.Right = new Unit(30, UnitType.Percentage);
            objChart.Margins.Bottom = new Unit(1, UnitType.Percentage);
            objChart.Margins.Left = new Unit(1, UnitType.Percentage);
            objChart.Margins.Top = new Unit(1, UnitType.Percentage);
            objChart.Background.MainColor = Color.White;
            objChart.Background.BorderColor = Color.White;*/

            // creating a series and adding it to the chart

            MarketsDBEntities dba = new MarketsDBEntities();

            Market m = dba.Markets.Find(MarketId);

            DateTime sd = DateTime.Now.AddMonths(-6);
            DateTime ed = DateTime.Now;

            Telerik.Charting.ChartSeriesItemsCollection sc = new Telerik.Charting.ChartSeriesItemsCollection();

            //m.PopulateDailyCandleList(DateTime.Now.AddYears(-4), ed);

            //MarketMachineClassLibrary.MarketDailyCandleList mdcl = m.mdcl;
            //MarketMachineClassLibrary.CandleList cl = mdcl.cl;

            int x = 0;
            double minny = 100000;
            double maxxy = 0;

            foreach (var t in m.DailyCandles.Where(i => i.Date >= sd))
            {
                Telerik.Charting.ChartSeriesItem i = new Telerik.Charting.ChartSeriesItem();

                i.XValue = t.Date.ToOADate();
                i.YValue = Double.Parse(t.Open.ToString());
                i.YValue2 = Double.Parse(t.Close.ToString());
                i.YValue3 = Double.Parse(t.Low.ToString());
                i.YValue4 = Double.Parse(t.High.ToString());
                sc.Add(i);

                if (Double.Parse(t.High.ToString()) > maxxy)
                {
                    maxxy = Double.Parse(t.High.ToString());
                }

                if (Double.Parse(t.Low.ToString()) < minny)
                {
                    minny = Double.Parse(t.Low.ToString());
                }

                x++;
            }

            Double offset = (maxxy - minny) * .2;

            objChart.PlotArea.YAxis.MaxValue = maxxy + offset;

            objChart.PlotArea.YAxis.MinValue = Math.Round(minny - offset, MidpointRounding.AwayFromZero);

            objChart.PlotArea.YAxis.LabelStep = 10;

            objChart.PlotArea.XAxis.AddRange(sd.ToOADate(), ed.ToOADate(), 1);
            objChart.Skin = "LightGreen";

            objChart.Legend.Appearance.Visible = false;

            objChart.Series.Add(new Telerik.Charting.ChartSeries());
            objChart.Series[0].Items.AddRange(sc);
            objChart.Series[0].Name = m.Name;
            objChart.Series[0].Type = Telerik.Charting.ChartSeriesType.CandleStick;
            objChart.Series[0].Appearance.BarWidthPercent = 80;

            objChart.Series[0].Appearance.Border.Color = Color.Black;
            objChart.Series[0].Appearance.Border.Width = 1;
            objChart.Series[0].Appearance.LabelAppearance.Visible = false;

            objChart.Series[0].PlotArea.XAxis.LayoutMode = Telerik.Charting.Styles.ChartAxisLayoutMode.Between;
            objChart.Series[0].PlotArea.XAxis.AutoScale = false;
            objChart.Series[0].PlotArea.XAxis.Appearance.MajorGridLines.Width = 2;
            objChart.Series[0].PlotArea.XAxis.Appearance.MinorGridLines.Visible = false;
            objChart.Series[0].PlotArea.XAxis.LabelStep = 10;

            objChart.Series[0].PlotArea.XAxis.Appearance.ValueFormat = Telerik.Charting.Styles.ChartValueFormat.ShortDate;
            objChart.Series[0].PlotArea.XAxis.Appearance.LabelAppearance.RotationAngle = 45;
            objChart.Series[0].PlotArea.XAxis.Appearance.LabelAppearance.Position.AlignedPosition = Telerik.Charting.Styles.AlignedPositions.Top;

            objChart.Series[0].PlotArea.YAxis.AxisMode = Telerik.Charting.ChartYAxisMode.Extended;
            objChart.Series[0].PlotArea.YAxis.AutoScale = false;
            objChart.Series[0].PlotArea.YAxis.Appearance.MajorGridLines.Width = 2;
            objChart.Series[0].PlotArea.YAxis.Appearance.MinorGridLines.Visible = false;
            objChart.ChartTitle.Visible = false;
            objChart.Appearance.Dimensions.Margins.Top = 10;
            objChart.Appearance.Dimensions.Margins.Bottom = 10;
            objChart.Appearance.Dimensions.Margins.Left = 10;
            objChart.Appearance.Dimensions.Margins.Right = 10;

            objChart.Series[0].PlotArea.Appearance.Dimensions.AutoSize = false;
            objChart.Series[0].PlotArea.Appearance.Dimensions.Width = 430;
            objChart.Series[0].PlotArea.Appearance.Dimensions.Height = 430;
            objChart.Series[0].PlotArea.Appearance.Dimensions.Paddings.Top = 10;
            objChart.Series[0].PlotArea.Appearance.Dimensions.Margins.Top = 10;

            //objChart.Text = "Minny: " + minny.ToString() + ", Maxxy: " + maxxy.ToString();

            // send the chart to the view engine

            /****************** TRADING RANGES

            foreach (MarketMachineClassLibrary.TradingRangeViewModel t in mdcl.cl.TheTradingRanges.Where(i => i.BeginDate >= sd).OrderBy(i => i.ActiveDate))
            {
                Telerik.Charting.ChartMarkedZone cz = new Telerik.Charting.ChartMarkedZone();
                cz.ValueStartX = t.BeginDate.ToOADate();
                cz.ValueEndX = t.ActiveDate.ToOADate();
                cz.ValueStartY = double.Parse(t.UpperPrice.ToString());
                cz.ValueEndY = double.Parse(t.LowerPrice.ToString());

                if (t.BullOrBear == "Bull")
                {
                    cz.Appearance.FillStyle.MainColor = System.Drawing.Color.LightGreen;

                }
                else if (t.BullOrBear == "Bear")
                {
                    cz.Appearance.FillStyle.MainColor = System.Drawing.Color.LightPink;
                }
                else
                {
                    cz.Appearance.FillStyle.MainColor = System.Drawing.Color.LightBlue;
                }

                objChart.Chart.PlotArea.MarkedZones.Add(cz);

            }

             */

            //return new ChartResult() { Chart = objChart };

            Image image = objChart.GetBitmap();

            ChartImage = image;

            //image.Save("c:\\temp\\test.jpg", ImageFormat.Jpeg);

            byte[] Ret;

            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            Ret = ms.ToArray();

            ChartData = Ret;
        }