Hi everyone,
I am new to the platform and I must say it is amazing.
I have experience in programming and with a friend who has experience in trading we are developing an algorithm.
But we are having difficulty understanding how positions are closed despite the fact that we have commented out the lines inherent in stop loss and take profit.
We are interested in understanding how the positions are closed since, without stop loss and take profit, the win rate is 92%, if we put this algorithm live with a demo account, would the positions be closed equally?
Also I ask you how we can set the stop loss and take profit in a more "smart" way, we would like to understand if it is a bad practice to put the stop and take orders directly under the MarketOrder and what would be the best practice?
Thank you in advance
Mia Alissi
Hello Emanuele,
From the code you provided, it seems that your positions are being closed by either the stop loss or take profit orders that you have set up. In your code, after a market order is placed, a stop market order (stop loss) and a limit order (take profit) are immediately placed. These orders are designed to close the position when the price reaches a certain level.
If you comment out the lines for the stop loss and take profit, the positions will not be closed automatically. They will remain open until you manually close them with a market order in the opposite direction of the open position. If you put this algorithm live with a demo account, the positions would remain open until you manually close them.
As for setting the stop loss and take profit in a more "smart" way, it really depends on your trading strategy. One common approach is to set the stop loss and take profit levels based on the volatility of the market. For example, you could use the Average True Range (ATR) indicator to measure market volatility and set your stop loss and take profit levels as a multiple of the ATR value.
Placing the stop and take orders directly under the MarketOrder is not necessarily a bad practice, but it does mean that your stop loss and take profit levels are fixed at the time of order placement and do not adapt to changing market conditions. One possible improvement could be to update your stop loss and take profit levels as the price moves in your favor. This is known as a trailing stop loss and a trailing take profit.
Here is an example of how you could implement a trailing stop loss in your code:
In this code,
self.highestPrice
andself.lowestPrice
are the highest and lowest prices seen since the position was opened.self.stopMarketTicket
is the order ticket for the stop market order. The stop price is updated whenever a new highest or lowest price is seen.I hope this helps! Let me know if you have any other questions.
Sources:
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.
Emanuele
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!