Overall Statistics
Total Trades
237
Average Win
1.06%
Average Loss
-0.44%
Compounding Annual Return
-13.848%
Drawdown
18.100%
Expectancy
-0.056
Net Profit
-4.393%
Sharpe Ratio
-0.278
Loss Rate
72%
Win Rate
28%
Profit-Loss Ratio
2.40
Alpha
-0.043
Beta
-0.258
Annual Standard Deviation
0.289
Annual Variance
0.083
Information Ratio
-0.692
Tracking Error
0.324
Treynor Ratio
0.311
Total Fees
$237.00
namespace QuantConnect.CSharp.Algorithms


{   
    public class ScalpingAlgorithm : QCAlgorithm
    {
    	private WilliamsFractals _wf;
    
    	
    	private string symbol = "SPXL";
    	
        public override void Initialize() 
        {
            SetStartDate(2016, 4, 1);         
            SetEndDate(2016, 7, 19);
            
            SetCash(12000);
            
            AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
            
            Securities[symbol].FeeModel = new ConstantFeeModel(1.0m);
            
            _wf = new WilliamsFractals();
            
           var fiveMinuteConsolidator= new TradeBarConsolidator(TimeSpan.FromMinutes(35));
            
            fiveMinuteConsolidator.DataConsolidated += FiveMinuteBarHandler;
            
             SubscriptionManager.AddConsolidator(symbol, fiveMinuteConsolidator);
        }

        public void FiveMinuteBarHandler(object sender, TradeBar data)
        {   
        	_wf.Update(data);
        	
            if (_wf.IsReady)
            {
				if (data.Price >= _wf.BarryUp)
				{
					SetHoldings(symbol, -1.0m);
				}
				else if (data.Price <= _wf.BarryDown)
				{
					SetHoldings(symbol, 1.0m);
				}
            }
        }
    }
}
using Accord.Extensions;
using QuantConnect.Data.Market;



namespace QuantConnect.Indicators
{
    public class WilliamsFractals : TradeBarIndicator
    {
        private readonly RollingWindow<TradeBar> _fractal;
        private readonly int _fractalMidIndex;

        private decimal _barryUp;
        private decimal _barryDown;

        public decimal BarryUp => _barryUp;
        public decimal BarryDown => _barryDown;
        public decimal MidPoint => (_barryUp - _barryDown) / 2m;

        public override bool IsReady => _fractal.IsReady;

        public WilliamsFractals(int fractalLength = 5) : this("WilliamsFractals" + fractalLength, fractalLength)
        {
        }

        public WilliamsFractals(string name, int fractalLength = 5) : base(name)
        {
            _fractal = new RollingWindow<TradeBar>(fractalLength);
            _fractalMidIndex = fractalLength / 2 - (fractalLength % 2 == 0 ? 1 : 0);
        }

        protected override decimal ComputeNextValue(TradeBar input)
        {
            _fractal.Add(input);

            if (!_fractal.IsReady) return MidPoint;

            if (_fractal.IndexOfMax((bar, index) => bar.High) == _fractalMidIndex)
            {
                _barryUp = input.High;
            }

            if (_fractal.IndexOfMin((bar, index) => bar.Low) == _fractalMidIndex)
            {
                _barryDown = input.Low;
            }

            return MidPoint;
        }
    }
}