| Overall Statistics |
|
Total Trades 209 Average Win 1.41% Average Loss -0.61% Compounding Annual Return 8077.096% Drawdown 10.600% Expectancy 1.148 Net Profit 103.779% Sharpe Ratio 7.35 Loss Rate 36% Win Rate 64% Profit-Loss Ratio 2.33 Alpha 0.319 Beta 204.372 Annual Standard Deviation 0.421 Annual Variance 0.177 Information Ratio 7.318 Tracking Error 0.421 Treynor Ratio 0.015 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(2018,1,1) #Set Start Date
self.SetEndDate(2018,2,28) #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