Пример #1
0
        /// <summary>
        /// Gets the buying power available for a trade
        /// </summary>
        /// <param name="context">A context object containing the algorithm's potrfolio, security, and order direction</param>
        /// <returns>The buying power available for the trade</returns>
        public override BuyingPower GetBuyingPower(BuyingPowerContext context)
        {
            var security  = context.Security;
            var portfolio = context.Portfolio;
            var direction = context.Direction;

            var baseCurrency = security as IBaseCurrencySymbol;

            if (baseCurrency == null)
            {
                return(context.ResultInAccountCurrency(0m));
            }

            var baseCurrencyPosition  = portfolio.CashBook[baseCurrency.BaseCurrencySymbol].Amount;
            var quoteCurrencyPosition = portfolio.CashBook[security.QuoteCurrency.Symbol].Amount;

            // determine the unit price in terms of the quote currency
            var unitPrice = new MarketOrder(security.Symbol, 1, DateTime.UtcNow).GetValue(security) / security.QuoteCurrency.ConversionRate;

            if (unitPrice == 0)
            {
                return(context.ResultInAccountCurrency(0m));
            }

            // NOTE: This is returning in units of the BASE currency
            if (direction == OrderDirection.Buy)
            {
                // invert units for math, 6500USD per BTC, currency pairs aren't real fractions
                // (USD)/(BTC/USD) => 10kUSD/ (6500 USD/BTC) => 10kUSD * (1BTC/6500USD) => ~ 1.5BTC
                return(context.Result(quoteCurrencyPosition / unitPrice, baseCurrency.BaseCurrencySymbol));
            }

            if (direction == OrderDirection.Sell)
            {
                return(context.Result(baseCurrencyPosition, baseCurrency.BaseCurrencySymbol));
            }

            return(context.ResultInAccountCurrency(0m));
        }
Пример #2
0
        /// <summary>
        /// Gets the buying power available for a trade
        /// </summary>
        /// <param name="context">A context object containing the algorithm's potrfolio, security, and order direction</param>
        /// <returns>The buying power available for the trade</returns>
        public virtual BuyingPower GetBuyingPower(BuyingPowerContext context)
        {
            var marginRemaining = GetMarginRemaining(context.Portfolio, context.Security, context.Direction);

            return(context.ResultInAccountCurrency(marginRemaining));
        }