Ejemplo n.º 1
0
        public Commodity ParsePriceExpression(string str, bool addPrice = true, DateTime?moment = null)
        {
            string buf   = str;
            string price = null;
            int    pos   = str.IndexOf('=');

            if (pos >= 0)
            {
                buf   = str.Remove(pos).Trim();
                price = str.Substring(pos + 1).Trim();
            }

            Commodity commodity = FindOrCreate(buf);

            if (commodity != null)
            {
                if (!String.IsNullOrEmpty(price) && addPrice)
                {
                    foreach (string p in price.Split(';'))
                    {
                        commodity.AddPrice(moment.HasValue ? moment.Value : TimesCommon.Current.CurrentDate, new Amount(p));
                    }
                }
                return(commodity);
            }
            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Exchange one commodity for another, while recording the factored price.
        /// </summary>
        public void Exchange(Commodity commodity, Amount perUnitCost, DateTime moment)
        {
            Logger.Debug("commodity.prices.add", () => String.Format("exchanging commodity {0} at per unit cost {1} on {2}", commodity, perUnitCost, moment));
            Commodity baseCommodity = commodity.IsAnnotated ? ((AnnotatedCommodity)commodity).Referent : commodity;

            baseCommodity.AddPrice(moment, perUnitCost);
        }
Ejemplo 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>));
        }