EMA crossover strategy is not plotting

adw108

New member
VIP
Hello, scripters. I hope you're all well.

I'm hoping to get a little guidance on my first strategy. It's a simple moving-average cross-over strategy.

I mimicked a couple of different things, including the default strategies found in TOS. However, I can't seem to make it trigger any transactions. I figured that I'm missing something obvious and thought I should ask the experts...

I'm working on a simple cross-over strategy. Here's what I've got so far...

Code:
#INPUTS
input price = close;
input tradeSize = 2;
input fastLength = 5;
input slowLength = 12;
input averageType = averageType.EXPONENTIAL;

#def FastMA = MovingAverage(averageType, price, fastLength);
#def SlowMA = MovingAverage(averageType, price, slowLength);

#DEFINITIONS
def buySignal = fastLength crosses above slowLength;
def sellSignal = fastLength crosses below slowLength;
def sellShortSignal = fastLength crosses below slowLength;
def buyToCover = fastLength crosses above slowLength;

#SALES
AddOrder(orderType.BUY_TO_OPEN, buySignal, open[-1], tradeSize, Color.CYAN, Color.CYAN);
AddOrder(orderType.SELL_TO_CLOSE, sellSignal, open[-1], tradeSize, Color.GREEN, Color.GREEN);
AddOrder(orderType.SELL_TO_OPEN, sellShortSignal, open[-1], tradeSize, Color.ORANGE, Color.ORANGE);
AddOrder(orderType.BUY_TO_CLOSE, buyToCover, open[-1], tradeSize, Color.BLUE, Color.BLUE);

#PLOT
plot FastMA = MovingAverage(averageType, price, fastLength);
plot SlowMA = MovingAverage(averageType, price, slowLength);
FastMA.SetDefaultColor(GetColor(1));
SlowMA.SetDefaultColor(GetColor(2));

I can get the EMAs to plot but I cannot get any sales. Do you think someone can help with this?

Thank you in advance!
 
The code was sort of incomplete. Try this:

Code:
#INPUTS
input price = close;
input tradeSize = 2;
input fastLength = 5;
input slowLength = 12;
input averageType = averageType.EXPONENTIAL;

def FastMA = MovingAverage(averageType, price, fastLength);
def SlowMA = MovingAverage(averageType, price, slowLength);

plot mva_1 = FastMa;
plot mva_2 = SlowMa;

#DEFINITIONS
def buySignal = FastMA crosses above SlowMA;
def sellSignal = FastMA crosses below SlowMA;
def sellShortSignal = FastMA crosses below SlowMA;
def buyToCover = FastMA crosses above SlowMA;

#SALES
AddOrder(orderType.BUY_TO_OPEN, buySignal, open[-1], tradeSize, Color.CYAN, Color.CYAN);
AddOrder(orderType.SELL_TO_CLOSE, sellSignal, open[-1], tradeSize, Color.GREEN, Color.GREEN);
AddOrder(orderType.SELL_TO_OPEN, sellShortSignal, open[-1], tradeSize, Color.ORANGE, Color.ORANGE);
AddOrder(orderType.BUY_TO_CLOSE, buyToCover, open[-1], tradeSize, Color.BLUE, Color.BLUE);
 
@BenTen

I would like to know if someone can help me with the a script to calculate the delta/difference between the 8 and 21 day ema as a lower study ? Any help would be appreciated. Thanks in advance.
 
#INPUTS input price = close; input tradeSize = 2; input fastLength = 5; input slowLength = 12; input averageType = averageType.EXPONENTIAL; def FastMA = MovingAverage(averageType, price, fastLength); def SlowMA = MovingAverage(averageType, price, slowLength); plot mva_1 = FastMa; plot mva_2 = SlowMa; #DEFINITIONS def buySignal = FastMA crosses above SlowMA; def sellSignal = FastMA crosses below SlowMA; def sellShortSignal = FastMA crosses below SlowMA; def buyToCover = FastMA crosses above SlowMA; #SALES AddOrder(orderType.BUY_TO_OPEN, buySignal, open[-1], tradeSize, Color.CYAN, Color.CYAN); AddOrder(orderType.SELL_TO_CLOSE, sellSignal, open[-1], tradeSize, Color.GREEN, Color.GREEN); AddOrder(orderType.SELL_TO_OPEN, sellShortSignal, open[-1], tradeSize, Color.ORANGE, Color.ORANGE); AddOrder(orderType.BUY_TO_CLOSE, buyToCover, open[-1], tradeSize, Color.BLUE, Color.BLUE);
Hello BenTen,

With exact code snippet, i am not getting EMA on my chart still. Anything else i need to check out?
 
Hello BenTen,

With exact code snippet, i am not getting EMA on my chart still. Anything else i need to check out?

Here is the code in a link and it is also below. https://tos.mx/ysehRkR

This code has to be put into a Strategy vs Study. The above snippet you posted was not formatted and the '#' descriptions made the whole snippet a text file vs thinkscript

Screenshot-2022-11-02-071119.png
Ruby:
#INPUTS
input price = close;
input tradeSize = 2;
input fastLength = 5;
input slowLength = 12;
input averageType = AverageType.EXPONENTIAL;
def FastMA = MovingAverage(averageType, price, fastLength);
def SlowMA = MovingAverage(averageType, price, slowLength);
plot mva_1 = FastMA;
plot mva_2 = SlowMA;
#DEFINITIONS
def buySignal = FastMA crosses above SlowMA;
def sellSignal = FastMA crosses below SlowMA;
def sellShortSignal = FastMA crosses below SlowMA;
def buyToCover = FastMA crosses above SlowMA;
#SALES
AddOrder(OrderType.BUY_TO_OPEN, buySignal, open[-1], tradeSize, Color.CYAN, Color.CYAN);
AddOrder(OrderType.SELL_TO_CLOSE, sellSignal, open[-1], tradeSize, Color.GREEN, Color.GREEN);
AddOrder(OrderType.SELL_TO_OPEN, sellShortSignal, open[-1], tradeSize, Color.ORANGE, Color.ORANGE);
AddOrder(OrderType.BUY_TO_CLOSE, buyToCover, open[-1], tradeSize, Color.BLUE, Color.BLUE);
 

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
236 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