Beispiel #1
0
        public void Should_define_a_ticker()
        {
            var result = Ticker.Build("GOOG");

            Assert.IsNotNull(result);
            Assert.AreEqual(result.Symbol, "GOOG");
        }
Beispiel #2
0
        public void Should_define_an_interval()
        {
            var result = Ticker.Build()
                         .SetInterval(Interval.OneHour);

            Assert.IsNotNull(result);
            Assert.AreEqual(result.Interval, Interval.OneHour);
        }
Beispiel #3
0
        public void Should_define_a_period()
        {
            var result = Ticker.Build()
                         .SetPeriod(Period.OneYear);

            Assert.IsNotNull(result);
            Assert.AreEqual(result.Period, Period.OneYear);
        }
Beispiel #4
0
        public void Should_define_splits()
        {
            var result = Ticker.Build()
                         .SetSplits(true);

            Assert.IsNotNull(result);
            Assert.IsTrue(result.Splits);
        }
Beispiel #5
0
        public void Should_define_dividends()
        {
            var result = Ticker.Build()
                         .SetDividends(true);

            Assert.IsNotNull(result);
            Assert.IsTrue(result.Dividends);
        }
Beispiel #6
0
        public void Should_define_local_time_zone()
        {
            var result = Ticker.Build()
                         .SetLocalTimeZone();

            Assert.IsNotNull(result);
            Assert.AreEqual(result.TimeZone, TimeZoneInfo.Local);
        }
Beispiel #7
0
        public void Should_define_a_time_zone()
        {
            var localTimeZone = TimeZoneInfo.Local;

            var result = Ticker.Build()
                         .SetTimeZone(localTimeZone);

            Assert.IsNotNull(result);
            Assert.AreEqual(result.TimeZone, localTimeZone);
        }
Beispiel #8
0
        public void Should_define_an_interval_of_dates_by_set_interval()
        {
            var startDate  = new DateTime(2020, 1, 1);
            var finishDate = new DateTime(2020, 5, 31);

            var result = Ticker.Build()
                         .SetInterval(startDate, finishDate);

            Assert.IsNotNull(result);
            Assert.AreEqual(result.StartDate, startDate);
            Assert.AreEqual(result.FinishDate, finishDate);
        }
Beispiel #9
0
        public void Should_download_ticker_data_from_yahoo_finance()
        {
            const string symbol = "ABEV3.SA";

            var ticker = Ticker.Build()
                         .SetSymbol(symbol)
                         .SetPeriod(Period.OneYear)
                         .SetInterval(Interval.OneDay)
                         .SetEvents(true);

            var result = ticker.Get();

            Assert.IsNotNull(ticker.Result);
            Assert.IsNotNull(result);

            Assert.AreEqual(result.Symbol, symbol);

            Assert.IsNotNull(result.Quotes);
            Assert.IsTrue(result.Quotes.Length > 0);

            Assert.IsNotNull(result.Dividends);
        }
Beispiel #10
0
        public void Should_download_ticker_data_from_yahoo_finance_by_start_finish_date()
        {
            const string symbol = "ABEV3.SA";

            var ticker = Ticker.Build()
                         .SetSymbol(symbol)
                         .SetStartDate(new DateTime(2019, 1, 1))
                         .SetFinishDate(new DateTime(2020, 12, 1))
                         .SetInterval(Interval.OneDay)
                         .SetEvents(true);

            var result = ticker.Get();

            Assert.IsNotNull(ticker.Result);
            Assert.IsNotNull(result);

            Assert.AreEqual(result.Symbol, symbol);

            Assert.IsNotNull(result.Quotes);
            Assert.IsTrue(result.Quotes.Length > 0);

            Assert.IsNotNull(result.Dividends);
        }
        public async Task Execute(CancellationToken stoppingToken)
        {
            var availableCash = await _tradingClient.GetCashAvailable();

            _logger.LogInformation("Cash Available : {0} USD", availableCash);

            var marketStatus = await _tradingClient.GetMarketStatus();

            var now  = TimeZoneInfo.ConvertTimeFromUtc(DateTime.Now.ToUniversalTime(), TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));
            var next = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);

            next = next.Add(TimeSpan.Parse(marketStatus.ChangeAt));
            var nextOrders = new List <Order>();

            var list = new List <string> {
                "MSFT"
            };

            foreach (var symbolName in list)
            {
                var symbol = await _tradingClient.GetInstrumentFromSymbol(symbolName);

                if (next.AddMinutes(-15) > now)
                {
                    var result = await Ticker.Build()
                                 .SetSymbol(symbolName)
                                 .SetPeriod(Period.OneMonth)
                                 .SetInterval(Interval.OneDay)
                                 .GetAsync();

                    var avg          = result.Quotes.Average(item => item.Close);
                    var currentPrice = result.Quotes.OrderBy(s => s.Period).Last().Close;
                    var diff         = avg - currentPrice;

                    if (diff <= 0)
                    {
                        nextOrders = GetOpenPositions(symbol.InstrumentId.ToString());
                    }
                    else if (diff / currentPrice > 0.05)
                    {
                        var amountToAdd = diff / currentPrice * 200;

                        if (amountToAdd > availableCash)
                        {
                            amountToAdd = availableCash;
                        }

                        var qtyToBuy = amountToAdd / currentPrice;

                        await _tradingClient.Buy(new Order
                        {
                            Quantity        = (float)Math.Ceiling(qtyToBuy),
                            TransactionType = TransactionType.Buy,
                            Time            = DateTime.Now,
                            Symbol          = symbol.InstrumentId.ToString()
                        });
                    }
                }
                else
                {
                    nextOrders = GetOpenPositions(symbol.InstrumentId.ToString());
                }
                foreach (var nextOrder in nextOrders)
                {
                    if (nextOrder.TransactionType == TransactionType.Buy)
                    {
                        await _tradingClient.Buy(nextOrder);
                    }
                    else
                    {
                        await _tradingClient.Sell(nextOrder);
                    }

                    nextOrder.Time = DateTime.Now;
                }

                await _dbContext.Orders.AddRangeAsync(nextOrders, stoppingToken);

                await _dbContext.SaveChangesAsync(stoppingToken);
            }
        }