Overall Statistics
Total Trades
11
Average Win
1.50%
Average Loss
-0.98%
Compounding Annual Return
9.221%
Drawdown
7.900%
Expectancy
0.512
Net Profit
9.227%
Sharpe Ratio
0.977
Loss Rate
40%
Win Rate
60%
Profit-Loss Ratio
1.52
Alpha
0.086
Beta
0.047
Annual Standard Deviation
0.095
Annual Variance
0.009
Information Ratio
-0.279
Tracking Error
0.143
Treynor Ratio
1.966
Total Fees
$57.06
using System.Collections.Concurrent;

namespace QuantConnect 
{   
    
    public class RollingWindowAlgorithm : QCAlgorithm
    {
        private const string symbol = "SPY";
        private const int WindowSize = 50;
        
        // the RollingWindow type allows us to easily build history
        private readonly RollingWindow<TradeBar> History = new RollingWindow<TradeBar>(WindowSize);
        
        public override void Initialize()
        {
            SetStartDate(2014, 01, 01);
            SetEndDate(2015, 01, 01);
            
            AddSecurity(SecurityType.Equity, symbol, Resolution.Daily);
        }
        
        public override void OnData(Slice data)
        {
            // verify the new data exists in the slice object
            TradeBar spy;
            if (!data.Bars.TryGetValue(symbol, out spy)) return;
            
            // add the new data to our history
            History.Add(spy);
            if (!History.IsReady) return;
            
            // History is full and ready with data
            
            // History[0] is the most recently added piece of data
            // History[n] is the data added n periods ago
            // History[30] is the data added 30 trading days a
            
            int quantity = Portfolio[symbol].Quantity;
            if (quantity <= 0 && History[0].Close > History[30].Close)
            {
                SetHoldings(symbol, 1.0);
            }
            else if (quantity >= 0 && History[0].Close < History[30].Close)
            {
                SetHoldings(symbol, -1.0);
            }
        }
    }
}