Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Concise algo all in one

Algo trade is one wonderful thing, but we cannot do better if just know it's advantage. Some demo algo is look very good, but just change test time period or test stock, the result is very different. So, i try to develop the algo that can be sustained and stable profit. Trade idea is first, programing skill is second. Let us begin from basic step by step.

1.trend follow strategy
2.anti-trend follow strategy
3.technical indicator based strategy
4.history statistics data strategy
5.momentum strategy

1.trend follow strategy
The trend follow stratgy, that principle is rising stocks will rise higher. "The trend is your friend, follow them". How to find a up trend? Here are some known ways:
1.1 current price is above moving average
1.2 current price is above bollinger upper line
1.3 reach a higher price than history data

1.1 moving average
Buy order if the current price is above moving average. Sell order if the current price is below moving average.
code demo 1.1

11 responses

1.2 bollinger bands
Buy order if the current price is above bollinger up line. Sell order if the current price is below bollinger up line.
code demo 1.2

1.3 reach a higher price than history data
Buy order if the price is reach a higher price than history data. Sell order if the price is down a lower price than history data.
demo 1.3

Conclusion: Trend follow strategy is most popular and effective strategy, but the problem is that winrate is not very good, because of the false breakthrough. There are some improve method, like use longer period time data, confirm trend before order, etc. From above basic mode, we can develop some more advanced algo, like Adaptive moving average, elliott wave.

Gang - thanks for sharing! I cloned your first algo and made a couple improvements:

  • Changed mavg() to use panda's calculation, which is faster than the built-in function
  • Used schedule_function to handle the date logic and trade once per day
  • Check for open orders before placing new orders. The "order_target" functions make their calculations based on the number of filled orders and are not aware of pending open orders.

One thing to consider is the algo doesn't make many transactions - only a couple per year. Nice start and looking forward to seeing the next iterations.

Disclaimer

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 Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian 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.

whoops, looks like my algo has a couple problems. First lines 18 and below should be within the for loop - they're not properly lined up right now.

And based on the custom plot, I think something is getting stuck in the open orders, since the recorded values are not changing. I can try to dig into it later

As the topic said, concise algo means i try to use the shortest code to demo basic develop idea. Some raw mode is not really good, let us improve them to be practical version step by step. And as you saw, i visualize some technical indicator using record function, that can help us to find problem and fix it.

I had a quick play with the Bollinger band algo and turned it into one my first ever trading strategy's

rules:
buy when close below lower band > sell when band is half away above moving average
must follow the trend (i used 200 sma for this)

I am some late to update post. Let us continue next chapter.

2.anti-trend follow strategy
The anti-trend follow stratgy, it also call reverse strategy or moving average regression strategy. To do it, here are some known ways:
2.1 buy order if current price is cross upward breakthrough bollinger lower line
2.2 buy order if williams overbought/oversold index is below than 20

demo2.1

2.2 I use the williams index to assess stock price is overbought/oversold or not.
Buy order if williams overbought/oversold index is below than 20. Sell order if williams overbought/oversold index is above than 80.
demo2.2

4.history statistics data strategy
Intelligence is from knowledge, knowledge is from experience. By analyzing historical statistics data, to find trading oppotunities. I develop a pairing trade algo, coca-cola vs pepsi-cola, first statistical analysis of their differences, then buy the lower volatility stock more. And this algo is also a hedging strategy .
demo4.1

5.momentum strategy
Yesterday is yesterday, tomorrow is tomorrow, today is now. Perhaps the historical data can not help us to decide buy or sell order, it just hint the important pressure/support price line. So, today's price is point.
demo5.1