Overall Statistics
namespace QuantConnect 
{   
    /*
    *   QuantConnect University: Full Basic Template:
    *
    *   The underlying QCAlgorithm class is full of helper methods which enable you to use QuantConnect.
    *   We have explained some of these here, but the full algorithm can be found at:
    *   https://github.com/QuantConnect/Lean/tree/master/Algorithm
    */
    /// <summary>
    /// Basic template algorithm simply initializes the date range and cash
    /// </summary>
     public class LRP : QCAlgorithm
    {
        //the leverage for each holding
        decimal leverage = 3.8m;//Config.GetValue<decimal>("leverage", 3.5m);
        //the days interval to perform rebalance
        int days = 391;//Config.GetInt("days", 30);
        DateTime rebalanced;

        decimal tlt = 0.3m;//Config.GetValue<decimal>("tlt", 0.5m);
        decimal spy = 0.5m;//Config.GetValue<decimal>("spy", 0.4m);
        decimal gld = 0.1m;

        /// <summary>
        /// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
        /// </summary>
        public override void Initialize()
        {
            SetStartDate(2004, 11, 19);  //Set Start Date
            SetEndDate(2017, 1, 1);    //Set End Date
            SetCash(100000);             //Set Strategy Cash. Should be 3 month T Bills.

            //using etf rather than futures
            AddEquity("SPY", Resolution.Daily, Market.USA, false, 4);
            AddEquity("TLT", Resolution.Daily, Market.USA, false, 4);
            AddEquity("GLD", Resolution.Daily, Market.USA, false, 4);

            gld = 1 - tlt - spy;

            //minimum of 0.1 gld
            if (gld < 0.1m || gld + tlt + spy > 1)
            {
                Quit();
            }

            rebalanced = this.Time;

            Schedule.On(DateRules.EveryDay(), TimeRules.AfterMarketOpen("SPY"), () =>
            {
                if (rebalanced.AddDays(days) < this.Time)
                {
                    rebalanced = this.Time;
                    SetHoldings("TLT", tlt * leverage);
                    SetHoldings("SPY", spy * leverage);
                    SetHoldings("GLD", gld * leverage);
                    Debug("Rebalance");
                }
            });

        }

        public override void OnMarginCall(List<SubmitOrderRequest> requests)
        {
            //if (!LiveMode)
            //{
            //    this.Quit();
            //}
        }

        /// <summary>
        /// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
        /// </summary>
        /// <param name="data">Slice object keyed by symbol containing the stock data</param>
        public override void OnData(Slice data)
        {

            if (!Portfolio.Invested)
            {
                SetHoldings("TLT", tlt * leverage);
                SetHoldings("SPY", spy * leverage);
                SetHoldings("GLD", gld * leverage);
                Debug("Purchased Stock");
            }
        }

    }
}