using System.Threading.Tasks;
using System.Collections.Generic;
public static void Main()
string symbol = "2888.HK";
DateTime startDate = new DateTime(2020, 01, 01);
DateTime endDate = DateTime.Now;
int timestampOfStartDate = (int)(startDate.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
int timestampOfEndDate = (int)(endDate.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
bool includeAdjustedClose = true;
string url = string.Format("https://query1.finance.yahoo.com/v7/finance/download/{0}?period1={1}&period2={2}&interval={3}&events=history&includeAdjustedClose={4}",
symbol, timestampOfStartDate, timestampOfEndDate, interval, includeAdjustedClose);
var responseData = GetWebContentAsync(url);
string[] lines = responseData.Result.Split("\n", StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine("Lines: " + lines.Length.ToString());
List<PriceData> myList = new();
for (int i = 1; i < lines.Length; i++)
string[] fields = lines[i].Split(',');
PriceData newData = new();
try{newData.Date = DateTime.Parse(fields[0]);} catch{}
try{newData.Open = float.Parse(fields[1]);} catch{}
try{newData.High = float.Parse(fields[2]);} catch{}
try{newData.Low = float.Parse(fields[3]);} catch{}
try{newData.Close = float.Parse(fields[4]);} catch{}
try{newData.AdjClose = float.Parse(fields[5]);} catch{}
try{newData.Volume = float.Parse(fields[6]);} catch{}
newData.Direction = newData.Close > lastClose ? "up" : newData.Close < lastClose ? "down" : "flat";
newData.Change = newData.Close - lastClose;
newData.Gain = newData.Close > lastClose ? newData.Change : 0;
newData.Loss = newData.Close < lastClose ? newData.Change * -1 : 0;
lastClose = newData.Close;
Console.WriteLine("List.Count: " + myList.Count().ToString());
for(int i = 0; i < myList.Count; i++)
if(i <= rsiPeriod) continue;
List<PriceData> mySelection = myList.GetRange(i - rsiPeriod, rsiPeriod);
myList[i].AvgGain = mySelection.Sum(x => x.Gain);
myList[i].AvgLoss = mySelection.Sum(x => x.Loss);
myList[i].RSI = 100 - (100 / (1 + myList[i].AvgGain / myList[i].AvgLoss));
string content = lines[0] + ",Direction,RSI\n";
foreach(PriceData pd in myList)
content += $"{pd.Date:yyyy-MM-dd},{pd.Open},{pd.High},{pd.Low},{pd.Close},{pd.AdjClose},{pd.Volume},{pd.Direction},{pd.RSI}\n";
Console.WriteLine(content);
internal static async Task<string> GetWebContentAsync(string url)
using var client = new HttpClient();
var content = await client.GetStringAsync(url);
public DateTime Date { get; set; }
public float Open { get; set; }
public float High { get; set; }
public float Low { get; set; }
public float Close { get; set; }
public float AdjClose { get; set; }
public float Volume { get; set; }
public string Direction { get; set; }
public float Change { get; set; }
public float Gain { get; set; }
public float Loss { get; set; }
public float AvgGain { get; set; }
public float AvgLoss { get; set; }
public float RSI { get; set; }