Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
I just made a Million Dollars!

As a result of evolution, humans have over the years developed a certain affinity towards distinguishing patterns. In the realm of stock markets, this tendency is of significant incidence. A day trader would place a buy order for a stock simply because of the fact that its SMA (simple moving average) for a 20 year period was greater than the SMA for 200 days and place a sell order in the inverse condition. A sequence of these trades following a similar pattern might or might not bear a positive Sharpe ratio. In an ideal case he should be able to tweak his current strategy to observe the sensitivity in returns with respect to that. Despite her strengths in distinguishing patterns, several limitations do come in the way of successfully pulling such an evolving strategy off. Limited human capacity, prevalence of biases and a simply lack of creativity are some of them – the list goes on. Perhaps a quote by Dr. Brett Steenbarger (author of The Psychology of trading, Wiley 2003) could set the theme of the following discussion:

“The most neglected area of development among traders is creativity. We focus on executing trades and managing risk, but rarely work on expanding our creative capacity to generate ideas. “

This is something that has bugged me personally for quite some while now. As a graduating senior (this year), it is almost disappointing that at this point I cannot claim prowess in Finance - the very field I am majoring. I place part of the blame in how finance courses in business universities are structured these days - less scientific and more management oriented.

Observing the ever-evolving state of financial markets and their complexities, taking steps to complement trading strategies with algorithmic trading ought to be a no-brainer. Tinkering with all the code is hardly less than a recourse for me. Particularly because of two reasons:

1) It helps me release pent up frustration rooting from the redundancy of management-centric finance majors.
2) I believe that Pakistan has a tonne of bottled up talent. We set the bar too low for ourselves - often content with doing the minimum amount of effort to reach our goals (and hardly anything beyond that). The purpose of this blog is to provide stimulus.

Financial markets in Pakistan are growing at their own pace too. We need creative minds who are willing to peek out of the box and realize the full extent of financial analysis and algorithmic backtesting.

To spark a little interest, following is a leverage adjusted algorithm which is taking long and short position in Apple inc. stock over the following time period : 01/04/2013 to 02/14/2016. Since this is an introductory article, I have chosen to keep the strategy short and simple.
When the Simple Moving Average (SMA) for 50 periods is greater than the SMA for 150 days during the same period, the algorithm places a buy order. The inverse is true for instances when SMA 150 days > SMA 50 days - the algo shorts the positions.

2 responses

I clone it because it was beginner level, and I am. I tried it with another stock...anyway.
Your clone had a warning at line 16, "Local variable 'open_orders' is assigned to but never used". I took it off, and it worked out.

open_orders = get_open_orders() ####### You had to add it further : I just learned that it is about leverage.

if sma_50 > sma_200:  
    if context.C not in open_orders:  
        order_target_percent(context.C,1.0)  
elif sma_200 > sma_50:  
    if context.C not in open_orders:  
        order_target_percent(context.C, -1.0)