Beispiel #1
0
        public void InPlaceRoundTo(int places)
        {
            Logger.Current.Debug("amount.roundto", () => String.Format("=====> roundto places {0}", places));
            switch (Type)
            {
            case ValueTypeEnum.Integer:
                return;

            case ValueTypeEnum.Amount:
                AsAmount.InPlaceRoundTo(places);
                return;

            case ValueTypeEnum.Balance:
                AsBalance.InPlaceRoundTo(places);
                return;

            case ValueTypeEnum.Sequence:
                foreach (Value value in AsSequence)
                {
                    value.InPlaceRoundTo(places);
                }
                return;

            default:
                break;
            }
        }
Beispiel #2
0
        public void InPlaceCeiling()
        {
            switch (Type)
            {
            case ValueTypeEnum.Integer:
                return;

            case ValueTypeEnum.Amount:
                AsAmount.InPlaceCeiling();
                return;

            case ValueTypeEnum.Balance:
                AsBalance.InPlaceCeiling();
                return;

            case ValueTypeEnum.Sequence:
                foreach (Value value in AsSequence)
                {
                    value.InPlaceCeiling();
                }
                return;

            default:
                ErrorContext.Current.AddErrorContext(String.Format("While ceiling {0}:", this));
                throw new ValueError(String.Format(ValueError.CannotCeilingSmth, this));
            }
        }
Beispiel #3
0
        public void InPlaceRoundTo(int places)
        {
            switch (Type)
            {
            case ValueTypeEnum.Integer:
                return;

            case ValueTypeEnum.Amount:
                AsAmount.InPlaceRoundTo(places);
                return;

            case ValueTypeEnum.Balance:
                AsBalance.InPlaceRoundTo(places);
                return;

            case ValueTypeEnum.Sequence:
                foreach (Value value in AsSequence)
                {
                    value.InPlaceRoundTo(places);
                }
                return;

            default:
                break;
            }
        }
Beispiel #4
0
        public Value Abs()
        {
            switch (Type)
            {
            case ValueTypeEnum.Integer:
                long val = AsLong;
                return(Value.Get(val < 0 ? -val : val));

            case ValueTypeEnum.Amount:
                return(Value.Get(AsAmount.Abs()));

            case ValueTypeEnum.Balance:
                return(Value.Get(AsBalance.Abs()));

            default:
                ErrorContext.Current.AddErrorContext(String.Format("While taking abs of {0}:", this));
                throw new ValueError(String.Format(ValueError.CannotAbsSmth, this));
            }
        }
Beispiel #5
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 #6
0
        public Value Number()
        {
            switch (Type)
            {
            case ValueTypeEnum.Void:
                return(Value.Zero);

            case ValueTypeEnum.Boolean:
                return(AsBoolean ? Value.One : Value.Zero);

            case ValueTypeEnum.Integer:
                return(Value.Get(AsLong));

            case ValueTypeEnum.Amount:
                return(Value.Get(AsAmount.Number()));

            case ValueTypeEnum.Balance:
                return(Value.Get(AsBalance.Number()));

            case ValueTypeEnum.Sequence:
                if (AsSequence.Any())
                {
                    Value temp = new Value();
                    foreach (Value value in AsSequence)
                    {
                        temp.InPlaceAdd(value.Number());
                    }
                    return(temp);
                }
                break;

            default:
                break;
            }
            ErrorContext.Current.AddErrorContext(String.Format("While calling number() on {0}:", this));
            throw new ValueError(String.Format(ValueError.CannotDetermineNumericValueOfSmth, this));
        }
Beispiel #7
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);
        }