Back

Dynamic Crypto universe portfolio

Hi i have a zipline pipline algo(2017-2019) that was long only with binance's data and works only during bull market. 

But i am trying to re-implement with Quantconnect bitfinex's *BTC paris so i can build long/shorts portfolio and hedge during bears market.

  1. .Univers should changes over time with new listing and delisting.
  2. Factor:Momentum/Returns over D days.
  3. Rebalance every R day at the close with Market order at the open: 
  • -longs=Top quantile Momentum with 50% of BTC Equity
  • shorts=Bottom Quantile of Momentum with 50% of BTC Equity

so i far i am stuck with the universe filtering giving me error:

from Alphas.HistoricalReturnsAlphaModel import HistoricalReturnsAlphaModel
from Execution.ImmediateExecutionModel import ImmediateExecutionModel
from Portfolio.EqualWeightingPortfolioConstructionModel import EqualWeightingPortfolioConstructionModel

class ResistanceNadionGearbox(QCAlgorithm):

def Initialize(self):
self.stateData = { };
self.SetStartDate(2019, 1, 19) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
# self.AddEquity("SPY", Resolution.Minute)
self.AddAlpha(HistoricalReturnsAlphaModel(7, Resolution.Daily))

self.SetExecution(ImmediateExecutionModel())

self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())

self.__numberOfSymbols = 100
self.__numberOfSymbolsFine = 5
self.SetUniverseSelection(FineFundamentalUniverseSelectionModel(self.CoarseSelectionFunction, self.FineSelectionFunction, None, None))


def OnData(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
Arguments:
data: Slice object keyed by symbol containing the stock data
'''

# if not self.Portfolio.Invested:
# self.SetHoldings("SPY", 1)

# sort the data by daily dollar volume and take the top 'NumberOfSymbols'
def CoarseSelectionFunction(self, coarse):
# sort descending by daily dollar volume
#sortedByDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True)

# return the symbol objects of the top entries from our sorted collection
#return [ x.Symbol for x in sortedByDollarVolume[:self.__numberOfSymbols] ]
for cf in coarse:
if cf.Symbol.SecurityType == SecurityType.Crypto:
#self.Debug(type(cf))
#self.Debug(type(cf.Symbol))
self.Debug(cf.Symbol.SecurityType)
self.Debug("SecurityType.Crypto={}".format(SecurityType.Crypto))
self.stateData[cf.Symbol] = SelectionData(cf.Symbol, 200)




# sort the data by P/E ratio and take the top 'NumberOfSymbolsFine'
def FineSelectionFunction(self, fine):
# sort descending by P/E ratio
sortedByPeRatio = sorted(fine, key=lambda x: x.ValuationRatios.PERatio, reverse=True)

# take the top entries from our sorted collection
return [ x.Symbol for x in sortedByPeRatio[:self.__numberOfSymbolsFine] ]

 

Update Backtest







0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hi Saber,

Thank you for your question, and sorry for the late reply. To implement your idea, we recommend you use ManualUniverseSelectionModel here. First, from crypto data library, we can find all pairs containing *BTC among all 346 Cryptocurrency Pairs in Bitfinex. Then, for each pair, we create Symbol object using Symbol.Create(symbol, SecurityType.Crypto, Market.Bitfinex) and add all Symbols to a List. Finally, we use self.SetUniverseSelection(ManualUniverseSelectionModel(Symbols)) to manually add them to Universe.

Currently, there is an issue when adding Crypto pairs (see GitHub issue here). Please note that for some crypto pairs (e.g. FUNBTC) we should add a subscription for the corresponding Forex pair (e.g. FUNUSD) since we need to maintain cash in the crypto (e.g. FUN). Therefore, when creating Symbol objects for crypto pairs, we do self.AddForex() as well. Please see the following backtest for details, thank you!

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed