Example #1
0
        public static void Initialize()
        {
            _accountData          = new AlpacaAccountData(new JObject());
            _positionsData        = new List <AlpacaPosition>();
            _positionShortHistory = new Dictionary <string, List <double> >();
            foreach (var stock in Settings.Stocks)
            {
                _positionShortHistory[stock] = new List <double>();
            }

            _initialized = true;
        }
Example #2
0
        private static async Task <bool> UpdateAccountData()
        {
            if (!_initialized)
            {
                return(false);
            }

            try
            {
                var client  = new HttpClient();
                var request = new HttpRequestMessage();
                request.Method = new HttpMethod("GET");
                request.Headers.Add("APCA-API-KEY-ID", Settings.AlpacaAPIKeyID);
                request.Headers.Add("APCA-API-SECRET-KEY", Settings.AlpacaSecretKey);
                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                request.RequestUri = new Uri(Settings.AlpacaEndpoint + "/v2/account", UriKind.RelativeOrAbsolute);

                var response = await client.SendAsync(request);

                if (!response.IsSuccessStatusCode)
                {
                    LogMessage.Invoke(null, response.ReasonPhrase);
                    return(false);
                }
                var contentString = await response.Content.ReadAsStringAsync();

                dynamic contentJson = JsonConvert.DeserializeObject(contentString);
                _accountData = new AlpacaAccountData((JObject)contentJson);
            }
            catch (Exception e)
            {
                LogMessage.Invoke(null, e.Message);
                return(false);
            }

            return(true);
        }
Example #3
0
        public void Update(ref Dictionary <string, List <MarketDataTimePoint> > marketData, AlpacaAccountData accountData, List <AlpacaPosition> positionData)
        {
            if (!_initialized)
            {
                return;
            }

            foreach (var stock in Settings.Stocks)
            {
                var count = marketData[stock].Count;
                if (count < 1)
                {
                    return;
                }
                if (count > 1)
                {
                    if (Convert.ToDouble(marketData[stock][count - 1].Price()) >
                        Convert.ToDouble(marketData[stock][count - 2].Price()))
                    {
                        _stockTextColors[stock] = Colors.Green;
                    }
                    else if (Convert.ToDouble(marketData[stock][count - 1].Price()) <
                             Convert.ToDouble(marketData[stock][count - 2].Price()))
                    {
                        _stockTextColors[stock] = Colors.Red;
                    }
                    else
                    {
                        _stockTextColors[stock] = Colors.Gray;
                    }
                }

                _stockTextBlocks[stock].Text = "(" + AlpacaData.GetPositionCount(stock) + ") " + stock + " - " +
                                               $"{Convert.ToDouble(marketData[stock][count - 1].Price()):#.00} " +
                                               accountData.Currency();
                _stockTextBlocks[stock].Width      = LegendCanvas.ActualWidth;
                _stockTextBlocks[stock].Foreground = new SolidColorBrush(_stockTextColors[stock]);

                if (stock == Settings.SelectedStock)
                {
                    _stockTextBlocks[stock].Background = new SolidColorBrush(_selectedStockBackgroungColor);
                    _stockTextBlocks[stock].FontWeight = FontWeights.ExtraBold;
                }
                else
                {
                    _stockTextBlocks[stock].Background = new SolidColorBrush(Colors.Transparent);
                    _stockTextBlocks[stock].FontWeight = FontWeights.Normal;
                }

                _equityTextBlock.Text   = $"Equity: {accountData.Equity():#.00} " + accountData.Currency();
                _equityTextBlock.Margin = new Thickness(0, LegendCanvas.ActualHeight - 20, 0, 0);
                _equityTextBlock.Width  = LegendCanvas.ActualWidth;
            }
        }