Overall Statistics
Total Trades
454
Average Win
0.7%
Average Loss
-0.69%
Compounding Annual Return
19.772%
Drawdown
18.600%
Expectancy
0.06
Net Profit
17.989%
Sharpe Ratio
0.938
Loss Rate
48%
Win Rate
52%
Profit-Loss Ratio
1.02
Alpha
-0.066
Beta
1.95
Annual Standard Deviation
0.217
Annual Variance
0.047
Information Ratio
0.566
Tracking Error
0.115
Treynor Ratio
0.104
using System;
using QuantConnect;
using QuantConnect.Algorithm;
using QuantConnect.Data.Market;
using QuantConnect.Indicators;

namespace Sandbox
{
    public class ThreeLineBreakChartingExample : QCAlgorithm
    
    {
        private SimpleMovingAverage sma;
        
        //init
        public override void Initialize()
        {
            // set start/end dates of backtest
            SetStartDate(2014, 01, 01);
            SetEndDate(2014, 12, 01);

            // request data
            AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);
            
            // define our 10 period sma
            sma = new SimpleMovingAverage(100);
        }

        private DateTime previous;
        decimal lineBreak;
        decimal previousHigh;
        decimal previousLow;
        int swing;
        private DateTime LastTradeTime;
        
        //ondata
        public void OnData(TradeBars data)
        {
            TradeBar spy;
            
            //check if values exist
            if (data.TryGetValue("SPY", out spy) && spy.Time.Date != previous.Date)
            {
                if(previousHigh > spy.High && swing == -1)
                {
                    lineBreak = spy.High;
                    swing = 1;
                } else
                if(previousLow < spy.Low && swing == 1)
                {
                    lineBreak = spy.Low;
                    swing = -1;
                } else
                {
                    lineBreak = spy.Close;
                    swing = 0;
                }
            
                // pump the data into sma
                sma.Update(spy.Time, lineBreak);
                
                //check outputs for the prootype
                //Log("Time: "+spy.Time+" Values: "+spy.Value);

                // plot sma
                Plot("Indicators", sma);
                Plot("Indicators", lineBreak);
                Plot("Indicators", swing);
                
                previous = spy.Time;
                previousHigh = spy.High;
                previousLow =spy.Low;
                
            }
            
            if(Portfolio["SPY"].HoldStock)
            {
                if((spy.Time - LastTradeTime).TotalHours > 6 && swing== 0)
                {
                    Liquidate("SPY");
                    //Log("Liquidated "+symbol+" because time reached: "+(t.Time - d.LastTradeTime).TotalSeconds+" Last trade time: "+d.LastTradeTime);
                }
            }
            
            //entry rules
            if (!Portfolio.HoldStock && lineBreak > 0)
            {
                SetHoldings("SPY", 1);
                LastTradeTime = spy.Time;
            } else
            if (!Portfolio.HoldStock && lineBreak < sma)
            {
                SetHoldings("SPY", -1);
                LastTradeTime = spy.Time;
            }
        }
    }
}