Hey, I’ve been having an issue when It comes to outputting bars to a JSON file, I was wondering if anyone here had any idea why it wasant working. Anything would be helpful. I dont get any errors when i run the code but it just give me a blank json string of {}.
public string SaveDataFromURL_ALPC(string symbol, DateTime start, DateTime end)
{
DateTime overflowStart = start;
DateTime overflowEnd = start;
int Timeframe = 5;
int limit = 500;
TimeSpan i = end - start;
int total = Convert.ToInt32(Math.Ceiling(i.TotalMinutes / Timeframe));
int j = 1;
int I = 0;
DateTime Previous = DateTime.UtcNow;
decimal counter = 1;
int counter2 = 1;
DateTime counter3a = DateTime.UtcNow;
DateTime counter3;
List<string> ts = new List<string>();
List<string> barsobjects = new List<string>();
while (j == 1)
{
Console.WriteLine(total);
overflowEnd.AddMinutes(limit*Timeframe);
int ii;
if (total <= limit) { ii = total; j = 2; Console.WriteLine("Done"); } else { ii = limit; }
total -= ii;
int setlimit = ii;
DateTime Stringstart = overflowStart;
DateTime Stringend = overflowEnd;
var output = Alpaca(symbol, setlimit, Stringstart, Stringend);
barsobjects.Add(JsonSerializer.Serialize(output));
Console.WriteLine(output);
Console.WriteLine(JsonSerializer.Serialize(output));
Console.ReadLine();
counter++;
counter2++;
I++;
overflowStart = overflowEnd;
if (DateTime.UtcNow.Second.CompareTo(Previous.Second) >= 1)
{
ts.Add("Time:" + DateTime.UtcNow.Second.ToString() +", Ups:" + counter);
counter = 0;
Previous = DateTime.UtcNow;
}
counter3 = DateTime.UtcNow;
Console.WriteLine("Average UPS" + (counter2 / decimal.Parse(counter3.Subtract(counter3a).TotalSeconds.ToString())));
}
foreach (var time in ts)
{
Console.WriteLine(time);
Console.WriteLine();
}
return "hello";//ignore, eventual output of a single json value
}
public List<barsobject> Alpaca(string symbol, int limit, DateTime Start, DateTime End)
{
List<barsobject> dataBarsObject = new List<barsobject>();
APIKeys API = new APIKeys();
string ApiKeyPrivate = API.ALPCPrivatekey;
string ApiKeyPublic = API.ALPCPublicKey;
IAlpacaDataClient alpacaDataClient;
alpacaDataClient = Environments.Paper.GetAlpacaDataClient(new SecretKey(ApiKeyPublic, ApiKeyPrivate));
var bsr = alpacaDataClient.GetBarSetAsync(new BarSetRequest(symbol, TimeFrame.FiveMinutes) { Limit = limit }.SetInclusiveTimeInterval(Start, End)).Result;
var x = bsr[symbol];
var y = x.ToList();
foreach(var items in y)
{
barsobject barsobjects = new barsobject()
{
DateTime = items.TimeUtc.GetValueOrDefault(),
Open = items.Open,
Close = items.Close,
High = items.High,
Low = items.Low,
Volume = items.Volume,
};
dataBarsObject.Add(barsobjects);
}
return dataBarsObject;
}