How to hold a value for few candles.

rengagopal

Member
VIP
I need thinkscript help.

I try to hold on to the close price when close price > hullmovingavg(close,16) I call this as 'Buy price', and count number of candles till close > hullmovingavg(close,16) I call this as ' hold count'. then when price < hullmovingavg(close,16) calculate the average price by (close / buyprice) / hold count.

Thank you!!
 
I need thinkscript help.

I try to hold on to the close price when close price > hullmovingavg(close,16) I call this as 'Buy price', and count number of candles till close > hullmovingavg(close,16) I call this as ' hold count'. then when price < hullmovingavg(close,16) calculate the average price by (close / buyprice) / hold count.

Thank you!!

Thank you I got it.


if you ask a question, then later figure it out, please post the code that worked for you.
there may be others with similar questions
 
I need thinkscript help.

I try to hold on to the close price when close price > hullmovingavg(close,16) I call this as 'Buy price', and count number of candles till close > hullmovingavg(close,16) I call this as ' hold count'. then when price < hullmovingavg(close,16) calculate the average price by (close / buyprice) / hold count.

Thank you!!

here is a code to calculate 2 average close prices.
...when price is above a hull average and when it is below it.
a colored bubble with the average is drawn below the bars.

Code:
# hull_avg_cross_00b

#https://usethinkscript.com/threads/how-to-hold-a-value-for-few-candles.15158/
#how to hold a value for few candles.
#rengagopal  4/12

#long,
#close crosses above hullmovingavg(close,16)
# calculate the average price by (close / short buyprice) / short hold count.

#count number of candles,
#while close > hullmovingavg(close,16)

#short,
#close crosses below hullmovingavg(close,16)
# calculate the average price by (close / long buyprice) / long hold count.

# count bars while close < hull

def na = Double.NaN;
def bn = BarNumber();

def avg1_price = close;
input avg1_len = 16;
#input avg1_type =  AverageType.EXPONENTIAL;
input avg1_type =  AverageType.hull;
def ma1 = MovingAverage(avg1_type, avg1_price, avg1_len);

plot z1 = ma1;
z1.SetDefaultColor(Color.CYAN);
z1.HideBubble();


def dir;
def longpr;
def longcnt;
def shortpr;
def shortcnt;
def diffavg;
def avg;
if bn == 1 then {
  dir = 0;
  longpr = 0;
  longcnt = 0;
  shortpr = 0;
  shortcnt = 0;
  avg = 0;
  diffavg = 0;
} else if close crosses above ma1 then {
  dir = 1;
  longpr = close;
  longcnt = 1;
  shortpr = 0;
  shortcnt = 0;
  avg = close;
  diffavg = 0;
} else if close crosses below ma1 then {
  dir = -1;
  longpr = 0;
  longcnt = 0;
  shortpr = close;
  shortcnt = 1;
  avg = close;
  diffavg = 0;
} else if dir[1] > 0 then {
  dir = dir[1];
  longpr = longpr[1];
  longcnt = longcnt[1] + 1;
  shortpr = 0;
  shortcnt = 0;
  avg = (fold j1 = 0 to longcnt
   with q1
   do q1 + getvalue(close,j1)) / longcnt;
  diffavg = (fold i1 = 0 to longcnt
   with p1
   do p1 + (getvalue(close,i1) - getvalue(longpr,i1))) / longcnt;
} else if dir[1] < 0 then {
  dir = dir[1];
  longpr = 0;
  longcnt = 0;;
  shortpr = shortpr[1];
  shortcnt = shortcnt[1] + 1;
  avg = (fold j2 = 0 to shortcnt
   with q2
   do q2 + getvalue(close,j2)) / shortcnt;
  diffavg = (fold i2 = 0 to shortcnt
   with p2
   do p2 + (getvalue(close,i2) - getvalue(shortpr,i2))) / shortcnt;
} else {
  dir = 0;
  longpr = longpr[1];
  longcnt = longcnt[1];
  shortpr = shortpr[1];
  shortcnt = shortcnt[1];
  avg = 0;
  diffavg = 0;
}

def ravg = round(avg,2);
def rdiffavg = round(diffavg,2);


input avg_bubble = yes;
addchartbubble(avg_bubble, low*0.999,
ravg
, (if dir > 0 then color.green else if dir < 0 then color.red else color.gray), no);


input diffavg_bubble = no;
addchartbubble(diffavg_bubble, low*0.999,
rdiffavg
, (if dir > 0 then color.green else if dir < 0 then color.red else color.gray), no);


input test1 = no;
addchartbubble(test1, low*0.999,
dir + "\n" +
close + "\n" +
longpr + "\n" +
longcnt + "\n" +
shortpr + "\n" +
shortcnt + "\n" +
ravg + "\n" +
rdiffavg
, (if dir > 0 then color.green else if dir < 0 then color.red else color.gray), no);

#
 

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