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 
{
    /*
    *   QuantConnect University: 50-10 EMA - Exponential Moving Average Cross
    *   
    *   The classic exponential moving average cross implementation using a custom class.
    *   The algorithm uses allows for a safety margin so you can reduce the "bounciness" 
    *   of the trading to confirm the crossing.
    */
    public class QCUMovingAverageCross : QCAlgorithm 
    { 
        //Define required variables:
        int _quantity = 0, _CCIbuysignal = 0, _CCIsellsignal = 0;
        decimal _price = 0;
        decimal _tolerance = 0.03m; //0.1% safety margin in prices to avoid bouncing.
        string _symbol = "SPY";
        DateTime _sampledToday = DateTime.Now;

        
        //Set up the EMA Class:
		

        BollingerBands _bb;
        OnBalanceVolume _OBV;
		MovingAverageConvergenceDivergence _MACDobv;
        
        
        
        //Initialize the data and resolution you require for your strategy:
        public override void Initialize() 
        {          
            SetStartDate(2013, 01, 02);
            SetEndDate(DateTime.Now);  
            SetCash(25000);
            AddSecurity(SecurityType.Equity, _symbol, Resolution.Minute);
            
            _bb = BB(_symbol, 50, 2, MovingAverageType.Exponential, Resolution.Daily);
            _OBV = OBV(_symbol,Resolution.Daily);

			_MACDobv = new MovingAverageConvergenceDivergence(15,15,2,MovingAverageType.Simple).Of(_OBV);
            
            //Chart plotter = new Chart("Plotter");
            
            //plotter.AddSeries(new Series("SPY", SeriesType.Line, index:0));
          

            //AddChart(plotter);
            
        	Chart chart1 = new Chart(_symbol);
            
            chart1.AddSeries(new Series(_symbol, SeriesType.Line, index:0));
            chart1.AddSeries(new Series("Buy", SeriesType.Scatter, index:0));
            chart1.AddSeries(new Series("Sell", SeriesType.Scatter, index:0));
            chart1.AddSeries(new Series("OBV", SeriesType.Line, index:0));
            chart1.AddSeries(new Series("Signal", SeriesType.Line, index:1));
            chart1.AddSeries(new Series("Fast", SeriesType.Line, index:1));
            chart1.AddSeries(new Series("Slow", SeriesType.Line, index:1));
            

            AddChart(chart1);
        }
        
        //Handle TradeBar Events: a TradeBar occurs on every time-interval
        public void OnData(TradeBars data) {
            
            //One data point per day:
            if (_sampledToday.Date == data[_symbol].Time.Date) return;

            //Only take one data point per day (opening price)
            _price = Securities[_symbol].Close;
            _sampledToday = data[_symbol].Time;
            
            //Wait until EMA's are ready:
            if (!_OBV.IsReady || !_MACDobv.IsReady) return;
            
            //Get fresh cash balance: Set purchase quantity to equivalent 10% of portfolio.
            decimal cash = Portfolio.Cash;
            int holdings = Portfolio[_symbol].Quantity;
            _quantity = Convert.ToInt32((cash * 0.5m) / _price);
            
            
            if (holdings > 0 || holdings == 0) 
            {
                //If we're long, or flat: check if EMA crossed negative: and crossed outside our safety margin:

		                    //Order(_symbol, -(holdings + _quantity));
		                    Plot(_symbol, "Sell", _price);
		                    Log(Time.ToShortDateString() + "> Go Short >  Holdings: " + holdings.ToString() + " Quantity:" + _quantity.ToString());

                
            } else if (holdings < 0 || holdings == 0) {
 
		                    Order(_symbol, Math.Abs(holdings) + _quantity);

		                    Plot(_symbol, "Buy", _price);
		                    Log(Time.ToShortDateString() + "> Go Long >  Holdings: " + holdings.ToString() + " Quantity:" + _quantity.ToString());

	                

            }
            
            

    		

            
        }
        
        public override void OnEndOfDay() 
        {
            try 
            {

                //#5 Manual Stock Plotter: Plot price once per day:
                if (_OBV.IsReady && _MACDobv.IsReady)
                {
    		Plot(_symbol, "Price", _price);
    		Plot(_symbol, "OBV", _OBV);
    		Plot(_symbol, "Signal", _MACDobv.Signal);
    		Plot(_symbol, "Fast", _MACDobv.Fast);
    		Plot(_symbol, "Slow", _MACDobv.Slow);

                }
            } 
            catch (Exception err) 
            {
                Error("OnEndOfDay Err:" + err.Message);
            }
        }
    }
}