コード例 #1
0
ファイル: ForexMarginModel.cs プロジェクト: pmerrill/Lean
 /// <summary>
 /// Gets the total margin required to execute the specified order in units of the account currency including fees
 /// </summary>
 /// <param name="security">The security to compute initial margin for</param>
 /// <param name="order">The order to be executed</param>
 /// <returns>The total margin in terms of the currency quoted in the order</returns>
 public override decimal GetInitialMarginRequiredForOrder(Security security, Order order)
 {
     //Get the order value from the non-abstract order classes (MarketOrder, LimitOrder, StopMarketOrder)
     //Market order is approximated from the current security price and set in the MarketOrder Method in QCAlgorithm.
     var orderFees = security.FeeModel.GetOrderFee(security, order);
     
     var orderCostInAccountCurrency = order.GetValue(security);
     return orderCostInAccountCurrency*InitialMarginRequirement + orderFees;
 }
コード例 #2
0
ファイル: ForexMarginModel.cs プロジェクト: skyfyl/Lean
        /// <summary>
        /// Gets the total margin required to execute the specified order in units of the account currency including fees
        /// </summary>
        /// <param name="security">The security to compute initial margin for</param>
        /// <param name="order">The order to be executed</param>
        /// <returns>The total margin in terms of the currency quoted in the order</returns>
        public override decimal GetInitialMarginRequiredForOrder(Security security, Order order)
        {
            var forex = (Forex)security;

            //Get the order value from the non-abstract order classes (MarketOrder, LimitOrder, StopMarketOrder)
            //Market order is approximated from the current security price and set in the MarketOrder Method in QCAlgorithm.
            var orderFees = security.TransactionModel.GetOrderFee(security, order);

            var price = order.Status.IsFill() ? order.Price : security.Price;
            var orderCostInAccountCurrency = order.GetValue(price)*forex.QuoteCurrency.ConversionRate;
            return orderCostInAccountCurrency*InitialMarginRequirement + orderFees;
        }
コード例 #3
0
        /// <summary>
        /// Uses the Interactive Brokers equities fixes fee schedule.
        /// </summary>
        /// <remarks>
        /// Default implementation uses the Interactive Brokers fee model of 0.5c per share with a maximum of 0.5% per order
        /// and minimum of $1.00.
        /// </remarks>
        /// <param name="security">The security matching the order</param>
        /// <param name="order">The order to compute fees for</param>
        /// <returns>The cost of the order in units of the account currency</returns>
        public override decimal GetOrderFee(Security security, Order order)
        {
            var price = order.Status.IsFill() ? order.Price : security.Price;
            var tradeValue = Math.Abs(order.GetValue(price));

            //Per share fees
            var tradeFee = 0.005m*order.AbsoluteQuantity;

            //Maximum Per Order: 0.5%
            //Minimum per order. $1.0
            var maximumPerOrder = 0.005m*tradeValue;
            if (tradeFee < 1)
            {
                tradeFee = 1;
            }
            else if (tradeFee > maximumPerOrder)
            {
                tradeFee = maximumPerOrder;
            }

            //Always return a positive fee.
            return Math.Abs(tradeFee);
        }
コード例 #4
0
 /// <summary>
 /// Prevent orders which would bring the account below a minimum cash balance
 /// </summary>
 public override bool CanSubmitOrder(Security security, Order order, out BrokerageMessageEvent message)
 {
     message = null;
     
     // we want to model brokerage requirement of _minimumAccountBalance cash value in account
     
     var orderCost = order.GetValue(security);
     var cash = _algorithm.Portfolio.Cash;
     var cashAfterOrder = cash - orderCost;
     if (cashAfterOrder < _minimumAccountBalance)
     {
         // return a message describing why we're not allowing this order
         message = new BrokerageMessageEvent(BrokerageMessageType.Warning, "InsufficientRemainingCapital", 
             string.Format("Account must maintain a minimum of ${0} USD at all times. Order ID: {1}", _minimumAccountBalance, order.Id)
             );
         return false;
     }
     return true;
 }
コード例 #5
0
        /// <summary>
        /// Gets the total margin required to execute the specified order in units of the account currency including fees
        /// </summary>
        /// <param name="security">The security to compute initial margin for</param>
        /// <param name="order">The order to be executed</param>
        /// <returns>The total margin in terms of the currency quoted in the order</returns>
        public virtual decimal GetInitialMarginRequiredForOrder(Security security, Order order)
        {
            //Get the order value from the non-abstract order classes (MarketOrder, LimitOrder, StopMarketOrder)
            //Market order is approximated from the current security price and set in the MarketOrder Method in QCAlgorithm.
            var orderFees = security.TransactionModel.GetOrderFee(security, order);

            var price = order.Status.IsFill() ? order.Price : security.Price;
            return order.GetValue(price)*InitialMarginRequirement + orderFees;
        }
コード例 #6
0
ファイル: ForexTransactionModel.cs プロジェクト: rchien/Lean
        /// <summary>
        /// Default implementation returns 0 for fees.
        /// </summary>
        /// <param name="security">The security matching the order</param>
        /// <param name="order">The order to compute fees for</param>
        /// <returns>The cost of the order in units of the account currency</returns>
        public override decimal GetOrderFee(Security security, Order order)
        {
            var forex = (Forex) security;

            // get the total order value in the account currency
            var price = order.Status.IsFill() ? order.Price : security.Price;
            var totalOrderValue = order.GetValue(price)*forex.QuoteCurrency.ConversionRate;
            var fee = _commissionRate*totalOrderValue;
            return Math.Max(_minimumOrderFee, fee);
        }
コード例 #7
0
        /// <summary>
        /// Default implementation returns 0 for fees.
        /// </summary>
        /// <param name="security">The security matching the order</param>
        /// <param name="order">The order to compute fees for</param>
        /// <returns>The cost of the order in units of the account currency</returns>
        public virtual decimal GetOrderFee(Security security, Order order)
        {
            if (order.Quantity == 0)
            {
                return 0m;
            }

            var price = order.Status.IsFill() ? order.Price : security.Price;
            return GetOrderFee(order.Quantity, order.GetValue(price) / order.Quantity);
        }