Beispiel #1
0
        /// <summary>
        /// Solves the level First_Steps.
        /// </summary>
        /// <returns>Returns if the level was solved or not.</returns>
        public bool Solve()
        {
            var wrapper    = new StockFighterAPI(this.apiKey);
            var gamemaster = new GamemasterAPI(this.apiKey);

            var targetNumberOfShares = 100;
            var totalBought          = 0;
            var solved = false;

            try
            {
                Console.Write("Starting level... ");
                var levelInfo = gamemaster.StartLevel(this.LevelName);
                Console.WriteLine(levelInfo.ok + "!");

                var account    = levelInfo.account;
                var instanceId = levelInfo.instanceId;
                var venues     = levelInfo.venues;

                Console.WriteLine(
                    "instanceId: " + instanceId
                    + "\naccount: " + account);


                foreach (var venue in venues)
                {
                    var stocks = wrapper.GetStocks(venue);

                    Console.WriteLine("Stocks for venue \"" + venue + "\":");

                    foreach (var stock in stocks.symbols)
                    {
                        Console.WriteLine("\t" + stock.symbol + " (" + stock.name + ")");

                        var quote = wrapper.GetQuote(venue, stock.symbol);

                        Console.WriteLine("\tQuote:");
                        Console.WriteLine("\t\tAsk: " + quote.ask);
                        Console.WriteLine("\t\tBid: " + quote.bid);
                        Console.WriteLine("\t\tTime: " + quote.quoteTime);
                        Console.WriteLine("\t\tLast trade at: " + quote.lastTrade);
                        Console.WriteLine("\n");

                        var orderbook = wrapper.GetOrderbook(venue, stock.symbol);

                        Console.WriteLine("\tBids: ");
                        foreach (var bid in orderbook.bids)
                        {
                            Console.WriteLine("\t\t" + bid.qty + " @ $" + bid.price);
                        }

                        Console.WriteLine("\n\tAsks: ");
                        foreach (var ask in orderbook.asks)
                        {
                            Console.WriteLine("\t\t" + ask.qty + " @ $" + ask.price);
                        }

                        Console.Write("\tAttempting to buy "
                                      + targetNumberOfShares
                                      + " shares of \"" + stock.symbol + "\"... ");

                        while (totalBought < targetNumberOfShares)
                        {
                            // Attempt to buy targetNumberOfShares shares
                            var orderRequest = new API.Requests.OrderRequest(
                                account,
                                venue,
                                stock.symbol,
                                0,
                                targetNumberOfShares - totalBought,
                                OrderDirection.Buy,
                                OrderType.Market);

                            var orderResponse = wrapper.PostOrder(orderRequest);
                            if (orderResponse != null && orderResponse.fills.Count > 0)
                            {
                                Console.WriteLine("order accepted!");

                                var orderCheck = wrapper.GetOrderStatus(
                                    orderResponse.id,
                                    orderResponse.venue,
                                    orderResponse.symbol);

                                var totalFilled = 0;

                                foreach (var fill in orderResponse.fills)
                                {
                                    Console.WriteLine("\tFills:");
                                    Console.WriteLine("\t\t" + fill.qty + " @ $" + fill.price);
                                    totalFilled += fill.qty;
                                }

                                Console.WriteLine("\tBought " + totalFilled + " shares!");
                                Console.WriteLine("\n\tOrder is open? \"" + orderCheck.open + "\".\n");

                                totalBought += totalFilled;

                                if (totalBought == targetNumberOfShares)
                                {
                                    solved = true;
                                }
                            }
                            else
                            {
                                Console.WriteLine("Couldn't buy!\n\t" + orderResponse.error);
                            }

                            Console.WriteLine("Progress so far: "
                                              + totalBought
                                              + "/"
                                              + targetNumberOfShares
                                              + " ("
                                              + (totalBought / targetNumberOfShares * 100)
                                              + "%)");
                        }
                    }
                }
            }

            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
            }

            return(solved);
        }
        /// <summary>
        /// Solves the level Chock_A_Block.
        /// </summary>
        /// <returns>Returns if the level was solved or not.</returns>
        public bool Solve()
        {
            var wrapper    = new StockFighterAPI(this.apiKey);
            var gamemaster = new GamemasterAPI(this.apiKey);

            var sharesToBuy = 100000;
            var solved      = false;

            try
            {
                Console.Write("Starting level... ");
                var levelInfo = gamemaster.StartLevel(this.LevelName);
                Console.WriteLine(levelInfo.ok + "!");

                var account    = levelInfo.account;
                var instanceId = levelInfo.instanceId;

                foreach (var venue in levelInfo.venues)
                {
                    Console.WriteLine("Venue \"" + venue + "\":");
                    var stocks = wrapper.GetStocks(venue);

                    foreach (var stock in stocks.symbols)
                    {
                        Console.WriteLine("Stock \"" + stock.name + "\" (" + stock.symbol + "):");
                        var buyingInterval = 50;

                        while (sharesToBuy > 0)
                        {
                            Console.WriteLine("Shares left to buy: " + sharesToBuy);

                            var quantity = Math.Min(sharesToBuy, buyingInterval);

                            var now = DateTime.Now;

                            Console.Write(
                                "Attempting to order "
                                + quantity
                                + " shares @ "
                                + DateTime.Now.ToString("HH:mm:ss.fff")
                                + "... ");

                            var order = new OrderRequest(
                                account,
                                venue,
                                stock.symbol,
                                0,
                                quantity,
                                OrderDirection.Buy,
                                OrderType.Market);

                            var response = wrapper.PostOrder(order);

                            var totalFilled = 0;
                            foreach (var fill in response.fills)
                            {
                                totalFilled += fill.qty;
                            }

                            Console.WriteLine(totalFilled + " filled!");

                            sharesToBuy -= totalFilled;
                        }

                        Console.WriteLine("All shares bought!");
                        solved = true;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }

            return(solved);
        }