public DataTable GetQuote(string symbol)
{
DataTable resultDataTable = null;
try
{
string webservice_url = "";
WebResponse wr;
Stream receiveStream = null;
StreamReader reader = null;
//DataRow r;
//https://query1.finance.yahoo.com/v7/finance/chart/HDFC.BO?range=1m&interval=1m&indicators=quote×tamp=true
webservice_url = string.Format(StockManager.urlGlobalQuote, symbol);
Uri url = new Uri(webservice_url);
var webRequest = WebRequest.Create(url);
webRequest.Method = "GET";
webRequest.ContentType = "application/json";
wr = webRequest.GetResponseAsync().Result;
receiveStream = wr.GetResponseStream();
reader = new StreamReader(receiveStream);
resultDataTable = getQuoteTableFromJSON(reader.ReadToEnd(), symbol);
reader.Close();
if (receiveStream != null)
receiveStream.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
if (resultDataTable != null)
{
resultDataTable.Clear();
resultDataTable.Dispose();
}
resultDataTable = null;
}
return resultDataTable;
}
public DataTable getQuoteTableFromJSON(string record, string symbol)
{
DataTable resultDataTable = null;
if (record.ToUpper().Contains("NOT FOUND"))
{
return null;
}
DateTime myDate;
double close;
double high;
double low;
double open;
int volume;
double change;
double changepercent;
double prevclose;
//double adjusetedClose = 0.00;
//string formatedDate;
var errors = new List<string>();
try
{
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(record, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Populate,
Error = delegate (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args)
{
errors.Add(args.ErrorContext.Error.Message);
args.ErrorContext.Handled = true;
//args.ErrorContext.Handled = false;
}
//Converters = { new IsoDateTimeConverter() }
});
Chart myChart = myDeserializedClass.chart;
Result myResult = myChart.result[0];
Meta myMeta = myResult.meta;
Indicators myIndicators = myResult.indicators;
//this will be typically only 1 row and quote will have list of close, high, low, open, volume
Quote myQuote = myIndicators.quote[0];
//this will be typically only 1 row and adjClose will have list of adjClose
//Adjclose myAdjClose = null;
//if (bIsDaily)
//{
// myAdjClose = myIndicators.adjclose[0];
//}
if (myResult.timestamp != null)
{
resultDataTable = new DataTable();
resultDataTable.Columns.Add("Symbol", typeof(string));
resultDataTable.Columns.Add("Open", typeof(decimal));
resultDataTable.Columns.Add("High", typeof(decimal));
resultDataTable.Columns.Add("Low", typeof(decimal));
resultDataTable.Columns.Add("Price", typeof(decimal));
resultDataTable.Columns.Add("Volume", typeof(int));
resultDataTable.Columns.Add("latestDay", typeof(DateTime));
resultDataTable.Columns.Add("previousClose", typeof(decimal));
resultDataTable.Columns.Add("change", typeof(decimal));
resultDataTable.Columns.Add("changePercent", typeof(decimal));
for (int i = 0; i < myResult.timestamp.Count; i++)
{
if ((myQuote.close[i] == null) && (myQuote.high[i] == null) && (myQuote.low[i] == null) && (myQuote.open[i] == null)
&& (myQuote.volume[i] == null))
{
continue;
}
//myDate = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(myResult.timestamp[i]).ToLocalTime();
myDate = convertUnixEpochToLocalDateTime(myResult.timestamp[i], myMeta.timezone);
//myDate = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(myResult.timestamp[i]);
//string formatedDate = myDate.ToString("dd-MM-yyyy");
//formatedDate = myDate.ToString("yyyy-dd-MM");
//myDate = System.Convert.ToDateTime(myResult.timestamp[i]);
//if all are null do not enter this row
if (myQuote.close[i] == null)
{
close = 0.00;
}
else
{
//close = (double)myQuote.close[i];
close = System.Convert.ToDouble(string.Format("{0:0.00}", myQuote.close[i]));
}
if (myQuote.high[i] == null)
{
high = 0.00;
}
else
{
//high = (double)myQuote.high[i];
high = System.Convert.ToDouble(string.Format("{0:0.00}", myQuote.high[i]));
}
if (myQuote.low[i] == null)
{
low = 0.00;
}
else
{
//low = (double)myQuote.low[i];
low = System.Convert.ToDouble(string.Format("{0:0.00}", myQuote.low[i]));
}
if (myQuote.open[i] == null)
{
open = 0.00;
}
else
{
//open = (double)myQuote.open[i];
open = System.Convert.ToDouble(string.Format("{0:0.00}", myQuote.open[i]));
}
if (myQuote.volume[i] == null)
{
volume = 0;
}
else
{
volume = (int)myQuote.volume[i];
}
prevclose = System.Convert.ToDouble(string.Format("{0:0.00}", myMeta.chartPreviousClose));
change = close - prevclose;
changepercent = (change / prevclose) * 100;
change = System.Convert.ToDouble(string.Format("{0:0.00}", change));
changepercent = System.Convert.ToDouble(string.Format("{0:0.00}", changepercent));
resultDataTable.Rows.Add(new object[] {
symbol,
Math.Round(open, 4),
Math.Round(high, 4),
Math.Round(low, 4),
Math.Round(close, 4),
volume,
myDate,
Math.Round(prevclose, 4),
Math.Round(change, 4),
Math.Round(changepercent, 4)
//adjusetedClose
});
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
if (resultDataTable != null)
{
resultDataTable.Clear();
resultDataTable.Dispose();
}
resultDataTable = null;
}
return resultDataTable;
}