public override void Refresh() { //Necessary hack, openpositions are deleted in another context when importing statements //so we need to detach and reload everything OpenPositions.Clear(); FXPositions.Clear(); using (var context = new DBContext()) { if (SelectedAccount.AccountId == "All") { OpenPositions.AddRange(context.OpenPositions.Include(x => x.Instrument).Include(x => x.Currency).ToList()); FXPositions.AddRange(context.FXPositions.Include(x => x.FXCurrency).ToList()); } else if (SelectedAccount != null) { OpenPositions.AddRange(context.OpenPositions.Where(x => x.AccountID == SelectedAccount.ID).Include(x => x.Instrument).Include(x => x.Currency).ToList()); FXPositions.AddRange(context.FXPositions.Where(x => x.AccountID == SelectedAccount.ID).Include(x => x.FXCurrency).ToList()); } //Add any accounts that exist in the db but are missing here var tmpAccounts = context.Accounts.ToList(); var newAccounts = tmpAccounts.Except(Accounts, new LambdaEqualityComparer <Account>((x, y) => x.ID == y.ID)); Accounts.AddRange(newAccounts); } UpdateChartSeries(); }
public void ProcessTransaction(OrderTransaction trans) { string comment; if (trans.OrderId == 8) { comment = ""; } IPositionInventory openPosition = OpenPositions.FirstOrDefault(p => p.GetSymbol() == trans.Symbol); if (openPosition == null) { openPosition = OpenPosition(trans, PositionInventoryMethod.Lifo); OpenPositions.Add(openPosition); } else { OrderTransaction transaction = ResolvePosition(openPosition, trans); if (openPosition.BuysCount() == 0 && openPosition.SellsCount() == 0) { OpenPositions.Remove(openPosition); } } }
public bool IsShort(Symbol symbol) { if (OpenPositions.Count > 0) { var position = OpenPositions.FirstOrDefault(b => b.Symbol == symbol); if (position != null && position.SellsCount() > 0) { return(true); } } return(false); }
private void CreatePositionLists() { foreach (Position pos_i in Positions) { bool positionIsClosed = pos_i.CurrentQuantity == 0; if (positionIsClosed) { ExitedPositions.Add(pos_i); } else { OpenPositions.Add(pos_i); } } }
internal int GetPosition(Symbol symbol) { var openPosition = OpenPositions.FirstOrDefault(); if (openPosition != null && openPosition.GetBuysQuantity(symbol) > 0) { return(openPosition.GetBuysQuantity(symbol)); } if (openPosition != null && openPosition.GetSellsQuantity(symbol) < 0) { return(openPosition.GetSellsQuantity(symbol)); } return(0); }
private void UpdateChartSeries() { if (SelectedAccount == null) { return; } UnrealizedPnL.Clear(); foreach (var tuple in OpenPositions .Where(x => x.Instrument != null) .OrderBy(x => x.UnrealizedPnL) .Select(x => new Tuple <string, decimal>(x.Instrument.Symbol, x.UnrealizedPnL))) { UnrealizedPnL.Add(tuple); } UnrealizedPnLChartModel.Axes[1].Maximum = UnrealizedPnL.Count; UnrealizedPnLChartModel.InvalidatePlot(true); }
public static void Main() { #region Test Code/Classes //string tickerSymbol = "AAPL"; ////initializes method from SMADayAverage class and sets it to a variable //SMADayAverage RealTimeSMA = new SMADayAverage(); ////calls method from SMADayAverage class //var SMAAverageDay_13 = RealTimeSMA.SMACurrentAverage(13, tickerSymbol); //var SMAAverageDay_8 = RealTimeSMA.SMACurrentAverage(8, tickerSymbol); //var SMAAverageDay_5 = RealTimeSMA.SMACurrentAverage(5, tickerSymbol); //Console.WriteLine($"20SMA: { SMAAverageDay_13}"); //Console.WriteLine($"10SMA: {SMAAverageDay_8}"); //Console.WriteLine($"5SMA: {SMAAverageDay_5}"); //Console.ReadLine(); ////initializes method from GetLatestPrice class and sets it to a variable //GetLatestPrice RealTimePrice = new GetLatestPrice(); ////call method from GetLatestPrice class and return the decemial value //var currentPrice = RealTimePrice.TickerCurrentPrice(tickerSymbol); //Console.WriteLine($"LatestPrice: {currentTickerPrice}"); ////initializes method from BuyShares class and then executes buy order for ticker //BuyShares buy = new BuyShares(); //var buyTicker = buy.ExecuteOrder("AAPL"); ////initializes method from SellShares class and then executes sell order for ticker //SellShares sell = new SellShares(); //var sellTicker = sell.ExecuteOrder("AAPL"); ////initializes method from Account Summary class then displays gain/loss on account for the intraday //AccountSummary ViewAccountSummary = new AccountSummary(); //var portfolioBalance = ViewAccountSummary.ExecuteAccountEquity(); #endregion #region Timer ////timeer for starting the real loop at 9:30am or around that time //var waitFiveMinutes = TimeSpan.FromMinutes(5); //int x = 0; //while (x < 18) //{ // Thread.Sleep(waitFiveMinutes); // x++; // Console.WriteLine("tick-tock"); //} #endregion List <string> tickerList = new List <string> { "AAPL", "MSFT", "CSCO" }; var waitFiveSeconds = TimeSpan.FromSeconds(5); int i = 0; while (i < 5399) { Thread.Sleep(waitFiveSeconds); i++; foreach (string tickerSymbol in tickerList) { //initializes method from GetLatestPrice class and sets it to a variable GetLatestPrice RealTimePrice = new GetLatestPrice(); //call method from GetLatestPrice class and return the decemial value var currentTickerPrice = RealTimePrice.TickerCurrentPrice(tickerSymbol); double currentPrice = Convert.ToDouble(currentTickerPrice); decimal _CurrentPrice = Convert.ToDecimal(currentTickerPrice); //Console.WriteLine($"LatestPrice: {currentTickerPrice}"); //initializes method from Account Summary class then displays amount in account and sets 5% of the account to be traded AccountSummary ViewAccountSummary = new AccountSummary(); var portfolioBalance = ViewAccountSummary.ExecuteAccountEquity(); //Console.WriteLine(portfolioBalance.Result); //converts portfolio balance to a double so it can be divided to get 5% of account var results = portfolioBalance.Result; double doublevalue = Convert.ToDouble(results); var tenPercentOfAccount = doublevalue * 0.10; //Console.WriteLine(fivePercentOfAccount); //sets the maximum gain and loss the account is willing to gin and or loss then converts those values to decimals var Loss = tenPercentOfAccount * -.05; decimal maximumLoss = Convert.ToDecimal(Loss); var Gain = tenPercentOfAccount * .1; decimal maximumGain = Convert.ToDecimal(Gain); //figures out how many share you can buy with 5% of account then convert value from double to int and round to the nearest whole number var shareCountDouble = tenPercentOfAccount / currentPrice; int shareCount = Convert.ToInt32(Math.Floor(shareCountDouble)); //Console.WriteLine(shareCount); //initializes method from OpenPositions class and pings back how many shares are open currently OpenPositions ViewOpenPositions = new OpenPositions(); var quantity = ViewOpenPositions.ViewOpenSharesQuantity(tickerSymbol); var sharesOpen = quantity.Result; //calls task method and returns value for symbol var symbol = ViewOpenPositions.ViewPositionSymbol(tickerSymbol); var tickerName = symbol.Result; //Console.WriteLine(tickerName); //calls task method and returns value for gain or loss var change = ViewOpenPositions.ViewPositionUnrealizedProfitLoss(tickerSymbol); var gainOrLoss = change.Result; //Console.WriteLine(gainloss); //initializes method from SMADayAverage class and sets it to a variable SMADayAverage RealTimeSMA = new SMADayAverage(); //calls method from SMADayAverage class var SMAAverageDay_11 = RealTimeSMA.SMACurrentAverage(11, tickerSymbol); var SMAAverageDay_8 = RealTimeSMA.SMACurrentAverage(8, tickerSymbol); var SMAAverageDay_5 = RealTimeSMA.SMACurrentAverage(5, tickerSymbol); Console.WriteLine(tickerSymbol); Console.WriteLine($"11SMA: {SMAAverageDay_11}"); Console.WriteLine($"8SMA: {SMAAverageDay_8}"); Console.WriteLine($"5SMA: {SMAAverageDay_5}"); Console.WriteLine("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); //safeguard for if the SMA API call encounters an exception and returns a 0 so it goes back to the beginning of loop if (SMAAverageDay_11 == 0 || SMAAverageDay_8 == 0 || SMAAverageDay_5 == 0) { continue; } else if (tickerName != tickerSymbol || string.IsNullOrEmpty(tickerName)) { if (SMAAverageDay_5 >= SMAAverageDay_8 && _CurrentPrice > SMAAverageDay_11) { //initializes method from BuyShares class and then executes buy order for ticker BuyShares buy = new BuyShares(); var buyTicker = buy.ExecuteOrder(tickerSymbol, shareCount); Console.WriteLine("-------------------------------------------------------------------------------------"); Console.WriteLine($"Bought {shareCount} shares of {tickerSymbol} at ${_CurrentPrice}"); Console.WriteLine("-------------------------------------------------------------------------------------"); continue; } else { continue; } } else if (tickerName == tickerSymbol) { if (SMAAverageDay_5 <= SMAAverageDay_8 && _CurrentPrice <= SMAAverageDay_11) { //initializes method from SellShares class and then executes sell order for ticker SellShares sell = new SellShares(); var sellTicker = sell.ExecuteOrder(tickerSymbol, sharesOpen); Console.WriteLine("-------------------------------------------------------------------------------------"); Console.WriteLine($"Sold {sharesOpen} shares of {tickerSymbol} at ${_CurrentPrice}"); continue; } else if (gainOrLoss <= maximumLoss) { //initializes method from SellShares class and then executes sell order for ticker SellShares sell = new SellShares(); var sellTicker = sell.ExecuteOrder(tickerSymbol, sharesOpen); Console.WriteLine("-------------------------------------------------------------------------------------"); Console.WriteLine($"Sold {sharesOpen} shares of {tickerSymbol} at ${_CurrentPrice}"); continue; } else if (gainOrLoss >= maximumGain) { //initializes method from SellShares class and then executes sell order for ticker SellShares sell = new SellShares(); var sellTicker = sell.ExecuteOrder(tickerSymbol, sharesOpen); Console.WriteLine("-------------------------------------------------------------------------------------"); Console.WriteLine($"Sold {sharesOpen} shares of {tickerSymbol} at ${_CurrentPrice}"); continue; } else { continue; } } } } AccountSummary AccountSummary = new AccountSummary(); var totalGainLoss = AccountSummary.ExecuteBalanceView(); Console.ReadLine(); }