Overall Statistics
Total Trades
3
Average Win
0%
Average Loss
0%
Compounding Annual Return
6.860%
Drawdown
4.400%
Expectancy
0
Net Profit
0%
Sharpe Ratio
1.004
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.01
Beta
0.432
Annual Standard Deviation
0.068
Annual Variance
0.005
Information Ratio
-1.424
Tracking Error
0.08
Treynor Ratio
0.159
Total Fees
$3.00
namespace QuantConnect 
{
    /*
    *   QuantConnect University: How can I model a basket of securities
    *   
    *   Combining securities into a single symbol.
    */
    public class BasketAlgorithm : QCAlgorithm
    {
        TradeBar _basket = new TradeBar();
        public static List<string> _symbols = new List<string>() { "SPY", "AAPL", "IBM" };
        
        public override void Initialize() 
        {
            SetStartDate(2013, 1, 1);         
            SetEndDate(DateTime.Now.Date.AddDays(-1));
            SetCash(25000); 
            foreach (var symbol in _symbols) 
            {
                AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
            }
        }
        
        //Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol.
        public override void OnData(Slice data)
        {
            if (data.Bars.Count != _symbols.Count) return;
            
            //Build the basket value:
            _basket.Open = (from bar in data.Bars.Values select bar.Open).Sum();
            _basket.High = (from bar in data.Bars.Values select bar.High).Sum();
            _basket.Low = (from bar in data.Bars.Values select bar.High).Sum();
            _basket.Close = (from bar in data.Bars.Values select bar.Close).Sum();
            
            //Basket value here:
            if (!Portfolio.HoldStock)
            {
                BasketBuy(0.5m);
            }
        }
        
        public void BasketBuy(decimal fraction) 
        {
            var symbolFraction = fraction / _symbols.Count;
            foreach(var symbol in _symbols)
            {
                SetHoldings(symbol, symbolFraction);
            }
        }
    }
}