Overall Statistics
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Python import PythonQuandl
from datetime import datetime, timedelta

class QuandlCryptoAlgorithm(QCAlgorithm):

    def Initialize(self):
        ''' Initialize the data and resolution you require for your strategy '''
        self.SetStartDate(2014, 1, 1)
        self.SetEndDate(datetime.now().date() - timedelta(1))
        self.SetCash(25000);

        # Symbol corresponding to the Quandl code
        self.BTCUSD = "BITFINEX/BTCUSD"
        self.AddData(QuandlCrypto, self.BTCUSD, Resolution.Daily)
        
        ## Set Boilinger Bands
        self.bband = self.BB(self.BTCUSD, 20, 2, MovingAverageType.Simple, Resolution.Daily)

        # Set WarmUp period
        self.SetWarmUp(20)
        
    def OnData(self, data):
        #if self.Portfolio.HoldStock: return
        if not (data.ContainsKey("BITFINEX/BTCUSD")): return

        price = data["BITFINEX/BTCUSD"].Price

        ## Sell if price is lower than lower band
        if not self.Portfolio["BITFINEX/BTCUSD"].Invested and price < self.bband.LowerBand.Current.Value:
            self.SetHoldings(self.BTCUSD,-1)
        ## Liquidate if price is higher than middle band        
        if self.Portfolio["BITFINEX/BTCUSD"].Invested and price > self.bband.MiddleBand.Current.Value:
                self.Liquidate()
     

class QuandlCrypto(PythonQuandl):
    def __init__(self):
        self.ValueColumnName = "Last"