Average Price gain loss

BlueRaven

New member
I'm trying to make an indicator (Probably just a label output) that will give the average gain and average loss in price for a stock...

So like an average up day would be 3 points and an average down day would be 4 points, allowing you to look for strong up or down days.

This is what I have so far... except it counts the 0's in the average which greatly skews it. I'm not sure if there is something the else statement could be or if there is a way to make an average skip certain numbers, or a different way to grab gains/losses.
input length = 50;
input avg_type = averagetype.simple;

def gain = if close > close[1] then close - close[1] else 0;

def avggain = MovingAverage(avg_type,gain, length);

AddLabel(yes, Concat("Avg Gain: ", avggain), Color.GREEN);

If this can't be done then I will probably just look for wider than average ATR, it just wont be as specific.
 
I'm trying to make an indicator (Probably just a label output) that will give the average gain and average loss in price for a stock...

So like an average up day would be 3 points and an average down day would be 4 points, allowing you to look for strong up or down days.

This is what I have so far... except it counts the 0's in the average which greatly skews it. I'm not sure if there is something the else statement could be or if there is a way to make an average skip certain numbers, or a different way to grab gains/losses.


If this can't be done then I will probably just look for wider than average ATR, it just wont be as specific.

this calculates the average of the up moves and an average of the down moves, (from close to close)
if finds the difference of those 2 and plots a histogram.

several labels show,
..the averages,
..the total up dollar moves and down
..the quantity of up moves and down


Code:
#avg_Price_gainloss

#https://usethinkscript.com/threads/average-price-gain-loss.18154/

declare lower;

# orig
#def gain = if close > close[1] then close - close[1] else 0;
#def avggain = MovingAverage(avg_type,gain, length);

def na = double.nan;
def bn = barnumber();

input length = 50;
input avg_type = AverageType.SIMPLE;

def up = close > close[1];
def dwn = close < close[1];
def diff = close - close[1];

def upgain = if up then diff else 0;
def uptotal = Sum(upgain, length);
def upqty = Sum(up, length);
def upavggain = uptotal / upqty;

def dwngain = if dwn then diff else 0;
def dwntotal = Sum(dwngain, length);
def dwnqty = Sum(dwn, length);
def dwnavggain = dwntotal / dwnqty;

def avgdiff = upavggain + dwnavggain;

AddLabel(yes, ("Avg up Gain: " + upavggain), Color.GREEN);
AddLabel(yes, ("Avg down Gain: " + dwnavggain), Color.GREEN);
AddLabel(yes, " " , Color.BLACK);

AddLabel(yes, ("past bars " + length), Color.MAGENTA);
AddLabel(yes, ("up total " + uptotal), Color.MAGENTA);
AddLabel(yes, ("down total " + dwntotal), Color.MAGENTA);
AddLabel(yes, ("up moves " + upqty), Color.MAGENTA);
AddLabel(yes, ("down moves " + dwnqty), Color.MAGENTA);

input show_lines = no;
plot z = 0;
plot zu = if show_lines then upavggain else na;
plot zd = if show_lines then dwnavggain else na;

plot z2 = avgdiff;
z2.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
z2.AssignValueColor(
     if avgdiff > 0 and avgdiff > avgdiff[1] then color.cyan
else if avgdiff > 0 and avgdiff < avgdiff[1] then color.blue
else if avgdiff < 0 and avgdiff > avgdiff[1] then color.yellow
else if avgdiff < 0 and avgdiff < avgdiff[1] then color.red
else color.gray);
#
 

Attachments

  • img1.JPG
    img1.JPG
    108.2 KB · Views: 73
I figured it out on my on own in the time the post took to be manually approved.

Maybe it can help someone else also.

You can also use it to scan to find big up/down days.

input length = 50;


def gain = sum(if close > close[1] then close - close[1] else 0,length);

def upday = close>close[1];

def gcount = sum(upday,length);

def avggain = round(gain/gcount);

def loss = sum(if close<close[1] then close-close[1] else 0, length);

def downday = close<close[1];

def dcount = sum(downday,length);

def avgloss = round(absvalue(loss/dcount));

def today = round(absvalue(close-close[1]));

def gainratio = floor(if close>close[1] then (today/avggain)*100 else 0);
def lossratio = floor(if close<close[1] then (today/avgloss)*100 else 0);



AddLabel(yes, Concat("Avg Gain: ", avggain), Color.GREEN);
AddLabel(yes, Concat("Avg Loss: ", avgloss), Color.red);
AddLabel(yes and close>close[1] , Concat("Change: ", today), Color.GREEN);
AddLabel(yes and close<close[1] , Concat("Change: ", today), Color.red);
AddLabel(yes and close==close[1] , Concat("Change: ", today), Color.yellow);


AddLabel(yes and close>close[1], Concat("G/L Ratio: ", Concat(gainratio,"%")), Color.GREEN);
AddLabel(yes and close<close[1], Concat("G/L Ratio: ", Concat(lossratio,"%")), Color.red);
AddLabel(yes and close==close[1], Concat("G/L Ratio: ", Concat(lossratio,"%")), Color.yellow);
 

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