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.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);
        }
    }
}