Exemplo n.º 1
0
        private ActionOption <ExchangeResult <T> > ExchangeInto(T type, float amount, TradingInventoryAdapter <T> targetInventory, TradingInventoryAdapter <T> sourceInventory, float exchangeRate)
        {
            var maxPurchase      = targetInventory.GetCurrentFunds() / exchangeRate;
            var amountToPurchase = Math.Min(amount, maxPurchase);

            return(sourceInventory
                   .TransferResourceInto(type, targetInventory, amountToPurchase)
                   .Then(withdrawn => new ExchangeResult <T>
            {
                amount = withdrawn,
                cost = withdrawn * exchangeRate,
                type = type
            }, exchangeResult =>
            {
                targetInventory.TransferResourceInto(moneyType, sourceInventory, exchangeResult.cost).Execute();
            }));
        }
        public ActionOption <ExchangeResult <T> > Sell(T type, float amount, TradingInventoryAdapter <T> selfInventory, TradingInventoryAdapter <T> marketInventory)
        {
            //throw new NotImplementedException();
            // using the buying rate because this is a sell from the "other". I.E. a purchase from the market
            var exchangeRateFunction      = marketBuyRates[type];
            var amountLeftAfterMarketsBuy = exchangeRateFunction.GetPointFromNetExtraValueFromPoint(marketInventory.GetCurrentFunds(), marketInventory.Get(type));// marketInventory.GetCurrentFunds() / exchangeRate;
            var maxSell = amountLeftAfterMarketsBuy - marketInventory.Get(type);

            var amountToSell = Math.Min(amount, maxSell);

            return(selfInventory
                   .TransferResourceInto(type, marketInventory, amountToSell)
                   .Then(totalDeposited => new ExchangeResult <T>
            {
                amount = totalDeposited,
                cost = exchangeRateFunction.GetIncrementalValue(marketInventory.Get(type), totalDeposited),
                type = type
            }, exchangeResult =>
            {
                marketInventory.TransferResourceInto(moneyType, selfInventory, exchangeResult.cost).Execute();
            }));
        }
        public ActionOption <ExchangeResult <T> > Purchase(T type, float amount, TradingInventoryAdapter <T> selfInventory, TradingInventoryAdapter <T> marketInventory)
        {
            //throw new NotImplementedException();
            // using the buying rate because this is a purchase from the "other". I.E. a sell from the perspective of the market
            var exchangeRateFunction       = marketSellRates[type];
            var amountLeftAfterMarketsSell = exchangeRateFunction.GetPointFromNetExtraValueFromPoint(-selfInventory.GetCurrentFunds(), marketInventory.Get(type));
            var maxPurchase = marketInventory.Get(type) - amountLeftAfterMarketsSell;

            var amountToPurchase = Math.Min(amount, maxPurchase);

            return(marketInventory
                   .TransferResourceInto(type, selfInventory, amountToPurchase)
                   .Then(withdrawn => new ExchangeResult <T>
            {
                amount = withdrawn,
                cost = -exchangeRateFunction.GetIncrementalValue(marketInventory.Get(type), -withdrawn),
                type = type
            }, exchangeResult =>
            {
                selfInventory.TransferResourceInto(moneyType, marketInventory, exchangeResult.cost).Execute();
            }));
        }