Overall Statistics
Total Trades
37
Average Win
0.03%
Average Loss
-0.02%
Annual Return
5.162%
Drawdown
0.100%
Expectancy
0.772
Net Profit
0.594%
Sharpe Ratio
4.6
Loss Rate
32%
Win Rate
68%
Profit-Loss Ratio
1.62
Trade Frequency
Daily trades
using System;
using System.Collections;
using System.Collections.Generic; 

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

    public partial class BasicTemplateAlgorithm : QCAlgorithm, IAlgorithm 
    { 
        string symbol = "EURUSD";        
        
            
        //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(2013,05,01));
            SetEndDate(new DateTime(2013, 05, 30)); 
            SetCash(20000); //Starting Cash in USD.
            AddSecurity(SecurityType.Forex, symbol, Resolution.Tick); //Minute, Second or Tick
            Securities[symbol].Model = new ForexTransactionModel(); // Forex transaction model
            SetRunMode(RunMode.Parallel); //Series or Parallel for intraday strategies.
        }

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

        }

        Hawkes buybuy = new Hawkes(0.5,  1.2, 1.8  );//(0.005, 0.028949, 0.050889);
        Hawkes sellbuy = new Hawkes(0.5, 1.2, 1.8);
        Hawkes sellsell = new Hawkes(0.5, 1.2, 1.8);
        Hawkes buysell = new Hawkes(0.5, 1.2, 1.8);

        Tick prevTick = null;
        int prevsign = -1;
        int buys = 0;
        int sells = 0;
        static Tick lastTick = null;
        //private int prevSign = 0;
        //Handle Tick Events - Only when you're requesting tick data
        public override void OnTick(Dictionary<string, List<Tick>> ticks) 
        {
            if (!ticks.ContainsKey(symbol) || ticks.Values.Count == 0) 
            {
                Log("Invalid Tick");
                return;
            }
    
    
            Tick t = ticks[symbol][ticks.Values.Count -1];
            {
                if (prevTick == null)
                {
                    prevTick = t;
                    buybuy.Process(0, prevTick.Time);
                    buysell.Process(0, prevTick.Time);
                    sellbuy.Process(0, prevTick.Time);
                    sellsell.Process(0, prevTick.Time);
                }
                else
                {
                    decimal bdiff = t.BidPrice - prevTick.BidPrice;
                    decimal adiff = t.AskPrice - prevTick.AskPrice;
                    
                    if (Math.Sign(bdiff) != Math.Sign(adiff)) return;
                    
                    decimal pdiff = bdiff + adiff;
                    pdiff *= 0.5m;
                    double buy = 0;
                    double sell = 0;
                    double bb, sb, ss, bs;
                    double count = (double)Math.Abs(pdiff / 0.00001m);
                    
                    if (pdiff > 0 && prevsign == -1)
                    {                
                        bb = buybuy.Process(0, t.Time);
                        sb = sellbuy.Process(count, t.Time);
                        ss = sellsell.Process(0, t.Time);
                        bs = buysell.Process(0, t.Time);
                        buy = bb + sb;
                        sell = ss + bs;
                        prevsign = 1;
                        prevTick = t;
                        buys = 0;
                        sells = 0;
                    }
                    else if (pdiff > 0 && prevsign == 1)
                    {
                        ++buys;
                        sells = 0;
                        count = count * 2;
                        bb = buybuy.Process(count, t.Time);
                        sb = sellbuy.Process(0, t.Time);
                        ss = sellsell.Process(0, t.Time);
                        bs = buysell.Process(0, t.Time);
                        buy = bb + sb;
                        sell = ss + bs;
                        prevsign = 1;
                        prevTick = t;
                    }
                    else if (pdiff < -0 && prevsign == 1)
                    {
                        bb = buybuy.Process(0, t.Time);
                        sb = sellbuy.Process(0, t.Time);
                        ss = sellsell.Process(0, t.Time);
                        bs = buysell.Process(count, t.Time);
                        buy = bb + sb;
                        sell = ss + bs;
                        prevsign = -1;
                        prevTick = t;
                        buys = 0;
                        sells = 0;
                    }
                    else if (pdiff < -0 && prevsign == -1)
                    {
                        buys = 0;
                        ++sells;
                        count = count * 2;
                        bb = buybuy.Process(0, t.Time);
                        sb = sellbuy.Process(0, t.Time);
                        ss = sellsell.Process(count, t.Time);
                        bs = buysell.Process(0, t.Time);
                        buy = bb + sb;
                        sell = ss + bs;
                        prevsign = -1;
                        prevTick = t;
                    }
        
                    if (Securities[symbol].Holdings.Quantity > 0 && ( sell / buy > 3 || (t.Time - lastTick.Time).TotalSeconds > 240))
                    {
                        Liquidate(symbol);
                    }
                    
                    if (Securities[symbol].Holdings.Quantity < 0 && (buy / sell > 3  || (t.Time - lastTick.Time).TotalSeconds > 240))
                    {
                        Liquidate(symbol);
                    }
                    
                    
                    if (Securities[symbol].Holdings.Quantity <= 0 && prevTick == t && buy / sell > 60)                    
                    {
                        if (Securities[symbol].Holdings.Quantity < 0) Liquidate(symbol);
                        Order(symbol, 10000);
                        lastTick = t;
                    }   
                    
                    if (Securities[symbol].Holdings.Quantity >= 0 && prevTick == t && sell / buy > 60)
                    {
                        if (Securities[symbol].Holdings.Quantity > 0) Liquidate(symbol);
                        Order(symbol, -10000);
                        lastTick = t;
                    }
                 }
            }
       }
    }
}
// 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;
        DateTime prevTime;
        bool first = true;

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

        public double Process( double count, DateTime dt)
        {
            if (first)
            {
                first = false;
                prevTime = dt;
                return mu_;
            }

            double seconds = (dt - prevTime).TotalSeconds;
            double exp = Math.Exp(-beta_ * seconds);
            bfactor_ *= exp;
            bfactor_ += exp * count;
            prevTime = dt;
            return mu_ + alpha_ * bfactor_;
        }
    }
}