Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
/*
 * Notice this algorithm references QuantConnect.Logging which is unfortunately not legal in back test
 * So it is useful only on your local machine which runs a clone of Lean.
 * 
 * To get it to compile on QuantConnect, I have commented out all references to the logging functions
 * Copy this algorithm into your QuantConnect.Algorithm.CSharp folder in your QuantConnect.Lean project
 *  and then uncomment the using, the declaration, the initialization and the call to mylog on OnData.
 *  
 * You also need to add a class called CustomFileLogHandler.cs in your QuantConnect.Logging project in
 *  your QuantConnect.Lean solution.  That file is also a part of this Project, but it is all commented
 *  out because QC does not allow you to reference the IO dll in the System namespace.  Add the file to the Logging project and
 *  uncomment the file's contents.
 *  
 */
using System;
using System.Collections.Generic;
using QuantConnect.Algorithm;
using QuantConnect.Data.Market;
// Uncomment this using statement
//using QuantConnect.Logging;
using QuantConnect.Util;

namespace QuantConnect
{
    /// <summary>
    /// Sample for adding custom logging to your Local copy of Lean 
    /// </summary>
    public class FileLoggingSampleAlgorithm : QCAlgorithm
    {
        #region "Custom Logging"
        // Uncomment this declaration
        //private ILogHandler mylog;
        #endregion
        /// <summary>
        /// Usual Initialize summary
        /// </summary>
        public override void Initialize()
        {
            //Initialize dates
            SetStartDate(new DateTime(2016, 1, 11));
            SetEndDate(new DateTime(2016, 1, 14));
            SetCash(50000);

            AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);

            // Uncomment this instantition
            //mylog = Composer.Instance.GetExportedValueByTypeName<ILogHandler>("CustomFileLogHandler");

        }
        /// <summary>
        /// Usuall OnData summary
        /// </summary>
        /// <param name="data">Trade bars for the event handler</param>
        public void OnData(TradeBars data)
        {
            foreach (KeyValuePair<Symbol, TradeBar> kvp in data)
            {
                // Add your indicator values to the logmsg
                string logmsg = string.Format(
                    "{0},{1},{2},{3},{4},{5},{6}",
                    kvp.Value.EndTime.ToShortTimeString(),
                    kvp.Value.Symbol.Value,
                    Math.Round(kvp.Value.Open, 4),
                    Math.Round(kvp.Value.High, 4),
                    Math.Round(kvp.Value.Low, 4),
                    Math.Round(kvp.Value.Close, 4),
                    kvp.Value.Volume
                    );
                // Uncomment this call
                //mylog.Debug(logmsg);
            }
        }
    }
}
/*
namespace QuantConnect.Logging {
    /// <summary>
    /// Provides an implementation of <see cref="ILogHandler"/> that writes all log messages to a file on disk.
    /// </summary>
    public class CustomFileLogHandler : ILogHandler
    {
        // we need to control synchronization to our stream writer since it's not inherently thread-safe
        private readonly object _lock = new object();
        private readonly Lazy<TextWriter> _writer;

        /// <summary>
        /// Initializes a new instance of the <see cref="FileLogHandler"/> class to write messages to the specified file path.
        /// The file will be opened using <see cref="FileMode.Append"/>
        /// </summary>
        /// <param name="filepath">The file path use to save the log messages</param>
        public CustomFileLogHandler(string filepath)
        {
            _writer = new Lazy<TextWriter>(
                () => new StreamWriter(File.Open(filepath, FileMode.Append, FileAccess.Write, FileShare.Read))
                );
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="FileLogHandler"/> class using 'log.txt' for the filepath.
        /// </summary>
        public CustomFileLogHandler()
            : this("mylog.csv")
        {
            if (File.Exists("mylog.csv"))
                File.Delete("mylog.csv");
        }

        /// <summary>
        /// Write error message to log
        /// </summary>
        /// <param name="text">The error text to log</param>
        public void Error(string text)
        {
            WriteMessage(text, "ERROR");
        }

        /// <summary>
        /// Write debug message to log
        /// </summary>
        /// <param name="text">The debug text to log</param>
        public void Debug(string text)
        {
            WriteMessage(text, "DEBUG");
        }

        /// <summary>
        /// Write debug message to log
        /// </summary>
        /// <param name="text">The trace text to log</param>
        public void Trace(string text)
        {
            WriteMessage(text, "TRACE");
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        /// <filterpriority>2</filterpriority>
        public void Dispose()
        {
            lock (_lock)
            {
                if (_writer.IsValueCreated)
                {
                    _writer.Value.Dispose();
                }
            }
        }

        /// <summary>
        /// Writes the message to the writer
        /// </summary>
        private void WriteMessage(string text, string level)
        {
            lock (_lock)
            {
                //_writer.Value.WriteLine("{0} {1}:: {2}", DateTime.UtcNow.ToString("o"), level, text);
                _writer.Value.WriteLine("{0}", text);
                _writer.Value.Flush();
            }
        }
    }
}
*/