Overall Statistics
Total Trades
9
Average Win
0%
Average Loss
-5.57%
Compounding Annual Return
-21.244%
Drawdown
26.000%
Expectancy
-1
Net Profit
-21.176%
Sharpe Ratio
-1.289
Loss Rate
100%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.131
Beta
-0.49
Annual Standard Deviation
0.171
Annual Variance
0.029
Information Ratio
-1.747
Tracking Error
0.232
Treynor Ratio
0.451
Total Fees
$42.76
/*
 * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
 * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using QuantConnect.Securities;

using System.Globalization;
using QuantConnect.Data.Market;

namespace QuantConnect
{
    public class TestFixedSetHoldingAlgorithm : FixedSetHoldingsAlgorithm
    {
        int step = 0;

        public override void Initialize()
        {
            SetStartDate(2013, 06, 01);
            SetEndDate(2014, 05, 30);
            SetCash(100000);
            AddSecurity(SecurityType.Equity, "MSFT", Resolution.Minute);
            AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);
            AddSecurity(SecurityType.Equity, "IBM", Resolution.Minute);
        }
        
        private int IntHoldings
        {
        	get { return (int)Portfolio.TotalHoldingsValue; }
        }
        
        private void AssertHoldings(int expected)
        {
        	if (IntHoldings != expected)
        	{
        		throw new Exception("Expected holdings value " + expected + " but was " + IntHoldings);
        	}
        }

        public void OnData(TradeBars data)
        {
            //First Order, Set 50% MSFT:
            if (!Portfolio.Invested)
            {
                FixedSetHoldings("MSFT", 0.5); step++;
                Debug("thv: " + Portfolio.TotalHoldingsValue.ToString("F5"));
                AssertHoldings(49971);
            }

            if (Time.Date == new DateTime(2013, 7, 1) && step == 1)
            {
                FixedSetHoldings("MSFT", 1); step++;
                Debug("thv: " + Portfolio.TotalHoldingsValue.ToString("F5"));
                AssertHoldings(99662);
            }

            if (Time.Date == new DateTime(2013, 8, 1) && step == 2)
            {
                FixedSetHoldings("IBM", 1, true); step++;
                Debug("thv: " + Portfolio.TotalHoldingsValue.ToString("F5"));
                AssertHoldings(91728);
            }

            if (Time.Date == new DateTime(2013, 9, 3) && step == 3)
            {
                FixedSetHoldings("IBM", -0.5, true); step++;
                Debug("thv: " + Portfolio.TotalHoldingsValue.ToString("F5"));
                AssertHoldings(43025);
            }

            if (Time.Date == new DateTime(2013, 10, 1) && step == 4)
            {
                FixedSetHoldings("SPY", -0.5); step++;
                Debug("thv: " + Portfolio.TotalHoldingsValue.ToString("F5"));
                AssertHoldings(86197);
            }

            if (Time.Date == new DateTime(2013, 11, 1) && step == 5)
            {
                FixedSetHoldings("IBM", -0.5, true);  //Succeed.
                Debug("thv: " + Portfolio.TotalHoldingsValue.ToString("F5"));
                AssertHoldings(42437);
                
                FixedSetHoldings("SPY", -0.5); step++;
                Debug("thv: " + Portfolio.TotalHoldingsValue.ToString("F5"));
                AssertHoldings(84851);
            }
        }
    }

}
using QuantConnect.Algorithm;
using QuantConnect.Orders;
using QuantConnect.Securities;
using System;

namespace QuantConnect
{
    public abstract class FixedSetHoldingsAlgorithm : QCAlgorithm
    {
        /// <summary>
        /// Alias for SetHoldings to avoid the M-decimal errors.
        /// </summary>
        /// <param name="symbol">string symbol we wish to hold</param>
        /// <param name="percentage">double percentage of holdings desired</param>
        /// <param name="liquidateExistingHoldings">liquidate existing holdings if neccessary to hold this stock</param>
        /// <seealso cref="MarketOrder"/>
        public void FixedSetHoldings(Symbol symbol, double percentage, bool liquidateExistingHoldings = false)
        {
            FixedSetHoldings(symbol, (decimal)percentage, liquidateExistingHoldings);
        }

        /// <summary>
        /// Alias for SetHoldings to avoid the M-decimal errors.
        /// </summary>
        /// <param name="symbol">string symbol we wish to hold</param>
        /// <param name="percentage">float percentage of holdings desired</param>
        /// <param name="liquidateExistingHoldings">bool liquidate existing holdings if neccessary to hold this stock</param>
        /// <param name="tag">Tag the order with a short string.</param>
        /// <seealso cref="MarketOrder"/>
        public void FixedSetHoldings(Symbol symbol, float percentage, bool liquidateExistingHoldings = false, string tag = "")
        {
            FixedSetHoldings(symbol, (decimal)percentage, liquidateExistingHoldings, tag);
        }

        /// <summary>
        /// Alias for SetHoldings to avoid the M-decimal errors.
        /// </summary>
        /// <param name="symbol">string symbol we wish to hold</param>
        /// <param name="percentage">float percentage of holdings desired</param>
        /// <param name="liquidateExistingHoldings">bool liquidate existing holdings if neccessary to hold this stock</param>
        /// <param name="tag">Tag the order with a short string.</param>
        /// <seealso cref="MarketOrder"/>
        public void FixedSetHoldings(Symbol symbol, int percentage, bool liquidateExistingHoldings = false, string tag = "")
        {
            FixedSetHoldings(symbol, (decimal)percentage, liquidateExistingHoldings, tag);
        }

        /// <summary>
        /// Automatically place an order which will set the holdings to between 100% or -100% of *PORTFOLIO VALUE*.
        /// E.g. SetHoldings("AAPL", 0.1); SetHoldings("IBM", -0.2); -> Sets portfolio as long 10% APPL and short 20% IBM
        /// E.g. SetHoldings("AAPL", 2); -> Sets apple to 2x leveraged with all our cash.
        /// </summary>
        /// <param name="symbol">Symbol indexer</param>
        /// <param name="percentage">decimal fraction of portfolio to set stock</param>
        /// <param name="liquidateExistingHoldings">bool flag to clean all existing holdings before setting new faction.</param>
        /// <param name="tag">Tag the order with a short string.</param>
        /// <seealso cref="MarketOrder"/>
        public void FixedSetHoldings(Symbol symbol, decimal percentage, bool liquidateExistingHoldings = false, string tag = "")
        {
            //Initialize Requirements:
            Security security;
            if (!Securities.TryGetValue(symbol, out security))
            {
                Error(symbol.ToString() + " not found in portfolio. Request this data when initializing the algorithm.");
                return;
            }

            //If they triggered a liquidate
            if (liquidateExistingHoldings)
            {
                foreach (var kvp in Portfolio)
                {
                    var holdingSymbol = kvp.Key;
                    var holdings = kvp.Value;
                    if (holdingSymbol != symbol && holdings.AbsoluteQuantity > 0)
                    {
                        //Go through all existing holdings [synchronously], market order the inverse quantity:
                        Order(holdingSymbol, -holdings.Quantity, false, tag);
                    }
                }
            }

            //Only place trade if we've got > 1 share to order.
            var quantity = FixedCalculateOrderQuantity(symbol, percentage);
            if (Math.Abs(quantity) > 0)
            {
                MarketOrder(symbol, quantity, false, tag);
            }
        }

        private bool TryOrderQuantity(int orderQuantity, Security security, decimal marginRemaining, decimal targetOrderValue)
        {
            //note that margin requirements and order value + fees are assumed to be monotonic w.r.t. orderQuantity,
            //otherwise binary search would not work and an exhaustive search would be necessary

            var order = new MarketOrder(security.Symbol, orderQuantity, UtcTime);
            var orderValue = order.GetValue(security);
            var orderFees = security.FeeModel.GetOrderFee(security, order);

            // calculate the margin required for the order
            var marginRequired = security.MarginModel.GetInitialMarginRequiredForOrder(security, order);

            return marginRequired <= marginRemaining && orderValue + orderFees <= targetOrderValue;
        }

        /// <summary>
        /// Calculate the order quantity to achieve target-percent holdings.
        /// </summary>
        /// <param name="symbol">Security object we're asking for</param>
        /// <param name="target">Target percentag holdings, this is an unlevered value, so 
        /// if you have 2x leverage and request 100% holdings, it will utilize half of the 
        /// available margin</param>
        /// <returns>Order quantity to achieve this percentage</returns>
        public int FixedCalculateOrderQuantity(Symbol symbol, decimal target)
        {
            var security = Securities[symbol];
            var price = security.Price;

            // can't order it if we don't have data
            if (price == 0) return 0;

            // if targeting zero, simply return the negative of the quantity
            if (target == 0) return -security.Holdings.Quantity;

            // this is the value in dollars that we want our holdings to have
            var targetPortfolioValue = target * Portfolio.TotalPortfolioValue;
            var quantity = security.Holdings.Quantity;
            var currentHoldingsValue = price * quantity;

            // remove directionality, we'll work in the land of absolutes
            var targetOrderValue = Math.Abs(targetPortfolioValue - currentHoldingsValue);
            var direction = targetPortfolioValue > currentHoldingsValue ? OrderDirection.Buy : OrderDirection.Sell;

            // determine the unit price in terms of the account currency
            var unitPrice = new MarketOrder(symbol, 1, UtcTime).GetValue(security);

            // calculate the total margin available
            var marginRemaining = Portfolio.GetMarginRemaining(symbol, direction);
            if (marginRemaining <= 0) return 0;

            // compute the initial order quantity
            int orderQuantity;
            int maxOrderQuantity = (int)(targetOrderValue / unitPrice); //upper bound
            int minOrderQuantity = 1; //lower bound

            if (TryOrderQuantity(maxOrderQuantity, security, marginRemaining, targetOrderValue))
            {
                orderQuantity = maxOrderQuantity;
            }
            else if (!TryOrderQuantity(minOrderQuantity, security, marginRemaining, targetOrderValue))
            {
                orderQuantity = 0;
            }
            else
            {
                //binary search
                for (;;)
                {
                    orderQuantity = (maxOrderQuantity + minOrderQuantity) / 2;
                    if (orderQuantity == minOrderQuantity)
                    {
                        orderQuantity = minOrderQuantity;
                        break;
                    }

                    if (TryOrderQuantity(orderQuantity, security, marginRemaining, targetOrderValue))
                    {
                        minOrderQuantity = orderQuantity;
                    }
                    else
                    {
                        maxOrderQuantity = orderQuantity;
                    }
                }
            }

            //Rounding off Order Quantity to the nearest multiple of Lot Size
            if (orderQuantity % Convert.ToInt32(security.SymbolProperties.LotSize) != 0)
            {
                orderQuantity = orderQuantity - (orderQuantity % Convert.ToInt32(security.SymbolProperties.LotSize));
            }

            // add directionality back in
            return (direction == OrderDirection.Sell ? -1 : 1) * orderQuantity;
        }
    }
}