예제 #1
0
        //-----------------------------------------------------
        // FOR HISTORICAL VALUES ONLY
        //
        //    (not working)
        //
        //-----------------------------------------------------



        //Example of 25 years with a 1d resolution
        //https://query1.finance.yahoo.com/v7/finance/chart/MSFT?range=25y&interval=1d&indicators=quote&includeTimestamps=true&includePrePost=false&corsDomain=finance.yahoo.com



        public System.Collections.Generic.IEnumerable <Candle> GetHistoricalPrice(string Symbol, int interval_s, DateTime?dateTimeFrom, DateTime?dateTimeTo)
        {
            String        market          = "NASDAQ";
            List <Candle> lCandle         = new List <Candle>();
            int           numberOfSeconds = 60;

            dateTimeTo = DateTime.Today;
            var url      = ApiYahoo.GetUrl(market, Symbol, interval_s, dateTimeFrom, dateTimeTo);
            var response = this.restCaller.Get(url);

            if (response == null)
            {
                throw new DataMisalignedException("Market: " + market + ", Symbol " + Symbol + ", Date From " + System.Convert.ToString(dateTimeFrom) + ", Date To " + System.Convert.ToString(dateTimeTo) + " generated exception misaligned data");
            }
            if (response.Length == 0)
            {
                return(new Candle[0]);
            }

            if ((numberOfSeconds < (24 * 60 * 60)) &&
                ((dateTimeTo == DateTime.MinValue) && dateTimeTo.Value.TimeOfDay.Equals(new TimeSpan(0, 0, 0))))
            {
                // If to value is set and the time has not been set (so it's 0:00) then is set to 23:59:59
                // to include values for that day in the result.
                dateTimeTo = dateTimeTo.Value.Add(new TimeSpan(23, 59, 59));
            }

            var data = JsonConvert.DeserializeObject <RootObject>(response);

            //OHCL
            for (int i = 0; i < data.chart.result[0].indicators.quote[0].close.Count; i++)
            {
                if (data.chart.result[0].indicators.quote[0].open[i].HasValue)
                {
                    Candle   candle     = new Candle();
                    DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
                    dtDateTime    = dtDateTime.AddSeconds(data.chart.result[0].timestamp[i]).ToLocalTime();
                    candle.Date   = dtDateTime;
                    candle.Open   = System.Convert.ToDecimal(data.chart.result[0].indicators.quote[0].open[i].Value.ToString("#.00"));
                    candle.High   = System.Convert.ToDecimal(data.chart.result[0].indicators.quote[0].high[i].Value.ToString("#.00"));
                    candle.Low    = System.Convert.ToDecimal(data.chart.result[0].indicators.quote[0].low[i].Value.ToString("#.00"));
                    candle.Close  = System.Convert.ToDecimal(data.chart.result[0].indicators.quote[0].close[i].Value.ToString("#.00"));
                    candle.Volume = System.Convert.ToDecimal(data.chart.result[0].indicators.quote[0].volume[i].Value.ToString("#"));
                    lCandle.Add(candle);
                    candle = null;
                }
            }

            //Trick! This automatically converts it to the ienumerable type return by value function :-)
            return(lCandle);
        }
예제 #2
0
파일: Main.cs 프로젝트: jfmiguez/AutoMarket
        private void btnTestYahoo_Click(object sender, EventArgs e)
        {
            API.ApiYahoo apiYahoo = new API.ApiYahoo();

            //get historical quotes from yahoo
            // List<API.Candle> mCandle = new List<API.Candle>();
            DateTime dtStart = System.Convert.ToDateTime(dtDateFrom.Text);
            DateTime dtEnd   = System.Convert.ToDateTime(dtDateTo.Text);

            lstRealTimeQuotes.Items.Clear();
            lstTestGoogle.Items.Clear();

            //Convert interval to seconds
            string sFactor = cboInterval.Text.Substring(cboInterval.Text.Length - 1, 1);
            int    seconds = 0;

            if (sFactor == "m")
            {
                seconds = System.Convert.ToInt32(cboInterval.Text.Replace("m", "")) * 60;
            }
            else if (sFactor == "h")
            {
                seconds = System.Convert.ToInt32(cboInterval.Text.Replace("h", "")) * 60 * 60;
            }
            else if (sFactor == "d")
            {
                seconds = System.Convert.ToInt32(cboInterval.Text.Replace("d", "")) * 60 * 60 * 24;
            }
            else if (sFactor == "k")
            {
                seconds = System.Convert.ToInt32(cboInterval.Text.Replace("wk", "")) * 60 * 60 * 24 * 5;
            }
            else if (sFactor == "o")
            {
                seconds = System.Convert.ToInt32(cboInterval.Text.Replace("mo", "")) * 60 * 60 * 24 * 5 * 4;
            }

            //pass variables to the historical price.
            var testa = apiYahoo.GetHistoricalPrice(txtSymbol.Text, seconds, dtStart, dtEnd);

            for (int i = 0; i < testa.Count(); i++)
            {
                lstTestGoogle.Items.Add(
                    testa.ElementAt(i).Date + ", " +
                    testa.ElementAt(i).Open + ", " +
                    testa.ElementAt(i).High + ", " +
                    testa.ElementAt(i).Low + ", " +
                    testa.ElementAt(i).Close + ", " +
                    testa.ElementAt(i).Volume
                    );
            }

            //Count the items in historical values
            lblCount.Text = testa.Count().ToString();

            //Get the quote from yahoo
            var testb = apiYahoo.GetQuote("MSFT");

            lstRealTimeQuotes.Items.Add("D: " + testb.Date.ToString("MM/dd/yyyy hh:mm:ss"));
            lstRealTimeQuotes.Items.Add("O: " + testb.Open);
            lstRealTimeQuotes.Items.Add("H: " + testb.High);
            lstRealTimeQuotes.Items.Add("C: " + testb.Close);
            lstRealTimeQuotes.Items.Add("L: " + testb.Low);
            lstRealTimeQuotes.Items.Add("V: " + testb.Volume);
        }
예제 #3
0
파일: Main.cs 프로젝트: jfmiguez/AutoMarket
        private void butTestBases_Click(object sender, EventArgs e)
        {
            API.ApiYahoo apiYahoo = new API.ApiYahoo();

            //get historical quotes from yahoo
            // List<API.Candle> mCandle = new List<API.Candle>();
            DateTime dtStart = System.Convert.ToDateTime(dtDateFrom.Text);
            DateTime dtEnd   = System.Convert.ToDateTime(dtDateTo.Text);

            lstRealTimeQuotes.Items.Clear();
            lstTestGoogle.Items.Clear();

            //Convert interval to seconds
            string sFactor = cboInterval.Text.Substring(cboInterval.Text.Length - 1, 1);
            int    seconds = 0;

            if (sFactor == "m")
            {
                seconds = System.Convert.ToInt32(cboInterval.Text.Replace("m", "")) * 60;
            }
            else if (sFactor == "h")
            {
                seconds = System.Convert.ToInt32(cboInterval.Text.Replace("h", "")) * 60 * 60;
            }
            else if (sFactor == "d")
            {
                seconds = System.Convert.ToInt32(cboInterval.Text.Replace("d", "")) * 60 * 60 * 24;
            }
            else if (sFactor == "k")
            {
                seconds = System.Convert.ToInt32(cboInterval.Text.Replace("wk", "")) * 60 * 60 * 24 * 5;
            }
            else if (sFactor == "o")
            {
                seconds = System.Convert.ToInt32(cboInterval.Text.Replace("mo", "")) * 60 * 60 * 24 * 5 * 4;
            }

            //pass variables to the historical price.
            IEnumerable <API.Candle> candleSticks = apiYahoo.GetHistoricalPrice(txtSymbol.Text, seconds, dtStart, dtEnd);



            //NOTE: THIS WILL BE PART OF THE EXECUTION ENGINE



            BaseData oData = new BaseData();

            //Generate a base finder and get all the DERIVED CLASSES of the base class.
            Base          baseFinder     = new Base();
            List <String> DerivedClasses = baseFinder.FindDerivedClasses();

            // Let's just get one of the classes and dynamically create an instance of the class
            // of the derived type!
            String strClass    = DerivedClasses[0];
            Type   DerivedType = baseFinder.DerivedClass(strClass);
            Object mClass      = (object)Activator.CreateInstance(DerivedType);

            //Call methods inside the class, first method will return something by value
            // second method will take parameters and return something from the dynamically created object.
            //System.Reflection.MethodInfo method = DerivedType.GetMethod("FindBase");
            //var somereturnvalue = method.Invoke(mClass, new object[0]);
            System.Reflection.MethodInfo method = DerivedType.GetMethod("FindBase");
            oData = (BaseData)method.Invoke(mClass, new object[] { candleSticks });
            System.Reflection.MethodInfo method2 = DerivedType.GetMethod("Findsomething");
            String param1           = "testing number ";
            int    param2           = 7;
            var    somereturnvalue2 = method2.Invoke(mClass, new object[] { param1, param2 });

            Console.WriteLine("> " + somereturnvalue2);

            // Generate a list of all derived classes (no, not method we are not going to over-populate)
            for (int i = 0; i < DerivedClasses.Count(); i++)
            {
                lstRealTimeQuotes.Items.Add(DerivedClasses[i]);
            }
        }