Simple MA Crossover Strategy with Target/Stops

Scotty B

New member
Hey All,
Probably an easy problem, but I'm trying to backtest a simple strategy that uses a crossover of an MA for entry and set hard targets and stops. The application will be to trade /MES on the 5min for quick 10pt scalps. So, buy/sell signal when the crossover happens and set stops at 5pts away with 10pt target. My problems is that entry signal generates just fine but the positions aren't closing out properly. For instance, I'll get a long entry signal but no sell signal, especially in choppy action. I'm still learning scripting and the addOrder function is probably the source of the problem. Any help would be appreciated. (The Short orders have been commented out for easier testing, but would like those to work as well)
Thanks!

Code:
# Strategy for moving average crossover
# with set targets and stop loss

# Inputs
input fastLength = 13;
input slowLength = 21;
input averageType = AverageType.HULL;
input Size = 1;
input offsetType = {percent, default value, tick};
input Stop = 5;
input Target = 10;

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

def entryPrice = entryPrice();

# Determine type of stops
def mult;
switch (offsetType) {
case percent:
    mult = entryPrice / 100;
case value:
    mult = 1;
case tick:
    mult = tickSize();
}

# Define Entry Signals
def LongE = FastMA crosses above SlowMA;
def ShortE = FastMA crosses below SlowMA;

# Entry Orders
AddOrder(OrderType.BUY_AUTO, LongE, Close[0], Size, tickColor = GetColor(1), arrowColor = GetColor(1), name = "LE" + Close[0]);
#AddOrder(OrderType.SELL_AUTO, ShortE, Close[0], Size, tickColor = GetColor(2), arrowColor = GetColor(2), name = "SE" + Close[0]);

# Define Exit criteria
def LongTarget = entryPrice + target * mult;
def LongStop = entryPrice - Stop * mult;
def ShortTarget = entryPrice - target * mult;
def ShortStop = entryPrice + Stop * mult;

# Close position at Target or Stop
addOrder(OrderType.SELL_TO_CLOSE, high >= LongTarget, LongTarget, Size, tickColor = GetColor(6), arrowColor = GetColor(6), name = "LTarget" + LongTarget);
addOrder(OrderType.SELL_TO_CLOSE, low <= LongStop, LongStop,  Size, tickColor = GetColor(6), arrowColor = GetColor(6), name = "LStop" + LongStop);
#addOrder(OrderType.SELL_TO_CLOSE, low <= ShortTarget, open[-1], Size, tickColor = GetColor(6), arrowColor = GetColor(6), name = "STarget");
#addOrder(OrderType.SELL_TO_CLOSE, high >= ShortStop, open[-1], Size,  tickColor = GetColor(6), arrowColor = GetColor(6), name = "SStop");
 
Can you show an example of it not working? I ran it and it seems to be working as it's currently defined. I would suggest changing your targets into Plots so that you can visually see them, makes it easier to see whether it's working or not.
 

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

Hey All,
Probably an easy problem, but I'm trying to backtest a simple strategy that uses a crossover of an MA for entry and set hard targets and stops. The application will be to trade /MES on the 5min for quick 10pt scalps. So, buy/sell signal when the crossover happens and set stops at 5pts away with 10pt target. My problems is that entry signal generates just fine but the positions aren't closing out properly. For instance, I'll get a long entry signal but no sell signal, especially in choppy action. I'm still learning scripting and the addOrder function is probably the source of the problem. Any help would be appreciated. (The Short orders have been commented out for easier testing, but would like those to work as well)
Thanks!

Code:
# Strategy for moving average crossover
# with set targets and stop loss

# Inputs
input fastLength = 13;
input slowLength = 21;
input averageType = AverageType.HULL;
input Size = 1;
input offsetType = {percent, default value, tick};
input Stop = 5;
input Target = 10;

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

def entryPrice = entryPrice();

# Determine type of stops
def mult;
switch (offsetType) {
case percent:
    mult = entryPrice / 100;
case value:
    mult = 1;
case tick:
    mult = tickSize();
}

# Define Entry Signals
def LongE = FastMA crosses above SlowMA;
def ShortE = FastMA crosses below SlowMA;

# Entry Orders
AddOrder(OrderType.BUY_AUTO, LongE, Close[0], Size, tickColor = GetColor(1), arrowColor = GetColor(1), name = "LE" + Close[0]);
#AddOrder(OrderType.SELL_AUTO, ShortE, Close[0], Size, tickColor = GetColor(2), arrowColor = GetColor(2), name = "SE" + Close[0]);

# Define Exit criteria
def LongTarget = entryPrice + target * mult;
def LongStop = entryPrice - Stop * mult;
def ShortTarget = entryPrice - target * mult;
def ShortStop = entryPrice + Stop * mult;

# Close position at Target or Stop
addOrder(OrderType.SELL_TO_CLOSE, high >= LongTarget, LongTarget, Size, tickColor = GetColor(6), arrowColor = GetColor(6), name = "LTarget" + LongTarget);
addOrder(OrderType.SELL_TO_CLOSE, low <= LongStop, LongStop,  Size, tickColor = GetColor(6), arrowColor = GetColor(6), name = "LStop" + LongStop);
#addOrder(OrderType.SELL_TO_CLOSE, low <= ShortTarget, open[-1], Size, tickColor = GetColor(6), arrowColor = GetColor(6), name = "STarget");
#addOrder(OrderType.SELL_TO_CLOSE, high >= ShortStop, open[-1], Size,  tickColor = GetColor(6), arrowColor = GetColor(6), name = "SStop");
Did you ever get the rest of the code figured out?
Thanks
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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