Overall Statistics
using System;
using System.Linq;
using QuantConnect.Indicators;
using QuantConnect.Models;

namespace QuantConnect.Algorithm.Examples
{
   
    public class MACDCross : QCAlgorithm
    {
        private const string Symbol = "XAUUSD";

       private RollingWindow<MovingAverageConvergenceDivergence> _win =new RollingWindow<MovingAverageConvergenceDivergence>(1);
       MovingAverageConvergenceDivergence _macd;
        MovingAverageConvergenceDivergence _macd_hist;
        public override void Initialize()
        {
            
            SetStartDate(2017, 01, 01);
            SetEndDate(2017, 07, 17);
             SetCash(10000);
             
         SetBrokerageModel(BrokerageName.OandaBrokerage);
             
            // request SPY data with minute resolution
            AddSecurity( SecurityType.Cfd,Symbol, Resolution.Minute);

            
         // AddSecurity(SecurityType.Forex, Symbol, Resolution.Minute);
            _macd = MACD(Symbol, 12, 26, 9, MovingAverageType.Exponential, Resolution.Daily);
            
          //  _macd_hist = MACD(Symbol, 12, 26, 9, MovingAverageType.Exponential, Resolution.Daily);
            
         
         
          
        }

     
        public  void OnData(QuoteBars  data)
        {

               _win.Add(_macd);
     _macd_hist =_win[0];
            if (!_macd.IsReady || !_macd_hist.IsReady) return;
            Log("started"); 
            
          

           
            var holdings = Portfolio[Symbol].Quantity;
            
          
         
            Log("last_fast " + _macd_hist.Fast); 
            Log("last_slow " +  _macd_hist.Slow); 
           
            if (holdings <= 0)
            {
            	Log("last_fast " +_macd_hist.Fast);
            	Log("last_slow " +_macd_hist.Slow); 
            	Log("fast " +  _macd.Fast);
            	Log("slow " + _macd.Slow); 
                
                if (  _macd.Fast >  _macd.Slow && _macd_hist.Fast <_macd_hist.Slow )
                {
                
                
                    Log("BUY  >> " + Securities[Symbol].Price);
                  
                   MarketOrder(Symbol, 200, false,  "buy 100 XAUUSD");
                  
                }
                
            }
            
            if (holdings >= 0)
            {
            	Log("last_fast " +_macd_hist.Fast);
            	Log("last_slow " +_macd_hist.Slow); 
            	Log("fast " +  _macd.Fast);
            	Log("slow " + _macd.Slow); 
              
               if (  _macd.Fast <  _macd.Slow &&_macd_hist.Fast > _macd_hist.Slow)
              {
              	 
               	 
                    Log("SELL  >> " + Securities[Symbol].Price);
                MarketOrder(Symbol,-200, false,  "sell 100 XAUUSD");
               }
               
               
          }

            
          
           Log("last_fast " +_macd_hist.Fast);
           Log("last_slow " +_macd_hist.Slow); 
           Log("fast " + _macd.Fast);
           Log("slow " + _macd.Slow);  
           Plot("MACD", _macd.Fast, _macd.Slow);
        }
    }
}