Overall Statistics
//Copyright HardingSoftware.com, 2018.
//Granted to the public domain.
//Use entirely at your own risk.
using System.Linq;

namespace QuantConnect 
{   
    public class MultiCoinFramework : QCAlgorithm
    {
    	string ticker ="BTCUSD";
    	Resolution resolution=Resolution.Tick;
    	int count = 0;
		SymbolData d = new SymbolData();
		
        public override void Initialize() 
        {
        	//SetBrokerageModel(BrokerageName.GDAX, AccountType.Cash);
            SetStartDate(2017, 3, 1); 
            SetEndDate(2018, 9, 1);
            SetCash(20000);
            Symbol symbol = QuantConnect.Symbol.Create(ticker, SecurityType.Crypto, Market.GDAX);
			AddCrypto(symbol, resolution);
	    }

        public override void OnData(Slice data) 
        {
      		Process(ticker, d, data.Time, data[ticker][data[ticker].Count - 1].LastPrice);
        }
        
        public void Process(string symbol, SymbolData d, DateTime t, Decimal close)
        {
            double mid = (double)close;
            
            if (d.PrevPrice == 0)
            {
                d.PrevPrice = mid;
                d.LastTick = t;
                return;
            }
            
            if (t.Date != d.LastTick.Date)
            {
            	count = 0;
                d.PrevPrice = mid;
                d.LastTick = t;
                return;
            }
            
            double pips = 1;
            
            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 && (t - d.LastTradeTime).TotalSeconds > 120)
            {
                Liquidate();
            }
            
            
            if (buyintensity > 2.5 && buyintensity > sellintensity && !Portfolio[symbol].HoldStock && count < 50)
            {
            	LimitOrder(symbol, 1, close);
                d.LastTradeTime = t;
                d.LastTradePrice = mid;
                count += 1;
            }
            else if (sellintensity > 2.5 && sellintensity > buyintensity && !Portfolio[symbol].HoldStock && (t-d.LastTradeTime).TotalSeconds > 300)
            {
                Liquidate();
                d.LastTradeTime = t;
                d.LastTradePrice = mid;
            }
                

            d.upTick = mid >= d.PrevPrice;
            d.downTick = d.PrevPrice >= mid;
            d.PrevPrice = mid;
            d.LastTick = t;
        }
    }
}
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 {
    public class SymbolData
    {
        private Hawkes i = new Hawkes(1, 1.4, 1.2);
        private Hawkes b = new Hawkes(1, 1.4, 1.2);
        private Hawkes s = new Hawkes(1, 1.4, 1.2);
        
        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 DateTime LastTick { get; set; }
        public bool upTick { get; set; }
        public bool downTick { get; set; }
        public int Qty { get; set; }
        public int Counter { get; set; }
        public bool Event { get; set; }
        public bool Buy { get; set; }
    }
}