Overall Statistics
Total Trades
19
Average Win
3.64%
Average Loss
-2.61%
Compounding Annual Return
12.005%
Drawdown
12.800%
Expectancy
0.730
Net Profit
40.542%
Sharpe Ratio
1.04
Loss Rate
28%
Win Rate
72%
Profit-Loss Ratio
1.40
Alpha
0.036
Beta
4.226
Annual Standard Deviation
0.116
Annual Variance
0.013
Information Ratio
0.867
Tracking Error
0.116
Treynor Ratio
0.028
Total Fees
$146.70
/*
 * 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;
using QuantConnect.Securities;

namespace QuantConnect.Algorithm.CSharp
{
    /// <summary>
    /// Regression Channel algorithm simply initializes the date range and cash
    /// </summary>
    /// <meta name="tag" content="indicators" />
    /// <meta name="tag" content="indicator classes" />
    /// <meta name="tag" content="placing orders" />
    /// <meta name="tag" content="plotting indicators" />
    public class RegressionChannelAlgorithm : QCAlgorithm
    {
        private Symbol _spy = QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA);
        private SecurityHolding _holdings;
        private RegressionChannel _rc;

        /// <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(2012, 1, 1);  //Set Start Date
            SetEndDate(2015, 1, 1);    //Set End Date
            SetCash(100000);           //Set Strategy Cash
            // Find more symbols here: http://quantconnect.com/data
            var equity = AddEquity(_spy, Resolution.Minute);
            _holdings = equity.Holdings;
            _rc = RC(_spy, 30, 2, Resolution.Daily);

            var stockPlot = new Chart("Trade Plot");
            stockPlot.AddSeries(new Series("Buy", SeriesType.Scatter, 0));
            stockPlot.AddSeries(new Series("Sell", SeriesType.Scatter, 0));
            stockPlot.AddSeries(new Series("UpperChannel", SeriesType.Line, 0));
            stockPlot.AddSeries(new Series("LowerChannel", SeriesType.Line, 0));
            stockPlot.AddSeries(new Series("Regression", SeriesType.Line, 0));
            AddChart(stockPlot);
        }

        /// <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 void OnData(TradeBars data)
        {
            if (!_rc.IsReady || !data.ContainsKey(_spy)) return;
            var value = data[_spy].Value;
           
            if (Time.Hour < 10 || Time.Hour > 15)
              Debug(" in OnData " + Time.ToString() + " Close=" + value + " rc_LowerChanngel="  +  _rc.LowerChannel );

            if (_holdings.Quantity <= 0 && value < _rc.LowerChannel)
            {
                SetHoldings(_spy, 1);
                Plot("Trade Plot", "Buy", value);

            }

            if (_holdings.Quantity >= 0 && value > _rc.UpperChannel)
            {
                SetHoldings(_spy, -1);
                Plot("Trade Plot", "Sell", value);
            }
        }

        public override void OnEndOfDay()
        {
        	Debug(" in  OnEndOfDay " + Time.ToString() + " rc_LowerChanngel=" +  _rc.LowerChannel );
        	
            Plot("Trade Plot", "UpperChannel", _rc.UpperChannel);
            Plot("Trade Plot", "LowerChannel", _rc.LowerChannel);
            Plot("Trade Plot", "Regression", _rc.LinearRegression);
        }
    }
}