401k Market Timing SPX Strategy for ThinkorSwim

conceptcar3

New member
Hey everyone, I made a strategy that times the SPX market, it has outperformed the SPX since 1928 and with less drawdown (risk) than buy and hold. Here is the strategy for you to check out. hope you enjoy. The method is rather simple, merely buy the SPX approximately 60 trading days (approximately 3 months, exact numbers can be seen in the source code below) after the 1 period moving average crosses over the 200 period EMA. the same goes for shorting it, this is a long short strategy.

Any feedback or thoughts appreciated! The code is very sloppy but it was the only way I could figure out how to do it. Make sure you are on a daily SPX chart. Also, very importantly, I use the strategies stoplosslx and stoplosssx to implement stop losses on each trade of the strategy, 6 percent on stoplosssx and 12 percent on stoplosslx. Enjoy!


Here is the code

Code:
#longtermspxstrategy

input price = close;
input fastLength = 1;
input slowLength = 200;
input averageType = AverageType.EXPONENTIAL;

plot FastMA = MovingAverage(averageType, price, fastLength);
plot SlowMA = MovingAverage(averageType, price, slowLength);
FastMA.SetDefaultColor(GetColor(1));
SlowMA.SetDefaultColor(GetColor(2));
def bullishsignal = fastma > slowma and fastma[1] > slowma[1] and fastma[2] > slowma[2] and fastma[3] > slowma[3] and fastma[4] > slowma[4] and fastma[5] > slowma[5] and fastma[6] > slowma[6] and fastma[7] > slowma[7] and fastma[8] > slowma[8] and fastma[9] > slowma[9] and fastma[10] > slowma[10] and fastma[11] > slowma[11] and fastma[12] > slowma[12] and fastma[13] > slowma[13] and fastma[14] > slowma[14] and fastma[15] > slowma[15] and fastma[16] > slowma[16] and fastma[17] > slowma[17] and fastma[18] > slowma[18] and fastma[19] > slowma[19] and fastma[20] > slowma[20] and fastma[21] > slowma[21] and fastma[22] > slowma[22] and fastma[23] > slowma[23] and fastma[24] > slowma[24] and fastma[25] > slowma[25] and fastma[26] > slowma[26] and fastma[27] > slowma[27] and fastma[28] > slowma[28] and fastma[29] > slowma[29] and fastma[30] > slowma[30] and fastma[31] > slowma[31] and fastma[32] > slowma[32] and fastma[33] > slowma[33] and fastma[34] > slowma[34] and fastma[35] > slowma[35] and fastma[36] > slowma[36] and fastma[37] > slowma[37] and fastma[38] > slowma[38] and fastma[39] > slowma[39] and fastma[40] > slowma[40] and fastma[41] > slowma[41] and fastma[42] > slowma[42] and fastma[43] > slowma[43] and fastma[44] > slowma[44] and fastma[45] > slowma[45] and fastma[46] > slowma[46] and fastma[47] > slowma[47] and fastma[48] > slowma[48] and fastma[49] > slowma[49] and fastma[50] > slowma[50] and fastma[51] > slowma[51] and fastma[52] > slowma[52] and fastma[53] > slowma[53] and fastma[54] > slowma[54] and fastma[55] > slowma[55] and fastma[56] > slowma[56] and fastma[57] < slowma[57];
def bearishsignal = fastma < slowma and fastma[1] < slowma[1] and fastma[2] < slowma[2] and fastma[3] < slowma[3] and fastma[4] < slowma[4] and fastma[5] < slowma[5] and fastma[6] < slowma[6] and fastma[7] < slowma[7] and fastma[8] < slowma[8] and fastma[9] < slowma[9] and fastma[10] < slowma[10] and fastma[11] < slowma[11] and fastma[12] < slowma[12] and fastma[13] < slowma[13] and fastma[14] < slowma[14] and fastma[15] < slowma[15] and fastma[16] < slowma[16] and fastma[17] < slowma[17] and fastma[18] < slowma[18] and fastma[19] < slowma[19] and fastma[20] < slowma[20] and fastma[21] < slowma[21] and fastma[22] < slowma[22] and fastma[23] < slowma[23] and fastma[24] < slowma[24] and fastma[25] < slowma[25] and fastma[26] < slowma[26] and fastma[27] < slowma[27] and fastma[28] < slowma[28] and fastma[29] < slowma[29] and fastma[30] < slowma[30] and fastma[31] < slowma[31] and fastma[32] < slowma[32] and fastma[33] < slowma[33] and fastma[34] < slowma[34] and fastma[35] < slowma[35] and fastma[36] < slowma[36] and fastma[37] < slowma[37] and fastma[38] < slowma[38] and fastma[39] < slowma[39] and fastma[40] < slowma[40] and fastma[41] < slowma[41] and fastma[42] < slowma[42] and fastma[43] < slowma[43] and fastma[44] < slowma[44] and fastma[45] < slowma[45] and fastma[46] < slowma[46] and fastma[47] < slowma[47] and fastma[48] < slowma[48] and fastma[49] < slowma[49] and fastma[50] < slowma[50] and fastma[51] < slowma[51] and fastma[52] < slowma[52] and fastma[53] < slowma[53] and fastma[54] < slowma[54] and fastma[55] < slowma[55] and fastma[56] < slowma[56] and fastma[57] < slowma[57] and fastma[58] < slowma[58] and fastma[59] < slowma[59] and fastma[60] < slowma[60] and fastma[61] < slowma[61] and fastma[62] < slowma[62] and fastma[63] > slowma[63];
def buy = bullishsignal;
def sell = bearishsignal;
AddOrder(OrderType.buy_auto, buy, open[-1], 1, Color.ORANGE, Color.ORANGE, "buy" + open[-1]);

AddOrder(OrderType.sell_auto, sell, open[-1], 1, Color.ORANGE, Color.ORANGE, "sell" + open[-1]);
 
I took a glance at the code. The way it was written, I don't think it is showing the right backtesting results. I will spend a bit more time on it before confirming for sure.
 
I took a glance at the code. The way it was written, I don't think it is showing the right backtesting results. I will spend a bit more time on it before confirming for sure.
I am not 100% sure, but I believe it is showing accurately. I checked this system manually before developing the code, and the code seems to line up with my manual backtest results, at least for the most part. Could be wrong though... but I think the code is correct! Let me know if you find some errors. Thanks for your help!
 
I noticed that with the above settings (buy waiting 57 days after moving avg cross, sell waiting 63 days) that it produced the best results, but the equity curve from 1928-1949 was not so good. Tweaking the settings so that it is 63 days after the cross for both long and short reduces net profit, but improves the 1928-1949 equity curve. In case anyone wants it, here is the code for 63 days after moving average cross in both directions with the smoother equity curve and slightly reduced returns.

Code:
input price = close;
input fastLength = 1;
input slowLength = 200;
input averageType = AverageType.EXPONENTIAL;

plot FastMA = MovingAverage(averageType, price, fastLength);
plot SlowMA = MovingAverage(averageType, price, slowLength);
FastMA.SetDefaultColor(GetColor(1));
SlowMA.SetDefaultColor(GetColor(2));
def bullishsignal = fastma > slowma and fastma[1] > slowma[1] and fastma[2] > slowma[2] and fastma[3] > slowma[3] and fastma[4] > slowma[4] and fastma[5] > slowma[5] and fastma[6] > slowma[6] and fastma[7] > slowma[7] and fastma[8] > slowma[8] and fastma[9] > slowma[9] and fastma[10] > slowma[10] and fastma[11] > slowma[11] and fastma[12] > slowma[12] and fastma[13] > slowma[13] and fastma[14] > slowma[14] and fastma[15] > slowma[15] and fastma[16] > slowma[16] and fastma[17] > slowma[17] and fastma[18] > slowma[18] and fastma[19] > slowma[19] and fastma[20] > slowma[20] and fastma[21] > slowma[21] and fastma[22] > slowma[22] and fastma[23] > slowma[23] and fastma[24] > slowma[24] and fastma[25] > slowma[25] and fastma[26] > slowma[26] and fastma[27] > slowma[27] and fastma[28] > slowma[28] and fastma[29] > slowma[29] and fastma[30] > slowma[30] and fastma[31] > slowma[31] and fastma[32] > slowma[32] and fastma[33] > slowma[33] and fastma[34] > slowma[34] and fastma[35] > slowma[35] and fastma[36] > slowma[36] and fastma[37] > slowma[37] and fastma[38] > slowma[38] and fastma[39] > slowma[39] and fastma[40] > slowma[40] and fastma[41] > slowma[41] and fastma[42] > slowma[42] and fastma[43] > slowma[43] and fastma[44] > slowma[44] and fastma[45] > slowma[45] and fastma[46] > slowma[46] and fastma[47] > slowma[47] and fastma[48] > slowma[48] and fastma[49] > slowma[49] and fastma[50] > slowma[50] and fastma[51] > slowma[51] and fastma[52] > slowma[52] and fastma[53] > slowma[53] and fastma[54] > slowma[54] and fastma[55] > slowma[55] and fastma[56] > slowma[56] and fastma[57] > slowma[57] and fastma[58] > slowma[58] and fastma[59] > slowma[59] and fastma[60] > slowma[60] and fastma[61] > slowma[61] and fastma[62] > slowma[62] and fastma[63] < slowma[63];
def bearishsignal = fastma < slowma and fastma[1] < slowma[1] and fastma[2] < slowma[2] and fastma[3] < slowma[3] and fastma[4] < slowma[4] and fastma[5] < slowma[5] and fastma[6] < slowma[6] and fastma[7] < slowma[7] and fastma[8] < slowma[8] and fastma[9] < slowma[9] and fastma[10] < slowma[10] and fastma[11] < slowma[11] and fastma[12] < slowma[12] and fastma[13] < slowma[13] and fastma[14] < slowma[14] and fastma[15] < slowma[15] and fastma[16] < slowma[16] and fastma[17] < slowma[17] and fastma[18] < slowma[18] and fastma[19] < slowma[19] and fastma[20] < slowma[20] and fastma[21] < slowma[21] and fastma[22] < slowma[22] and fastma[23] < slowma[23] and fastma[24] < slowma[24] and fastma[25] < slowma[25] and fastma[26] < slowma[26] and fastma[27] < slowma[27] and fastma[28] < slowma[28] and fastma[29] < slowma[29] and fastma[30] < slowma[30] and fastma[31] < slowma[31] and fastma[32] < slowma[32] and fastma[33] < slowma[33] and fastma[34] < slowma[34] and fastma[35] < slowma[35] and fastma[36] < slowma[36] and fastma[37] < slowma[37] and fastma[38] < slowma[38] and fastma[39] < slowma[39] and fastma[40] < slowma[40] and fastma[41] < slowma[41] and fastma[42] < slowma[42] and fastma[43] < slowma[43] and fastma[44] < slowma[44] and fastma[45] < slowma[45] and fastma[46] < slowma[46] and fastma[47] < slowma[47] and fastma[48] < slowma[48] and fastma[49] < slowma[49] and fastma[50] < slowma[50] and fastma[51] < slowma[51] and fastma[52] < slowma[52] and fastma[53] < slowma[53] and fastma[54] < slowma[54] and fastma[55] < slowma[55] and fastma[56] < slowma[56] and fastma[57] < slowma[57] and fastma[58] < slowma[58] and fastma[59] < slowma[59] and fastma[60] < slowma[60] and fastma[61] < slowma[61] and fastma[62] < slowma[62] and fastma[63] > slowma[63];
def buy = bullishsignal;
def sell = bearishsignal;
AddOrder(OrderType.buy_auto, buy, open[-1], 1, Color.ORANGE, Color.ORANGE, "buy" + open[-1]);

AddOrder(OrderType.sell_auto, sell, open[-1], 1, Color.ORANGE, Color.ORANGE, "sell" + open[-1]);
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
295 Online
Create Post

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top