I am trying to figure out how to sell securities during the day while having a daily resolution for my indicators. For example, let's say my indicator gave me a signal for tomorrow to buy SPY. The backtest below does the right job of buying SPY at the opening price. In addition, I have added a profit target and stop loss orders which should be executed as soon as the target or stop loss has been hit, even if it is during the trading day. For example, let's say I buy SPY at the open at 300. During the day, it goes up to 304. My profit target is 1 dollar, so the algo should exit during the day as soon as the price goes above 301. Currently, the algo is waiting till the end of the day and sending the close order on the following day even when the profit target or stop loss was hit on the same day. Could someone please guide me on how to resolve this issue?
Nico Xenox
Hey Rishab Maheshwari,
you could add a scheduled event that fires each minute. It would look something like this:
Hope it helps ;)
Best,
Nico
Rishab Maheshwari
I get what you are trying to do with the one minute, but is there any way to get a smaller time frame? Currently, in my brokerage, I can set up an OCO order which will exit as soon as the stop loss or target price is met without having to wait a minute. Can this functionality be tested on QuantConnect?
Nico Xenox
Hey Rishab Maheshwari,
you could try to do a scheduled event that is only seconds. Another method would be to change the resolution of the equity to minute or seconds. Then the OnData gets called each second/minute. For the buy logic, you could use a daily consolidator. Here's a boot camp lesson that talks about the consolidators.
Best,
Nico
Rishab Maheshwari
Hey Nico Xenox,
If I was to implement the following code
how would I be able to get the current price of SPY each minute since when I added SPY as equity I used the following line:
Nico Xenox
Hey Rishab Maheshwari,
as I mentioned if you want to work with multiple resolutions you will have to add consolidators to your code.
I attached a working code.
Please accept this if you're satisfied with my answer.
Best,
Nico
Rishab Maheshwari
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!