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
using System;
using System.Collections.Generic;
using System.Linq;
using QuantConnect.Data;
using QuantConnect.Data.Consolidators;
using QuantConnect.Data.Market;
using QuantConnect.Indicators;

namespace QuantConnect.Algorithm.CSharp
{
    /// Example structure for structuring an algorithm with indicator and consolidator data for many tickers.

    public class MultipleSymbolConsolidationAlgorithm : QCAlgorithm
    {
        /// This is the period of bars we'll be creating
        public readonly TimeSpan BarPeriod = TimeSpan.FromDays(1);
        /// This is the period of our sma indicators
        public readonly int SimpleMovingAveragePeriod = 55;
        /// This is the number of consolidated bars we'll hold in symbol data for reference
        public readonly int RollingWindowSize = 10;
        /// Holds all of our data keyed by each symbol
        public readonly Dictionary<string, SymbolData> Data = new Dictionary<string, SymbolData>();
        /// Contains all of our equity symbols
        public readonly IReadOnlyList<string> EquitySymbols = new List<string>
        {
            "AAPL",
            "SPY",
            "IBM"
        };
        /// Contains all of our forex symbols
        public readonly IReadOnlyList<string> ForexSymbols = new List<string>
        {
            "EURUSD",
            "USDJPY",
            "EURGBP",
            "EURCHF",
            "USDCAD",
            "USDCHF",
            "AUDUSD",
            "NZDUSD",
        };

        /// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
        public override void Initialize()
        {
            SetStartDate(2017, 12, 01);
            SetEndDate(2018, 02, 01);
            // Set cash allocation for backtest
            // In live trading this is ignored and your real account is used.
            SetCash(50000);
            // Specify the OANDA Brokerage: This gives lets us know the fee models & data.
            SetBrokerageModel(BrokerageName.OandaBrokerage);

            // initialize our equity data
            //foreach (var symbol in EquitySymbols)
            //{
            //    var equity = AddEquity(symbol);
            //    Data.Add(symbol, new SymbolData(equity.Symbol, BarPeriod, RollingWindowSize));
            //}

            // initialize our forex data
            foreach (var symbol in ForexSymbols)
            {
                var forex = AddForex(symbol);
                Data.Add(symbol, new SymbolData(forex.Symbol, BarPeriod, RollingWindowSize));
            }

            // loop through all our symbols and request data subscriptions and initialize indicatora
            foreach (var kvp in Data)
            {
                // this is required since we're using closures below, for more information
                // see: http://stackoverflow.com/questions/14907987/access-to-foreach-variable-in-closure-warning
                var symbolData = kvp.Value;

                // define a consolidator to consolidate data for this symbol on the requested period
                var consolidator = symbolData.Symbol.SecurityType == SecurityType.Equity
                    ? (IDataConsolidator)new TradeBarConsolidator(BarPeriod)
                    : (IDataConsolidator)new QuoteBarConsolidator(BarPeriod);

                // define our indicator
                //symbolData.SMA = new SimpleMovingAverage(CreateIndicatorName(symbolData.Symbol, "SMA" + SimpleMovingAveragePeriod, Resolution.Minute), SimpleMovingAveragePeriod);
                symbolData.HA = new HeikinAshi(CreateIndicatorName(symbolData.Symbol, "HA", Resolution.Daily));
                // wire up our consolidator to update the indicator
                consolidator.DataConsolidated += (sender, baseData) =>
                {
                    // 'bar' here is our newly consolidated data
                    var bar = (IBaseDataBar)baseData;
                    // update the indicator
                    //symbolData.SMA.Update(bar.Time, bar.Close);
                    symbolData.HA.Update(bar);
                    // Add the latest result to history
                    symbolData.HA_History.Add(symbolData.HA);
                    // we're also going to add this bar to our rolling window so we have access to it later
                    symbolData.Bars.Add(bar);
                };

                // we need to add this consolidator so it gets auto updates
                SubscriptionManager.AddConsolidator(symbolData.Symbol, consolidator);
            }
        }

        /// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
        /// <param name="data">TradeBars IDictionary object with your stock data</param>
        public override void OnData(Slice data)
        {
            // loop through each symbol in our structure
            foreach (var symbolData in Data.Values)
            {
                // this check proves that this symbol was JUST updated prior to this OnData function being called
                //if (symbolData.IsReady && symbolData.WasJustUpdated(Time))
                //if (symbolData.HA.Open < symbolData.HA.Close) //&& (symbolData.HA[1].Open > symbolData.HA[1].Close))
                if ((symbolData.HA.Open < symbolData.HA.Close) 
                	&& (symbolData.HA_History.Count > 1)
                	&& (symbolData.HA_History[1].Open > symbolData.HA_History[1].Close))
                {
                  MarketOrder(symbolData.Symbol, 1);
                }
            }
        }

        /// End of a trading day event handler. This method is called at the end of the algorithm day (or multiple times if trading multiple assets).
        /// <remarks>Method is called 10 minutes before closing to allow user to close out position.</remarks>
        //public override void OnEndOfDay()
        //{
        //    int i = 0;
        //    foreach (var kvp in Data.OrderBy(x => x.Value.Symbol))
        //    {
                // we have too many symbols to plot them all, so plot ever other
        //        if (kvp.Value.IsReady && ++i%2 == 0)
        //        {
        //            Plot(kvp.Value.Symbol.ToString(), kvp.Value.SMA);
        //        }
        //    }
        //}

        /// Contains data pertaining to a symbol in our algorithm
        public class SymbolData
        {
            /// This symbol the other data in this class is associated with
            public readonly Symbol Symbol;
            /// A rolling window of data, data needs to be pumped into Bars by using Bars.Update( tradeBar ) and
            /// can be accessed like:
            ///  mySymbolData.Bars[0] - most first recent piece of data
            ///  mySymbolData.Bars[5] - the sixth most recent piece of data (zero based indexing)
            public readonly RollingWindow<IBaseDataBar> Bars;
            /// The period used when population the Bars rolling window.
            public readonly TimeSpan BarPeriod;
            /// The simple moving average indicator for our symbol
            public SimpleMovingAverage SMA;
            /// The HeikinAshi indicator for our symbol
            public HeikinAshi HA;
			
			/// The HeikinAshi indicator history for our symbol
            public readonly RollingWindow<HeikinAshi> HA_History;
			
            /// Initializes a new instance of SymbolData
            public SymbolData(Symbol symbol, TimeSpan barPeriod, int windowSize)
            {
                Symbol = symbol;
                BarPeriod = barPeriod;
                Bars = new RollingWindow<IBaseDataBar>(windowSize);
                HA_History = new RollingWindow<HeikinAshi>(windowSize);
            }

            /// Returns true if all the data in this instance is ready (indicators, rolling windows, ect...)
            public bool IsReady
            {
                get { return Bars.IsReady && SMA.IsReady; }
            }

            /// Returns true if the most recent trade bar time matches the current time minus the bar's period, this
            /// indicates that update was just called on this instance
            /// <param name="current">The current algorithm time</param>
            /// <returns>True if this instance was just updated with new data, false otherwise</returns>
            public bool WasJustUpdated(DateTime current)
            {
                return Bars.Count > 0 && Bars[0].Time == current - BarPeriod;
            }
        }
    }
}