from AlgorithmImports import *
class CoinAPIDataAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 6, 1)
self.SetEndDate(2020, 7, 1)
self.SetCash(100000)
self.SetCash("BTC", 1000)
# Kraken accepts both Cash and Margin type account.
self.SetBrokerageModel(BrokerageName.Bitfinex, AccountType.Margin)
# Warm up the security with the last known price to avoid conversion error
self.SetSecurityInitializer(lambda security: security.SetMarketPrice(self.GetLastKnownPrice(security)))
self.UniverseSettings.Resolution = Resolution.Daily
# Add universe selection of cryptos based on coarse fundamentals
self.AddUniverse(CryptoCoarseFundamentalUniverse(Market.Bitfinex, self.UniverseSettings, self.UniverseSelectionFilter))
self.AddAlpha(ConstantAlphaModel(InsightType.Price, InsightDirection.Up, timedelta(days=1), 0.025, None))
self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
def UniverseSelectionFilter(self, crypto_coarse):
return [d.Symbol for d in sorted([x for x in crypto_coarse if x.VolumeInUsd], key=lambda x: x.VolumeInUsd, reverse=True)[:5]]
def OnSecuritiesChanged(self, changes):
for security in changes.AddedSecurities:
# Historical data
history = self.History(security.Symbol, 30, Resolution.Daily)
self.Debug(f"We got {len(history)} items from our history request")