Overall Statistics
Total Trades
116
Average Win
0.33%
Average Loss
-0.27%
Compounding Annual Return
4.875%
Drawdown
3.900%
Expectancy
0.010
Net Profit
0.715%
Sharpe Ratio
0.451
Loss Rate
55%
Win Rate
45%
Profit-Loss Ratio
1.23
Alpha
0.021
Beta
0.128
Annual Standard Deviation
0.117
Annual Variance
0.014
Information Ratio
-1.152
Tracking Error
0.173
Treynor Ratio
0.413
Total Fees
$544.40

using System;
using System.Linq;
using QuantConnect.Indicators;
using QuantConnect.Models;

namespace QuantConnect.Algorithm.Examples
{


    public class SMACross : QCAlgorithm
    {
        private const string Symbol = "SPY";

        private SimpleMovingAverage fast;
        private SimpleMovingAverage slow;

        TradeBar _spyMinutes;

        public override void Initialize()
        {
            SetStartDate(2016, 06, 01);
            //SetEndDate(2015, 12, 31);
            SetEndDate(DateTime.Now.Date.AddDays(-1));
            SetCash(100000);
            AddSecurity(SecurityType.Equity, Symbol, Resolution.Second);
            Transactions.MarketOrderFillTimeout = TimeSpan.FromMinutes(10);

            // define our daily trade bar consolidator. we can access the daily bar
            // from the DataConsolidated events, this consolidator can only be used
            // for a single symbol!
            var minConsolidator = new TradeBarConsolidator(TimeSpan.FromMinutes(30));

            // attach our event handler. the event handler is a function that will be called each time we produce
            // a new consolidated piece of data.
            minConsolidator.DataConsolidated += OnFiveMinutes;

            // this call adds our daily consolidator to the manager to receive updates from the engine
            SubscriptionManager.AddConsolidator(Symbol, minConsolidator);

            int fastPeriod = 2;
            int slowPeriod = 4;
            fast = new SimpleMovingAverage(Symbol + "_SMA_" + fastPeriod, fastPeriod);
            slow = new SimpleMovingAverage(Symbol + "_SMA_" + slowPeriod, slowPeriod);

            // we need to manually register these indicators for automatic updates
            RegisterIndicator(Symbol, fast, minConsolidator);
            RegisterIndicator(Symbol, slow, minConsolidator);
        }


        private void OnFiveMinutes(object sender, TradeBar consolidated)
        {
            _spyMinutes = consolidated;
            //Log(consolidated.Time.ToString("o") + " >> " + Symbol  + ">> LONG  >> 100 >> " + Portfolio[Symbol].Quantity);

            // if you want code to run every five minutes then you can run it inside of here
            if (!slow.IsReady) return;

            // only once per day 
            // Commented the following line to simulate intraday - Vats
            //if (previous.Date == data.Time.Date) return;

            // in OnData, returns outside of 9am - 2pm
            //if (Time.Hour <= 9 || Time.Hour > 16) return;


            const decimal tolerance = 0 * 0.10000m;
            var holdings = Portfolio[Symbol].Quantity;
            Log(consolidated.Close+"//" +fast+"//"+slow);



            {
                if (fast > slow * (1 + tolerance))
                {
                    if (holdings <= 0)
                    {
                        // Log (System.DateTime.Now.Hour.ToString()) ;
                        // Log("BUY  >> " + holdings + "@ price " + Securities[Symbol].Price);
                        SetHoldings(Symbol, 1);
                        // Log("NET POSITION BEFORE NEXT TRANSACTION >> " + holdings);
                    }
                }


                if (fast < slow)
                {
                    if (holdings > 0)
                    {
                        //Log (System.DateTime.Now.Hour.ToString()) ;
                        //Log("SELL >> " + holdings + "@ price " + Securities[Symbol].Price);
                        SetHoldings(Symbol, -1);
                        //Liquidate(Symbol);
                    }
                }
            }
        }


        private DateTime previous;
        public void OnData(TradeBars data)
        {




            Plot(Symbol, "Price", data[Symbol].Price);
            Plot(Symbol, fast, slow);
            previous = data.Time;
        }

        bool IsLastTradingday(TradeBar b)
        {
            if (b.Time.Hour == 15 && b.Time.Minute == 59)
                return true;
            else
                return false;
        } // IsLastTradingMin

    }
}