Exemplo n.º 1
0
        public async Task StartAsync()
        {
            // register new sandbox account
            var sandboxAccount = await _context.RegisterAsync(BrokerAccountType.Tinkoff);

            _accountId = sandboxAccount.BrokerAccountId;

            // set balance
            foreach (var currency in new[] { Currency.Rub, Currency.Usd, Currency.Eur })
            {
                await _context.SetCurrencyBalanceAsync(currency, Random.Next(1, 10) * 100_000,
                                                       sandboxAccount.BrokerAccountId);
            }

            await CheckBalanceAsync();

            // select random instrument
            var instrumentList = await _context.MarketStocksAsync();

            var randomInstrumentIndex = Random.Next(instrumentList.Total);
            var randomInstrument      = instrumentList.Instruments[randomInstrumentIndex];

            Console.WriteLine($"Selected Instrument:\n{randomInstrument.ToString().Replace(", ", "\n")}");
            Console.WriteLine();

            // get candles
            var now        = DateTime.Now;
            var candleList = await _context.MarketCandlesAsync(randomInstrument.Figi, now.AddMinutes(-5), now, CandleInterval.Minute);

            foreach (var candle in candleList.Candles)
            {
                Console.WriteLine(candle);
            }
            Console.WriteLine();

            Console.WriteLine("Buy 1 lot");
            await _context.PlaceMarketOrderAsync(new MarketOrder(randomInstrument.Figi, 1, OperationType.Buy,
                                                                 _accountId));

            await CheckBalanceAsync();

            await Task.Delay(1000);

            Console.WriteLine("Sell 1 lot");
            await _context.PlaceMarketOrderAsync(new MarketOrder(randomInstrument.Figi, 1, OperationType.Sell,
                                                                 _accountId));

            await CheckBalanceAsync();
        }
        public async Task PlaceMarketOrderTest()
        {
            _handler.Expect(HttpMethod.Post, $"{BaseUri}orders/market-order")
            .WithQueryString(new Dictionary <string, string>
            {
                ["figi"]            = Figi,
                ["brokerAccountId"] = BrokerAccountId
            })
            .WithJsonContentFromFile("market-order-request")
            .RespondJsonFromFile("market-order-response");

            var response =
                await _context.PlaceMarketOrderAsync(new MarketOrder(Figi, 10, OperationType.Buy,
                                                                     BrokerAccountId));

            var expected = new PlacedMarketOrder(
                "40fcdfa2-084b-41bc-8f9d-3414a5eb9e8c",
                OperationType.Buy,
                OrderStatus.Fill,
                "That's Why",
                10,
                5,
                new MoneyAmount(Currency.Usd, 1.44m));

            response.Should().BeEquivalentTo(expected);
        }
Exemplo n.º 3
0
 public async Task MakeOrder(string figi, int lots, OperationType operation, decimal?price = null)
 {
     if (price == null)
     {
         await _sandbox.PlaceMarketOrderAsync(new MarketOrder(figi, lots, operation, _accountId));
     }
     else
     {
         await _sandbox.PlaceLimitOrderAsync(new LimitOrder(figi, lots, operation, price.Value, _accountId));
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Начальная инициализация портфеля
        /// </summary>
        /// <returns></returns>
        private async Task InitialBalance()
        {
            //инициализируем начальный баланс случайными значениями
            foreach (var currency in new[] { Currency.Rub, Currency.Usd, Currency.Eur })
            {
                await _context.SetCurrencyBalanceAsync(currency, _random.Next(3, 10) * 1_000_000, _accountId);
            }

            //покупаем акции по FIGI, которые в настройках
            foreach (string tradeFigi in _tradingBaseStrategy.SettingProvider.TradeFigis)
            {
                await _context.PlaceMarketOrderAsync(new MarketOrder(tradeFigi, 1, OperationType.Buy, _accountId));
            }
        }