# Need help with RTH script{}

#### Peppermshrimp

##### New member
VIP
In order to have a MACD Strategy only work during regular trading hours, I've added the following around the code already working code:
#Set up time of day to trade

input NumberDays = 10;
def ShowDay = if GetDay() >= GetLastDay()-NumberDays then 1 else 0;

.
.
.
} else
Double.Nan;}
I get the following error:

I would appreciate any help, thank you.

Last edited:
Solution
In order to have a MACD Strategy only work during regular trading hours, I've added the following around the code already working code:

I get the following error:

View attachment 20995

I would appreciate any help, thank you.

can't use def or input within if then's

no need to put a bunch of code within if then.
add 1 code group after the other.
then alter the plot formulas , with if then, to only plot when both conditions are true
In order to have a MACD Strategy only work during regular trading hours, I've added the following around the code already working code:

I get the following error:

View attachment 20995

I would appreciate any help, thank you.

can't use def or input within if then's

no need to put a bunch of code within if then.
add 1 code group after the other.
then alter the plot formulas , with if then, to only plot when both conditions are true

Thank you for your response. I'll work on that today.

can't use def or input within if then's

no need to put a bunch of code within if then.
add 1 code group after the other.
then alter the plot formulas , with if then, to only plot when both conditions are true

I am still getting an error, after moving the if-statement. (I am obviously not understanding the syntax of the if-statement) Here's the code segment:

else double.nan;

AddOrder(OrderType.SELL_TO_CLOSE, sell, open[0], order_size, Color.RED, Color.RED, name = "S")
else double.nan;

Before adding the if-statement, I tested the values of TradeTime and ShowDay using AddLabel. The values were both set to TRUE. Here is the error message:

Last edited by a moderator:
I am still getting an error, after moving the if-statement. (I am obviously not understanding the syntax of the if-statement) Here's the code segment:

Before adding the if-statement, I tested the values of TradeTime and ShowDay using AddLabel. The values were both set to TRUE. Here is the error message:

View attachment 21072
You have the syntax wrong. You need to put the condition inside of the AddOrder function. So would look something like: AddOrder(OrderType.BUY_TO_OPEN, TradeTime and ShowDay);

Hopefully that helps.

You have the syntax wrong. You need to put the condition inside of the AddOrder function. So would look something like: AddOrder(OrderType.BUY_TO_OPEN, TradeTime and ShowDay);

Hopefully that helps.
I apologize, but that is still a little cryptic for me. Are you saying that I do something like this?

I did try this, and although I didn't get a syntax error, it still shows buy/sell during extended hours, which is what I'm trying to avoid. It seems like I would want the if-statement around the entire AddOrder.

I'm sorry I'm so slow at this, it seems so counter-intuitive to other programming languages I know.

Last edited:
I apologize, but that is still a little cryptic for me. Are you saying that I do something like this?

I did try this, and although I didn't get a syntax error, it still shows buy/sell during extended hours, which is what I'm trying to avoid. It seems like I would want the if-statement around the entire AddOrder.
The second input in the AddOrder function is a conditional...which means you don't need a separate if around it or inside it. I am assuming that 'buy' is something you have defined as the trading condition you want. If that's the case, then the statement should look like:

If that isn't correct, post the entire code here and I will see if I can help.

It is still not working. I don't get an error, but the buy/sell are showing up in after-hours trading, which is what I'm trying to remove. Here's the code.

# macd_histo_colors_arrows_strat

#Accessing items in MACD.Diff
#Peppermshrimp 11/2

#Is there a way to reference 4 different ‘trends’ in MACD.Diff,
#other than just to change the colors?
#I am referring to ‘Negative and Down’, ‘Negative and Up’,
#‘Positive and Down’, and ‘Positive and Up’. (see images below)
#I would like to set up a Strategy that puts an up arrow on the
#chart when these switch between ‘Negative and Down’ to ‘Negative
#and Up’, and puts a down arrow on the chart when they switch from
#‘Positive and Down’ to ‘Positive and Up’. The only thinkscript I can
#find with respect to these 4 trends is when the color is being defined.

#Set up time of day to trade

def Today = if GetDay() == GetLastDay() then 1 else 0;

input NumberDays = 10;
def ShowDay = if GetDay() >= GetLastDay()-NumberDays then 1 else 0;

def bn = barnumber();

def ref1 = if bn == 1 then close else ref1[1];

# macd
# TD Ameritrade IP Company, Inc. (c) 2007-2023
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;

def Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
def Avg = MovingAverage(averageType, Value, MACDLength);
def Diff = Value - Avg;

# color changes, lowest and highest
def Diff_color_num =
if Diff > 0 and Diff < Diff[1] then -1
else if Diff < 0 and Diff > Diff[1] then 1
else 0;

# require x colors in a row to be considered a new color
input consecutive_bars = 2;
def qty = consecutive_bars;
def numsum = if Sum(Diff_color_num, qty) == (Diff_color_num * qty) then 1 else 0;

def new_color = if (numsum and getvalue(diff_color_num, qty) != diff_color_num) then 1 else 0;

# create a trade signal , from the 1st of consequetive signals
def trade = if bn == 1 then 0
else if Diff_color_num == -1 and new_color then -1
else if Diff_color_num == 1 and new_color then 1

input order_size = 100;

AddLabel(yes, if TradeTime and ShowDay then "True" else "False"); #Verifying Time of day

AddOrder(OrderType.SELL_TO_CLOSE, TradeTime and ShowDay and sell, open[-1], order_size, Color.RED, Color.RED, name = "S");

It is still not working. I don't get an error, but the buy/sell are showing up in after-hours trading, which is what I'm trying to remove. Here's the code.

the 2nd bar in after hours has trades because,
the end time (1600) doesn't exist in normal trading hours (or any period), so you don't set it >= , just >

addorders use open[-1] , the bar after a signal, so sometimes the 1st bar ( and 2nd) in after hours could have a buy or sell.

if you really don't want trades in after hours, go here and copy this code that finds the last bar of the day. and incorporate that into condition logic in addorder. ( use ! in front of lastbarofday to use the inverse of it)

if you don't want trades in after hours, just turn off after hours, so it's not visible.

changing the >= to > worked. Thank you so much! I am not doing this just to turn off after-hour trading, I want to be able to set/change the trading times and view the reports for that specific time. For example, only view the report for the first hour or the last hour of the trading day.

Thanks again!

Now that you mention it...I WOULD like to have the script close out any open orders at the end of the set time period (not necessarily the end of the day). I have printed out the code for the end of day script that you posted, hopefully I can figure it out from that.

87k+ Posts
211 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?