Hello everyone,
I'm working on a strategy where I have multiple long positions in USDCAD with different entry prices. Here's a simplified example of my challenge:
- I open a long position at a price of 90 with a take-profit of 20 pips.
- The price drops to 60, and I open another long position with a take-profit of 20 pips.
- The price rises to 80, triggering the take-profit for the second position.
Question:
How can I ensure that the take-profit at 80 closes the position opened at 60 and not the one opened at 90?
I know Interactive Brokers and other broker support that natively already; I understand that QuantConnect doesn't natively support attaching take-profit orders to specific entries like some broker APIs do. What is the recommended approach to handle such scenarios in QuantConnect?
Mia Alissi
To handle multiple long positions with different entry prices and ensure that take-profit orders close the correct position in QuantConnect, you can follow these steps:
Initialize Data Structures: Use a dictionary to store each position's entry price and associated take-profit level. This will help you track which position corresponds to which take-profit order.
Place Orders and Track Them: When you open a new position, store its entry price and calculate the take-profit level. Use unique tags or IDs to associate each take-profit order with its corresponding position.
Monitor Order Events: Implement the
on_order_event
method to track when orders are filled. Use theOrderEvent
properties to identify which order was filled and update your data structures accordingly.Close the Correct Position: When a take-profit order is filled, use the stored information to determine which position it corresponds to and update your records to reflect the closed position.
Here's a simplified example of how you might implement this:
Key Points:
on_order_event
method to handle order fills and update your position tracking.This approach allows you to manage multiple positions with different entry prices and ensure that each take-profit order closes the correct position.
Sources: Other Order Types - QuantConnect.com, Handling Data - QuantConnect.com, Stop Market Orders - QuantConnect.com, Head & Shoulders TA Pattern Detection, Limit If Touched Regression Algorithm
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.
Tpang
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!