Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Need Help with RSI,Trailing Stop - New to Quantopian & and willing to Pay for help

Hello,

I am very new to Python and have been struggling for the past 3 days trying to write this algorithm. I have been rifling the resources of this website and others, but I cannot find anyone with a build similar to mine. I need the algorithm to:
1. Scan the Q1500US for stocks with my target RSI range (30-50)
2. Buy stocks at RSI 30 and sell at 50.
3. Create positions at 10% of the portfolio
4. Maintain a 2% trailing stop on orders
5. Sell positions at 20 days if RSI has not reached 50.

You can take a look at what I have done so far, but my code is pieced together from multiple sources. I really want to get this working so I can fine tune it. I am looking for a mentor to help me on this and am willing to compensate him or her for their effort.

~Blake

7 responses

Blake

Take a look at this. I think it's what you're looking for. I didn't fully check for exceptions or spend any time optimizing but it should be pretty clear.

Let me know what you think,

Dan

Oh, I should have mentioned I took some liberty with your "buy stocks at 30" request. Typically one buys when the RSI crosses from below a value to above a value. That is what I implemented. There are some 'constants' in the initialize section that can be played with. For instance, I'm not a big fan of trailing stops so I set it to 10%. Also, the conventional "sell" trigger is an RSI above 70 so I changed that. Also, I just started from scratch and didn't really use your code so maybe it's not that instructional?

Here is the same code but with those change of constants.

Thank you very much Dan! If you PM me a paypal ID i will send you something as a thank you.

I cleaned up the code a bit. Was getting some "depreciated" warnings (changed sort to sort_value) and also was getting a dreaded "SettingWithCopyWarning" (changed to .assign method to create new column in output).

Biggest change was the addition of split adjusting the 'highest_high' used to set the trailing stop price. Got the idea from this post https://www.quantopian.com/posts/dividend-bug-fixed-in-pipeline-algorithms . This was an oversight in the first algorithm. Price data should never be saved and carried over without adjusting for splits.

Added some better comments too.

Please let me know if anyone catches any other bugs or has suggestions to improve.

Dan

Oops, NaNs can get into the data. The above algorithm didn't handle the trailing stop being set to a NaN (if the high was ever a NaN) so simply punted and added the code:

 "output['high_highest'].fillna(0, inplace=True)"  

into the update_output_data function.

Revised code is attached.

Here's another take on this algorithm. Removed the trailing stop logic and added shorting. Beta is less as one would expect.

Thank you Dan! Dont forget to send me your paypal ID.