Overall Statistics
Total Trades
683
Average Win
0.10%
Average Loss
-0.13%
Annual Return
17.191%
Drawdown
0.400%
Expectancy
0.674
Net Profit
78.497%
Sharpe Ratio
4.297
Loss Rate
6%
Win Rate
94%
Profit-Loss Ratio
0.78
Alpha
0.129
Beta
-0.008
Annual Standard Deviation
0.03
Annual Variance
0.001
Information Ratio
-0.19
Tracking Error
0.166
Treynor Ratio
-16.922
/*
* QUANTCONNECT.COM - Equity Transaction Model
* Default Equities Transaction Model
*/

/**********************************************************
* USING NAMESPACES
**********************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuantConnect.Securities {

    /******************************************************** 
    * QUANTCONNECT PROJECT LIBRARIES
    *********************************************************/
    using QuantConnect.Models;


    /******************************************************** 
    * CLASS DEFINITIONS
    *********************************************************/
    /// <summary>
    /// Default Transaction Model for Equity Security Orders
    /// </summary>
    public class CustomTransactionModel : SecurityTransactionModel {

        /******************************************************** 
        * CLASS PRIVATE VARIABLES
        *********************************************************/


        /******************************************************** 
        * CLASS PUBLIC VARIABLES
        *********************************************************/

        /******************************************************** 
        * CLASS CONSTRUCTOR
        *********************************************************/
        /// <summary>
        /// Initialise the Algorithm Transaction Class
        /// </summary>
        public CustomTransactionModel() {

        }

        /******************************************************** 
        * CLASS PROPERTIES
        *********************************************************/

        /// <summary>
        /// Get the Slippage approximation for this order:
        /// </summary>
        public override decimal GetSlippageApproximation(Security security, Order order) {
            return 0.15m;
        }

    } // End Algorithm Transaction Filling Classes

} // End QC Namespace
// QuantConnect Simulator C# File, Created on 3-6-2014 by Satyapravin Bezwada
using System;
using System.Collections;
using System.Collections.Generic;

namespace QuantConnect {
    public class Hawkes
    {
        double mu_ = 0, alpha_ = 0, beta_ = 0, bfactor_ = 0;

        public Hawkes(double mu, double alpha, double beta)
        {
            mu_ = mu;
            alpha_ = alpha;
            beta_ = beta;
        }

        public double Process( double count, bool decay)
        {
            double exp = Math.Exp(-beta_);
            
            if (decay) bfactor_ *= exp;
            bfactor_ += exp * count;
            return mu_ + alpha_ * bfactor_;
        }
    }
}
using System;
using System.Collections;
using System.Collections.Generic; 

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

    
    public partial class BasicTemplateAlgorithm : QCAlgorithm, IAlgorithm 
    { 
        string SPY = "SPY";        
        Dictionary<string, SymbolData> symbols = new Dictionary<string, SymbolData>();
        //int sizeToTake = 5;
        //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(new DateTime(2010,01,01));
            SetEndDate(new DateTime(2014, 07,25)); 
            SetCash(25000); //Starting Cash in USD.
            
            symbols.Add(SPY, new SymbolData());

            symbols[SPY].TickSize = 0.01;
        

            AddSecurity(SecurityType.Equity, SPY, Resolution.Second); //Minute, Second or Tick
            Securities[SPY].Model = new CustomTransactionModel();
        }
        
       //Handle TradeBar Events: a TradeBar occurs on a time-interval (second or minute bars)
        public override void OnTradeBar(Dictionary<string, TradeBar> data) 
        {
            foreach(var symbol in symbols.Keys)
            {
                if (data.ContainsKey(symbol))
                {
                    Process(symbol, symbols[symbol], data[symbol]);
                }
            }            
        }

        //Handle Tick Events - Only when you're requesting tick data
        public override void OnTick(Dictionary<string, List<Tick>> ticks) 
        {
            foreach(var symbol in symbols.Keys)
            {
                if (ticks.ContainsKey(symbol) && ticks[symbol].Count > 0)
                {
                    //Process(symbol, symbols[symbol], ticks[symbol][ticks[symbol].Count - 1]);
                }
            }
        } 
        
        public void Process(string symbol, SymbolData d, TradeBar t)
        {
            double mid = (double)t.Close;//(t.Price);
            
            if (d.PrevPrice == 0)
            {
                d.PrevPrice = mid;
                d.LastTick = t.Time;
                return;
            }
            
            if (t.Time.Date != d.LastTick.Date)
            {
                d.PrevPrice = mid;
                d.LastTick = t.Time;
                return;
            }
            
            //if (t.AskPrice - t.BidPrice > (decimal)(3 * d.TickSize)) return;
            
            double pips = Math.Abs(d.PrevPrice - mid)/d.TickSize;
            
            if (pips > 100) return;
            double buyintensity = 0;
            double sellintensity = 0;
            
            if (d.PrevPrice > mid)
            {
                buyintensity = d.BuyIntensity.Process(0, true);
                sellintensity = d.SellIntensity.Process(pips, !d.downTick);
            }
            else if (mid > d.PrevPrice)
            {
                buyintensity = d.BuyIntensity.Process(pips, !d.upTick);
                sellintensity = d.SellIntensity.Process(0, true);
            }
            else
            {
                buyintensity = d.BuyIntensity.Process(0, true);
                sellintensity = d.SellIntensity.Process(0, true);
            }
            
            if (Portfolio[symbol].HoldStock)
            {
                if ((t.Time - d.LastTradeTime).TotalSeconds > 60)
                {
                    d.LastTradePrice = 0;                    
                    Liquidate(symbol);
                }
            }
            
            {
                if (buyintensity > 10 && buyintensity > sellintensity && !Portfolio[symbol].HoldStock)
                {
                    Order(symbol, -100);
                    d.Qty = 1;
                    d.LastTradePrice = mid;
                    d.LastTradeTime = t.Time;
                    //Debug(t.Time.ToString("yyyyMMdd HH:mm:ss.fff"));
                }
                else if (sellintensity > 10 && sellintensity > buyintensity && !Portfolio[symbol].HoldStock)
                {
                    Order(symbol, 100);
                    d.Qty = -1;
                    d.LastTradePrice = mid;
                    d.LastTradeTime = t.Time;
                    //Debug(t.Time.ToString("yyyyMMdd HH:mm:ss.fff"));
                }
            }
            
            d.upTick = mid > d.PrevPrice;
            d.downTick = d.PrevPrice > mid;
            d.PrevPrice = mid;
            d.LastTick = t.Time;
        }
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using QuantConnect.Securities;
using QuantConnect.Models;

namespace QuantConnect {

    public class SymbolData
    {
        private Hawkes i = new Hawkes(1, 1.2, 1.8);
        private Hawkes b = new Hawkes(1, 1.2, 1.8);
        private Hawkes s = new Hawkes(1, 1.2, 1.8);
        
        public double LastTradePrice { get; set; }
        public DateTime LastTradeTime { get; set; }
        public Hawkes Intensity { get { return i; }  }
        public Hawkes BuyIntensity { get { return b; } }
        public Hawkes SellIntensity { get { return s; } }
        public double TickSize { get; set; }
        public double PrevPrice { get; set; }
        public bool upTick { get; set; }
        public bool downTick { get; set; }
        public DateTime LastTick { get; set; }  
        public int Qty { get; set; }
    }
}