Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Newbie to Python - Need help in coding this simple snippet

Hi all,
I am new to Python, though have been coding trading algos in Amibroker for a number of years now. I am trying to get to grips with Python / Numpy / Pandas but it'll be a while before I can express some complex logic in Python. I have been going through the basic samples and can manipulate/add basic logic to these but struggle with anything requiring more complexity. I'll appreciate some pointers in achieving a simple task.

I need a variable 'trend' to be defined on all the bars - the way 'trend' takes its values is -

numBarsUp is the number of bars since 10 period RSI was higher than 55 for two consecutive bars,
numBarsDown is the number of bars since 10 period RSI was lower than 45 for two consecutive bars.

if numBarsUp is less than numBarsDown then 'trend' equals 1
else if numBarsUp is greater than numBarsDown then 'trend' equals -1,
and for initial bars when none of these two conditions has been met - then 'trend' equals 0.

Appreciate all the help.

10 responses

I think this does what you describe. I just used the built-in ta-lib function for RSI, so be aware that this will calculate RSI based on minute data if you run in minute mode... probably not what you want.

Thanks Ray, this is very helpful. What I don't get is how trend gets a value other than 0,1 and -1 at places - it seems to take a value of 0.2, -0.2, 0.6, -0.6 at places! A good starting point for me nonetheless to experiment further.

Zoom in :) Those fractional values seem to be a result of Quantopian doing some interpolation for the summary chart.

Yep - Zooming in did the trick. Thanks.

Also be aware that RSI is calculated in a way you may or may not expect. Some sites use a moving average of the up or down day to calculate RS. I think the library used here does not .

So for example, if the stock prices do this:
2.0
2.50
1.50
2.0

Some sites will make a matrix of gains and losses:

na     na  
0.50   na  
na     1.00  
0.50   na  

Now, some places will calculate the 3-day RA using a rolling mean:
0.50 / 1.00

But the QA library (I think) uses the raw numbers:
0.50 + 0.50 / 1.0

I have a function that uses a moving average, if it interests you. I can't seem to paste it here - if it interests you I can paste it into another backtest.

I personally am used to RSI calculation without any averaging - so this is what I expected - however, for learning purposes I won't mind looking at the calculation that uses a rolling mean.

This backtest has the function that I was referring to. I end up avoiding the built-in TA-Lib stuff because in minute mode it operates on minute data, when I'm usually interested in daily data. This function lets you choose whether or not to use a moving average (ma=True/False).

I hope I'm not too late to the party but I was searching the community posts about python, since that's the only language I know, and about trends since my future trading techniques will be based on trends, so naturally this thread caught my attention. I took Ray's source code from the 1st post and played around with it on some stocks I follow and changed the RSI values in handle_data section and the RSI time period to see what seemed to work best and I liked what I saw. I added on to his work, for a more short term trader, a mean- reversion buying parameter by duplicating the 10 day RSI and making a 3 day RSI to find short term counter moves to the longer, more powerful trend. Then the buying statement at the end has you buy when there is a downward counter move to the up market. The limitations of this are it only works in a bull market and the RSI values may not be to your liking. Also the draw-down is much too great for my liking and would really like to bring it to about 35% if possible. If you find this helpful and iterate on it, I'd love to know what you did to improve it.
FYI this is my first program in the quantopian setting so I don't yet know all the features or if I am using some of the RSI features incorrectly.
Thanks!

Thanks for the second algo Ray. I like the logic of short counter trend moves Weston, and I think it is a move in the right direction, what would improve this I think would be to add more options for exiting the position - too much for me to do in Python given my current limitations but that is the direction I would def explore.

Hi Weston,

I just want to clarify that the part of my code that trades was really just for fun. I was more interested in showing the calculations that implement Ashish's idea. When I have a spare moment I will dig into your changes. Thank you for sharing.

I personally have been experimenting with using the slope of RSI as another signal (which is why my function can operate on a big pandas dataset), but so far without much success.

Ray