Example #1
0
        private async Task AnalizeBuy()
        {
            foreach (var stock in Settings.Stocks)
            {
                var data      = MarketData.GetData()[stock];
                var lastIndex = data.Count - 1;

                if (lastIndex < 2)
                {
                    return;
                }

                var p0 = Convert.ToDouble(data[lastIndex - 0].Price());
                var p1 = Convert.ToDouble(data[lastIndex - 1].Price());
                var p2 = Convert.ToDouble(data[lastIndex - 2].Price());

                var buyVolume = 5;

                if (p0 > p1 && p1 > p2 && p0 - p1 > p1 - p2 && AlpacaData.GetPositionCount(stock) == 0 &&
                    AlpacaData.GetAccountData().Equity() - p0 * buyVolume >= 0)
                {
                    await Buy(stock, p0, buyVolume);
                }

                //if ((p0 < p1 || p0 - p1 < p1 - p2) && AlpacaData.GetPositionCount(stock) > 0)
                //    await Sell(stock, p2);
            }
        }
Example #2
0
        private async Task AnalizeSell()
        {
            foreach (var stock in Settings.Stocks)
            {
                if (AlpacaData.GetPositionCount(stock) == 0)
                {
                    continue;
                }

                var stockShortHistory = AlpacaData.GetPositionShortHistory()[stock];

                if (stockShortHistory.Count < 2)
                {
                    return;
                }

                var p0 = stockShortHistory[stockShortHistory.Count - 1];
                var p1 = stockShortHistory[stockShortHistory.Count - 2];

                if (p0 < p1 && AlpacaData.GetPositionCount(stock) > 0)
                {
                    await Sell(stock, p0);
                }
            }
        }
Example #3
0
 private async void UpdateMarketData(object sender, EventArgs e)
 {
     _marketDataUpdater.Stop();
     //await MarketData.Update();
     MarketData.UpdateFake();
     _marketDataUpdater.Start();
     //await AnalizeBuy();
     Plot.Update(ref MarketData.GetData());
     Legend.Update(ref MarketData.GetData(), AlpacaData.GetAccountData(), AlpacaData.GetPositionns());
 }
Example #4
0
 private void UpdatePositions()
 {
     Positions.Clear();
     foreach (var position in AlpacaData.GetPositionns())
     {
         Positions.Text +=
             $"{position.Symbol()} {position.Volume():#.00} {position.CurrenPrice():#.00}{AlpacaData.GetAccountData().Currency()}" +
             Environment.NewLine;
     }
     Positions.Foreground = new SolidColorBrush(Colors.Purple);
 }
Example #5
0
        private async void UpdateAlpacaData(object sender, EventArgs e)
        {
            _accountDataUpdater.Stop();
            await AlpacaData.Update();

            _accountDataUpdater.Start();
            await AnalizeSell();

            UpdatePositions();
            Legend.Update(ref MarketData.GetData(), AlpacaData.GetAccountData(), AlpacaData.GetPositionns());
        }
Example #6
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;
            }
        }
Example #7
0
        private async Task Sell(string stock, double price)
        {
            var stockCount = AlpacaData.GetPositionCount(stock);

            if (await MakeTransaction(stock, stockCount, TransactionOption.SELL))
            {
                AlpacaData.ClearPositionShortHistory(stock);
                LogMessage("Sell " + stockCount + " of " + stock + " at " + price);
            }
            else
            {
                LogMessage("Sell " + stockCount + " of " + stock + " at " + price + " - FAILED");
            }
        }
Example #8
0
        private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var tb = (TextBlock)sender;

            foreach (var price in _stockTextBlocks)
            {
                if (!price.Value.Equals(tb))
                {
                    continue;
                }
                Settings.SelectedStock = price.Key;
                break;
            }
            SelectedStockChenged.Invoke(sender, new EventArgs());
            Update(ref MarketData.GetData(), AlpacaData.GetAccountData(), AlpacaData.GetPositionns());
        }
Example #9
0
 private void InitializeAccountData()
 {
     AlpacaData.Initialize();
     AlpacaData.LogMessage += LogMessage;
 }
Example #10
0
 private void LegendCanvas_SizeChanged(object sender, SizeChangedEventArgs e)
 {
     Update(ref MarketData.GetData(), AlpacaData.GetAccountData(), AlpacaData.GetPositionns());
 }