private async void DisplayPricingData(string ticker) { List <double> priceslastyear = new List <double>(); List <string> dateslastyear = new List <string>(); // Creating the connection object IAvapiConnection connection = AvapiConnection.Instance; IAvapiResponse_TIME_SERIES_MONTHLY_ADJUSTED m_time_series_monthly_adjustedResponse; // Set up the connection and pass the API_KEY provided by alphavantage.co connection.Connect("7NIMRBR8G8UB7P8C"); // Get the TIME_SERIES_MONTHLY_ADJUSTED query object Int_TIME_SERIES_MONTHLY_ADJUSTED time_series_monthly_adjusted = connection.GetQueryObject_TIME_SERIES_MONTHLY_ADJUSTED(); // Perform the TIME_SERIES_MONTHLY_ADJUSTED request and get the result m_time_series_monthly_adjustedResponse = await time_series_monthly_adjusted.QueryPrimitiveAsync( ticker); var data = m_time_series_monthly_adjustedResponse.Data; if (data.Error) { Console.WriteLine(data.ErrorMessage); } else { int counter = 0; foreach (var timeseries in data.TimeSeries) { //Grab 1 year worth of months data if (counter < 12) { //add raw prices and dates to arrays priceslastyear.Add(double.Parse(timeseries.adjustedclose, CultureInfo.InvariantCulture.NumberFormat)); Debug.WriteLine(float.Parse(timeseries.adjustedclose, CultureInfo.InvariantCulture.NumberFormat)); Debug.WriteLine(timeseries.DateTime); Microcharts.Entry tempEntry = new Microcharts.Entry(float.Parse(timeseries.adjustedclose, CultureInfo.InvariantCulture.NumberFormat)) { Label = timeseries.DateTime, ValueLabel = timeseries.adjustedclose//Color = investmentList[i].color.ToSKColor() }; entries.Add(tempEntry); } counter = counter + 1; } } var chart = new LineChart() { Entries = entries, LineMode = LineMode.Straight, LineSize = 8, PointSize = 18, LabelTextSize = 25 }; this.chartView.Chart = chart; }
private async void MinHistory(object sender, RoutedEventArgs e) { var values = new ChartValues <DateTimePoint>(); Int_TIME_SERIES_MONTHLY_ADJUSTED time_series_montly_adjusted = _connection.GetQueryObject_TIME_SERIES_MONTHLY_ADJUSTED(); try { IAvapiResponse_TIME_SERIES_MONTHLY_ADJUSTED time_series_weekly_adjustedResponse = await time_series_montly_adjusted.QueryPrimitiveAsync(_args.Symbol); var data = time_series_weekly_adjustedResponse.Data; if (data.Error) { MessageBox.Show("Failed to fetch data", "Error"); SeriesCollection[0].Values = Read("\\min.csv"); ResetZoomOnClick(sender, e); return; } else { foreach (var timeseries in data.TimeSeries) { values.Add(new DateTimePoint(DateTime.ParseExact(timeseries.DateTime, "yyyy-MM-dd", CultureInfo.InvariantCulture), double.Parse(timeseries.adjustedclose) * _exchangeRate)); } Write("\\min.csv", values); } } catch (Exception) { MessageBox.Show("Previse zahteva poslato u kratkom vremene server misli da si spamer! Oladi malo sa kliktanjem.", "Error"); SeriesCollection[0].Values = Read("\\min.csv"); ResetZoomOnClick(sender, e); return; } SeriesCollection[0].Values = Read("\\min.csv"); //SeriesCollection[0].Values = values; ResetZoomOnClick(sender, e); }
static async Task <Tuple <double, double, List <double> > > SR_FetchPricingDataandExpectedReturn(string ticker) { //****************************************************************************************** //The purpose of this function is to do all calculations related to sharperatio that involve pricing data // for a single company. This includes STDdev, and expected return. These will be stored in a dictionary //****************************************************************************************** List <double> priceslastyear = new List <double>(); List <double> monthlyreturnpercentages = new List <double>(); // Creating the connection object IAvapiConnection connection = AvapiConnection.Instance; IAvapiResponse_TIME_SERIES_MONTHLY_ADJUSTED m_time_series_monthly_adjustedResponse; // Set up the connection and pass the API_KEY provided by alphavantage.co connection.Connect("7NIMRBR8G8UB7P8C"); // Get the TIME_SERIES_MONTHLY_ADJUSTED query object Int_TIME_SERIES_MONTHLY_ADJUSTED time_series_monthly_adjusted = connection.GetQueryObject_TIME_SERIES_MONTHLY_ADJUSTED(); // Perform the TIME_SERIES_MONTHLY_ADJUSTED request and get the result m_time_series_monthly_adjustedResponse = await time_series_monthly_adjusted.QueryPrimitiveAsync( ticker); Console.WriteLine("******** STRUCTURED DATA TIME_SERIES_MONTHLY_ADJUSTED ********"); var data = m_time_series_monthly_adjustedResponse.Data; if (data.Error) { Console.WriteLine(data.ErrorMessage); return(null); } else { Console.WriteLine("PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP"); int counter = 0; foreach (var timeseries in data.TimeSeries) { //Grab 5 years worth of months data if (counter < 60) { //add raw prices to array priceslastyear.Add(double.Parse(timeseries.adjustedclose, CultureInfo.InvariantCulture.NumberFormat)); if (counter > 0) { //take pricing data and calculate percent change between each pair of months //Console.Write(((priceslastyear[counter] / priceslastyear[counter - 1]) - 1) * 100); monthlyreturnpercentages.Add(((priceslastyear[counter] / priceslastyear[counter - 1]) - 1) * 100); } counter = counter + 1; } } //Setup counter to average pricing data double running_average = 0; //Calculate STDDev of data double std_dev = StdDev(monthlyreturnpercentages, true); //Sum all the values in our list for (var i = 0; i < monthlyreturnpercentages.Count(); i++) { running_average = monthlyreturnpercentages[i] + running_average; } //Finishing taking average by dividing by the number of entries running_average = running_average / monthlyreturnpercentages.Count(); //Annualize the average (multiply by 12 since monthly) running_average = running_average * 12; //Add the values to the tuple and print! Console.Write(ticker + ", "); Console.Write(std_dev + ", "); Console.Write(running_average + ", "); var pricingdata = Tuple.Create(std_dev, running_average, monthlyreturnpercentages); //Return this value return(pricingdata); } }
static async Task <double> CalculateSharpeRatioForCompany(string ticker) { List <double> priceslastyear = new List <double>(); List <double> monthlyreturnpercentages = new List <double>(); // Creating the connection object IAvapiConnection connection = AvapiConnection.Instance; IAvapiResponse_TIME_SERIES_MONTHLY_ADJUSTED m_time_series_monthly_adjustedResponse; // Set up the connection and pass the API_KEY provided by alphavantage.co connection.Connect("7NIMRBR8G8UB7P8C"); // Get the TIME_SERIES_MONTHLY_ADJUSTED query object Int_TIME_SERIES_MONTHLY_ADJUSTED time_series_monthly_adjusted = connection.GetQueryObject_TIME_SERIES_MONTHLY_ADJUSTED(); // Perform the TIME_SERIES_MONTHLY_ADJUSTED request and get the result m_time_series_monthly_adjustedResponse = await time_series_monthly_adjusted.QueryPrimitiveAsync( ticker); Console.WriteLine("******** STRUCTURED DATA TIME_SERIES_MONTHLY_ADJUSTED ********"); var data = m_time_series_monthly_adjustedResponse.Data; if (data.Error) { Console.WriteLine(data.ErrorMessage); return(0); } else { int counter = 0; foreach (var timeseries in data.TimeSeries) { //Grab 5 years worth of months data if (counter < 60) { //add raw prices to array priceslastyear.Add(double.Parse(timeseries.adjustedclose, CultureInfo.InvariantCulture.NumberFormat)); if (counter > 0) { //take pricing data and calculate percent change between each pair of months //Console.Write(((priceslastyear[counter] / priceslastyear[counter - 1]) - 1) * 100); monthlyreturnpercentages.Add(((priceslastyear[counter] / priceslastyear[counter - 1]) - 1) * 100); } counter = counter + 1; } } double HistAvgMonthlyReturn = 0; for (var i = 0; i < monthlyreturnpercentages.Count; i++) { //Console.WriteLine(monthprice); HistAvgMonthlyReturn = HistAvgMonthlyReturn + monthlyreturnpercentages[i]; } //Calculate the average monthly return HistAvgMonthlyReturn = HistAvgMonthlyReturn / monthlyreturnpercentages.Count; //Console.WriteLine(HistAvgMonthlyReturn); //Here we need to calculate the Historical Monthly standard deviation based on the Std dev // of the monthlyreturnpercentages double Summation = 0; for (var i = 0; i < monthlyreturnpercentages.Count; i++) { Summation = Summation + Math.Pow(monthlyreturnpercentages[i] - HistAvgMonthlyReturn, 2); } Summation = Summation / monthlyreturnpercentages.Count; double HistAvgMonthlyStdDev = Math.Sqrt(Summation); //Annualize our standard deviation double AnnualStdDev = HistAvgMonthlyStdDev * Math.Sqrt(12); double AnnualReturn = ((Math.Pow((100 + HistAvgMonthlyReturn), 12)) - 100); double Riskfreerate = 3; //Console.WriteLine(HistAvgMonthlyReturn); Console.WriteLine(ticker); Console.WriteLine(AnnualReturn); //Console.WriteLine(Riskfreerate); //Console.WriteLine(AnnualStdDev); //Calculate the SharpeRatio for the Company double SharpeRatioforCompany = (AnnualReturn - Riskfreerate) / AnnualStdDev; //Console.WriteLine(SharpeRatioforCompany); return(AnnualReturn); } }