Hey guys,
How do you sell half in a shorting position? These are my lines of code, but the second line seems to be actually using my profits to buy half of what I can buy as opposed to exiting half of my shorting position and taking that profit.
self.MarketOrder(self.symbol.Symbol, -(shares_to_buy)) # Enters here.
self.MarketOrder(self.symbol.Symbol, (held_stocks // 2)) # Supposed to sell half on targets instead of buying.
What am I doing wrong?
Best,
Jesse
Nico Xenox
Hey Jesse Fleming
You could open a order ticket and close half of the quantity from there.
If you're not sure if the order has filled you can also use this:
Meaning that you would buy back the half quantity that you shorted.
Hope it helps ;)
Jesse Fleming
Hey Nico Xenox ! Your service is always greatly appreciated.
I am sorry to report back and say that this does the same thing and has the same issue as before. I am not exiting my short position or buying them back, I am entering a long position at the worst times and using the profits I have from my shorting position to long with.
And now I have two questions for you my friend:
Do you have any other ideas of how to exit a short position?
And,
if your next idea works, how can I combine a longing and shorting strategy together? Is there a way to differentiate the short and long positions in my if statements? I am aware of self.Portfolio.Invested but will it conflate the two rendering it completely useless in a combined algorithm. Because in my if statements, i must be able to say ‘if we are in short position’ and ‘if we are in long position’. Otherwise, I would be getting incorrect entries and notifications constantly.
Thank you so much for your time,
You deserve a raise.
Jesse
Jesse Fleming
Ignore that second question I had. I can use a long toggle and a short toggle to differentiate between the two. Now the only thing I am confused about is the first question I had!
How to buy half of a shorting position in an algorithmic trading platform haha
Looking forward to hearing from you brother!
Nico Xenox
Hey Jesse Fleming,
Yes you're right the quantity on the ticket does not update. But for closing a short position you have to buy back something. I played around a little bit and I will give you the tools for your algo.
What you could do is the following:
This does the job. (NOTE: numbers in the debugger wont change/reload before it enters again the OnData event.)
If you dont sell the half exactly after shorting then there should be no problem ;)
Jesse Fleming
You have outdone yourself once again.
Thank you for your hard work.
I'm sure we will be chatting soon again Mr. Nico
Cheers brother
Jesse Fleming
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!