Overall Statistics
Total Trades
2
Average Win
891.90%
Average Loss
0%
Compounding Annual Return
1042.714%
Drawdown
36.100%
Expectancy
0
Net Profit
891.896%
Sharpe Ratio
2.755
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
0.101
Beta
0.881
Annual Standard Deviation
0.698
Annual Variance
0.487
Information Ratio
-0.451
Tracking Error
0.322
Treynor Ratio
2.183
Total Fees
$0.00
import clr
clr.AddReference("System")
clr.AddReference("QuantConnect.Algorithm")
clr.AddReference("QuantConnect.Indicators")
clr.AddReference("QuantConnect.Common")

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

# shorthands for units
K, M, B = 10**3, 10**6, 10**9

### <summary>
### GDAX Playground.
### </summary>
### <meta name="tag" content="crypto bitcoin GDAX benchmark order PostType playground" />
class MovingAverageCrossAlgorithm(QCAlgorithm):

    def Initialize(self):
        
        #-- Parameters ---------------------------------------------------------
        self.crypto = "BTCUSD"
        self.cash = 10*K
        self.SetStartDate(2017, 01, 01)
        resolution = Resolution.Daily
        #-----------------------------------------------------------------------
        
        self.cash = d.Decimal(self.cash)
        self.SetCash(self.cash)
        self.SetBrokerageModel(BrokerageName.GDAX, AccountType.Cash)
        self.AddCrypto(self.crypto, resolution)
        
        #DefaultOrderProperties = GDAXOrderProperties()
        #DefaultOrderProperties.PostType = True
        
        self.SetBenchmark(SecurityType.Crypto, self.crypto)
        
        self.Schedule.On(self.DateRules.On(2017, 12, 1),    \
                self.TimeRules.At(0, 00),    \
                Action(self.RealizeGains))
        
        self.start = True


    def OnData(self, data):
        
        if self.start:
            
            self.SetHoldingsByLimit(self.crypto, 1.00)
            
            self.start = False
        
        security = self.Securities[self.crypto]
        self.Plot("Price", security.Price)
        self.Plot("Quantity", security.Holdings.Quantity)
        portfolio = self.Portfolio
        self.Plot("Leverage", portfolio.TotalAbsoluteHoldingsCost / portfolio.TotalPortfolioValue)
    
    
    def RealizeGains(self):
       
       self.SetHoldingsByLimit(self.crypto, 0.00)
    
    
    # works like SetHoldings (ratio is of totalPortfolioValue)
    def SetHoldingsByLimit(self, symbol, ratio):
        
        security = self.Securities[symbol]
        if not security.IsTradable:
            self.Debug("{} is not tradable.".format(symbol))
            return    # passive fail
        ratio = d.Decimal(ratio)
        price, quantity = security.Price, security.Holdings.Quantity

        # Keep 0.5%    (for the limit order, rounding errors, and safety)
        totalPortfolioValue = self.Portfolio.TotalPortfolioValue * d.Decimal(0.995)
        
        # +0.1% Limit Order    (to make sure it executes quickly)
        limitPrice = price * d.Decimal(1.001)
        desiredQuantity = totalPortfolioValue * ratio / price
        orderQuantity = desiredQuantity - quantity
        self.LimitOrder(self.crypto, orderQuantity, limitPrice)


# END