Ejemplo n.º 1
0
 /// <summary>Uses Ruffini's rule</summary>
 public PolynomialEq EliminateRootNoRemainder(double root)
 {
     List<double> tempList = new List<double>(coefficients.Count);
     List<double> newCoef = new List<double>(coefficients.Count); //last guy is the remainder
     newCoef.Add(coefficients[0]);
     for (int i = 1; i < coefficients.Count - 1; i++) {
         tempList.Add( newCoef.Last()* root);
         newCoef.Add(coefficients[i] + tempList.Last());
     }
     return new PolynomialEq(newCoef.ToArray());
 }
 public static Token TestForTokenInference(List<Token> tokens, Token tokenToAdd)
 {
     if (tokens.Count() > 0) {
         //When a minus or plus sign is read as a negative number, add a plus sign before the number
         //IE: 4 -5 becomes 4 + -5
         TokenizerRules.CharInfo lastTknInfo = TokenizerRules.CharacterInfo.GetCharInfo(tokens.Last().TokenString);
         TokenizerRules.CharInfo currentTknInfo = TokenizerRules.CharacterInfo.GetCharInfo(tokenToAdd.TokenString);
         TokenizerRules.CharInfo lastCharInfo = TokenizerRules.CharacterInfo.GetCharInfo(tokenToAdd.TokenString[0].ToString());
         if ((tokenToAdd.TokenType == TokenType.numberLiteral)
             && (tokens.Last().TokenType == TokenType.numberLiteral
                     || (lastTknInfo != null && lastTknInfo.Type == TokenizerRules.CharType.closedBrace))
                     || ((Keywords.KeywordLookup.ContainsKey(tokens.Last().TokenString.ToLower()) && Keywords.KeywordLookup[tokens.Last().TokenString.ToLower()].TokenType == TokenType.numberLiteral)
             && (lastCharInfo != null && lastCharInfo.Type == TokenizerRules.CharType.plusMinus))) {
             return new Token("+", TokenType.operatorOrPunctuation);
         }
         //Infer a multiplication sign between two sets of parenthesis
         //IE: (3)(5) becomes (3)*(5)
         if ((TokenizerRules.CharacterInfo.ContainsKey(tokenToAdd.TokenString) && TokenizerRules.CharacterInfo.ContainsKey(tokens.Last().TokenString))
             && TokenizerRules.CharacterInfo[tokenToAdd.TokenString].Type == TokenizerRules.CharType.openBrace
             && TokenizerRules.CharacterInfo[tokens.Last().TokenString].Type == TokenizerRules.CharType.closedBrace) {
                 return new Token("*", TokenType.operatorOrPunctuation);
         }
         //Infer a multiplication sign between parenthesis and a number (that doesn't start with a minus sign)
         //IE: 3(4) becomes 3*(4)
         if (TokenizerRules.CharacterInfo.ContainsKey(tokenToAdd.TokenString)
             && TokenizerRules.CharacterInfo[tokenToAdd.TokenString].Type == TokenizerRules.CharType.openBrace
             && tokens.Last().TokenType == TokenType.numberLiteral) {
             return new Token("*", TokenType.operatorOrPunctuation);
         }
         //IE: (4)3 becomes (4)*3
         if (TokenizerRules.CharacterInfo.ContainsKey(tokens.Last().TokenString)
             && tokenToAdd.TokenType == TokenType.numberLiteral
             && TokenizerRules.CharacterInfo[tokens.Last().TokenString].Type == TokenizerRules.CharType.closedBrace
             && tokenToAdd.TokenString[0] != '-') {
             return new Token("*", TokenType.operatorOrPunctuation);
         }
     }
     return null;
 }
        private IEnumerable<RouteElement> GetRandomRoute( IRoadElement startElement )
        {
            var result = new List<RouteElement>
                             {
                                 new RouteElement
                                     {
                                         CanStopOnIt = true,
                                         PriorityType = PriorityType.None,
                                         RoadElement = startElement
                                     }
                             };

            result.AddRange( startElement.Routes.GetRandomRoute( this._rng ) );

            while ( true )
            {
                var nextRoute = result.Last().RoadElement.Routes.GetRandomRoute( this._rng ).ToArray();
                if ( nextRoute.Length == 0 ) { break; }

                result.AddRange( nextRoute );
            }
            return result;
        }
Ejemplo n.º 4
0
        public FilesUploadResult LoadPostPictures(int id)
        {
            List<string> result = new List<string>();
            UserDTO user = _userSvc.Get(id);
            string rootPath = HttpContext.Current.Request.MapPath("~/Temp/");
            string userDirectoryPath = Path.Combine(rootPath, user.email);
            if (!Directory.Exists(userDirectoryPath))
            {
                string standartImageDirectoryPath = Path.Combine(userDirectoryPath, "Standart");
                string mediumImageDirectoryPath = Path.Combine(userDirectoryPath, "Medium");
                string smallImageDirectoryPath = Path.Combine(userDirectoryPath, "Small");
                Directory.CreateDirectory(userDirectoryPath);
                Directory.CreateDirectory(standartImageDirectoryPath);
                Directory.CreateDirectory(mediumImageDirectoryPath);
                Directory.CreateDirectory(smallImageDirectoryPath);
            }

            var httpRequest = HttpContext.Current.Request;

            if (httpRequest.Files.Count > 0)
            {
                FilesUploadResult res = new FilesUploadResult { };
                foreach (string file in httpRequest.Files)
                {

                    var postedFile = httpRequest.Files[file];
                    result.Add(Convert.ToString(Guid.NewGuid() + Path.GetExtension(postedFile.FileName)));
                    var standartImagePath = HttpContext.Current.Server.MapPath("~/temp/" + user.email + "/Standart/" + result.Last());
                    var mediumImagePath = HttpContext.Current.Server.MapPath("~/temp/" + user.email + "/Medium/" + result.Last());
                    var smallImagePath = HttpContext.Current.Server.MapPath("~/temp/" + user.email + "/Small/" + result.Last());
                    postedFile.SaveAs(standartImagePath);
                    PictureProvider.SaveMiniatureImage(standartImagePath, mediumImagePath, 200);
                    PictureProvider.SaveMiniatureImage(standartImagePath, smallImagePath, 100);
                    res.Files = new List<FileUploadResult>
                        {
                            new FileUploadResult {
                                Name = postedFile.FileName,
                                Size = postedFile.ContentLength,
                                Url = "/temp/" + user.email + "/Standart/" + result.Last(),
                                DeleteUrl = "http://test",
                                DeleteType = "DELETE",
                                ThumbnailUrl = "/temp/" + user.email + "/Medium/" + result.Last(),
                                NewName = result.Last()
                            }
                        };
                }
                return res;
            }
            else
            {
                throw new Exception();
            }
        }
Ejemplo n.º 5
0
        private double suggestSellPrice(List<Order> asks)
        {
            if (null == asks || !asks.Any())
            {
                return _sellOrderPrice > 0.0 ? _sellOrderPrice : _minSellPrice;
            }

            const int DEC_PLACES = 14;
            double increment = 2.0 * _minPriceUpdate;

            //Find first ASK price higher than minPrice
            foreach (Order ask in asks)
            {
                //Don't consider own order
                if (ask.SequenceNumber == _sellOrderId)   //TODO: this is a pure guess. In case orderbook bid/ask cannot be matched with my order, use old method (price and amount equality)
                {
                    continue;
                }

                if (ask.Price >= _minSellPrice)
                {
                    double sellPrice = Math.Round(ask.Price - increment, DEC_PLACES);

                    //The difference is too small. Leave previous price to avoid server call
                    if (-1 != _sellOrderId && Math.Abs(sellPrice - _sellOrderPrice) < _minPriceUpdate)
                    {
                        log(String.Format("DEBUG: SELL price {0:0.00000} too similar, using previous", sellPrice));
                        return _sellOrderPrice;
                    }

                    if (sellPrice > _maxSellPrice)
                    {
                        return _maxSellPrice;
                    }
                    return sellPrice;
                }
            }

            //Order book filled with junk. Use order before last, so we see it in chart
            double price = asks.Last().Price - increment;
            if (-1 != _sellOrderId && Math.Abs(price - _sellOrderPrice) < _minPriceUpdate)
            {
                return _sellOrderPrice;
            }
            return Math.Round(price, DEC_PLACES);
        }
Ejemplo n.º 6
0
        private double suggestBuyPrice(List<Order> bids)
        {
            if (null == bids || !bids.Any())
            {
                return _buyOrderPrice > 0.0 ? _buyOrderPrice : _maxBuyPrice;
            }

            const int DEC_PLACES = 14;
            double increment = 2.0 * _minPriceUpdate;

            //Find first BID price lower than max. price
            foreach (Order bid in bids)
            {
                //Don't consider own order
                if (bid.SequenceNumber == _sellOrderId)   //TODO: this is a pure guess. In case orderbook bid/ask cannot be matched with my order, use old method (price and amount equality)
                {
                    continue;
                }

                if (bid.Price <= _maxBuyPrice)
                {
                    double buyPrice = Math.Round(bid.Price + increment, DEC_PLACES);

                    //The difference is too small. Leave previous price to avoid server call
                    if (-1 != _buyOrderId && Math.Abs(buyPrice - _buyOrderPrice) < _minPriceUpdate)
                    {
                        log(String.Format("DEBUG: BUY price {0:0.00000} too similar, using previous", buyPrice));
                        return _buyOrderPrice;
                    }

                    if (buyPrice > _maxBuyPrice)
                    {
                        return _maxBuyPrice;
                    }
                    return buyPrice;
                }
            }

            //Order book filled with junk. Use order before last, so we see it in chart
            double price = bids.Last().Price + increment;
            if (-1 != _buyOrderId && Math.Abs(price - _buyOrderPrice) < _minPriceUpdate)
            {
                return _buyOrderPrice;
            }
            return Math.Round(price, DEC_PLACES);
        }
Ejemplo n.º 7
0
        /// <summary>There's a dump on market, best time to SELL.</summary>
        private static bool runningSelloff(List<TradeResponse> tradeHistory)
        {
            //There's lot of activity in recent time and price is falling
            const double PRICE_DIFF = 3 * 0.001;  //0.3%

            var endTime = tradeHistory.Last().TimeTyped;
            var startTime = endTime.AddSeconds(-90);    //Trades of past 90 seconds
            var pastTrades = tradeHistory.Where(trade => trade.TimeTyped >= startTime).ToList();
            if (pastTrades.Count < 10)        //TODO: tune up
                return false;

            var startPrice = pastTrades.First().price;
            var endPrice = pastTrades.Last().price;
            return (startPrice > endPrice && startPrice - endPrice >= startPrice * PRICE_DIFF);
        }
Ejemplo n.º 8
0
        private double suggestPrice(List<FiatAsk> asks, double minPrice, double maxPrice, int currentOrderId, double currentOrderPrice, double minPriceUpdate)
        {
            if (null == asks || !asks.Any())
            {
                return currentOrderPrice > 0.0 ? currentOrderPrice : minPrice;
            }

            const int DEC_PLACES = 14;
            double increment = 2.0 * minPriceUpdate;

            //Find first ASK price higher than minPrice
            foreach (FiatAsk ask in asks)
            {
                //Don't consider own order
                if (_config.AccessKey == ask.Account && ask.Sequence == currentOrderId)
                {
                    continue;
                }

                if (ask.Price >= minPrice)
                {
                    double sellPrice = Math.Round(ask.Price - increment, DEC_PLACES);

                    //The difference is too small. Leave previous price to avoid server call
                    if (-1 != currentOrderId && Math.Abs(sellPrice - currentOrderPrice) < minPriceUpdate)
                    {
                        log(String.Format("DEBUG: price {0:0.00000} too similar, using previous", sellPrice));
                        return currentOrderPrice;
                    }

                    if (sellPrice > maxPrice)
                    {
                        return maxPrice;
                    }
                    return sellPrice;
                }
            }

            //Order book filled with junk. Use order before last, so we see it in chart
            double price = asks.Last().Price - increment;
            if (-1 != currentOrderId && Math.Abs(price - currentOrderPrice) < minPriceUpdate)
            {
                return currentOrderPrice;
            }
            if (price < minPrice)
            {
                return minPrice;
            }
            return Math.Round(price, DEC_PLACES);
        }
Ejemplo n.º 9
0
        private double suggestSellPrice(List<MarketOrder> asks)
        {
            if (null == asks || !asks.Any())
            {
                return _sellOrderPrice > 0.0 ? _sellOrderPrice : _sellMinPrice;
            }

            const int DEC_PLACES = 14;
            double increment = 2.0 * _minPriceUpdate;

            //Find first ASK price higher than minPrice
            foreach (MarketOrder ask in asks)
            {
                //Don't consider own order
                if (ask.Amount.eq(_sellOrderAmount) && ask.Price.eq(_sellOrderPrice))
                {
                    continue;
                }

                if (ask.Price >= _sellMinPrice)
                {
                    double sellPrice = Math.Round(ask.Price - increment, DEC_PLACES);

                    //The difference is too small. Leave previous price to avoid server call
                    if (-1 != _sellOrderId && Math.Abs(sellPrice - _sellOrderPrice) < _minPriceUpdate)
                    {
                        log($"DEBUG: price {sellPrice:0.#####} too similar, using previous");
                        return _sellOrderPrice;
                    }

                    if (sellPrice > _sellMaxPrice)
                    {
                        return _sellMaxPrice;
                    }
                    return sellPrice;
                }
            }

            //Order book filled with junk. Use order before last, so we see it in order book
            double price = asks.Last().Price - increment;
            if (-1 != _sellOrderId && Math.Abs(price - _sellOrderPrice) < _minPriceUpdate)
            {
                return _sellOrderPrice;
            }
            if (price < _sellMinPrice)
            {
                return _sellMinPrice;
            }
            return Math.Round(price, DEC_PLACES);
        }
Ejemplo n.º 10
0
        private double suggestBuyPrice(List<MarketOrder> bids)
        {
            if (null == bids || !bids.Any())
            {
                return _buyOrderPrice > 0.0 ? _buyOrderPrice : _buyMaxPrice;
            }

            const int DEC_PLACES = 14;
            double increment = 2.0 * _minPriceUpdate;

            //Find first BID price lower than maxPrice
            foreach (MarketOrder bid in bids)
            {
                //Don't consider own order
                if (bid.Amount.eq(_buyOrderAmount) && bid.Price.eq(_buyOrderPrice))
                {
                    continue;
                }

                if (bid.Price <= _buyMaxPrice)
                {
                    double buyPrice = Math.Round(bid.Price + increment, DEC_PLACES);

                    //The difference is too small. Leave previous buy price to avoid server call
                    if (-1 != _buyOrderId && Math.Abs(buyPrice - _buyOrderPrice) < _minPriceUpdate)
                    {
                        log($"DEBUG: price {buyPrice:0.#####} too similar, using previous");
                        return _buyOrderPrice;
                    }

                    if (buyPrice < _buyMinPrice)
                    {
                        return _buyMinPrice;
                    }
                    return buyPrice;
                }
            }

            //Order book filled with junk. Use order before last, so we see it in order book
            double price = bids.Last().Price + increment;
            if (-1 != _buyOrderId && Math.Abs(price - _buyOrderPrice) < _minPriceUpdate)
            {
                return _buyOrderPrice;
            }
            if (price > _buyMaxPrice)
            {
                return _buyMaxPrice;
            }
            return Math.Round(price, DEC_PLACES);
        }
Ejemplo n.º 11
0
        private bool isRising(List<Candle> candles)
        {
            const int MIN_CANDLES = 3;

            if (candles.Count < MIN_CANDLES)
                return false;
            candles = candles.TakeLast(MIN_CANDLES).ToList();

            //Present candle is significant rise
            if (candles.Last().ClosingPrice > candles.Last().OpeningPrice + 3.0*PRICE_SIGNIFICANCE_LIMIT)
                return true;

            //Previous 2 candles were rises and latest price was rise too //TODO: maybe something more sofisticated
            return candles[0].ClosingPrice > candles[0].OpeningPrice + PRICE_SIGNIFICANCE_LIMIT &&
                   candles[1].ClosingPrice > candles[1].OpeningPrice + PRICE_SIGNIFICANCE_LIMIT &&
                   candles[1].ClosingPrice < candles.Last().ClosingPrice;   //Most recent price decides
        }