3 Easy Lines of Code to Swing Trade SPY

It's time to stop treating your swing trading as a hobby. It's time to get serious.

Understand the odds of every trade you make is the only way to make consistent money swing trading stocks.  

No more guessing and no more listening to gurus that don't back up their "hot picks" with proven odds and statistics.

In this post, I layout three lines of code for a simple SPY swing trading strategy that you can use to time the S&P 500 with 82% winning odds.

And the best part is you don't even need a computer, but some basic counting is required.


The Trend Is Your Friend

I used to listen to TV personalities and internet gurus, but I never made money.

Actually, I lost a lot.  How could so many self-described "pro traders" be so wrong all the time?  

I still remember the day, around 2005, when I threw up my hands in disgust over another "pro trader's" opinion that lost me a ton of money.

I decided then and there that I was going to test any and all trading ideas I could get my eager hands on.  

And of all the ideas I tested, gleaned off internet forums and blowhards on TV, only one was true:

"The trend is your friend."

The "trend" is which way a market is moving, and there are three directions, sideways, down or up.  

You only want to trade in the direction of the trend.

Sounds intuitive, but I've known plenty of traders that try to "call tops" or "call bottoms".  They don't make money, they just want to brag to their friends or resentful spouse about how they were "right".  

Betting against a market's direction is the stupidest thing you can do with your money, besides heading to Vegas.

The simplest way to determine the trend is with a moving average.  

The 200 and 100-day moving averages are standard for many swing traders, but we're going to use a non-standard one to stay away from the herd.

Let's use the ETF SPY (which tracks the S&P 500) to demonstrate the first line of code in a swing trading strategy.  

Looking at closing prices only, let's ask the computer if SPY is above its 66-day moving average:

close > average (close, 66)

If this expression is true, we want to look for swing trades in the long direction.  

You only want to buy if price is above the 66-day moving average (this also works on individual stocks and many commodities).

Now, let's move on to the second part of our swing trading strategy, when exactly do you trade SPY?

Only Buy Dips in Up Trending Markets

Once you establish that a market is moving higher, you only want to buy dips in that uptrend.  

You never want to short an up-trending market as you will get your head handed to you.  I know this because I used to bet against the market.

I don't know what it is about new traders and wanting to short stocks, but the majority do.  

Humans tend to over-estimate their abilities and this is where I think a lot of that psychology comes from.  

Check yourself if you are thinking "I'm smarter than the markets".  

Nope, you aren't, and neither am I, that's why we test our ideas.  

But how do you tell a computer what a dip is?  

Use absolute quantifiable terms here; you can't just say "oh the market should find support here."  Also keep your definition of a dip super simple.  

You don't want to over optimize and run the risk of curve fitting your swing trading strategies.

So, let's look at SPY and ask the computer a simple question, is today's close the lowest in 3 days?  You can play around with the number of days to look back at, but I've found 3 works well.

close < lowest (close, 3)

If this expression is true we have ourselves a "dip".  

We now have an uptrend established, a dip in the uptrend and it's time to buy!

(This proves to be remarkably profitable as you will see in a minute.)

Sell the Rally for a Profit

Do you know what the hardest part of trading truly is?  

It's not knowing when to buy, it's knowing when to sell.  

You might already intuitively know that.  How many stocks have you held onto for too long?  How many stocks do you currently have in your portfolio that are severely under water?  

Anecdotally, my brother once showed me his portfolio and it was full of losers!  I asked him why he didn't sell them, and he responded. "I never know when to sell!"

This is why you must have a system in place, especially when swing trading stocks. 

Getting in is easy, getting out is hard.

So how do we know when to sell our position in SPY once we bought the dip?  

We must wait for a rally.  But what is a "rally"?  How do we think like a computer to give it exact instructions?  

Well, let's use the inverse of how we defined a dip above.  Let's ask the computer to tell us when SPY has made a close higher than the previous 19 days.

close  >= highest (close, 19)

If this expression is true, it's time to sell SPY and most likely bank a nice profit.  

Okay, let's wrap these three simple ideas into a trading strategy for SPY.  Then let's plug it into the computer and see what happens.  

Computers don't lie to your face like the suits on Wall Street or internet grifters selling doom and gloom and "technical analysis".  

Let's recap what we've put together so far in a visual form:

Simple Swing Trading Strategy for SPY Flowchart:

3 Lines of Code for Swing Trading SPY With 82% Winning Odds

SPY Swing Trading Strategy Flowchart

It's truly that simple, these trading strategy rules are about as basic as they come.

Now, let's look at how unbelievably powerful this little swing trading strategy is over time:

Swing Trade SPY Equity Curve

SPY Swing Trading Strategy Equity Growth

Each of the green dots above signals a new equity high for this trading model.  

Incredible isn't it?  

This is the power of testing ideas before jumping headfirst into the stock market.

Let's looks at some example trades produced by this simple swing trading system:

How to swing trade SPY

SPY Swing Trading Strategy Trade Examples

You can see when SPY dips it's usually very profitable to be a buyer.  

I often get asked if this is the best spy options strategy, but I usually recommend people stay away from options, unless they really know what they are doing. 

OK. Let's look at the statistics of this simple swing trade strategy:

SPY Swing Trading System Trade Performance

Trading Statistics of a Simple Swing Trading System for SPY

But can you believe such simple rules can time the stock market with such great odds?

So next time you are about to make a trade, ask yourself if you truly know the odds of making or losing money.  

Because if you don't know the exact odds how do you even know how much money to risk?

This is not rocket science.  Anyone can do this.

Even your financial adviser could, but I guarantee they have no idea how to even start.  

So, I implore you to take control and start testing your ideas before you risk another penny!

Coming up with trading ideas and testing them is not hard; if you are like me, you'll start to enjoy it.  

And your account certainly will too.

Conclusion: 3 Lines of Code for Swing Trading SPY With 82% Winning Odds

  • Always trade in the direction of the trend - never against it
  • Buy dips in up trending markets and sell into rallies for consistent profits

TradeStation Code

Simple spy trading system code

About the Author

Hello! I'm Kurt the "Relaxed Trader" writing the stuff on this website. Feel free to ask me questions. I love talking to fellow traders that want to use computers to beat the stock market. Shoot me an email: Kurt@relaxedtrader.com

Leave a Reply 17 comments