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;

input StartTradingTime = 1000;
input EndTradingTime = 1600;

def TradeTime = SecondsFromTime(StartTradingTime) >= 0 and SecondsTillTime(EndTradingTime) >=0;

if (ShowDay and TradeTime) {
.
.
.
} else
Double.Nan;}
I get the following error:

1707599673597.png


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
 
Solution

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

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:

AddLabel(yes, if TradeTime and ShowDay then "True" else "False");

if ShowDay and TradeTime then
AddOrder(OrderType.BUY_TO_OPEN, buy, open[0], order_size, Color.GREEN, Color.GREEN, name = "B")
else double.nan;

if ShowDay and TradeTime then
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?
AddOrder(OrderType.BUY_TO_OPEN, if TradeTime and ShowDay then buy else double.nan, open[-1], order_size, Color.GREEN, Color.GREEN, name = "B");

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:
AddOrder(OrderType.BUY_TO_OPEN, TradeTime and ShowDay and buy, open[-1], order_size, Color.GREEN, Color.GREEN, name = "B");

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

#https://usethinkscript.com/threads/accessing-items-in-macd-diff.17075/
#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;

input StartTradingTime = 1000;
input EndTradingTime = 1600;

def TradeTime = SecondsFromTime(StartTradingTime) >= 0 and SecondsTillTime(EndTradingTime) >=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
else trade[1];


def buy = (trade[0] == 1 and trade[1] == -1);
def sell = (trade[0] == -1 and trade[1] == 1);
input order_size = 100;

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

AddOrder(OrderType.BUY_TO_OPEN, TradeTime and ShowDay and buy, open[-1], order_size, Color.GREEN, Color.GREEN, name = "B");

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


# alerts , sounds
#alert(condition, text, alert type, sound);

alert(buy, "crossed up" ,alert.BAR, sound.DING); # Sound.Ding, higher , up sound
alert(sell, "crossed down" ,alert.BAR, sound.bell); # Sound.Bell, lower , down sound
 
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 >
#def TradeTime = SecondsFromTime(StartTradingTime) >= 0 and SecondsTillTime(EndTradingTime) >= 0;
def TradeTime = SecondsFromTime(StartTradingTime) >= 0 and SecondsTillTime(EndTradingTime) > 0;

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 ( !lastBarOfDay and TradeTime and ShowDay and buy ) then do a trade
https://usethinkscript.com/threads/finding-the-first-and-last-bar-of-the-day-in-thinkorswim.526/

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.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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