Overall Statistics
Total Trades
1167
Average Win
0.22%
Average Loss
-0.05%
Compounding Annual Return
-3.944%
Drawdown
7.000%
Expectancy
-0.283
Net Profit
-4.060%
Sharpe Ratio
-0.675
Loss Rate
87%
Win Rate
13%
Profit-Loss Ratio
4.72
Alpha
-0.028
Beta
0.015
Annual Standard Deviation
0.04
Annual Variance
0.002
Information Ratio
-0.934
Tracking Error
0.114
Treynor Ratio
-1.851
Total Fees
$2334.00
namespace QuantConnect 
{
    /*
    *   QuantConnect University: 50-10 EMA - Exponential Moving Average Cross
    *   
    *   The classic exponential moving average cross implementation using a custom class.
    *   The algorithm uses allows for a safety margin so you can reduce the "bounciness" 
    *   of the trading to confirm the crossing.
    */
    public class QCUMovingAverageCross : QCAlgorithm 
    { 
        //Define required variables:
        int quantity = 0;
        decimal price = 0;
        decimal tolerance = 0m; //0.1% safety margin in prices to avoid bouncing.
        string symbol = "USDJPY";
      //  DateTime sampledToday = DateTime.Now;
        
        //Set up the EMA Class:
   //     ExponentialMovingAverage emaShort;
   //     ExponentialMovingAverage emaLong;
        SimpleMovingAverage emaShort;
        SimpleMovingAverage emaLong;
        
        
        //Initialize the data and resolution you require for your strategy:
        public override void Initialize() 
        {          
            SetStartDate(2016, 01, 04);
            SetEndDate(DateTime.Now);  
            SetCash(25000);
            AddSecurity(SecurityType.Forex, symbol, Resolution.Minute);
         //   AddForex(symbol, Resolution.Minute);
       //     var minConsolidator = new TradeBarConsolidator(TimeSpan.FromMinutes(5));
        //    minConsolidator.DataConsolidated += OnThirtyMinutes;
            
        //    SubscriptionManager.AddConsolidator(symbol, minConsolidator);
            
            emaShort = new SimpleMovingAverage(10);
            emaLong = new SimpleMovingAverage(50);
            
            RegisterIndicator(symbol, emaShort, TimeSpan.FromMinutes(30));
            RegisterIndicator(symbol, emaLong, TimeSpan.FromMinutes(30));
         //   emaShort = EMA(symbol, 10, Resolution.Daily);
         //   emaLong = EMA(symbol, 50, Resolution.Daily);
         
         
          
        }
        
        private void OnThirtyMinutes(object sender, TradeBar consolidated)
        {
            price = consolidated.Close;
            
         
            //Log(consolidated.Time.ToString("o") + " >> " + Symbol  + ">> LONG  >> 100 >> " + Portfolio[Symbol].Quantity);
            
            // if you want code to run every five minutes then you can run it inside of here
        }
        
        //Handle TradeBar Events: a TradeBar occurs on every time-interval
        public void OnData(TradeBars data) {
            
            //One data point per day:
         //   if (sampledToday.Date == data[symbol].Time.Date) return;
            
            //Only take one data point per day (opening price)
            price = Securities[symbol].Close;
         //   sampledToday = data[symbol].Time;
            
            //Wait until EMA's are ready:
            if (!emaShort.IsReady || !emaLong.IsReady) return;
            
            //Get fresh cash balance: Set purchase quantity to equivalent 10% of portfolio.
            decimal cash = Portfolio.Cash;
            int holdings = Portfolio[symbol].Quantity;
            //quantity = Convert.ToInt32((cash * 0.5m) / price);
            quantity = 10000;
            
            bool longCond = price > emaShort && price > emaLong && (price-emaLong)<60;
            bool shortCond = price < emaShort && price < emaLong && (emaLong-price)<60;
            
            if (holdings > 0 || holdings == 0) {
                //If we're long, or flat: check if EMA crossed negative: and crossed outside our safety margin:
                if (shortCond) 
                {
                    //Now go short: Short-EMA signals a negative turn: reverse holdings
                    if (holdings >0) 
                    {
                    	MarketOrder(symbol, -20000);
                    } else 
                    {
                    	MarketOrder(symbol, -10000);
                    }
                    Log(Time.ToShortDateString() + " > Go Short > Holdings: " + holdings.ToString() + " Quantity:" + quantity.ToString() + " price: " +price+ " maShort: " + emaShort.Samples+" maLong: "+emaLong.Samples);
                }
                
            } else if (holdings < 0 || holdings == 0) {
                //If we're short, or flat: check if EMA crossed positive: and crossed outside our safety margin:
                if (longCond) 
                {
                	if (holdings <0) 
                    {
                    	MarketOrder(symbol, 20000);
                    } else 
                    {
                    	MarketOrder(symbol, 10000);
                    }
                    //Now go long: Short-EMA crossed above long-EMA by sufficient margin
                    Log(Time.ToShortDateString() + "> Go Long >  Holdings: " + holdings.ToString() + " Quantity:" + quantity.ToString() + " price: " +price+ " maShort: " + emaShort.Samples+" maLong: "+emaLong.Samples); 
                }
            }
        }
    }
}