Overall Statistics
Total Trades
432
Average Win
2.61%
Average Loss
-3.28%
Compounding Annual Return
33.091%
Drawdown
30.100%
Expectancy
0.099
Net Profit
190.110%
Sharpe Ratio
0.758
Loss Rate
39%
Win Rate
61%
Profit-Loss Ratio
0.80
Alpha
0.258
Beta
-0.084
Annual Standard Deviation
0.334
Annual Variance
0.111
Information Ratio
0.545
Tracking Error
0.352
Treynor Ratio
-3.01
Total Fees
$0.00
namespace QuantConnect.MeanReversion
{
    public class MeanReversion : QCAlgorithm
    {
    	string symbol1 = "AUDCAD";
        string symbol2 = "USDCAD";
        RollingWindow<decimal> Close1;
        RollingWindow<decimal> Close2;
    	
        public override void Initialize() 
        {
            SetStartDate(2014, 1, 1); 
            SetEndDate(DateTime.Now);

            SetCash(100000);
            SetBrokerageModel(BrokerageName.OandaBrokerage);
            
            AddSecurity(SecurityType.Forex, symbol1, Resolution.Daily);
            AddSecurity(SecurityType.Forex, symbol2, Resolution.Daily);
            
            Close1 = new RollingWindow<decimal>(5);
            Close2 = new RollingWindow<decimal>(5);
        }    
        public void OnData(QuoteBars data) 
        {
        	if (!data.ContainsKey(symbol1)) return;
        	if (!data.ContainsKey(symbol2)) return;
        	
        	var Open1 = data[symbol1].Open;
        	var Open2 = data[symbol2].Open;
        	
        	var close1 = data[symbol1].Close;
        	Close1.Add(close1);
        	if (!Close1.IsReady) return;
        	var close2 = data[symbol2].Close;
        	Close2.Add(close2);
        	if (!Close2.IsReady) return;
        	
        	var Return1 = Math.Log(Convert.ToDouble(Close1[0])/Convert.ToDouble(Close1[1]));
        	var Return2 = Math.Log(Convert.ToDouble(Close2[0])/Convert.ToDouble(Close2[1]));
        	
        	var AverageReturn = (Return1+Return2)/2;
        	var DemeanedReturn1 = Return1-AverageReturn;
        	var DemeanedReturn2 = Return2-AverageReturn;
        	
        	var spread = (Math.Max(DemeanedReturn1,DemeanedReturn2)-Math.Min(DemeanedReturn1,DemeanedReturn2))*1000;
        	
            if (!Portfolio[symbol1].IsLong && DemeanedReturn1<0 && spread > 4)
            	{
                	SetHoldings(symbol1, 4, false, "Long " + symbol1);
                	SetHoldings(symbol2, -4, false, "Short " + symbol2);
               	}
            if (!Portfolio[symbol2].IsLong && DemeanedReturn1>0 && spread > 4)
                {
                	SetHoldings(symbol2, 4, false, "Long " + symbol2);
                	SetHoldings(symbol1, -4, false, "Short " + symbol1);
            	}
    	}
    }
}