Need help with mean reversion strategy looking at close 80 days ago

alfred

New member
VIP
I have a simple strategy (written in C code) but I have been unable to find existing tS code that does the same thing. I also wasted many hours with chatGPT trying to get it converted properly (too many debugging errors in ToS and a comedy of errors in the chat transcripts). I would like to (learn how to ) create and backtest this strategy and I want to stay on ToS so I am looking for help to make this simple strategy work on ToS platform.

If the close in SPY is less than the close of 80 periods ago (stocks in downtrend), I am looking for a mean reversion trade.
I buy SPY when the 6-day EMA crosses below the 5-day SMA.
I sell SPY and buy TLT when the EMA crosses above the SMA.
If the close in SPY is higher than the close of 80 periods ago, I am looking for a mean reversion trade. The "fast" EMA will be somewhere near the current SPY price.
No sell signal can be generated until the "fast" EMA is below the 80-period price of SPY; that's when the trend is considered over, and the mean reversion trades begin.

I have a couple versions of chatGPT-produced code that will compile(?) but they would keep me perpetually in a trade, either SPY or TLT, never in a neutral state.

Is it okay to post the C code here to ask for help converting to thinkScript and making it backtest-ready?
 
I have a simple strategy (written in C code) but I have been unable to find existing tS code that does the same thing. I also wasted many hours with chatGPT trying to get it converted properly (too many debugging errors in ToS and a comedy of errors in the chat transcripts). I would like to (learn how to ) create and backtest this strategy and I want to stay on ToS so I am looking for help to make this simple strategy work on ToS platform.

If the close in SPY is less than the close of 80 periods ago (stocks in downtrend), I am looking for a mean reversion trade.
I buy SPY when the 6-day EMA crosses below the 5-day SMA.
I sell SPY and buy TLT when the EMA crosses above the SMA.
If the close in SPY is higher than the close of 80 periods ago, I am looking for a mean reversion trade. The "fast" EMA will be somewhere near the current SPY price.
No sell signal can be generated until the "fast" EMA is below the 80-period price of SPY; that's when the trend is considered over, and the mean reversion trades begin.

I have a couple versions of chatGPT-produced code that will compile(?) but they would keep me perpetually in a trade, either SPY or TLT, never in a neutral state.

Is it okay to post the C code here to ask for help converting to thinkScript and making it backtest-ready?

hello and welcome

post C code ? sure
just post it in a code window,
in a new post header,
click on </>, to open a code window.
then paste the code in it.
option - can pick a language to format it and color it as c, or whatever you want.


----------------------------


i tried to rewrite your rules. take a look and edit them.
you need to be a lot more specific when listing out the rules for a study to follow.

i don't understand why you described the 2nd half so differently than the first.
it should be the opposite of the first half?

ema near ? what is near 1% , 10% , 0.0001 % , $0.01 ??
fast ema? in the first half, you mention specific ema's. in the 2nd, you changed the description. don't do that. stay consistent.

you keep mentioning 'mean reversion' , but that has no meaning to me. it doesn't tell me what price parameter to look at, or what to do. so leave it out and just say what has to happen.

you mention 2 averages , but don't say what are the stocks supplying the data to them?
.."I buy SPY when the 6-day EMA crosses below the 5-day SMA."..
ema of what?
sma of what?
spy or tlt or some other stock?


# SPY is in downtrend
# if close(SPY) < close(spy)[80]
# buy SPY when 6-day EMA crosses below the 5-day SMA.
# sell SPY and buy TLT when the EMA crosses above the SMA.


# SPY is in uptrend
# if close(SPY) > close(spy)[80]
# The "fast" EMA will be somewhere near the current SPY price.
# No sell signal can be generated until the "fast" EMA is below the 80-period price of SPY, that's when the trend is considered over, and the mean reversion trades begin.
 
Last edited:
Thanks for the reply. Here is the C code which should tell you much better than I can put into words what I am interested in trying out in ToS.

Code:
#Price of SPY 80 days/periods ago
var V3 = LOOKBACK(p.AdjustedLow, periods: 80);
#variable se0 is assigned that number
var se0 = v3;
#v0 is 5-period SMA "slow"
var v0 = SMA(p.AdjustedOpen, periods: 5);
#v2 is 6-period EMA, smoothing of 3 "fast"
var v2 = EMA(p.AdjustedClose, periods: 6, smoothing: 3);
#v1 = v2
var v1 = v2;
#6-period EMA is cmpared to SPY price 80 days/periods ago; the smaller of the two values becomes v1
#if v1 > v0, time to "sell" SPY and get into TLT
v1 = Math.Min(v1, se0);
return v0 < v1;
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
545 Online
Create Post

Similar threads

Similar threads

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