Help me with SL and TP

Back

Hi, everybody!

I want to write a simple algo strategy, where I buy a certain stock at market oper or some minutes thereafter and sell at market close. IF the stock moves more than a certain % from the price at the open, I want to both stop loss or take profit, if the % the instrument has moved is either down or up.

 

Let's say, like in this example, that I want to buy AA at market open. If the price at market open is 10$ and the stock goes after opening at 11$, I want to take profit @ 10.1$ given an hypotetical 1% SL-TP target.

If the stock goes to 9$, I want to SL @ 9.9$.

If the stock remains during the day in the range 9.9$-10.1$ , I want to sell all the stock I own at market close (or, like in the code I attached, 2 minutes before).

 

So this is the strategy, in general, but I can't code it properly. The code which I attached doesn't either SL or TP, but just buys at market open and sells what was bought at market open at market close.

 

Can you help me? thanks

Update Backtest








 
0

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.


Up

0

Done good progresses but I fear that the price fluctuates it triggers a stop loss order after a take profit or viceversa...

 

0


Hi Emiliano,

Great start with this algorithm. One thing worth pointing out though is

open = self.Securities[self.symbol].Open

is actually retreiving the open price of the bar from 9:31-9:32AM. To get the daily open, we'd have to adjust the time we call EveryDayAfterMarketOpen to be 1 minute after the open.

self.Schedule.On(self.DateRules.EveryDay(self.symbol),
self.TimeRules.AfterMarketOpen(self.symbol, 1),
self.EveryDayAfterMarketOpen)

Additionally, the algorithm can trigger both Ticket1 and Ticket2 because the active orders aren't updated when one of the orders fills. To implement OCO orders, refer to this related thread.

Best,
Derek Melchin

0

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.


thank you Derek!

0

Update Backtest





0

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.


Loading...

This discussion is closed