SPY trading strategy script help

touchups

New member
Hello. Trying to get this to work. I don't know how to write code. I have been using the "edit studies" tab to try to set this up. Failing for hours lol... This is the strategy: Is SPY above the 66 day SMA? NO=do nothing, if YES, then is todays closing price the lowest in 3 days? NO=do nothing, if YES, then BUY SPY. Selling is dependent on the closing price of SPY being the highest over the past 19 days. NO=do nothing, if YES, then SELL spy. That's it. Thank you for any suggestions.
 
Solution
@touchups Here's a strategy based on my understanding of your criteria. Put it under the Strategy section to view the buys/sells, and test it throughly -
Code:
#Is SPY above the 66 day SMA?
def buy_filter_1 = close > Average(close, 66);

#Is todays closing price the lowest in 3 days?
def buy_filter_2 = close < Lowest(close, 3)[1];

#Selling is dependent on the closing price of SPY being the highest over the past 19 days.
def exit_criteria = close > Highest(close, 19)[1];

plot avg = Average(close, 66);
plot entry = Lowest(close, 3)[1];
plot exit = Highest(close, 19)[1];

AddOrder(OrderType.BUY_TO_OPEN, buy_filter_1 and buy_filter_2, tickcolor = Color.CYAN, arrowcolor = Color.CYAN, name = "buy");
AddOrder(OrderType.SELL_TO_CLOSE...
@touchups Here's a strategy based on my understanding of your criteria. Put it under the Strategy section to view the buys/sells, and test it throughly -
Code:
#Is SPY above the 66 day SMA?
def buy_filter_1 = close > Average(close, 66);

#Is todays closing price the lowest in 3 days?
def buy_filter_2 = close < Lowest(close, 3)[1];

#Selling is dependent on the closing price of SPY being the highest over the past 19 days.
def exit_criteria = close > Highest(close, 19)[1];

plot avg = Average(close, 66);
plot entry = Lowest(close, 3)[1];
plot exit = Highest(close, 19)[1];

AddOrder(OrderType.BUY_TO_OPEN, buy_filter_1 and buy_filter_2, tickcolor = Color.CYAN, arrowcolor = Color.CYAN, name = "buy");
AddOrder(OrderType.SELL_TO_CLOSE, exit_criteria, arrowcolor = Color.MAGENTA, name = "sell");
 
Solution

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

WOW. Amazing work. Thank you! For the record (good or bad), this is not my strategy btw. I ran across it on relaxed trader, but it was written for TS. It does seem to work (don't know if it is the best method out there), BUT obviously only in an uptrend market. Not this funk were in right now. I tracked it back just one year. I had to look manually. I don't know how to back test an idea that automatically spits out results. Wish I did. Biggest flaw is downs that last more then 19 days, then you must take a loss. That is just built in. No way around that with this method. And you are susceptible to opening spikes since you are buying based on yesterday's close. REALLY appreciate you writing this. Thank you again.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
478 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