SMA/EMA Strategy Help

keepbladin

New member
Hey everyone, new here and don't know how to code at all so I'm looking for help for building a strategy code I can add and backtest on my TOS 1hr SPY chart (No extended hours).
Strategy is:
Buy Long trigger: 10SMA crosses above the 50SMA while the 5EMA is above the 13EMA and then close long once the 5EMA crosses below the 13EMA.

Buy Short trigger 10SMA crosses below the 50SMA while 5EMA is below the 13EMA and then close short once the 5EMA crosses below the 13EMA.

Hopefully someone can help, thanks!
 
Solution
Hey everyone, new here and don't know how to code at all so I'm looking for help for building a strategy code I can add and backtest on my TOS 1hr SPY chart (No extended hours).
Strategy is:
Buy Long trigger: 10SMA crosses above the 50SMA while the 5EMA is above the 13EMA and then close long once the 5EMA crosses below the 13EMA.

Buy Short trigger 10SMA crosses below the 50SMA while 5EMA is below the 13EMA and then close short once the 5EMA crosses below the 13EMA.

Hopefully someone can help, thanks!

This should get you started. Add the following code to a create a new Strategy.

Capture.jpg
Code:
#Buy Long trigger: 10SMA crosses above the 50SMA while the 5EMA is above the 13EMA and then close long once the 5EMA crosses below...
Hey everyone, new here and don't know how to code at all so I'm looking for help for building a strategy code I can add and backtest on my TOS 1hr SPY chart (No extended hours).
Strategy is:
Buy Long trigger: 10SMA crosses above the 50SMA while the 5EMA is above the 13EMA and then close long once the 5EMA crosses below the 13EMA.

Buy Short trigger 10SMA crosses below the 50SMA while 5EMA is below the 13EMA and then close short once the 5EMA crosses below the 13EMA.

Hopefully someone can help, thanks!

This should get you started. Add the following code to a create a new Strategy.

Capture.jpg
Code:
#Buy Long trigger: 10SMA crosses above the 50SMA while the 5EMA is above the 13EMA and then close long once the 5EMA crosses below the 13EMA.

#Buy Short trigger 10SMA crosses below the 50SMA while 5EMA is below the 13EMA and then close short once the 5EMA crosses below the 13EMA.
def SMA10 = SimpleMovingAvg(close, 10);
def SMA50 = SimpleMovingAvg(close, 50);
def EMA5  = ExpAverage(close, 5);
def EMA13 = ExpAverage(close, 13);

def Long  = SMA10 crosses above SMA50 and EMA5 > EMA13;
def XLong = EMA5 crosses below EMA13;

AddOrder(OrderType.BUY_TO_OPEN, Long);
AddOrder(type = OrderType.SELL_TO_CLOSE, condition = XLong);

def Short  = SMA10 crosses below SMA50 and EMA5 < EMA13;
def XShort = EMA5 crosses above EMA13;

AddOrder(OrderType.SELL_TO_OPEN, Short);
AddOrder(type = OrderType.BUY_TO_CLOSE, condition = XShort);
 
Solution

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

amazing, thank you!


is it possible instead of exiting the trade by EMA crossing, exiting by a profit percentage instead? Is there a code for that? something small, 5%?

Here is a possible way to evaluate using a percentage profit exit.

Ruby:
#Buy Long trigger: 10SMA crosses above the 50SMA while the 5EMA is above the 13EMA and then close long once the 5EMA crosses below the 13EMA.

#Buy Short trigger 10SMA crosses below the 50SMA while 5EMA is below the 13EMA and then close short once the 5EMA crosses below the 13EMA.

input XPct = .05;

def SMA10 = SimpleMovingAvg(close, 10);
def SMA50 = SimpleMovingAvg(close, 50);
def EMA5  = ExpAverage(close, 5);
def EMA13 = ExpAverage(close, 13);

def Long  = SMA10 crosses above SMA50 and EMA5 > EMA13;
def XLong = EMA5 crosses below EMA13;
def LP    = if long then open[-1] else LP[1];
def XLP   = LP + XPct * LP;


AddOrder(OrderType.BUY_TO_OPEN, Long, tickColor = Color.LIGHT_GREEN, arrowColor = Color.LIGHT_GREEN);
AddOrder(type = OrderType.SELL_TO_CLOSE, condition = close crosses XLP, tickColor = Color.LIGHT_GREEN, arrowColor = Color.LIGHT_GREEN);

def Short  = SMA10 crosses below SMA50 and EMA5 < EMA13;
def XShort = EMA5 crosses above EMA13;
def SP    = if short then open[-1] else SP[1];
def XSP   = SP - XPct * SP;


AddOrder(OrderType.SELL_TO_OPEN, Short);
AddOrder(type = OrderType.BUY_TO_CLOSE, condition = close crosses XSP);
 
Here is a possible way to evaluate using a percentage profit exit.

Ruby:
#Buy Long trigger: 10SMA crosses above the 50SMA while the 5EMA is above the 13EMA and then close long once the 5EMA crosses below the 13EMA.

#Buy Short trigger 10SMA crosses below the 50SMA while 5EMA is below the 13EMA and then close short once the 5EMA crosses below the 13EMA.

input XPct = .05;

def SMA10 = SimpleMovingAvg(close, 10);
def SMA50 = SimpleMovingAvg(close, 50);
def EMA5  = ExpAverage(close, 5);
def EMA13 = ExpAverage(close, 13);

def Long  = SMA10 crosses above SMA50 and EMA5 > EMA13;
def XLong = EMA5 crosses below EMA13;
def LP    = if long then open[-1] else LP[1];
def XLP   = LP + XPct * LP;


AddOrder(OrderType.BUY_TO_OPEN, Long, tickColor = Color.LIGHT_GREEN, arrowColor = Color.LIGHT_GREEN);
AddOrder(type = OrderType.SELL_TO_CLOSE, condition = close crosses XLP, tickColor = Color.LIGHT_GREEN, arrowColor = Color.LIGHT_GREEN);

def Short  = SMA10 crosses below SMA50 and EMA5 < EMA13;
def XShort = EMA5 crosses above EMA13;
def SP    = if short then open[-1] else SP[1];
def XSP   = SP - XPct * SP;


AddOrder(OrderType.SELL_TO_OPEN, Short);
AddOrder(type = OrderType.BUY_TO_CLOSE, condition = close crosses XSP);
Thank you for this SleepyZ! Made some changes and starting to learn a little more about coding myself through all of this.
Is there a way to create alerts in TOS when the strategy triggers a buy/sell in either the long or short?
 
Thank you for this SleepyZ! Made some changes and starting to learn a little more about coding myself through all of this.
Is there a way to create alerts in TOS when the strategy triggers a buy/sell in either the long or short?

This should help. Add the code to the bottom of your script

Ruby:
input alerts = yes;
alert(alerts and Long, "Long", Alert.bar, Sound.chimes);
alert(alerts and Short, "Short", Alert.bar, sound.ding);
 
This should help. Add the code to the bottom of your script
So I noticed that the alert sounds trigger perfectly but the strategy itself doesn't long or short right away and on average looks like it doesn't trigger the buy/sell for 2/3 hourly candles. Is there a reason why the long and short are so much later than the actual strategy triggers?
 
Last edited:
Thread starter Similar threads Forum Replies Date
S SMA or EMA begin calculating at the open? Questions 1
W SMA/EMA with variable length? Questions 1
X DEMA EMA and SMA Questions 2
D SMA/EMA Labels Help Questions 8
C Colored EMA/SMA Questions 5

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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