Overall Statistics
Total Trades
1578
Average Win
1.78%
Average Loss
-0.49%
Compounding Annual Return
6069.043%
Drawdown
13.700%
Expectancy
1.100
Net Profit
6069.043%
Sharpe Ratio
6.246
Loss Rate
54%
Win Rate
46%
Profit-Loss Ratio
3.62
Alpha
0
Beta
215.167
Annual Standard Deviation
0.474
Annual Variance
0.225
Information Ratio
6.217
Tracking Error
0.474
Treynor Ratio
0.014
Total Fees
$0.00
import clr
clr.AddReference("System")
clr.AddReference("QuantConnect.Algorithm")
clr.AddReference("QuantConnect.Indicators")
clr.AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
import decimal as d

class MovingCrossAlgorithm(QCAlgorithm):
    '''Basic template algorithm simply initializes the date range and cash'''

    def Initialize(self):
        '''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''

        self.SetStartDate(2017,1,1)  #Set Start Date
        self.SetEndDate(2017,12,31)   #Set End Date
        self.SetCash(100000)         #Set Strategy Cash
        # Find more symbols here: http://quantconnect.com/data
        self.AddCrypto("BTCUSD", Resolution.Hour)
        
        # Set EMAs
        #These will be used for buy / sell signals
        self.fast = self.EMA("BTCUSD", 5, Resolution.Hour);
        self.slow = self.EMA("BTCUSD", 10, Resolution.Hour);
        
        #These will be used to indicate size of position
        self.twentyema = self.EMA("BTCUSD", 20, Resolution.Hour);
        self.fiftyema = self.EMA("BTCUSD", 50, Resolution.Hour);
        self.onehundredema = self.EMA("BTCUSD", 100, Resolution.Hour);
        self.twohundredema = self.EMA("BTCUSD", 200, Resolution.Hour);
        
        # Make sure you're not chasing
        self.longchase = 1;
        self.shortchase = 1;
        
        
    def OnData(self, data):
        
        #If the 10EMA is ready and the 5EMA is below the 10EMA, we can set 
        #self.longchase = 0 so we know we're not chasing a trend that started
        #before the algorithm did
        if self.fast.Current.Value < self.slow.Current.Value and self.slow.IsReady:
            self.longchase = 0
        
        #If the 10EMA is not ready, don't do anything else
        if not self.slow.IsReady:
            return
        
        # define a small tolerance on our checks to avoid bouncing
        tolerance = 0.00015;
        
        #define confidence for position size
        confidence = 0;
        
        if self.Portfolio["BTCUSD"].Quantity == 0: #If we have no position currently
            if self.fast.Current.Value > self.slow.Current.Value * d.Decimal(1 + tolerance): #If the fast EMA is above the slow EMA
                if self.Securities["BTCUSD"].Price > self.fast.Current.Value: #If the price is above the fast EMA
                    if self.longchase == 0: #If we're not chasing
                        if self.Securities["BTCUSD"].Price > self.twentyema.Current.Value: #Increase position size if above the 20EMA
                            confidence += .25
                        if self.Securities["BTCUSD"].Price > self.fiftyema.Current.Value: #Increase position size if above the 50EMA
                            confidence += .25
                        if self.Securities["BTCUSD"].Price > self.onehundredema.Current.Value: #Increase position size if above the 100EMA
                            confidence += .25
                        if self.Securities["BTCUSD"].Price > self.twohundredema.Current.Value: #Increase position size if above the 200EMA
                            confidence += .25
                        self.Debug("LONG "+str(confidence)+" position  >> {0}".format(self.Securities["BTCUSD"].Price))
                        self.SetHoldings("BTCUSD", confidence) #Then initiate a position whose size corresponds to the number of EMAs price is above

        if self.Portfolio["BTCUSD"].Quantity > 0 and self.Securities["BTCUSD"].Price < self.fast.Current.Value: #If we have a position and price is below the fast EMA
            self.Debug("SELL  >> {0}".format(self.Securities["BTCUSD"].Price))
            self.Liquidate("BTCUSD") #Then sell all