5 min first bar's position compared to ema 21

rta

New member
Please help with this code, I don't know coding, so copy and past from different indicators of our forum members.
I want 5 min first bar's position compared to ema 21, and study should stop after closing of first bar.
Here is the code I compiled,

input agg = AggregationPeriod.DAY;
input StartTime = 0930;
input EndTime = 0935;

def c = close;
def o = open;
def ORActive = if SecondsFromTime(StartTime) > 0 and
SecondsTillTime(EndTime) >= 0
then 1
else 0;

def BC = if ORActive and !ORActive[1]
then c
else Double.NaN;

def BO = if ORActive and !ORActive[1]
then o
else Double.NaN;



input length = 21;

def AvgExp = ExpAverage(close, length);

plot trend = if o > avgexp and c > avgexp and c > o then 111 else if o > avgexp and c > avgexp and c < o then 11 else if o < avgexp and c > avgexp and c > o then 1 else if o < avgexp and c < avgexp and c < o then -111 else if o < avgexp and c < avgexp and c > o then -11 else if o > avgexp and c < avgexp and c < o then -1 else 0;

assignBackgroundColor(if trend > 0 then color.dark_green else if trend < 0 then color.Dark_red else color.Gray);

After first bar is over, it changes for second bar.
Please help....
Thanks
 
Please help with this code, I don't know coding, so copy and past from different indicators of our forum members.
I want 5 min first bar's position compared to ema 21, and study should stop after closing of first bar.
Here is the code I compiled,

input agg = AggregationPeriod.DAY;
input StartTime = 0930;
input EndTime = 0935;

def c = close;
def o = open;
def ORActive = if SecondsFromTime(StartTime) > 0 and
SecondsTillTime(EndTime) >= 0
then 1
else 0;

def BC = if ORActive and !ORActive[1]
then c
else Double.NaN;

def BO = if ORActive and !ORActive[1]
then o
else Double.NaN;



input length = 21;

def AvgExp = ExpAverage(close, length);

plot trend = if o > avgexp and c > avgexp and c > o then 111 else if o > avgexp and c > avgexp and c < o then 11 else if o < avgexp and c > avgexp and c > o then 1 else if o < avgexp and c < avgexp and c < o then -111 else if o < avgexp and c < avgexp and c > o then -11 else if o > avgexp and c < avgexp and c < o then -1 else 0;

assignBackgroundColor(if trend > 0 then color.dark_green else if trend < 0 then color.Dark_red else color.Gray);

After first bar is over, it changes for second bar.
Please help....
Thanks

if you only want color changes for the first bar, then you need to alter the trend formula.

add this to the existing formula
plot trend = !ORActive then 0 else if....
 
Please help with this code, I don't know coding, so copy and past from different indicators of our forum members.
I want 5 min first bar's position compared to ema 21, and study should stop after closing of first bar.
Here is the code I compiled,

input agg = AggregationPeriod.DAY;
input StartTime = 0930;
input EndTime = 0935;

def c = close;
def o = open;
def ORActive = if SecondsFromTime(StartTime) > 0 and
SecondsTillTime(EndTime) >= 0
then 1
else 0;

def BC = if ORActive and !ORActive[1]
then c
else Double.NaN;

def BO = if ORActive and !ORActive[1]
then o
else Double.NaN;



input length = 21;

def AvgExp = ExpAverage(close, length);

plot trend = if o > avgexp and c > avgexp and c > o then 111 else if o > avgexp and c > avgexp and c < o then 11 else if o < avgexp and c > avgexp and c > o then 1 else if o < avgexp and c < avgexp and c < o then -111 else if o < avgexp and c < avgexp and c > o then -11 else if o > avgexp and c < avgexp and c < o then -1 else 0;

assignBackgroundColor(if trend > 0 then color.dark_green else if trend < 0 then color.Dark_red else color.Gray);

After first bar is over, it changes for second bar.
Please help....
Thanks
I’m intrigued. May I ask how you implement aforementioned strategy?
 
if you only want color changes for the first bar, then you need to alter the trend formula.

add this to the existing formula
plot trend = !ORActive then 0 else if....
Hi halcyonguy,
Thanks for reply,
I was trying to create watchlist column, but +1, -1, 0 goes on changing on completion of each 5 Min bar.
I want study to stop after 1st bar.
Please healp.
Thanks

Than
I’m intrigued. May I ask how you implement aforementioned strategy?
Thanks for reply khpro59,
This was for watchlist column.. but it goes on working on each bar, should stop after 1st bar.
Please help.
 
Hi halcyonguy,
Thanks for reply,
I was trying to create watchlist column, but +1, -1, 0 goes on changing on completion of each 5 Min bar.
I want study to stop after 1st bar.
Please healp.
Thanks

Than

Thanks for reply khpro59,
This was for watchlist column.. but it goes on working on each bar, should stop after 1st bar.
Please help.

Perhaps this is over simplification and not what you want, but based upon what you wrote as your conditions, I think it might.

The optional bubbles are used for testing purposes. The image shows the same figures were used for both the 1m and 5m charts. The colors may change during the development of the bars, but once the range is complete, the color should not change.
Screenshot 2023-12-07 163403.png
Code:
input agg       = AggregationPeriod.FIVE_MIN;
input StartTime = 0930;
input EndTime   = 0935;

def c = close(period = agg);
def o = open(period = agg);

def ORActive = if SecondsFromTime(StartTime) >= 0 and
                  SecondsFromTime(EndTime) < 0
               then 1
               else 0;

input length = 21;
def avgexp   = ExpAverage(close(period = agg), length);

def trend    = if oractive and !oractive[-1]
               then if O > avgexp and C > avgexp and C > O
                    then 1
                    else if O < avgexp and C < avgexp and C < O
                    then -1
                    else 0
               else trend[1];

input showbubbles = yes;
AddChartBubble(showbubbles and ORActive and !oractive[-1], high , "C: " + C + "\nBO: " + O , if C > O then Color.GREEN else Color.RED);
AddChartBubble(showbubbles and ORActive and !ORActive[-1], high , "C: " + C + "\nAV: " + avgexp, if C > avgexp then Color.GREEN else Color.RED);
AddChartBubble(showbubbles and ORActive and !ORActive[-1], high , "O: " + O + "\nAV: " + avgexp, if O > avgexp then Color.GREEN else Color.RED);

AssignBackgroundColor(if trend > 0 then Color.DARK_GREEN else if trend < 0 then Color.DARK_RED else Color.GRAY );

#
 
Last edited:
Perhaps this is over simplification and not what you want, but based upon what you wrote as your conditions, I think it might.

The optional bubbles are used for testing purposes. The image shows the same figures were used for both the 1m and 5m charts. The colors may change during the development of the bars, but once the range is complete, the color should not change.
Hi SleepyZ,
Thank you very much for code, this has worked as per my requirement,
I have removed bubbles and used it as watchlist column.
working perfectly fine, Thank youuuuuuuu....
 
Perhaps this is over simplification and not what you want, but based upon what you wrote as your conditions, I think it might.

The optional bubbles are used for testing purposes. The image shows the same figures were used for both the 1m and 5m charts. The colors may change during the development of the bars, but once the range is complete, the color should not change.
Thanks for reply,
This is watchlist column scan of 1st 5 Min bar. I want to take trades on position of 1st bar compared to ema21. If 1st bar opens below 21 ema and closes above ema21 then most likely that day stock will remain in positive zone and I will take long trades. same way reverse is true.
if OHLC is below ema21 and ema 21 < ema50 and red bar then stock will remain below ema21 most of the day. Same way reverse is true. But if OHLC is below ema21 and ema 21 > ema50 and green bar then likely stock will rally up. same way reverse is true.

Please someone help.
add this to the existing formula
plot trend = !ORActive then 0 else if.... This is not working.
Can someone write full code, this will be helpful to all...
Thanks
 

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