Overall Statistics
Total Trades
5422
Average Win
0.01%
Average Loss
-0.01%
Annual Return
-38.407%
Drawdown
36.400%
Expectancy
-0.956
Net Profit
-36.578%
Sharpe Ratio
-28.4
Loss Rate
98%
Win Rate
2%
Profit-Loss Ratio
1.34
Trade Frequency
Hourly trades
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace QuantConnect 
{
    using QuantConnect.Securities;
    using QuantConnect.Models; 

    /**
     * Simple Moving Average Cross
    **/
    public partial class BasicTemplateAlgorithm : QCAlgorithm, IAlgorithm 
    { 

        string symbol = "XLF";
        List<decimal> fast_sma_values = new List<decimal>();
        List<decimal> slow_sma_values = new List<decimal>();
        int fast_sma_period = 50;
        int slow_sma_period = 200;
        decimal last_fast_sma = Decimal.Zero;
        decimal last_slow_sma = Decimal.Zero;       

        //Initialize the data and resolution you require for your strategy:
        public override void Initialize() 
        {
            //Initialize the start, end dates for simulation; cash and data required.
            SetStartDate(2013, 06, 01);
            SetEndDate(DateTime.Now.Date.AddDays(-1)); 
            SetCash(30000); //Starting Cash in USD.
            AddSecurity(SecurityType.Equity, symbol, Resolution.Tick); //Minute, Second or Tick
            SetRunMode(RunMode.Parallel); //Series or Parallel for intraday strategies.
        }

        //Handle Tick Events - Only when you're requesting tick data
        public override void OnTick(Dictionary<string, List<Tick>> ticks) 
        {
            foreach (Tick tick in ticks[symbol]) {
                
                //get current values of sma
                decimal current_fast_sma = __getFastSma(tick.Price);
                decimal current_slow_sma = __getSlowSma(tick.Price);

                if (!last_fast_sma.Equals(Decimal.Zero) && !last_slow_sma.Equals(Decimal.Zero) &&
                    !current_fast_sma.Equals(Decimal.Zero) && !current_slow_sma.Equals(Decimal.Zero))
                {
                    //check fast cross over slow
                    if ((last_fast_sma < last_slow_sma) && (current_fast_sma > current_slow_sma))
                    {
                        //BUY
                        if ((!Securities.ContainsKey(symbol)) || (Securities[symbol].Holdings.Quantity == 0)) {
                            Order(symbol, 50, OrderType.Market);
                        }
                        //BUY or EXIT SHORT
                        //Order(symbol, 50, OrderType.Market);
                    }
                    //check slow cross over fast
                    else if ((last_fast_sma > last_slow_sma) && (current_fast_sma < current_slow_sma))
                    {
                        //SELL
                        if ((Securities.ContainsKey(symbol)) && (Securities[symbol].Holdings.Quantity > 0)) {
                            Order(symbol, -50, OrderType.Market);
                        }
                        //SHORT SELL or SELL
                        //Order(symbol, -50, OrderType.Market);
                    }
                }

                //updates last values of sma
                last_fast_sma = current_fast_sma;
                last_slow_sma = current_slow_sma;
            }
            
            if (!ticks.ContainsKey(symbol)) {
                Debug("OtherSymbols=" + ticks.Keys);  
            }
        }
        
        /* get the current value of fast sma*/
        public decimal __getFastSma(decimal price)
        {
            fast_sma_values.Add(price);
            //check count
            if (fast_sma_values.Count <= fast_sma_period) {
                return Decimal.Zero;
            }
            //calcule sma
            fast_sma_values.RemoveAt(0);
            return (fast_sma_values.Sum() / fast_sma_values.Count);            
        }
        
        /* get the current value of slow sma*/
        public decimal __getSlowSma(decimal price)
        {
            slow_sma_values.Add(price);
            //check count
            if (slow_sma_values.Count <= slow_sma_period) {
                return Decimal.Zero;
            }
            //calcule sma
            slow_sma_values.RemoveAt(0);
            return (slow_sma_values.Sum() / slow_sma_values.Count);           
        }


        //Handle TradeBar Events: a TradeBar occurs on a time-interval (second or minute bars)
        public override void OnTradeBar(Dictionary<string, TradeBar> data) {}

    }
}