Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
-4.677
Tracking Error
0.605
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Brokerages import *
from QuantConnect.Orders import *


class BasicTemplateCryptoAlgorithm(QCAlgorithm):
    
    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._macd = {}
        self._stoch = {}
        
        self.SetStartDate(2020, 1, 1)  #Set Start Date
        self.SetEndDate(2021, 4, 1)    #Set End Date
        self.SetCash(1000)
        
        # Set Strategy Cash (EUR)
        # EUR/USD conversion rate will be updated dynamically
        # self.SetCash("EUR", 10000)

        # self.SetCash("BTC", 1)
        # self.SetCash("ETH", 5)

        self.SetBrokerageModel(BrokerageName.GDAX, AccountType.Cash)
        
        symbol = self.AddCrypto("BTCUSD", Resolution.Minute).Symbol
        # self.AddCrypto("ETHUSD", Resolution.Minute)
        # self.AddCrypto("BTCEUR", Resolution.Minute)
        
        self.MACD(symbol, 12, 26, 9, MovingAverageType.Simple, Resolution.Minute).Histogram.Updated += self.MacdHistoUpdated
        self.MACD(symbol, 12, 26, 9, MovingAverageType.Simple, Resolution.Minute).Updated += self.MacdUpdated
        self.MACD(symbol, 12, 26, 9, MovingAverageType.Simple, Resolution.Minute).Signal.Updated += self.MacdSignalUpdated
        
        self.macdHistogram = RollingWindow[IndicatorDataPoint](5)
        self.Macd = RollingWindow[IndicatorDataPoint](5)
        self.macdSignal = RollingWindow[IndicatorDataPoint](5)
    
        self.STO(symbol, 14, Resolution.Minute).StochK.Updated += self.StochKUpdated
        self.STO(symbol, 14, Resolution.Minute).StochD.Updated += self.StochDUpdated
        
        self.stochk = RollingWindow[IndicatorDataPoint](5)
        self.stochd = RollingWindow[IndicatorDataPoint](5)
        
        self.SetWarmup(40, Resolution.Minute)
        self.control = False
        
    def MacdSignalUpdated(self, sender, updated):
        self.macdSignal.Add(updated)
    def MacdUpdated(self, sender, updated):
        self.Macd.Add(updated)
    def MacdHistoUpdated(self, sender, updated):
        self.macdHistogram.Add(updated)
    
    def StochKUpdated(self, sender, updated):
        self.stochk.Add(updated)
    def StochDUpdated(self, sender, updated):
        self.stochd.Add(updated)
        
    def OnData(self, data):
        # if not (self._macd["Histogram"].IsReady and self._macd["MACD"].IsReady and self._macd["SIGNAL"].IsReady and self._stoch['StochK'].IsReady and self._stoch['StochD'].IsReady): return
        # and self.macdSignal and self.Macd and self.macdHistogram
        if not (self.IsWarmingUp and self.macdHistogram.IsReady):
            return
    
        if self.macdHistogram[0] < self.macdHistogram[1] and ((self.stochk[0] > self.stochd[0] and self.stochk[1] < self.stochd[1]) or self.stochk[0] > self.stochd[0]) and self.macdHistogram[0] < 0 and self.stochd[0] < 40 and self.control == False:
            self.control = True
            usdTotal = self.Portfolio.CashBook["USD"].Amount
            limitPrice = round(self.Securities["BTCUSD"].Price * 0.95, 2)
            # use only half of our total USD
            quantity = usdTotal * 0.5 / limitPrice
            self.MarketOrder("BTCUSD", quantity)
            
        elif (self.macdSignal[0] > self.Macd[0] and self.macdSignal[1] < self.Macd[1])and self.control == True:
            self.control = False
            limitPrice = round(self.Securities["BTCUSD"].Price * 1.00, 2)
            quantity = self.Portfolio.CashBook["BTC"].Amount
            self.MarketOrder("BTCUSD", -quantity)

        


    def OnOrderEvent(self, orderEvent):
        self.Debug("{} {}".format(self.Time, orderEvent.ToString()))

    def OnEndOfAlgorithm(self):
        self.Log("{} - TotalPortfolioValue: {}".format(self.Time, self.Portfolio.TotalPortfolioValue))
        self.Log("{} - CashBook: {}".format(self.Time, self.Portfolio.CashBook))