Use These 3 Easy Lines of Code for 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 swing trading system that you can use to time the market 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.  But of all the ideas I tested that I gleaned off internet forums or blowhards on TV, only one was true:

"The trend is your friend."

The "trend" is which way the market is moving, and there are only three directions, down, sideways, or up.  You only want to trade in the direction of that 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 could ever do with your money besides heading to Vegas.

The simplest way to measure trend is with a moving average.  Let's use the ETF SPY (which tracks the S&P 500) to demonstrate the first line of code.  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, then we are in business for looking for swing trades in the long direction.  You only want to buy if prices are above the 66-day moving average (this also works on stocks and commodities).

Let's move on to the second part of our simple swing trading system, how to know when to buy 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 systems.

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 out of 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 single swing trading system 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.  

But first, let's recap what we've put together so far in a visual form:

Simple Swing Trading System for SPY Flowchart:

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

SPY Swing Trading System Flowchart

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

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

Equity Growth of a Simple Swing Trading System for SPY

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 market before you know what you are doing.

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

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

Trade Examples of a Simple Swing Trading System for SPY

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

Let's look at the statistics:

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

Trading Statistics of a Simple Swing Trading System for SPY

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. And 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

  • Simple rules work!
  • Buy dips in up trending markets
  • Sell rallies for consistent profits

Full TradeStation Code

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

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

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:

Leave a Reply 12 comments