| Overall Statistics |
|
Total Trades 308 Average Win 1.64% Average Loss -0.79% Compounding Annual Return 5510.265% Drawdown 23.000% Expectancy 0.841 Net Profit 170.679% Sharpe Ratio 6.101 Loss Rate 40% Win Rate 60% Profit-Loss Ratio 2.08 Alpha 0 Beta 208.535 Annual Standard Deviation 0.467 Annual Variance 0.218 Information Ratio 6.072 Tracking Error 0.467 Treynor Ratio 0.014 Total Fees $77065.18 |
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.SetBrokerageModel(BrokerageName.GDAX, AccountType.Cash)
self.SetStartDate(2018,1,1) # Set Start Date
self.SetEndDate(2018,3,23) # Set End Date
self.SetCash(100000) # Set Strategy Cash
# Find more symbols here: http://quantconnect.com/data
self.AddCrypto("BTCUSD", Resolution.Hour)
self.SetWarmUp(200) #Warm up 200 bars for all subscribed data.
# 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);
def OnData(self, data):
# 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;
#Debug data
self.Debug("Current Position "+str(self.Portfolio.CashBook["BTC"].Amount)+" BTC")
self.Debug("The price is now $"+str(self.Securities["BTCUSD"].Price))
self.Debug("5EMA: "+str(self.fast.Current.Value))
self.Debug("10EMA: "+str(self.slow.Current.Value))
self.Debug("10EMA adjusted: "+str(self.slow.Current.Value * d.Decimal(1 + tolerance)))
self.Debug("20EMA: "+str(self.twentyema.Current.Value))
self.Debug("50EMA: "+str(self.fiftyema.Current.Value))
self.Debug("100EMA: "+str(self.onehundredema.Current.Value))
self.Debug("200EMA: "+str(self.twohundredema.Current.Value))
if self.Portfolio.CashBook["BTC"].Amount == 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.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
if confidence > 0:
self.SetHoldings("BTCUSD", confidence) # Then initiate a position whose size corresponds to the number of EMAs price is above
self.Debug("Initiated a "+str(confidence)+" position")
if self.Portfolio.CashBook["BTC"].Amount > 0 and self.Securities["BTCUSD"].Price < self.fast.Current.Value: # If we have a position and price is below the fast EMA
self.Liquidate("BTCUSD") # Then sell all
self.Debug("LIQUIDATE")