Overall Statistics
Total Trades
2
Average Win
15.84%
Average Loss
-10.27%
Compounding Annual Return
3.944%
Drawdown
5.900%
Expectancy
0.271
Net Profit
3.94%
Sharpe Ratio
0.443
Loss Rate
50%
Win Rate
50%
Profit-Loss Ratio
1.54
Alpha
0.062
Beta
-0.066
Annual Standard Deviation
0.097
Annual Variance
0.01
Information Ratio
-1.583
Tracking Error
0.153
Treynor Ratio
-0.654
/*
 * 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 QuantConnect.Data.Market;
using QuantConnect.Indicators;

namespace QuantConnect.Algorithm.Examples
{
    /// <summary>
    /// In this algorithm we'll compute/plot the ratio between coke and pepsi
    /// </summary>
    public class IndicatorRatioExampleAlgorithm : QCAlgorithm
    {
        /// <summary>
        /// Our EMA of Coca cola minute close prices
        /// </summary>
        public Identity KO_Close;
        /// <summary>
        /// Our EMA of Pepsi minute close prices
        /// </summary>
        public Identity PEP_Close;
        /// <summary>
        /// Our KO/PEP ratio via minute close prices
        /// </summary>
        public CompositeIndicator<IndicatorDataPoint> KO_Over_PEP;
        /// <summary>
        /// This is an EMA of our KO_Over_PEP ratio indicator
        /// </summary>
        public ExponentialMovingAverage EMA_KO_Over_PEP;
        /// <summary>
        /// Bollinger bands of our ratio
        /// </summary>
        public BollingerBands BB_Ratio;

        /// <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(2013, 01, 01);  //Set Start Date
            SetEndDate(2014, 01, 01);    //Set End Date
            SetCash(100000);             //Set Strategy Cash'

            // Find more symbols here: http://quantconnect.com/data
            AddSecurity(SecurityType.Equity, "KO", Resolution.Minute);
            AddSecurity(SecurityType.Equity, "PEP", Resolution.Minute);

            // the identity indicator 'lifts' values into the indicator system for usage with other
            // indicators
            KO_Close = Identity("KO");
            PEP_Close = Identity("PEP");

            // This will create a new indicator that is the ko_close divided by the pep_close
            KO_Over_PEP = KO_Close.Over(PEP_Close, "Raw Ratio");

            // but we want the EMA of our ratio, so make a new EMA and define it as 'of' the ratio
            EMA_KO_Over_PEP = new ExponentialMovingAverage("EMA_Ratio", 1200).Of(KO_Over_PEP);

            // we'll also create a bollinger band of the EMA of the ratio for plotting
            BB_Ratio = new BollingerBands("BB_Ratio", 1200, 2).Of(EMA_KO_Over_PEP);
        }

        /// <summary>
        /// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
        /// </summary>
        /// <param name="data">TradeBars IDictionary object with your stock data</param>
        public void OnData(TradeBars data)
        {
            if (BB_Ratio.IsReady && !Portfolio.Invested)
            {
                MarketOrder("KO", -2000);
                MarketOrder("PEP", (int)(2000 * KO_Over_PEP));
            }

            // plot every afternoon
            if (data.Time.Hour == (3 + 12) && data.Time.Minute == 50)
            {
                // wait for bollinger bands to get ready
                if (BB_Ratio.IsReady)
                {
                    Plot("Ratio", EMA_KO_Over_PEP, BB_Ratio.UpperBand, BB_Ratio.LowerBand);
                }
            }
        }
    }
}