Пример #1
0
        public async Task <object> ParseData_Daily(string data, int maxrecords = -1)
        {
            Data.Daily dd = new Data.Daily();

            try {
                using (StringReader sr = new StringReader(data)) {
                    using (CsvReader csv = new CsvReader(sr, CultureInfo.InvariantCulture)) {
                        csv.Read();
                        csv.ReadHeader();
                        while (maxrecords != 0 && csv.Read())
                        {
                            dd.Prices.Add(new Data.Daily.Price()
                            {
                                Date   = csv.GetField <DateTime>("timestamp"),
                                Open   = csv.GetField <decimal>("open"),
                                High   = csv.GetField <decimal>("high"),
                                Low    = csv.GetField <decimal>("low"),
                                Close  = csv.GetField <decimal>("adjusted_close"),
                                Volume = csv.GetField <decimal>("volume")
                            });

                            maxrecords--;
                        }
                    }
                }

                return(dd);
            } catch (Exception ex) {
                await Log.Error($"{MethodBase.GetCurrentMethod().DeclaringType}: {MethodBase.GetCurrentMethod().Name}", ex);

                return(null);
            }
        }
Пример #2
0
        public async Task <object> GetData_Daily(Data.Asset asset, int limit = 500)
        {
            try {
                IAlpacaDataClient data = null;

                if (!String.IsNullOrWhiteSpace(Settings.API_Alpaca_Live_Key) && !String.IsNullOrWhiteSpace(Settings.API_Alpaca_Live_Secret))
                {
                    data = Environments.Live.GetAlpacaDataClient(new SecretKey(Settings.API_Alpaca_Live_Key, Settings.API_Alpaca_Live_Secret));
                }
                else if (!String.IsNullOrWhiteSpace(Settings.API_Alpaca_Paper_Key) && !String.IsNullOrWhiteSpace(Settings.API_Alpaca_Paper_Secret))
                {
                    data = Environments.Paper.GetAlpacaDataClient(new SecretKey(Settings.API_Alpaca_Paper_Key, Settings.API_Alpaca_Paper_Secret));
                }
                else
                {
                    return(new ArgumentNullException());
                }

                // Maximum 1000 bars per API call
                var bars = await data.GetBarSetAsync(new BarSetRequest(asset.Symbol, TimeFrame.Day) { Limit = limit });

                Data.Daily ds = new Data.Daily();
                foreach (var bar in bars[asset.Symbol])
                {
                    if (bar.TimeUtc != null)
                    {
                        ds.Prices.Add(new Data.Daily.Price()
                        {
                            Date   = bar.TimeUtc ?? new DateTime(),
                            Open   = bar.Open,
                            High   = bar.High,
                            Low    = bar.Low,
                            Close  = bar.Close,
                            Volume = bar.Volume
                        });
                    }
                }

                return(ds);
            } catch (Exception ex) {
                if (ex.Message != "Too Many Requests")                      // This is handled elsewhere- does not need to be error logged
                {
                    await Log.Error($"{MethodBase.GetCurrentMethod().DeclaringType}: {MethodBase.GetCurrentMethod().Name}", ex);
                }
                return(ex.Message);
            }
        }