Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Moving average crossover strategy code

hi,
I am new here. I have little knowledge of python coding. I want an algorithm for sma20 and sma50 crossover. I want to select only 2 stocks: AAPL and TSLA. I want tgt_percentage = 0.01, and stoploss_percentage = 0.01, it means risk/reward = 1/1.
can anyone help me to write this algorithm? I have written it as

from quantopian.pipeline import Pipeline
from quantopian.pipeline.filters import QTradableStocksUS
from quantopian.research import prices, symbols
from quantopian.pipeline.data import EquityPricing
from quantopian.pipeline.domain import US_EQUITIES
from quantopian.research import run_pipeline
from quantopian.pipeline.factors import SimpleMovingAverage

pipe = Pipeline(
columns={
'price': EquityPricing.close.latest,
'volume': EquityPricing.volume.latest,
'sma20': SimpleMovingAverage(inputs=[EquityPricing.close], window_length=20),
'sma50': SimpleMovingAverage(inputs=[EquityPricing.close], window_length=50),
},
domain=US_EQUITIES,
)

result = run_pipeline(pipe, '2016-01-15', '2019-06-01')
result.tail()

please assist me in how to write the whole algorithm for the above-said strategy.

9 responses

Great beginners question AKSHAY!

Welcome to Quantopian.

Try something like this. Let me know if you have questions about the code.

BTW, you don't need pipeline for this example.
Pipeline is typically used to generate a dynamic selection of stocks from a large universe.

thankyou so much sir, I appreciate your work.

Hi Ammar,

You would need to specify the stocks as a list. Also you should use 'sid' rather than 'symbol'. SID stands for "security ID". Unlike symbols which can be reused as stocks are de-listed, sid's are unique and cannot be reused. To get the sid for a stock such as AAPL, start typing sid(AAPL - and a list of stocks will pop up on the screen. Double click on AAPL.

If you have a long list of stocks you should decide the maximum number of stocks to be long at any given time and how to weight them. You might also consider how to rank stocks if multiple stocks meet the buy condition at the same time. If the universe of stocks is large, it is probably better to create a pipeline with a survivorship free predefined filter such as Q500US rather than a fixed list of stocks, but that's a more advanced topic.

The attached code is a start. To finish it, you would need to complete the list of stocks and also (optional) add some code to rank stocks in case there are more buys than open slots.

what if i just wanted to buy stocks and hold for a year - say like 40% of my portfolio and 60% ETF what would code be guys

Mr. Steve Jost,

I am a newbie/ wannabe programmer. Your code is great and has answered a lot of questions for me. I have one question though if you’re still monitoring this thread.

Question?

What changes would I have to make to your code if I wanted my stop loss to be a standard moving average and what changes would I have to do to draw from pipeline results instead of inputting in sid’s?

Sorry two questions.. but your code and answers have been by far the most helpful that I have found.

Thank you for your time.

Hi Tim,

Try something this as a starting point.
Let me know if you have questions about the code.

Thank you for your help! Have a great day.