Esempio n. 1
0
        /// <summary>
        /// Weighted Moving Average
        /// </summary>
        /// <returns>The wma.</returns>
        /// <param name="candleSticks">Candle sticks.</param>
        /// <param name="weight">Weight.</param>
        /// <param name="priceSource">Price source.</param>
        public static MovingAverage WMA(CandleStickCollection candleSticks, int weight = 14,
                                        PriceSource priceSource = PriceSource.Close)
        {
            int count = candleSticks.Count;

            double[] priceArray = priceSource.GetArrayFromCandleStickCollection(candleSticks);
            double[] wma        = new double[count];
            double[] weights    = new double[weight];
            double   sum        = weight * (weight + 1) / 2;

            for (int i = 0; i < weight; i++)
            {
                weights[i] = i / sum;
            }

            for (int i = weight - 1; i < count; i++)
            {
                for (int j = 0; j < weight; j++)
                {
                    wma[i] = wma[i] + priceArray[j + i] * weight;
                }
            }

            return(new MovingAverage(wma, MAType.WMA));
        }
Esempio n. 2
0
        public static BollingerBands BollingerBands(CandleStickCollection candleSticks, int period = 14,
                                                    double multiplier = 2, PriceSource priceSource = PriceSource.Close)
        {
            BollingerBands bollingerBands = new BollingerBands(candleSticks, period, multiplier, priceSource);

            return(bollingerBands);
        }
Esempio n. 3
0
        /// <summary>
        /// Relatives the index of the strength.
        /// </summary>
        /// <returns>The strength index.</returns>
        /// <param name="candleSticks">Candle sticks.</param>
        /// <param name="period">Period.</param>
        public static RelativeStrengthIndex RelativeStrengthIndex(CandleStickCollection candleSticks, int period = 14,
                                                                  PriceSource priceSource = PriceSource.Close)
        {
            int count = candleSticks.Count;

            double[] priceArray = priceSource.GetArrayFromCandleStickCollection(candleSticks);
            double[] rsi        = new double[count];

            double[] up   = new double[count];
            double[] down = new double[count];
            double[] rs   = new double[count];

            for (int i = 1; i < count; i++)
            {
                if (priceArray[i] > priceArray[i - 1])
                {
                    up[i] = priceArray[i] - priceArray[i - 1];
                }
                else
                {
                    down[i] = priceArray[i - 1] - priceArray[i];
                }
            }

            for (int i = period; i < count; i++)
            {
                rs[i]  = MovingAverages.SMMA(up, period, i - period, period).MA[i] / MovingAverages.SMMA(down, period, i - period, period).MA[i];
                rsi[i] = 100 - 1 / (1 + rs[i]);
            }

            return(new RelativeStrengthIndex(rsi));
        }
Esempio n. 4
0
 public void SetUp()
 {
     source = new PriceSource {
         RequestInterval     = 180,
         LastSuccessfulCheck = new DateTime(2011, 1, 14, 17, 55, 30)
     };
 }
Esempio n. 5
0
        /// <summary>
        /// Smoothed Moving Average/Running Moving Average
        /// </summary>
        /// <returns>The smma.</returns>
        /// <param name="candleSticks">Candle sticks.</param>
        /// <param name="period">Period.</param>
        /// <param name="priceSource">Price source.</param>
        public static MovingAverage SMMA(CandleStickCollection candleSticks, int period = 14,
                                         PriceSource priceSource = PriceSource.Close)
        {
            int count = candleSticks.Count;

            double[] smma       = new double[count];
            double[] priceArray = priceSource.GetArrayFromCandleStickCollection(candleSticks);

            double sum = priceArray[0];

            //Iterate for first sum over period n
            for (int i = 0; i < period - 1; i++)
            {
                sum = sum + priceArray[i];
            }

            // First n values are zero
            smma[period - 1] = sum / period;

            for (int i = period; i < count; i++)
            {
                // No need to iterate through every sum
                sum = sum - priceArray[i - period] + priceArray[i];

                smma[i] = (sum - smma[0] + priceArray[i]) / (double)period;
            }

            return(new MovingAverage(smma, MAType.SMMA));
        }
Esempio n. 6
0
        /// <summary>
        /// Simple Moving Average
        /// </summary>
        /// <returns>The SMA.</returns>
        /// <param name="candleSticks">Candle stick data</param>
        /// <param name="period">Time period (size of candlestick time inerval * period)</param>
        /// <param name="priceSource">Price Source (enum: open, high, low, close)</param>
        public static MovingAverage SMA(CandleStickCollection candleSticks, int period = 14,
                                        PriceSource priceSource = PriceSource.Close)
        {
            int count = candleSticks.Count;

            double[] priceArray = priceSource.GetArrayFromCandleStickCollection(candleSticks);
            double[] sma        = new double[count];
            double   sum        = priceArray[0];

            // Compute the first sum
            for (int i = 1; i < period - 1; i++)
            {
                sum = sum + priceArray[i];
            }

            sma[period - 1] = sum / period;

            for (int j = period; j < count - 1; j++)
            {
                // No more iterating required for the other sums
                sum    = sum - priceArray[j - period] + priceArray[j];
                sma[j] = sum / period;
            }

            return(new MovingAverage(sma, MAType.SMA));
        }
 public override DataRow[] GetLikeSources(PriceSource source)
 {
     return(dtSources.Select(String.Format("({0} = '{1}') and ({2} = '{3}') and (ISNULL({4}, '') = '{5}') and (ISNULL({6}, '') = '{7}') and (ISNULL({8}, '') = '{9}')",
                                           SourcesTableColumns.colPricePath, source.PricePath,
                                           SourcesTableColumns.colPriceMask, source.PriceMask,
                                           SourcesTableColumns.colFTPDir, source.FtpDir,
                                           SourcesTableColumns.colFTPLogin, source.FtpLogin,
                                           SourcesTableColumns.colFTPPassword, source.FtpPassword)));
 }
Esempio n. 8
0
        public static MovingAverageConvergenceDivergence MACD(CandleStickCollection candleSticks, int fast = 12, int slow = 26, int length = 9,
                                                              PriceSource priceSource = PriceSource.Close)
        {
            MovingAverage fastMA = MovingAverages.EMA(candleSticks, fast, priceSource);
            MovingAverage slowMA = MovingAverages.EMA(candleSticks, slow, priceSource);

            double[] macd = SubtractArray(fastMA.MA, slowMA.MA);
            return(new MovingAverageConvergenceDivergence(macd, length));
        }
        public PriceServiceHostFactory()
        {
            var priceSource = new PriceSource();
            Log.Information("Started Generator");

            _service = new PricingService(priceSource);
            Log.Information("Started Service");

            _cleanup.Add(priceSource);
        }
Esempio n. 10
0
        public void TestCheckDownloadInterval_IfFailed()
        {
            var source = new PriceSource {
                PriceDateTime   = DateTime.Now,
                RequestInterval = 10,
                PriceItemId     = 1,
            };

            handler.FailedSources.Add(source.PriceItemId);
            Assert.IsTrue(handler.IsReadyForDownload(source));
        }
Esempio n. 11
0
        public static IPriceImporter Create(PriceSource pricesource)
        {
            switch (pricesource)
            {
            case PriceSource.Scryfall:
                return(new ScryfallPriceImporter());

            default:
                throw new PriceImporterException("Unknown PriceSource type:" + pricesource);
            }
        }
Esempio n. 12
0
        void IAuctionEventListener.CurrentPrice(int inPrice, int inIncrement, PriceSource inBidderSource)
        {
            switch (inBidderSource)
            {
            case PriceSource.FromSniper:
                mListener.SniperWinning();
                break;

            case PriceSource.FromOtherBidder:
                mAuction.Bid(inPrice + inIncrement);
                mListener.SniperBidding();
                break;
            }
        }
 protected override void GetFileFromSource(PriceSource source)
 {
     try {
         var file = FtpDownloader.GetFileFromSource(source, DownHandlerPath);
         if (file != null)
         {
             CurrFileName  = file.FileName;
             CurrPriceDate = file.FileDate;
         }
     }
     catch (Exception e) {
         throw new FtpSourceHandlerException(e);
     }
 }
        private async Task <JArray> GetJson()
        {
            var results = new List <JObject>();

            foreach (var baseCcy in PriceSource.GetAllBaseCurrencies())
            {
                //We do multiple requests, one for each base ccy
                var symbols         = string.Join(',', PriceSource.GetAllQuoteCurrencies(baseCcy));
                var queryParameters = $"?base={baseCcy}&symbols={symbols}";
                var json            = await _adapter.GetRequestJson(queryParameters);

                results.Add(json);
            }
            return(new JArray(results));
        }
 public void CurrentPrice(int price, int increment, PriceSource priceSource)
 {
     switch(priceSource)
     {
         case PriceSource.FromSniper:
             _snapshot = _snapshot.Winning(price);
             break;
         case PriceSource.FromOtherBidder:
             int bid = price + increment;
             _auction.Bid(bid);
             _snapshot = _snapshot.Bidding(price, bid);
             break;
     }
     NotifyChange();
 }
        public void CurrentPrice(int price, int increment, PriceSource priceSource)
        {
            switch (priceSource)
            {
            case PriceSource.FromSniper:
                _snapshot = _snapshot.Winning(price);
                break;

            case PriceSource.FromOtherBidder:
                int bid = price + increment;
                _auction.Bid(bid);
                _snapshot = _snapshot.Bidding(price, bid);
                break;
            }
            NotifyChange();
        }
Esempio n. 17
0
        /// <summary>
        /// Cumulative Moving Average
        /// </summary>
        /// <returns>The CMA.</returns>
        /// <param name="candleSticks">Candle sticks.</param>
        /// <param name="priceSource">Price source.</param>
        public static MovingAverage CMA(CandleStickCollection candleSticks,
                                        PriceSource priceSource = PriceSource.Close)
        {
            int count = candleSticks.Count;

            double[] cma        = new double[count];
            double[] priceArray = priceSource.GetArrayFromCandleStickCollection(candleSticks);
            cma[0] = priceArray[0];

            for (int i = 1; i < count; i++)
            {
                cma[i] = cma[i - 1] + (priceArray[i] - cma[i - 1]) / (double)i;
            }

            return(new MovingAverage(cma, MAType.CMA));
        }
Esempio n. 18
0
        public void TestCheckDownloadInterval()
        {
            var source = new PriceSource {
                PriceDateTime   = DateTime.Now,
                RequestInterval = 10
            };

            source.UpdateLastCheck();
            Assert.IsFalse(handler.IsReadyForDownload(source));
            source.RequestInterval = 0;
            Assert.IsTrue(handler.IsReadyForDownload(source));
            Thread.Sleep(5000);
            source.PriceDateTime   = DateTime.Now.Subtract(new TimeSpan(0, 1, 0));
            source.RequestInterval = 4;
            Assert.IsTrue(handler.IsReadyForDownload(source));
        }
Esempio n. 19
0
        public BollingerBands(CandleStickCollection candleSticks, int period = 20,
                              double multiplier = 2, PriceSource priceSource = PriceSource.Close)
        {
            int length = candleSticks.Count - period;

            double[]          prices = priceSource.GetArrayFromCandleStickCollection(candleSticks);
            StandardDeviation standardDeviation;

            LowerBand  = UpperBand = new double[length];
            MiddleBand = MovingAverages.SMA(candleSticks, period, priceSource).MA;

            for (int i = 0; i < length; i++)
            {
                standardDeviation = new StandardDeviation(prices.GetSegment(i, i + period), MiddleBand.GetSegment(i, i + period));
                UpperBand[i]      = MiddleBand[i] + multiplier * standardDeviation.Value;
                LowerBand[i]      = MiddleBand[i] - multiplier * standardDeviation.Value;
            }
        }
Esempio n. 20
0
        public static double[] GetArrayFromCandleStickCollection(this PriceSource p,
                                                                 CandleStickCollection c)
        {
            int count = c.Count;

            double[] priceArray = new double[count];

            switch (p)
            {
            case PriceSource.Open:
                for (int i = 0; i < count - 1; i++)
                {
                    priceArray[i] = c[i].Open;
                }
                return(priceArray);

            case PriceSource.High:
                for (int i = 0; i < count - 1; i++)
                {
                    priceArray[i] = c[i].High;
                }
                return(priceArray);

            case PriceSource.Low:
                for (int i = 0; i < count - 1; i++)
                {
                    priceArray[i] = c[i].Low;
                }
                return(priceArray);

            case PriceSource.Close:
                for (int i = 0; i < count - 1; i++)
                {
                    priceArray[i] = c[i].Close;
                }
                return(priceArray);

            default:
                Exception exception = new Exception("Invalid price source value");
                throw (exception);
            }
        }
        public static List <PriceSourceItem> GetPrice(Gpu gpu, PriceSource priceSource)
        {
            Guard.AgainstFalse <ArgumentNullException>(!string.IsNullOrWhiteSpace(gpu.Asin));

            List <PriceSourceItem> priceSourceItems = new List <PriceSourceItem>();
            AmazonEndpoint?        amazonEndpoint   = null;

            if (priceSource != null)
            {
                WriteLine($"Getting Price From {priceSource?.Name ?? "NULL"} For GPU {gpu.Name}");
                amazonEndpoint = PriceSourceTypeHelper.ToAmazonEndpoint(priceSource.PriceSourceType);
            }

            List <PriceSourceItem> result = Amazon.AmazonService.SearchItemLookupOperation(gpu.Asin, amazonEndpoint);

            result.ForEach(p => p.PriceSourceId = priceSource.Id);
            priceSourceItems.AddRange(result);

            return(priceSourceItems);
        }
Esempio n. 22
0
        /// <summary>
        /// Exponential Moving Average
        /// </summary>
        /// <returns>The EMA.</returns>
        /// <param name="candleSticks">Candle sticks.</param>
        /// <param name="period">Period.</param>
        /// <param name="priceSource">Price source.</param>
        public static MovingAverage EMA(CandleStickCollection candleSticks, int period = 14,
                                        PriceSource priceSource = PriceSource.Close)
        {
            int count = candleSticks.Count;

            double[] ema = new double[count];

            double w = 1 / period;

            // Set intial value to first price value
            ema[0] = priceSource.GetValueFromCandleStick(candleSticks[0]);

            for (int i = 1; i < count; i++)
            {
                ema[i] = w * priceSource.GetValueFromCandleStick(candleSticks[i])
                         + (1.0 - w) * ema[i - 1];
            }

            return(new MovingAverage(ema, MAType.EMA));
        }
Esempio n. 23
0
        /// <summary>
        /// Gets the selected price from a candle stick.
        /// </summary>
        /// <returns>The value from candle stick.</returns>
        /// <param name="p">P.</param>
        /// <param name="c">C.</param>
        public static double GetValueFromCandleStick(this PriceSource p, CandleStick c)
        {
            switch (p)
            {
            case PriceSource.Open:
                return(c.Open);

            case PriceSource.High:
                return(c.High);

            case PriceSource.Low:
                return(c.Low);

            case PriceSource.Close:
                return(c.Close);

            default:
                Exception exception = new Exception("Invalid price source value");
                throw(exception);
            }
        }
Esempio n. 24
0
        public void CurrentPrice(int price, int increment, PriceSource priceSource)
        {
            switch (priceSource)
            {
            case PriceSource.FromSniper:
                snapShot = snapShot.Winning(price);
                break;

            case PriceSource.FromOtherBidder:
                int bid = price + increment;
                if (item.AllowsBid(bid))
                {
                    auction.Bid(bid);
                    snapShot = snapShot.Bidding(price, bid);
                }
                else
                {
                    snapShot = snapShot.Losing(price);
                }
                break;
            }

            notifyChange();
        }
        public static DownloadedFile GetFileFromSource(PriceSource source, string downHandlerPath)
        {
            var ftpHost = source.PricePath;

            if (ftpHost.StartsWith(@"ftp://", StringComparison.OrdinalIgnoreCase))
            {
                ftpHost = ftpHost.Substring(6);
            }
            if (ftpHost.EndsWith(@"/"))
            {
                ftpHost = ftpHost.Substring(0, ftpHost.Length - 1);
            }

            var pricePath = source.FtpDir;

            if (!pricePath.StartsWith(@"/", StringComparison.OrdinalIgnoreCase))
            {
                pricePath = @"/" + pricePath;
            }

            var ftpFileName   = String.Empty;
            var downFileName  = String.Empty;
            var shortFileName = String.Empty;
            var priceDateTime = source.PriceDateTime;

            using (var ftpClient = new FTP_Client()) {
                ftpClient.PassiveMode = true;
                ftpClient.Connect(ftpHost, 21);
                ftpClient.Authenticate(source.FtpLogin, source.FtpPassword);
                ftpClient.SetCurrentDir(pricePath);

                var dsEntries = ftpClient.GetList();

                foreach (DataRow entry in dsEntries.Tables["DirInfo"].Rows)
                {
                    if (Convert.ToBoolean(entry["IsDirectory"]))
                    {
                        continue;
                    }

                    shortFileName = entry["Name"].ToString();

                    var priceMaskIsMatched = PriceProcessor.FileHelper.CheckMask(shortFileName, source.PriceMask);
                    if (priceMaskIsMatched)
                    {
                        var fileLastWriteTime = Convert.ToDateTime(entry["Date"]);
#if DEBUG
                        priceDateTime = fileLastWriteTime;
                        ftpFileName   = shortFileName;
#endif
                        if (((fileLastWriteTime.CompareTo(priceDateTime) > 0) &&
                             (DateTime.Now.Subtract(fileLastWriteTime).TotalMinutes > Settings.Default.FileDownloadInterval)) ||
                            ((fileLastWriteTime.CompareTo(DateTime.Now) > 0) && (fileLastWriteTime.Subtract(priceDateTime).TotalMinutes > 0)))
                        {
                            priceDateTime = fileLastWriteTime;
                            ftpFileName   = shortFileName;
                        }
                    }
                }

                if (String.IsNullOrEmpty(ftpFileName))
                {
                    return(null);
                }
                downFileName = Path.Combine(downHandlerPath, ftpFileName);
                using (var file = new FileStream(downFileName, FileMode.Create))
                    ftpClient.ReceiveFile(ftpFileName, file);
            }

            return(new DownloadedFile(downFileName, priceDateTime));
        }
Esempio n. 26
0
 public AutoDownloadPriceViewModel(PriceSource priceSource)
     : base("Download new price")
 {
     _priceImporter = PriceImporterFactory.Create(priceSource);
 }
 public PricingService(PriceSource source)
 {
     _source = source;
 }
Esempio n. 28
0
 public void CurrentPrice(int price, int increment, PriceSource priceSource)
 {
 }