Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
New to Quantopian. Need Help

Hello,
I recently joined quantopian. I have very basic Python knowledge. I am having trouble coding a very simple strategy and would like if someone could code this strategy so I can see how everything is coded. For this strategy simply backtest it on AAPL. This strategy is as follows:

Uses Daily Data
Long Only Strategy
Allocates 5% of account to buying shares of AAPL
when AAPL closes above its 200 day SMA and the previous days close was below the 200 day SMA from the previous day , go long.
set an end of day stop loss at 3 ATR below the entry price (if AAPL closes 3 ATR below the entry point then exit the current position)
Use the 200 SMA as a trailing stop. if AAPL closes below its 200 day SMA then exit current postions.

Your help is greatly appreciated. Thank you!

4 responses

Take a look at this. Didn't check it over too well but I think it sort of does what you want. I wasn't exactly sure what you meant by set an end of day stop loss at 3 ATR below the entry price (if AAPL closes 3 ATR below the entry point then exit the current position) so didn't include that in the close rules. But you should be able to understand the underlying structure.

The first step is to define all the pieces of data you will be needing and then create those as columns in your pipeline. Then in your algorithm set up simple queries against this data to determine when to open and close positions. This logic can be performed before trading starts, at pre-defined times, or every minute, depending upon what you are looking to do. The attached algorithm simply does this logic once each day at open and then executes the trades.

Good luck.

Thank you very much. This helped clear some things up. Earlier today I was attempting to use talib to make an SMA but I continue to run into an error message. All I'm attempting to do is plot the closing price and the 200 day SMA.

import talib 



def initialize(context):  
    context.aapl = sid(19725)  
    schedule_function(algo, date_rules.every_day(),time_rules.market_close(minutes=5))

def algo(context,data):  
    hist = data.history(context.aapl,'price',200,'1d')  
    current = data.current(context.aapl,'price')  
    sma = talib.SMA(hist,timeperiod = 20)  

    record(sma=sma, current=current)  

then I get this error message

ValueError: Record only supports numeric values. Non-numeric value passed for key 'sma'  
There was a runtime error on line 23.  

I do not understand why my SMA is not a numeric value.

Click on the line number in the margin at the record() line. When that breakpoint is reached, type sma at the prompt and hit enter. You'll see it's an ndarray. You can click to expand that to see the list of values. Then try sma[-1], that'll return just the last number, possibly the one you want. That [-1] can go on the end of the talib.SMA line so sma will be just that last number instead of the ndarray.

Thank you very much!!!!! I did not realize that console popped up when you did that. That helped a lot. I learned how to compare current and past data! Thank you so much that is a huge help!