Exemplo n.º 1
0
        public override bool Equals(object obj)
        {
            if (!(obj is PricePoint))
            {
                return(false);
            }

            PricePoint other = (PricePoint)obj;

            return(When == other.When && Price == other.Price);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Ported from commodity_history_impl_t::find_price
        /// </summary>
        public PricePoint?FindPrice(Commodity source, DateTime moment, DateTime oldest = default(DateTime))
        {
            RecentEdgeWeight recentEdgeWeight           = new RecentEdgeWeight(moment, oldest);
            IGraph <Commodity, PriceGraphEdge> filtered = PriceGraph.Filter((comm1, comm2, edge) => recentEdgeWeight.Filter(comm1, comm2, edge));

            Logger.Current.Debug("history.find", () => String.Format("sv commodity = {0}", source.Symbol));
            if (source.Flags.HasFlag(CommodityFlagsEnum.COMMODITY_PRIMARY))
            {
                Logger.Current.Debug("history.find", () => "sv commodity is primary");
            }
            Logger.Current.Debug("history.find", () => "tv commodity = none ");

            DateTime mostRecent = moment;
            Amount   price      = null;

            foreach (EdgeDescriptor <Commodity, PriceGraphEdge> edgeDescriptor in filtered.AdjacentVertices(source))
            {
                Logger.Current.Debug("history.find", () => String.Format("u commodity = {0}", edgeDescriptor.Vertex1.Symbol));
                Logger.Current.Debug("history.find", () => String.Format("v commodity = {0}", edgeDescriptor.Vertex2.Symbol));

                PricePoint point = edgeDescriptor.Edge.PricePoint;
                if (price == null || point.When > mostRecent)
                {
                    mostRecent = point.When;
                    price      = point.Price;
                }

                Logger.Current.Debug("history.find", () => String.Format("price was = {0}", price.Unrounded()));

                if (price.Commodity == source)
                {
                    price = new Amount(price.GetInvertedQuantity(), edgeDescriptor.GetInvertedVertex(source));
                }

                Logger.Current.Debug("history.find", () => String.Format("price is  = {0}", price.Unrounded()));
            }

            if (Amount.IsNullOrEmpty(price))
            {
                Logger.Current.Debug("history.find", () => "there is no final price");
                return(null);
            }
            else
            {
                Logger.Current.Debug("history.find", () => String.Format("final price is = {0}", price.Unrounded()));
                return(new PricePoint(mostRecent, price));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Parse commodity prices from a textual representation
        /// </summary>
        /// <remarks>
        /// ported from parse_price_directive
        /// </remarks>
        public Tuple <Commodity, PricePoint> ParsePriceDirective(string line, bool doNotAddPrice = false, bool noDate = false)
        {
            string timeField = StringExtensions.NextElement(ref line);

            if (String.IsNullOrWhiteSpace(timeField))
            {
                return(default(Tuple <Commodity, PricePoint>));
            }
            string dateField = line;

            string   symbolAndPrice = null;
            DateTime dateTime       = default(DateTime);
            string   symbol         = null;

            if (!noDate && Char.IsDigit(timeField[0]))
            {
                symbolAndPrice = StringExtensions.NextElement(ref timeField);
                if (String.IsNullOrWhiteSpace(symbolAndPrice))
                {
                    return(default(Tuple <Commodity, PricePoint>));
                }

                dateTime = TimesCommon.Current.ParseDateTime(dateField + " " + timeField);
            }
            else if (!noDate && Char.IsDigit(dateField[0]))
            {
                symbolAndPrice = timeField;

                dateTime = TimesCommon.Current.ParseDate(dateField);
            }
            else
            {
                symbol         = dateField;
                symbolAndPrice = timeField;
                dateTime       = TimesCommon.Current.CurrentTime;
            }

            if (String.IsNullOrEmpty(symbol))
            {
                symbol = Commodity.ParseSymbol(ref symbolAndPrice);
            }

            PricePoint point = new PricePoint(dateTime, new Amount());

            point.Price.Parse(ref symbolAndPrice, AmountParseFlagsEnum.PARSE_NO_MIGRATE);
            Validator.Verify(point.Price.Valid());

            Logger.Debug(DebugCommodityDownload, () => "Looking up symbol: " + symbol);
            Commodity commodity = FindOrCreate(symbol);

            if (commodity != null)
            {
                Logger.Debug(DebugCommodityDownload, () => String.Format("Adding price for {0}: {1} {2}", symbol, point.When, point.Price));
                if (!doNotAddPrice)
                {
                    commodity.AddPrice(point.When, point.Price, true);
                }
                commodity.Flags |= CommodityFlagsEnum.COMMODITY_KNOWN;
                return(new Tuple <Commodity, PricePoint>(commodity, point));
            }

            return(default(Tuple <Commodity, PricePoint>));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Ported from commodity_history_impl_t::find_price
        /// </summary>
        public PricePoint?FindPrice(Commodity source, Commodity target, DateTime moment, DateTime oldest = default(DateTime))
        {
            if (source == target)
            {
                throw new InvalidOperationException("Source commodity is equal to Target");
            }

            RecentEdgeWeight recentEdgeWeight           = new RecentEdgeWeight(moment, oldest);
            IGraph <Commodity, PriceGraphEdge> filtered = PriceGraph.Filter((comm1, comm2, edge) => recentEdgeWeight.Filter(comm1, comm2, edge));

            Logger.Current.Debug("history.find", () => String.Format("u commodity = {0}", source.Symbol));
            Logger.Current.Debug("history.find", () => String.Format("v commodity = {0}", target.Symbol));

            IEnumerable <EdgeDescriptor <Commodity, PriceGraphEdge> > shortestPath = filtered.FindShortestPath(source, target, (edge) => edge.Weight.Ticks);

            // Extract the shortest path and performance the calculations
            DateTime leastRecent = moment;
            Amount   price       = null;

            Commodity lastTarget = target;

            foreach (EdgeDescriptor <Commodity, PriceGraphEdge> edgeDescriptor in shortestPath.Reverse())
            {
                PricePoint point = edgeDescriptor.Edge.PricePoint;

                Commodity uComm = edgeDescriptor.Vertex1;
                Commodity vComm = edgeDescriptor.Vertex2;

                bool firstRun = false;
                if (Amount.IsNullOrEmpty(price))
                {
                    leastRecent = point.When;
                    firstRun    = true;
                }
                else if (point.When < leastRecent)
                {
                    leastRecent = point.When;
                }

                Logger.Current.Debug("history.find", () => String.Format("u commodity = {0}", uComm.Symbol));
                Logger.Current.Debug("history.find", () => String.Format("v commodity = {0}", vComm.Symbol));
                Logger.Current.Debug("history.find", () => String.Format("last target = {0}", lastTarget.Symbol));

                // Determine which direction we are converting in
                Amount pprice = new Amount(point.Price);
                Logger.Current.Debug("history.find", () => String.Format("pprice    = {0}", pprice.Unrounded()));

                if (!firstRun)
                {
                    Logger.Current.Debug("history.find", () => String.Format("price was = {0}", price.Unrounded()));
                    if (pprice.Commodity != lastTarget)
                    {
                        price *= pprice.Inverted();
                    }
                    else
                    {
                        price *= pprice;
                    }
                }
                else if (pprice.Commodity != lastTarget)
                {
                    price = pprice.Inverted();
                }
                else
                {
                    price = pprice;
                }
                Logger.Current.Debug("history.find", () => String.Format("price is  = {0}", price.Unrounded()));

                if (lastTarget == vComm)
                {
                    lastTarget = uComm;
                }
                else
                {
                    lastTarget = vComm;
                }

                Logger.Current.Debug("history.find", () => String.Format("last target now = {0}", lastTarget.Symbol));
            }

            if (Amount.IsNullOrEmpty(price))
            {
                Logger.Current.Debug("history.find", () => "there is no final price");
                return(null);
            }
            else
            {
                price = new Amount(price.Quantity, target);
                Logger.Current.Debug("history.find", () => String.Format("final price is = {0}", price.Unrounded()));
                return(new PricePoint(leastRecent, price));
            }
        }