Beispiel #1
0
        // Return the "market value" of a given value at a specific time.
        // (equal to value(...
        public Value ValueOf(DateTime moment = default(DateTime), Commodity inTermOf = null)
        {
            switch (Type)
            {
            case ValueTypeEnum.Integer:
                return(Value.Empty);

            case ValueTypeEnum.Amount:
                Amount val = AsAmount.Value(moment, inTermOf);
                if (val != null)
                {
                    return(Value.Get(val));
                }
                return(Value.Empty);

            case ValueTypeEnum.Balance:
                Balance bal = AsBalance.Value(moment, inTermOf);
                if (!Balance.IsNull(bal))
                {
                    return(Value.Get(bal));
                }
                return(Value.Empty);

            case ValueTypeEnum.Sequence:
                Value temp = new Value();
                foreach (Value value in AsSequence)
                {
                    temp.PushBack(value.ValueOf(moment, inTermOf));
                }
                return(temp);

            default:
                break;
            }

            ErrorContext.Current.AddErrorContext(String.Format(ValueError.WhileFindingValuationOfSmth, this));
            throw new ValueError(String.Format(ValueError.CannotFindTheValueOfSmth, this));
        }
Beispiel #2
0
        public Value ExchangeCommodities(string commodities, bool addPrices = false, DateTime moment = default(DateTime))
        {
            if (Type == ValueTypeEnum.Sequence)
            {
                Value temp = new Value();
                foreach (Value value in AsSequence)
                {
                    temp.PushBack(value.ExchangeCommodities(commodities, addPrices, moment));
                }
                return(temp);
            }

            // If we are repricing to just a single commodity, with no price
            // expression, skip the expensive logic below.

            if (commodities.IndexOfAny(CommaOrEqual) < 0)
            {
                return(ValueOf(moment, CommodityPool.Current.FindOrCreate(commodities)));
            }

            IList <Commodity> comms = new List <Commodity>();
            IList <bool>      force = new List <bool>();

            IEnumerable <string> tokens = commodities.Split(',').Select(s => s.Trim());

            foreach (string name in tokens)
            {
                int  nameLen = name.Length;
                bool isForce = name.Last() == '!';

                Commodity commodity = CommodityPool.Current.ParsePriceExpression(isForce ? name.Remove(nameLen - 1) : name, addPrices, moment);
                if (commodity != null)
                {
                    Logger.Current.Debug("commodity.exchange", () => String.Format("Pricing for commodity: {0}", commodity.Symbol));
                    comms.Add(commodity.Referent);
                    force.Add(isForce);
                }
            }

            int index = 0;

            foreach (Commodity comm in comms)
            {
                switch (Type)
                {
                case ValueTypeEnum.Amount:
                    Logger.Current.Debug("commodity.exchange", () => String.Format("We have an amount: {0}", AsAmount));
                    if (!force[index] && comms.Contains(AsAmount.Commodity.Referent))
                    {
                        break;
                    }

                    Logger.Current.Debug("commodity.exchange", () => "Referent doesn't match, pricing...");
                    Amount val = AsAmount.Value(moment, comm);
                    if (val != null)
                    {
                        return(Value.Get(val));
                    }

                    Logger.Current.Debug("commodity.exchange", () => "Was unable to find a price");
                    break;

                case ValueTypeEnum.Balance:
                    Balance temp     = new Balance();
                    bool    rePriced = false;

                    Logger.Current.Debug("commodity.exchange", () => String.Format("We have a balance: {0}", AsBalance));
                    foreach (KeyValuePair <Commodity, Amount> pair in AsBalance.Amounts)
                    {
                        Logger.Current.Debug("commodity.exchange", () => String.Format("We have a balance amount of commodity: {0} == {1}", pair.Key.Symbol, pair.Value.Commodity.Symbol));
                        if (!force[index] && comms.Contains(pair.Key.Referent))
                        {
                            temp = temp.Add(pair.Value);
                        }
                        else
                        {
                            Logger.Current.Debug("commodity.exchange", () => "Referent doesn't match, pricing...");
                            Amount val1 = pair.Value.Value(moment, comm);
                            if (val1 != null)
                            {
                                Logger.Current.Debug("commodity.exchange", () => String.Format("Re-priced member amount is: {0}", val1));
                                temp     = temp.Add(val1);
                                rePriced = true;
                            }
                            else
                            {
                                Logger.Current.Debug("commodity.exchange", () => "Was unable to find price");
                                temp = temp.Add(pair.Value);
                            }
                        }
                    }

                    if (rePriced)
                    {
                        Logger.Current.Debug("commodity.exchange", () => String.Format("Re-priced balance is: {0}", temp));
                        return(Value.Get(temp));
                    }

                    break;

                default:
                    break;
                }

                ++index;
            }

            return(this);
        }