Dear all
I'm new to QC and not a skilled Python programmer. I try to create a strategy using the framework that uses fundamental data to select stocks from various monrningstar sectors. But I also want to generate long OR short insights base on wheter SPY is trending up or down. I fail to understand how to achieve this.
Thus, how can I use SPY and check if the the current price is above or below the SuperTrend while keeping SPY out from my selected trading universe? Any pointers och code snippets would be appreciated.
Johan
Johan Ekh
Anyone?
Lars Klawitter
hi Johan,
I'm routinely using symbols that I don't intend to actually trade (e.g. benchmarks or as in your case to determine the current market regime).
In such a case I would just create an array of excluded symbols during Initialize and check against those symbols before creating orders/emitting insights.
There might be a more elegant way of doing this, but it works.
I can have a look later on and post some snippets, but it's really quite straightforward.
Lars Klawitter
Let's say in Initialize I have created a symbols for SPY, I don't want the algo to trade. I would add the ticker in an array of exclusions:
As I normally maintain an array with all symbols in my universe (to train models etc.), I'd make sure the excluded symbols are not added in OnSecuritiesChanged:
If using the algorithm Framework, Insights would be generated in the Alpha Model's Update Method. So this is where I'd also check for the symbol to be excluded (if using the classic algorithm this would obviously be somewhere in OnData):
That way I'll have SPY in my universe and can use it to extract information, but I can make sure it won't get traded.
Does that make sense?
Nico Xenox
Hey Johan Ekh,
updated post:
adding some links that might help building your algo:
Johan Ekh
Thanks alot! I will read this carefully and see if I can make it work as intended. If not, I will report back and add my code.
Thank you!
Johan Ekh
Hi again
I'm still struggling to get it to work as I want. Basically, I want to convert a long only strategy from the community to a long/short strategy based on the SPY trend. The main.py and the AlphaModel.py are included below in the long only versions. Could anyone indicate how to achive this in a simple way?
Main.py:
AlphaModel.py:
Nico Xenox
Hey Johan Ekh,
I will provide a possible way, it is not said that this will fit you and you're free to change it however you like.
In the FineSelectionFunction(main.py) we filter for equities with values in ROE, NetMargin and EPSRATIO that are higher than 0 because of the “long” strategy. What we could do is delete those so that we also have negative ones that we will short.
After that we will have to rewrite the Update function (AlphaModel.py) to also give Insights into the down direction:
So what is done here is that we take the best ones on the positive side and the worst ones on the negative site:
Hope it helps ;)
Johan Ekh
Thank you very much Nico!
This was very helpful! One last thing that I struggle with is to generate the insights, long or short, depending on the current market trend. I want to go either long OR short depending on the trend but I fail to understand the best way to use an indicator when using the framework. Could you indicate the simplest way to check the trend using SPY and Supertrend?
BR / Johan
Nico Xenox
Hey Johan Ekh,
you could use the SUPER-TREND indicator provided by QC. Then use something like a symbol class to feed the indicator, check if it is ready and update it. After that you will be able to sort the symbols depending on the trend they are in and then use the code that I provided to construct negative and positive insights.
You might want to take a look at the posts in the forum, there are countless examples of this.
Best,
Nico
Johan Ekh
Thank you! I´m trying it and will report back!
Johan
Johan Ekh
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!