Back

Trade when Indicator is Triggered

Within my algorithm, I have developed a rolling window where the algorithm trades based off an indicator. I understand that the last piece of that indicator says that if the price is higher than the previous day's close, buy 100 shares. Once the algorithm does that, it will not trade again unless the profit taking % is hit or the stop loss is hit. What I ultimately want for my algorithm is for it to be able to buy 100 shares every time the indicator is triggered, whether I am invested or not. Right now it limits me if I am already invested, to not invest again. If I take out the self.portfolio.notinvested, the algorithm will trade over 100 times and I understand that is because my indicator is dependent on one price being higher than the other. Does anyone know a way to just trade that one time for 100 shares, and if the indicator is triggered again (even if I am already invested), it will invest in 100 more shares? I have tried to write the last part as self.Securities[AAPL].Price == .50 + self.Window[0].Close, but I know that is way too specific and it does not work. Does anyone know any other ways? If you need clarification, please let me know.

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.


Generally, what you want to achieve is to detect a cross-over rather than simply one price being above another price. The general approach for doing that is the following, using an example of a price (P) crossing over a moving average (MA):

  • Check that P(t-1) < MA(t-1) (<-- we can call this the pre-condition)
  • Check that P(t) > MA(t) (<-- we can call this the post-condition)
  • Trade
You need to find a way to translate your buy criteria into a pre-condition and a post-condition, and check that the pre-condition happened just before the post-condition.
0

I definitely think this is a good method, but my main issue is that I would like it to trade when the indicator is triggered even if it is already invested. Are there ways to write that specifically where if you are already invested and the indicator is triggered again, send an order?

0

If you structure your pre- and post-conditions properly, it should not matter whether you're invested or not (i.e. you should be able to remove the "if invested"-check altogether).

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