Esempio n. 1
0
        public static async Task <Instrument> GetInstrumentAsync(StocksDbContext stocksDbContext, Dictionary <string, string> settings, Models.TdAmeritrade.Account.Instrument instrument, CancellationToken cancellationToken = default)
        {
            Instrument ins;
            IReadOnlyCollection <Models.TdAmeritrade.Account.Instrument> instruments;
            JsonSerializerOptions jsonSerializerOptions;
            string json;

            jsonSerializerOptions = new();
            jsonSerializerOptions.PropertyNameCaseInsensitive = true;

            try
            {
                ins = stocksDbContext.Instrument.Single(x => x.Symbol == instrument.Symbol && x.Cusip == instrument.Cusip && x.AssetType == instrument.AssetType);
            }

            catch
            {
                json = await Requester.SendRequestAsync(Enums.HttpVerb.Get, $"{settings["InstrumentsUri"]}/{instrument.Cusip}?apikey={settings["ApiKey"]}", null, cancellationToken);

                instruments = JsonSerializer.Deserialize <IReadOnlyCollection <Models.TdAmeritrade.Account.Instrument> >(json, jsonSerializerOptions);
                ins         = new Instrument(instruments.Single());
            }

            return(ins);
        }
Esempio n. 2
0
 public Position(StocksDbContext stocksDbContext, Dictionary <string, string> settings, Models.TdAmeritrade.Account.Position position, string accountId)
 {
     Instrument                     = Instrument.GetInstrumentAsync(stocksDbContext, settings, position.Instrument).Result;
     Exchange                       = Instrument.Exchange;
     Symbol                         = Instrument.Symbol;
     AccountId                      = accountId;
     ShortQuantity                  = position.ShortQuantity;
     AveragePrice                   = position.AveragePrice;
     CurrentDayProfitLoss           = position.CurrentDayProfitLoss;
     CurrentDayProfitLossPercentage = position.CurrentDayProfitLossPercentage;
     LongQuantity                   = position.LongQuantity;
     SettledLongQuantity            = position.SettledLongQuantity;
     SettledShortQuantity           = position.SettledShortQuantity;
     MarketValue                    = position.MarketValue;
     Updated                        = DateTime.UtcNow;
 }
        public SecuritiesAccount(StocksDbContext stocksDbContext, Dictionary <string, string> settings, Models.TdAmeritrade.Account.SecuritiesAccount securitiesAccount)
        {
            AccountId               = securitiesAccount.AccountId;
            Type                    = securitiesAccount.Type;
            RoundTrips              = securitiesAccount.RoundTrips;
            IsDayTrader             = securitiesAccount.IsDayTrader;
            IsClosingOnlyRestricted = securitiesAccount.IsClosingOnlyRestricted;
            Positions               = new HashSet <Position>();
            if (securitiesAccount.Positions != null)
            {
                foreach (Models.TdAmeritrade.Account.Position position in securitiesAccount.Positions)
                {
                    Positions.Add(new Position(stocksDbContext, settings, position, securitiesAccount.AccountId));
                }
            }

            if (securitiesAccount.CurrentBalances != null)
            {
                CurrentBalances = new CurrentBalances(securitiesAccount.CurrentBalances, securitiesAccount.AccountId);
            }

            Updated = DateTime.UtcNow;
        }