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
namespace QuantConnect
{
    public class ConsolidationAlgorithm : QCAlgorithm
    {
        private const decimal stop_loss = 0.25m;
        private const decimal take_profit = 0.50m;

        // Tradebar  quoteBar;

        private const string RootSP500 = Futures.Indices.SP500EMini;
        private readonly HashSet<Symbol> _futureContracts = new HashSet<Symbol>();

        // private decimal new_SL = 0.0m ;
        // private decimal new_TP = 0.0m ;
        // int quantity = 1;
        // int count = 0;
        // int loss= 0 ;
        private TradeBar _spyMinutes;
        public Symbol _symbol = QuantConnect.Symbol.Create(RootSP500, SecurityType.Future, Market.USA);
        private Dictionary<FuturesContract, WilliamsPercentR> _williamsRs;

        public override void Initialize()
        {
            SetStartDate(year: 2013, month: 10, day: 8);
            SetEndDate(year: 2014, month: 6, day: 11);
            SetCash(startingCash: 25000);
            var futureSP500 = AddFuture(RootSP500);
            futureSP500.SetFilter(TimeSpan.Zero, TimeSpan.FromDays(value: 91));
            SetBenchmark(x => 0);
            _williamsRs = _williamsRs = new Dictionary<FuturesContract, WilliamsPercentR>();
        }

        public override void OnData(Slice slice)
        {
            foreach (var chain in slice.FutureChains)
            {
                foreach (var contract in chain.Value)
                {
                    if (!_futureContracts.Contains(contract.Symbol))
                    {
                        _futureContracts.Add(contract.Symbol);
                        var consolidator = new TradeBarConsolidator(TimeSpan.FromHours(12));

                        consolidator.DataConsolidated += OnDataConsolidated;
                        SubscriptionManager.AddConsolidator(contract.Symbol, consolidator);


                        _williamsRs[contract] = (new WilliamsPercentR(14));
                        
                        RegisterIndicator(contract.Symbol, _williamsRs[contract], consolidator);

                        Log("|||||Added new consolidator for " + contract.Symbol.Value);
                    }
                }
            }
        }

        private void OnDataConsolidated(object sender, TradeBar quoteBar)
        {
        	
        	Log("OnDataConsolidated called");

            var indicators = _williamsRs.Where(x => x.Key.Symbol == quoteBar.Symbol).ToList();

            foreach (var willR in indicators)
            {
                if (willR.Key.Expiry < Time)
                {
                    // Drop expired indicators
                    var msg = string.Format("Drop Williams R for {0} at expiration.", willR.Key.Symbol);
                    Log(msg);
                    _williamsRs.Remove(willR.Key);
                }
                else
                {
                	var msg = string.Format("Williams R for {0} is {1:F4}", willR.Key.Symbol, willR.Value.Current.Value);
                   	Log(msg);
                   	
                    if (willR.Value.IsReady)
                    {
                       // Implement your logic here 
                       Log("Strategy Logic run. ");
                    }
                }
            }
        }
    }
}