public Models.Movie Update(Models.Movie movieModel) { if (movieModel.Format == null) { throw new ArgumentNullException("Format", "The Format property must not be null."); } if (movieModel.Director == null) { throw new ArgumentNullException("Director", "The Director property must not be null."); } var existing = db.Movies.SingleOrDefault(x => x.Id == movieModel.Id); if (existing == null) { return(null); } existing.Cost = movieModel.Cost; existing.Description = movieModel.Description; existing.Title = movieModel.Title; existing.ImagePath = movieModel.ImagePath; Data.Director existingDirectors = db.Directors.SingleOrDefault(x => x.Id == movieModel.Director.Id); existing.Director = existingDirectors ?? throw new ArgumentException("The associated Director cannot be found."); Data.Format existingFormats = db.Formats.SingleOrDefault(x => x.Id == movieModel.Format.Id); existing.Format = existingFormats ?? throw new ArgumentException("The associated format cannot be found."); Data.Rating existingRatings = db.Ratings.SingleOrDefault(x => x.Id == movieModel.Rating.Id); existing.Rating = existingRatings ?? throw new ArgumentException("The associated Rating cannot be found."); db.SaveChanges(); return(movieModel); }
public async Task ClearOrders(Data.Format format) { IAlpacaTradingClient trading = null; try { if (format == Data.Format.Live) { if (String.IsNullOrWhiteSpace(Settings.API_Alpaca_Live_Key) || String.IsNullOrWhiteSpace(Settings.API_Alpaca_Live_Secret)) { return; } trading = Environments.Live.GetAlpacaTradingClient(new SecretKey(Settings.API_Alpaca_Live_Key, Settings.API_Alpaca_Live_Secret)); } else if (format == Data.Format.Paper) { if (String.IsNullOrWhiteSpace(Settings.API_Alpaca_Paper_Key) || String.IsNullOrWhiteSpace(Settings.API_Alpaca_Paper_Secret)) { return; } trading = Environments.Paper.GetAlpacaTradingClient(new SecretKey(Settings.API_Alpaca_Paper_Key, Settings.API_Alpaca_Paper_Secret)); } await trading.DeleteAllOrdersAsync(); return; } catch (Exception ex) { await Log.Error($"{MethodBase.GetCurrentMethod().DeclaringType}: {MethodBase.GetCurrentMethod().Name}", ex); return; } }
public int Add(Models.Movie movieModel) { if (movieModel.Format == null) { throw new ArgumentNullException("Format", "The Format property must not be null."); } if (movieModel.Director == null) { throw new ArgumentNullException("Director", "The Director property must not be null."); } Data.Movie newRow = new Data.Movie(); newRow.Cost = movieModel.Cost; newRow.Description = movieModel.Description; newRow.Title = movieModel.Title; newRow.ImagePath = movieModel.ImagePath; Data.Rating existingRatings = db.Ratings.SingleOrDefault(x => x.Id == movieModel.Rating.Id); newRow.Rating = existingRatings ?? throw new ArgumentException("The associated rating cannot be found."); Data.Director existingDirectors = db.Directors.SingleOrDefault(x => x.Id == movieModel.Director.Id); newRow.Director = existingDirectors ?? throw new ArgumentException("The associated Director cannot be found."); Data.Format existingFormats = db.Formats.SingleOrDefault(x => x.Id == movieModel.Format.Id); newRow.Format = existingFormats ?? throw new ArgumentException("The associated format cannot be found."); db.Movies.Add(newRow); db.SaveChanges(); return(newRow.Id); }
public async Task <object> GetTradeableCash(Data.Format format) { IAlpacaTradingClient trading = null; try { if (format == Data.Format.Live) { if (String.IsNullOrWhiteSpace(Settings.API_Alpaca_Live_Key) || String.IsNullOrWhiteSpace(Settings.API_Alpaca_Live_Secret)) { return(null); } trading = Environments.Live.GetAlpacaTradingClient(new SecretKey(Settings.API_Alpaca_Live_Key, Settings.API_Alpaca_Live_Secret)); } else if (format == Data.Format.Paper) { if (String.IsNullOrWhiteSpace(Settings.API_Alpaca_Paper_Key) || String.IsNullOrWhiteSpace(Settings.API_Alpaca_Paper_Secret)) { return(null); } trading = Environments.Paper.GetAlpacaTradingClient(new SecretKey(Settings.API_Alpaca_Paper_Key, Settings.API_Alpaca_Paper_Secret)); } var account = await trading.GetAccountAsync(); return(account.TradableCash); } catch (Exception ex) { await Log.Error($"{MethodBase.GetCurrentMethod().DeclaringType}: {MethodBase.GetCurrentMethod().Name}", ex); return(ex.Message); } }
public async Task <Trading.OrderResult> PlaceOrder_SellStopLimit( Data.Format format, string symbol, int shares, decimal stopPrice, decimal limitPrice, TimeInForce timeInForce = TimeInForce.Gtc) { IAlpacaTradingClient trading = null; try { if (format == Data.Format.Live) { if (String.IsNullOrWhiteSpace(Settings.API_Alpaca_Live_Key) || String.IsNullOrWhiteSpace(Settings.API_Alpaca_Live_Secret)) { return(Trading.OrderResult.Fail); } trading = Environments.Live.GetAlpacaTradingClient(new SecretKey(Settings.API_Alpaca_Live_Key, Settings.API_Alpaca_Live_Secret)); } else if (format == Data.Format.Paper) { if (String.IsNullOrWhiteSpace(Settings.API_Alpaca_Paper_Key) || String.IsNullOrWhiteSpace(Settings.API_Alpaca_Paper_Secret)) { return(Trading.OrderResult.Fail); } trading = Environments.Paper.GetAlpacaTradingClient(new SecretKey(Settings.API_Alpaca_Paper_Key, Settings.API_Alpaca_Paper_Secret)); } // Prevents exceptions or unwanted behavior with Alpaca API var account = await trading.GetAccountAsync(); if (trading == null || account == null || account.IsAccountBlocked || account.IsTradingBlocked || account.TradeSuspendedByUser) { return(Trading.OrderResult.Fail); } // Prevents unintentionally short selling (selling into negative digits, the API interprets that as intent to short-sell) var positions = await trading.ListPositionsAsync(); if (!positions.Any(p => p.Symbol == symbol)) // If there is no position for this symbol { return(Trading.OrderResult.Fail); } var position = await trading.GetPositionAsync(symbol); // If there were no position, this would throw an Exception! if (position == null || position.Quantity < shares) // If the current position doesn't have enough shares { return(Trading.OrderResult.Fail); } var order = await trading.PostOrderAsync(StopLimitOrder.Sell(symbol, shares, stopPrice, limitPrice).WithDuration(timeInForce)); return(Trading.OrderResult.Success); } catch (Exception ex) { await Log.Error($"{MethodBase.GetCurrentMethod().DeclaringType}: {MethodBase.GetCurrentMethod().Name}", ex); return(Trading.OrderResult.Fail); } }
public int Create(Models.Format format) { Data.Format newFormat = new Data.Format { Description = format.Description }; db.Formats.Add(newFormat); db.SaveChanges(); return(newFormat.Id); }
public async Task <object> GetOrders_OpenSell(Data.Format format) { object result = await GetOrders_Open(format); if (result is List <Data.Order> pmldo) { return(pmldo.Where(o => o.Transaction == Data.Order.Direction.Sell).ToList()); } else { return(result); } }
public async Task <object> GetOrders_Open(Data.Format format) { IAlpacaTradingClient trading = null; IAlpacaDataClient data = null; try { if (format == Data.Format.Live) { if (String.IsNullOrWhiteSpace(Settings.API_Alpaca_Live_Key) || String.IsNullOrWhiteSpace(Settings.API_Alpaca_Live_Secret)) { return(null); } trading = Environments.Live.GetAlpacaTradingClient(new SecretKey(Settings.API_Alpaca_Live_Key, Settings.API_Alpaca_Live_Secret)); data = Environments.Live.GetAlpacaDataClient(new SecretKey(Settings.API_Alpaca_Live_Key, Settings.API_Alpaca_Live_Secret)); } else if (format == Data.Format.Paper) { if (String.IsNullOrWhiteSpace(Settings.API_Alpaca_Paper_Key) || String.IsNullOrWhiteSpace(Settings.API_Alpaca_Paper_Secret)) { return(null); } trading = Environments.Paper.GetAlpacaTradingClient(new SecretKey(Settings.API_Alpaca_Paper_Key, Settings.API_Alpaca_Paper_Secret)); data = Environments.Paper.GetAlpacaDataClient(new SecretKey(Settings.API_Alpaca_Paper_Key, Settings.API_Alpaca_Paper_Secret)); } return((await trading.ListOrdersAsync( new ListOrdersRequest() { OrderStatusFilter = OrderStatusFilter.Open, LimitOrderNumber = 1000 })) .Select <IOrder, Data.Order>((q) => { return new Data.Order() { Symbol = q.Symbol, Transaction = q.OrderSide == OrderSide.Buy ? Data.Order.Direction.Buy : Data.Order.Direction.Sell, Quantity = (int)q.Quantity }; }) .ToList()); } catch (Exception ex) { await Log.Error($"{MethodBase.GetCurrentMethod().DeclaringType}: {MethodBase.GetCurrentMethod().Name}", ex); return(ex.Message); } }
public async Task <Trading.OrderResult> PlaceOrder_BuyMarket( Data.Format format, string symbol, int shares, TimeInForce timeInForce = TimeInForce.Gtc, bool useMargin = false) { IAlpacaTradingClient trading = null; IAlpacaDataClient data = null; try { if (format == Data.Format.Live) { if (String.IsNullOrWhiteSpace(Settings.API_Alpaca_Live_Key) || String.IsNullOrWhiteSpace(Settings.API_Alpaca_Live_Secret)) { return(Trading.OrderResult.Fail); } trading = Environments.Live.GetAlpacaTradingClient(new SecretKey(Settings.API_Alpaca_Live_Key, Settings.API_Alpaca_Live_Secret)); data = Environments.Live.GetAlpacaDataClient(new SecretKey(Settings.API_Alpaca_Live_Key, Settings.API_Alpaca_Live_Secret)); } else if (format == Data.Format.Paper) { if (String.IsNullOrWhiteSpace(Settings.API_Alpaca_Paper_Key) || String.IsNullOrWhiteSpace(Settings.API_Alpaca_Paper_Secret)) { return(Trading.OrderResult.Fail); } trading = Environments.Paper.GetAlpacaTradingClient(new SecretKey(Settings.API_Alpaca_Paper_Key, Settings.API_Alpaca_Paper_Secret)); data = Environments.Paper.GetAlpacaDataClient(new SecretKey(Settings.API_Alpaca_Paper_Key, Settings.API_Alpaca_Paper_Secret)); } var account = await trading.GetAccountAsync(); if (trading == null || account == null || account.IsAccountBlocked || account.IsTradingBlocked || account.TradeSuspendedByUser) { return(Trading.OrderResult.Fail); } var order = await trading.PostOrderAsync(MarketOrder.Buy(symbol, shares).WithDuration(timeInForce)); return(Trading.OrderResult.Success); } catch (Exception ex) { await Log.Error($"{MethodBase.GetCurrentMethod().DeclaringType}: {MethodBase.GetCurrentMethod().Name}", ex); return(Trading.OrderResult.Fail); } }
public async Task <object> GetPositions(Data.Format format) { IAlpacaTradingClient client = null; try { if (format == Data.Format.Live) { if (String.IsNullOrWhiteSpace(Settings.API_Alpaca_Live_Key) || String.IsNullOrWhiteSpace(Settings.API_Alpaca_Live_Secret)) { return(false); } client = Environments.Live.GetAlpacaTradingClient(new SecretKey(Settings.API_Alpaca_Live_Key, Settings.API_Alpaca_Live_Secret)); } else if (format == Data.Format.Paper) { if (String.IsNullOrWhiteSpace(Settings.API_Alpaca_Paper_Key) || String.IsNullOrWhiteSpace(Settings.API_Alpaca_Paper_Secret)) { return(false); } client = Environments.Paper.GetAlpacaTradingClient(new SecretKey(Settings.API_Alpaca_Paper_Key, Settings.API_Alpaca_Paper_Secret)); } if (client == null) { return(false); } var positions = await client.ListPositionsAsync(); return(positions.Select(p => new Data.Position() { ID = p.AssetId.ToString(), Symbol = p.Symbol, Quantity = p.Quantity }).ToList()); } catch (Exception ex) { await Log.Error($"{MethodBase.GetCurrentMethod().DeclaringType}: {MethodBase.GetCurrentMethod().Name}", ex); return(null); } }