Overall Statistics
Total Trades
261
Average Win
0.74%
Average Loss
-0.23%
Compounding Annual Return
3.264%
Drawdown
7.800%
Expectancy
0.368
Net Profit
12.821%
Sharpe Ratio
0.531
Loss Rate
68%
Win Rate
32%
Profit-Loss Ratio
3.24
Alpha
0.023
Beta
-0.013
Annual Standard Deviation
0.044
Annual Variance
0.002
Information Ratio
0.088
Tracking Error
0.233
Treynor Ratio
-1.776
Total Fees
$127.40
namespace QuantConnect 
{   
    public class ForexMovingAvgCross : QCAlgorithm
    {
    	//Create list of currency pairs to be traded
    	List<string> Pairs = new List<string>
        {
        	"AUDUSD",
        	//"EURCHF",
        	"EURGBP",
        	//"EURJPY",
        	"EURUSD",
        	//"GBPUSD",
        	"NZDUSD",
        	"USDCAD",
        	"USDCHF",
        	"USDJPY"
        };
        
        //Create an instance of the SymbolData object
        List<SymbolData> SymbolData = new List<SymbolData>();
            
        //Initialize the data and resolution you require for your strategy:
        public override void Initialize() 
        {
        	
        	//Override the default fee model with 
        	SetBrokerageModel(BrokerageName.FxcmBrokerage);
			
            //Start and End Date range for the backtest:
            SetStartDate(2007, 4, 1);         
            SetEndDate(2010, 12, 31);
            
            //Cash allocation
            SetCash(100000);
            
            //Set Warmup Period
            //SetWarmUp(TimeSpan.FromDays(100));
            
            //Iterate through the pairs list and prepare data
            foreach (var symbol in Pairs)
            {
            	AddForex(symbol, Resolution.Minute, Market.FXCM);
            	//IEnumerable<TradeBar> bars = History(symbol, 100, Resolution.Daily);
            	var atr = ATR(symbol, 100);
            	var fastMA = SMA(symbol, 10, Resolution.Daily);
            	var slowMA = SMA(symbol, 100, Resolution.Daily);
            	SymbolData.Add(new SymbolData
            	{
            		Symbol = symbol,
            		Atr = atr,
            		FastMA = fastMA,
            		SlowMA = slowMA
            	});
            }
            //IEnumerable<Slice> slices = History(TimeSpan.FromDays(100), Resolution.Daily);
        }

        //Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol.
        public void OnData(TradeBars data) 
        {   
            foreach(var symbolObj in SymbolData)
            {
            	if (!Portfolio[symbolObj.Symbol].Invested)
            	{
            		if (symbolObj.FastMA >= symbolObj.SlowMA)
            		{
            			//SetHoldings(symbolObj.Symbol, symbolObj.TargetLeverage);
            			MarketOrder(symbolObj.Symbol, symbolObj.AdjustedLotSize);
            		}
            		else
            		{
            			//SetHoldings(symbolObj.Symbol, -symbolObj.TargetLeverage);
            			MarketOrder(symbolObj.Symbol, -symbolObj.AdjustedLotSize);
            		}
            	}
            	else if (Portfolio[symbolObj.Symbol].IsLong && (symbolObj.FastMA < symbolObj.SlowMA))
            	{
            		//SetHoldings(symbolObj.Symbol, -symbolObj.TargetLeverage);
            		Liquidate(symbolObj.Symbol);
            		MarketOrder(symbolObj.Symbol, -symbolObj.AdjustedLotSize);
            	}
            	else if (Portfolio[symbolObj.Symbol].IsShort && (symbolObj.FastMA >= symbolObj.SlowMA))
            	{
            		//SetHoldings(symbolObj.Symbol, symbolObj.TargetLeverage);
            		Liquidate(symbolObj.Symbol);
            		MarketOrder(symbolObj.Symbol, symbolObj.AdjustedLotSize);
            	}
            }
        }
    }
    class SymbolData
	{
	    public string Symbol;
        
    	public AverageTrueRange Atr { get; set; }
	    public SimpleMovingAverage FastMA { get; set; }
	    public SimpleMovingAverage SlowMA { get; set; }

		// Calculate the optimal leverage based on ATR measured volatility
		public double TargetLeverage
		{
			get
			{
				return .02D;
			}
		}

		// Calculate the adjusted lot size based on the account balance
    	public int AdjustedLotSize
    	{
        	get
	        {
    	        return (int)Math.Floor(10055d/1000d) * 1000;
        	}
	    }
    }
}