Overall Statistics
Total Trades
549
Average Win
5.83%
Average Loss
-9.71%
Compounding Annual Return
5.987%
Drawdown
89.900%
Expectancy
0.075
Net Profit
13.621%
Sharpe Ratio
0.691
Loss Rate
33%
Win Rate
67%
Profit-Loss Ratio
0.60
Alpha
1.053
Beta
-0.265
Annual Standard Deviation
1.499
Annual Variance
2.247
Information Ratio
0.646
Tracking Error
1.506
Treynor Ratio
-3.913
Total Fees
$0.00
namespace QuantConnect 
{   
    
    public class BasicTemplateAlgorithm : QCAlgorithm
    {
    	//public string pair1 = "EURAUD"; //2015 200window 
    	public string pair1 = "NZDJPY";
    	RollingWindow<double> window;
    	RollingWindow<double> diffWindow;
    	const int WL = 50;
    	bool IsLong = false;
    	BollingerBands bb;
    	List<OrderTicket> pendingOrders;
    	
        public override void Initialize() 
        {
        	SetStartDate(2015,01,01);
            SetEndDate(2016, 12, 30);              
            
            //SetBrokerageModel(BrokerageName.FxcmBrokerage);
            SetBrokerageModel(BrokerageName.OandaBrokerage);
            
            SetCash(10000);
            
            
            var res = Resolution.Hour;
            AddForex(pair1, res, Market.Oanda);
            //AddForex(pair1, res, Market.FXCM);
            bb = BB(pair1, WL, 1.0M, MovingAverageType.Simple, res);
            
            window = new RollingWindow<double>(WL);
            diffWindow = new RollingWindow<double>(WL);
            pendingOrders = new List<OrderTicket>();
            
            SetWarmup(WL); 
        }
        
        private double getAverage(RollingWindow<double> window){
           var doubleList = new List<double>();
		   for (int i = 0; i < WL; i++) doubleList.Add(window[i]);	
		   return doubleList.Average(); 
        }
        
		private double getStandardDeviation(RollingWindow<double> window) 
		{  
		   var doubleList = new List<double>();
		   for (int i = 0; i < WL; i++) doubleList.Add(window[i]);
		   double average = doubleList.Average();  
		   double sumOfDerivation = 0;  
		   foreach (double value in doubleList)  
		   {  
		      sumOfDerivation += (value) * (value);  
		   }  
		   double sumOfDerivationAverage = sumOfDerivation / (doubleList.Count - 1);  
		   return Math.Sqrt(sumOfDerivationAverage - (average*average));  
		}  
        
	    public void OnData(TradeBars data) {
			window.Add((double)(decimal)data[pair1].Close);
			if(!window.IsReady) return;
			if(!bb.IsReady) return;
			
			
			var sm = getAverage(window);
		   	var diff = (double)(data[pair1].Close) - sm; 
		   	
		   	diffWindow.Add(diff);
		   	if(!diffWindow.IsReady) return;
		   	if (IsWarmingUp) return;
	    	
	    	var std = getStandardDeviation(diffWindow);

	    	Log("diff: " + diff.ToString());
	    	Log("std: " + std.ToString());
	    	
	    	var close = (double)data[pair1].Close;
	    	
	    	if(!Portfolio.Invested){
	    		
	    		if(close < bb.LowerBand){
	    			SetHoldings(pair1, 10);
	    			IsLong = true;
	    		}
	    		else if (close > bb.UpperBand ){
	    			SetHoldings(pair1, -10);
	    			IsLong = false;
	    		}

	    	}
	    	else{
	    		// Average criss cross
	    		if(IsLong && close > sm + 0.5* std)
	    			Liquidate();
	    		else if (!IsLong && close < sm - 0.5*std)
	    			Liquidate();
	    	}
			
	    	
        }
        
	  
    }
}