I've implemented my own trigger order system with LEAN that places both a stop and a target order when an entry condition is met, and cancels with OCO logic when one is filled.
Crypto assets only allow sell orders up to a max of the position held, so the position can't end up short. This means I can't have both a stop order and a target order active, since that is selling 2x the total position held.
How can I have both orders active at the same time?
Charles Fort
An example, this is the error I get when the second order is placed:
Insufficient buying power to complete order (Value:-176.6991), Reason: Your portfolio holds 0.04779423 BTC, 0.04779423 BTC of which are reserved for open orders, but your Sell order is for 0.04779423 BTC. Cash Modeling trading does not permit short holdings so ensure you only sell what you have, including any additional open orders..
Charles Fort
I figured this out for my use case, I just want to model this OCO behavior so I changed the security's buying power model like this:
var security = AddCrypto(symbol, Resolution.Minute, null, true, 500); security.SetFeeModel(new FreeFeeModel(this)); security.BuyingPowerModel = new SecurityMarginModel();
This allows setting both orders, since I have margin available. I am assuming I can use a lot of leverage in this example because I don't want to think about that right now.
Henry111
Hi, Charles
Do you mean with your above method, we can do Margin account with Crypto, like Binance? I am not familiar with C#, can you help translate above setting to Python if possible?
Â
Thanks,
henry z
Derek Melchin
Hi Henry111,
To enable margin trading with crypto in a Python algorithm, we can use
self.SetBrokerageModel(BrokerageName.Bitfinex, AccountType.Margin) security = self.AddCrypto('BTCUSD', Resolution.Daily) security.BuyingPowerModel = SecurityMarginModel(3.3)
Currently, the margin account type isn't available for the Binance brokerage. 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.
Charles Fort
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!