NOOB's 1st Strategy

smoss611

New member
So I've been trading a while, and I wanted to teach myself thinkScript. I have been tinkering with the code and using snippets from other sources, but I finally wrote my own very basic from scratch.
After deciphering the various errors, I finally got it to work. (Sort of)

I can get the script entered with no errors; however, I do not show any P/L at the bottom of my screen. It shows with other copied scripts, so I'm missing something basic.
I'm sure this is a bit of a softball, but thanks in advance.

Code:
# Trial 1

def SMAprice = close;
def SMAfast = 50;
def SMAslow = 200;

# MACD
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
def MACDAverageType = AverageType.EXPONENTIAL;
def ZeroLine = 0;

def buy = fastLength[1] crosses above slowLength[1] and fastLength[1] < ZeroLine[1] and SMAfast > SMAslow;
addOrder(OrderType.BUY_AUTO, buy, name = "CrossoverBUY, tradeSize = 100");

def sell = SMAfast crosses below SMAslow or fastLength crosses below slowLength;
addOrder(OrderType.SELL_TO_CLOSE, close, name = "CrossoverExit", tradeSize = 100);
 
Solution
So I've been trading a while, and I wanted to teach myself thinkScript. I have been tinkering with the code and using snippets from other sources, but I finally wrote my own very basic from scratch.
After deciphering the various errors, I finally got it to work. (Sort of)

I can get the script entered with no errors; however, I do not show any P/L at the bottom of my screen. It shows with other copied scripts, so I'm missing something basic.
I'm sure this is a bit of a softball, but thanks in advance.

Code:
# Trial 1

def SMAprice = close;
def SMAfast = 50;
def SMAslow = 200;

# MACD
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
def MACDAverageType = AverageType.EXPONENTIAL;
def ZeroLine = 0;

def buy = fastLength[1]...
So I've been trading a while, and I wanted to teach myself thinkScript. I have been tinkering with the code and using snippets from other sources, but I finally wrote my own very basic from scratch.
After deciphering the various errors, I finally got it to work. (Sort of)

I can get the script entered with no errors; however, I do not show any P/L at the bottom of my screen. It shows with other copied scripts, so I'm missing something basic.
I'm sure this is a bit of a softball, but thanks in advance.

Code:
# Trial 1

def SMAprice = close;
def SMAfast = 50;
def SMAslow = 200;

# MACD
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
def MACDAverageType = AverageType.EXPONENTIAL;
def ZeroLine = 0;

def buy = fastLength[1] crosses above slowLength[1] and fastLength[1] < ZeroLine[1] and SMAfast > SMAslow;
addOrder(OrderType.BUY_AUTO, buy, name = "CrossoverBUY, tradeSize = 100");

def sell = SMAfast crosses below SMAslow or fastLength crosses below slowLength;
addOrder(OrderType.SELL_TO_CLOSE, close, name = "CrossoverExit", tradeSize = 100);

Glad you tried!! What you wrote is more akin to a moving average study, which is built-in TOS (see code below). You will see that you were comparing the length values rather than the moving averages of those lengths. Once you correct that, look at the built-in MACD strategy and see how you might incorporate your idea from that into your revised script. If you are having problems, post that script here and someone will assist you.

Ruby:
#
# TD Ameritrade IP Company, Inc. (c) 2013-2021
#

input price = close;
input fastLength = 20;
input slowLength = 50;
input averageType = AverageType.EXPONENTIAL;

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

AddOrder(OrderType.BUY_AUTO, FastMA crosses above SlowMA, tickColor = GetColor(1), arrowColor = GetColor(1), name = "MovAvgTwoLinesStratLE");
AddOrder(OrderType.SELL_AUTO, FastMA crosses below SlowMA, tickColor = GetColor(2), arrowColor = GetColor(2), name = "MovAvgTwoLinesStratSE");
 
Solution

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

Glad you tried!! What you wrote is more akin to a moving average study, which is built-in TOS (see code below). You will see that you were comparing the length values rather than the moving averages of those lengths. Once you correct that, look at the built-in MACD strategy and see how you might incorporate your idea from that into your revised script. If you are having problems, post that script here and someone will assist you.
So I was "Describing" one part of the moving average without creating the calculation for it. Makes sense! Thanks for the reply (and not the solution). I would like to arrive at the solution on my own.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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