Overall Statistics
Total Trades
18
Average Win
3.08%
Average Loss
-0.59%
Compounding Annual Return
18.001%
Drawdown
7.200%
Expectancy
2.960
Net Profit
18.001%
Sharpe Ratio
1.321
Loss Rate
36%
Win Rate
64%
Profit-Loss Ratio
5.22
Alpha
0.119
Beta
-0.011
Annual Standard Deviation
0.089
Annual Variance
0.008
Information Ratio
0.307
Tracking Error
0.13
Treynor Ratio
-10.987
Total Fees
$0.00
using System;
using System.Globalization;
using QuantConnect.Data;
using QuantConnect.Indicators.CandlestickPatterns;

namespace QuantConnect.Algorithm.CSharp
{   
    /// <summary>
    /// Basic template algorithm simply initializes the date range and cash
    /// </summary>
    public class CandlestickClosingMarubozuAlgorithm : QCAlgorithm
    {
        private string _symbol = "YAHOO/INDEX_SPY";
        private ClosingMarubozu _pattern = new ClosingMarubozu();

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

            AddData<CloseMar>(_symbol, Resolution.Daily);

            _pattern = CandlestickPatterns.ClosingMarubozu(_symbol);
        }

        /// <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(CloseMar data)
        {
            // Debug("Close: " + data.Close + " - AdjustedClose: " + data.AdjustedClose + " - Value: " + data.Value);

            if (_pattern == 1)
            {
                // Bullish ClosingMarubozu, go long
                Debug(Time + " -> found Bullish ClosingMarubozu");
                SetHoldings(_symbol, 1);
            }
            else if (_pattern == -1)
            {
                // Bearish ClosingMarubozu, go short
                Debug(Time + " -> found Bearish ClosingMarubozu");
                SetHoldings(_symbol, -1);
            }
        }
    }

    public class CloseMar : TradeBar
    {
        public decimal AdjustedClose { get; set; }
        public new decimal Value { get; set; }

        public override DateTime EndTime
        {
            get { return Time + Period; }
            set { Time = value - Period; }
        }

        public new TimeSpan Period
        {
            get { return QuantConnect.Time.OneDay; }
        }

        /// <summary>
        /// Return the URL external source for the data: QuantConnect will download it an read it line by line automatically:
        /// </summary>
        public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLive)
        {
            var startDate = new DateTime(2010, 01, 01).ToString("yyyy-MM-dd");
            var endDate = DateTime.Now.ToString("yyyy-MM-dd");

            //QUANDL WRAPPER ON YAHOO FINANCE API TO SORT DATA:
            //https://www.quandl.com/api/v1/datasets/YAHOO/INDEX_SPY.csv?trim_start=2010-01-01&trim_end=2015-12-31&sort_order=asc
            return new SubscriptionDataSource("https://www.quandl.com/api/v1/datasets/" + config.Symbol + ".csv?trim_start=" + startDate + "&trim_end=" + endDate + "&sort_order=asc&exclude_headers=true", SubscriptionTransportMedium.RemoteFile);
        }

        /// <summary>
        /// Convert each line of the file above into an object.
        /// </summary>
        public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLive)
        {
            CloseMar cmBar = new CloseMar();

            try
            {
                string[] data = line.Split(',');
                //Required.
                cmBar.Symbol = config.Symbol;
                cmBar.Time = DateTime.ParseExact(data[0], "yyyy-MM-dd", CultureInfo.InvariantCulture);

                //User configured / optional data on each bar:
                cmBar.Open = Convert.ToDecimal(data[1]);
                cmBar.High = Convert.ToDecimal(data[2]);
                cmBar.Low = Convert.ToDecimal(data[3]);
                cmBar.Close = Convert.ToDecimal(data[4]);
                cmBar.Volume = Convert.ToInt64(Convert.ToDecimal(data[5]));
                cmBar.AdjustedClose = Convert.ToDecimal(data[6]);

                //This is the value the engine uses for portfolio calculations
                cmBar.Value = cmBar.AdjustedClose;
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }
            return cmBar;
        }
    }
}