Hello all,
I would like to expand the existing EMA indicator so that it works according to these rules:
go long: when 5 EMA crosses above 9 EMA + 9EMA is not pointing downward (so just up or level)
exit long: when 9 EMA is pointing downward
go short: when 5 EMA crosses below 9 EMA + 9 EMA is not point upward (so just down or level)
exit short: when 9 EMA is pointing upward
Any ideas how I can modify the existing script? I'm by no means a coder, so I'm very much stumped, really.
Many thanks
Thomas
H Huxley Li
I want the same, too.
Fernando Arámburu
Hi, i'm trying to do something similiar... altough i'm still kind of defining what means downward and upward: I mean, x-ward (x = down or up) comparing to what? to the first value in the EMA, to the previous of the current (meaning yesterday for a daily strategy) or what?
I'm trying to make some magic keeping previous values in different structures (list, dictionaries, etc) but yet i'm not getting what I expected. I also look into the project source code to see if I can get that directly on the EMA object (by building a new AdvancedEMA but failed there by now).
Also, I'm not sure if all the magic i'm trying to do couldn't be done in a more simple way....
Stefano Raggi
To compare the value of an indicator to previous values of itself, we can use the RollingWindow class.
The code in the attached backtest implements the algorithm above:
Fernando Arámburu
Stefano, It's amazing how easy you solve this while I was thinking in a very complex solution. Any way, I understand that this works when compared with previous value, but what if I want to compare the direction (up or down) based on a period of time (let's say same value for both EMAs , let's say 10 values ago) or based on the period of each EMA (so, EMA 5 with the value from the first day of the current EMA 5, so five periods ago, while EMA 9 would be determining it's direction based on the value of today and the day from 9 periods ago). Thanks in advance anyway!
Stefano Raggi
To reference older values, you just need to set a larger window size on the RollingWindow and add a lookback parameter to the Rising/Falling extension methods.
Wil Groves
Could anyone share the same code in python? Fairly new to this and would also like to implement.
Rahul Chowdhury
Â
Hi Wil,
Here is a python translation of the code. I did not create a seperate class to house the extension methods, and instead, left it within QCAlgorithm.I highly recommend you also complete the boot camps to learn more about Lean.
Thomas Hendrikx
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!