Overall Statistics
Total Trades
277
Average Win
3.68%
Average Loss
-4.98%
Compounding Annual Return
189.831%
Drawdown
39.100%
Expectancy
0.235
Net Profit
257.136%
Sharpe Ratio
1.311
Loss Rate
29%
Win Rate
71%
Profit-Loss Ratio
0.74
Alpha
1.005
Beta
0.002
Annual Standard Deviation
0.767
Annual Variance
0.589
Information Ratio
1.203
Tracking Error
0.776
Treynor Ratio
405.878
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(2016,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, 5);
	    			IsLong = true;
	    		}
	    		else if (close > bb.UpperBand ){
	    			SetHoldings(pair1, -5);
	    			IsLong = false;
	    		}

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