Hi,
I am creating a portfolio with both crypto and equities
Would like to be able to short crypto (currently only available for bitfinex brokerageModel) in the portfolio
However if I set brokerageModel as bitfinex, I cannot have equities in the portfolio..
Is there anyway to do both long & short for both equities & crypto in the same portfolio? Or workard? :D
Derek Melchin
Hi Pranava,
When backtesting, the default brokerage model will allow trading of all security types. However, if we want to use the specifications in the Bitfinex fee model, we'll need to set the fee model of crypto assets. Refer to these docs for guidance.
In regards to live trading, we only support one connection/portfolio, so it's not possible to hold equities and crypto.
Best,
Derek Melchin
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.
Pranava
Hi Derek,
Thanks for the reply! Yes understand for live trading only support 1 connection. Mainly at the purpose for backtesting for now.
I added SPY (equity) and BTCUSD (crypto, market bitfinex which suppose to allow long & short order).
I did not specify the brokerageModel, thus should be the default brokerage model
self.symbols = [ Symbol.Create("SPY", SecurityType.Equity, Market.USA), Symbol.Create("BTCUSD", SecurityType.Crypto, Market.Bitfinex), ]
When I try to short:
for symbol in self.symbols: if not self.Portfolio[symbol].Invested: self.SetHoldings(symbol, -0.5)
I get this error:
Backtest Handled Error: Unable to compute order quantity of BTCUSD. Reason: The cash model does not allow shorting. Returning null.
My short order for SPY went thru but not the one for BTCUSD.
Anyway to allow shorting for BTCUSD? I am willing to sacrifice the fidelity of the brokerage fees etc. Contemplating creating custom BTCUSD symbol and trading it..
Pranava
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 * import decimal as d import numpy as np class BasicTemplateAlgorithm(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,12, 1) #Set Start Date self.SetEndDate(2018,12,10) #Set End Date self.SetCash(100000) #Set Strategy Cash # self.SetBrokerageModel(BrokerageName.Bitfinex, AccountType.Margin) # allows short selling of crypto # self.SetBrokerageModel(BrokerageName.Default, AccountType.Margin) # self.AddEquity("SPY", Resolution.Daily, Market.USA) # self.AddCrypto("BTCUSD", Resolution.Daily, Market.Bitfinex) # self.AddCrypto("ETHUSD", Resolution.Daily, Market.Bitfinex) self.symbols = [ Symbol.Create("SPY", SecurityType.Equity, Market.USA), Symbol.Create("BTCUSD", SecurityType.Crypto, Market.Bitfinex), Symbol.Create("ETHUSD", SecurityType.Crypto, Market.Bitfinex), ] self.SetUniverseSelection(ManualUniverseSelectionModel(self.symbols) ) self.UniverseSettings.Resolution = Resolution.Daily def OnData(self, data): for key in self.Portfolio.Keys: if data.ContainsKey(key): if not self.Portfolio[key].Invested: self.Debug(str(self.Time) + " trading " + str(key)) self.SetHoldings(key, -0.2)
Attached a code for testing.
I need to use have self.SetBrokerageModel(BrokerageName.Bitfinex, AccountType.Margin) to allow shorting of BTCUSD. However if I set that, I cannot add SPY as a security..
Any help is appreciate!
Derek Melchin
Hi Pranava,
To sell BTC, we first need to have BTC in the cashbook.
self.SetCash('BTC', 100)
We recommend creating orders manually for cryptocurrencies instead of using the SetHoldings method. The SetHoldings method isn't consistent when coins are added to the cashbook.
See the attached backtest for reference.
Best,
Derek Melchin
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.
Pranava
Hi derek thanks for the suggestion. However tat is equivalent to me going long at the start of the algorithm. The returns is affected by the initial btc holdings.
Have implemented a workard using custom crypto data.
Really hope your team can add the support for crypto in a margin mode. So we can combine crypto into a combined long&short portfolio with equities etc.
Thks for the help on this issue!
Derek Melchin
Hi Pranava,
To short BTC, we need to override the default buying power model. We can accomplish this with
btc = self.AddCrypto("BTCUSD") btc.BuyingPowerModel = SecurityMarginModel(3.3)
See the attached backtest for reference.
Best,
Derek Melchin
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.
.ekz.
Thanks for this reponse, Derek Melchin --I'm actually trying to short crypto as well, on Bitfinex.
In your sample above, what exactly does this line do? It's not clear from the docs, which constructor will be invoked. Also, what's the significance of ‘3.3’ ?
Fred Painchaud
Hi ekz,
SecurityMarginModel is a BuyingPowerModel. It “represents a simple, constant margin model by specifying the percentages of required margin”. Here the required margin is set to 0 (default). The constructor called is:
SecurityMarginModel (decimal leverage, decimal requiredFreeBuyingPowerPercent=0)
3.3 is the max leverage you want to use. It should be set according to the max you want and below or equal to the max your broker authorizes.
Fred
.ekz.
Just seeing this response. Thanks Fred Painchaud. I will give this a shot.
@pranava were you able to use this successfully to short in your crypto backtests? What about live? Any lessons/tips/caveats to share?
Jesse Pangburn
Derek Melchin It seems like no one confirmed this working for them, so just figured I'd let people know this worked for me. I was trying to short FETUSD and it wasn't working until I found your comment and added the line suggested:
George Riley
Hi all,
related to this - I used the SecurityMarginModel() as an empty constructor in a CustomSecurityInitializer for crypto.
In the docu I learned that this approach would lead to a non leveraged (1x) buying power. However, default appears to be 2x. Only when I call
my CashBook has the same values it should have according to the trading amounts (1x).
Anyone else can confirm?
Louis Szeto
Hi George
That would be the correct understanding. Just a reminder that it might cause liquidity issue when you're trying to rebalance your positions, so make sure the cashflow inflow comes before the outflow in the same size.
Best
Louis
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.
Pranava
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!