コード例 #1
0
        public async Task Execute(IJobExecutionContext context)
        {
            _logger.LogInformation($"Starting {GetType().Name} job");

            await _alpacaClient.DisconnectStreamApis();

            foreach (var strategy in _strategies.Where(x => x.StockStrategy.TradingFrequency == Domain.TradingFrequency.Minute))
            {
                var position = await _trackingRepository.GetOrCreateEmptyPosition(strategy.StockStrategy);

                if (position.NumberOfShares > 0)
                {
                    var marketPriceTask = _alpacaClient.GetStockData(position.StockSymbol, strategy.StockStrategy.TradingFrequency, 1);
                    await _alpacaClient.PlaceOrder(new Domain.Order
                    {
                        StockSymbol     = strategy.StockStrategy.StockSymbol,
                        MarketPrice     = (await marketPriceTask).FirstOrDefault()?.ClosingPrice ?? 0,
                        OrderPlacedTime = DateTime.UtcNow,
                        SharesBought    = position.NumberOfShares * (-1)
                    });
                }
            }

            _logger.LogInformation($"Finished {GetType().Name} job");
        }
コード例 #2
0
        internal async Task HandleSell(decimal marketPrice, StrategysStock stockStrategy)
        {
            var currentPosition = await _trackingRepository.GetOrCreateEmptyPosition(stockStrategy);

            if (currentPosition.NumberOfShares > 0)
            {
                var order = new Order
                {
                    StockSymbol     = stockStrategy.StockSymbol,
                    SharesBought    = currentPosition.NumberOfShares * (-1),
                    MarketPrice     = marketPrice,
                    OrderPlacedTime = DateTime.UtcNow
                };

                await _trackingRepository.AddPendingOrder(stockStrategy, order);

                await _alpacaClient.PlaceOrder(order);
            }
        }
コード例 #3
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation($"Starting {GetType().Name} job");

            var currentPositions = (await _alpacaClient.GetPositions()).GroupBy(x => x.StockSymbol).Select(x => new Position
            {
                StockSymbol    = x.Key,
                NumberOfShares = x.Select(y => y.NumberOfShares).Sum()
            });

            foreach (var currentPosition in currentPositions)
            {
                var strategies = _strategies.Where(x => string.Equals(x.StockStrategy.StockSymbol, currentPosition.StockSymbol, StringComparison.OrdinalIgnoreCase));
                if (strategies.Any())
                {
                    var sharesPerPosition = currentPosition.NumberOfShares / strategies.Count();

                    foreach (var strategy in strategies)
                    {
                        await ReconcileStock(sharesPerPosition, strategy.StockStrategy);
                    }

                    var leftoverShares = currentPosition.NumberOfShares % strategies.Count();

                    await ReconcileStock(leftoverShares, strategies.First().StockStrategy);
                }
                else
                {
                    _logger.LogDebug($"Selling all {currentPosition.NumberOfShares} unneeded shares of {currentPosition.StockSymbol}");
                    await _alpacaClient.PlaceOrder(new Order
                    {
                        OrderPlacedTime = DateTime.UtcNow,
                        SharesBought    = currentPosition.NumberOfShares * (-1),
                        StockSymbol     = currentPosition.StockSymbol,
                    }, OrderTiming.GoodTillCanceled);
                }
            }

            _logger.LogInformation($"Finished {GetType().Name} job");
        }