# Positive Volume Index

#### ThinkStudy

##### New member
Anyway to do a Positive Volume Index on TOS that plots a moving average and has customizable inputs? [/IMG] [/IMG]

Last edited by a moderator:
Solution

The final script with priceclose feild working:

# ########################################################
# Positive Volume Index
# ########################################################

declare lower;

input AvgType = averageType.SIMPLE;
input Length = 50;
input priceclose = close;

def index = compoundValue(1, index + if (volume > volume and priceclose != 0) then 100 * (priceclose - priceclose) / priceclose else 0, 100);

plot PVI = index;
plot AVG = MovingAverage(AvgType, index, Length);
PVI.SetDefaultColor(GetColor(5));
AVG.SetDefaultColor(color.yellow);

#### ThinkStudy

##### New member
Here's what i got, but it doesn't work:

# Positive Volume Index

declare lower;

input AvgType = averageType.SIMPLE;
input Length = 50;
input priceclose = close;

def index = compoundValue(1, index + if (volume > volume and close != 0) then 100 * (close - close) / close else 0, 100);

plot PVI = index;
plot AVG = MovingAverage(AvgType, close, Length);
PVI.SetDefaultColor(GetColor(5));
AVG.SetDefaultColor(color.yellow);

Last edited by a moderator:

#### ThinkStudy

##### New member
im not too concerned about having ALL the Moving Average Type input options of Simple, Exponential, Double Exponential, Triple Exponential, Hull, Time Series, Triangular, Variable, VIDYA, Weighted and Welles Wilder. I jsut mainly want a moving average with the Field Inputs of Open, High, Low, Close, hl/2, hl/3, hlcc/4 and ohlc/4 , and the period length input.

Last edited by a moderator:

#### Slippage

##### Well-known member
plot AVG = MovingAverage(AvgType, close, Length);
You want an average of `index` instead of an average of `close`.

• ThinkStudy

#### ThinkStudy

##### New member
You want an average of `index` instead of an average of `close`.
Thank You! That did the trick.

# ########################################################
# Positive Volume Index
# ########################################################

declare lower;

input AvgType = averageType.SIMPLE;
input Length = 50;
input priceclose = close;

def index = compoundValue(1, index + if (volume > volume and close != 0) then 100 * (close - close) / close else 0, 100);

plot PVI = index;
plot AVG = MovingAverage(AvgType, index, Length);
PVI.SetDefaultColor(GetColor(5));
AVG.SetDefaultColor(color.yellow);

#### ThinkStudy

##### New member
You want an average of `index` instead of an average of `close`.
it doesnt look like the input priceclose feild is working, everything else is working on it though. do you know how to fix that?

#### Slippage

##### Well-known member
it doesnt look like the input priceclose feild is working, everything else is working on it though. do you know how to fix that?
priceclose is not used anywhere in the code.

Do you need it to work? If you use open or high or low instead of close does this still give you a useful result? The built in PositiveVolumeIndex study has no option for price type. It always uses close. So I would just delete the `input priceclose = close;` line.

If you want it to work then everywhere on the line of `def index =` should be priceclose instead of close.

• ThinkStudy

2019 Donor

#### Slippage

##### Well-known member

Ruby:
``````# ########################################################
# Positive Volume Index
# ########################################################

declare lower;

input AvgType = averageType.SIMPLE;
input Length = 50;

def index = compoundValue(1, index + if (volume > volume and close != 0) then 100 * (close - close) / close else 0, 100);

plot PVI = index;
plot AVG = MovingAverage(AvgType, index, Length);
PVI.SetDefaultColor(GetColor(5));
AVG.SetDefaultColor(color.yellow);``````

#### ThinkStudy

##### New member

The final script with priceclose feild working:

# ########################################################
# Positive Volume Index
# ########################################################

declare lower;

input AvgType = averageType.SIMPLE;
input Length = 50;
input priceclose = close;

def index = compoundValue(1, index + if (volume > volume and priceclose != 0) then 100 * (priceclose - priceclose) / priceclose else 0, 100);

plot PVI = index;
plot AVG = MovingAverage(AvgType, index, Length);
PVI.SetDefaultColor(GetColor(5));
AVG.SetDefaultColor(color.yellow);

#### conmayne

##### New member
VIP
Hi I've been trying to write code for this scanner. Specifically for when the 1. PVI has reversed direction and showing an inflection point positive . 2. This happens underneath the AVG line. 3. The PVI is not crossed up over the avg line 4. The average has a positive slope over the past 5 bars.

It runs but it gives mixed results and is driving me crazy!!!

script posvolume {
# Positive Volume Index
# ########################################################

# ########################################################
# Positive Volume Index
# ########################################################

declare lower;

input AvgType = averageType.SIMPLE;
input Length = 50;
input priceclose = close;

def index = compoundValue(1, index + if (volume > volume and priceclose != 0) then 100 * (priceclose - priceclose) / priceclose else 0, 100);

plot PVI = index;
plot AVG = MovingAverage(AvgType, index, Length);
PVI.SetDefaultColor(GetColor(5));
AVG.SetDefaultColor(color.yellow);

}

input AvgType = AverageType.EXPONENTIAL;
input Length = 30;
input priceclose = close;

def index = CompoundValue(1, index + if (volume > volume and priceclose != 0) then 100 * (priceclose - priceclose) / priceclose else 0, 100);

def PVI = index;
def AVG = MovingAverage(AvgType, index, Length);

def condition1 = Between(posvolume("length" = 30)."PVI", 0.99 * posvolume("length" = 30)."AVG", posvolume("length" = 30)."AVG") within 1 bar;
def condition2 = posvolume("length" = 30)."PVI" > posvolume("length" = 30)."PVI" within 1 bar;

def condition3 = PVI < AVG within 1 bar;

plot example = condition1 and condition2 and condition3 within 1 bar;

#### conmayne

##### New member
VIP
Hi I've been trying to write code for this scanner. Specifically for when the 1. AVG has reversed direction and showing an inflection point positive and positive slope for at least 2 bars. I also want to incorporate rsi and scan for when rsi is under the avg given the first condition is true. IS IT POSSIBLE TO REPLACE PVI WITH RSI ON THE STUDY???

THIS IS WHAT IM TALKING ABOUT VISUALLY FOR THE AVG LINE

input AvgType = AverageType.EXPONENTIAL;
input Length = 30;
input priceclose = close;

def index = CompoundValue(1, index + if (volume > volume and priceclose != 0) then 100 * (priceclose - priceclose) / priceclose else 0, 100);

def PVI = index;
def AVG = MovingAverage(AvgType, index, Length);

def condition2 = posvolume("length" = 30)."avg" > posvolume("length" = 30)."avg" within 1 bar;

def condition3 = RSI < AVG within 1 bar; (OBVIOUSLY I KNOW THIS WONT WORK BUT THIS IS THE CONDITION I WANT TO CODE FOR INCLUDED IN MY SCANNER AND STUDY)

plot example = condition2 and condition3 is true within 1 bar;

Last edited:

#### majidg

##### Member
Hi I've been trying to write code for this scanner. Specifically for when the 1. avg has reversed direction and showing an inflection point positive and postive slope for at least 2 bars. I also want to incorporate rsi and scan for when rsi is under the avg given the first condition is true.

THIS IS WHAT IM TALKING ABOUT VISUALLY FOR THE AVG LINE
It runs but it gives mixed results and is driving me crazy!!!

script posvolume {
# Positive Volume Index
# ########################################################

# ########################################################
# Positive Volume Index
# ########################################################

declare lower;

input AvgType = averageType.SIMPLE;
input Length = 50;
input priceclose = close;

def index = compoundValue(1, index + if (volume > volume and priceclose != 0) then 100 * (priceclose - priceclose) / priceclose else 0, 100);

plot PVI = index;
plot AVG = MovingAverage(AvgType, index, Length);
PVI.SetDefaultColor(GetColor(5));
AVG.SetDefaultColor(color.yellow);

}

input AvgType = AverageType.EXPONENTIAL;
input Length = 30;
input priceclose = close;

def index = CompoundValue(1, index + if (volume > volume and priceclose != 0) then 100 * (priceclose - priceclose) / priceclose else 0, 100);

def PVI = index;
def AVG = MovingAverage(AvgType, index, Length);

def condition2 = posvolume("length" = 30)."avg" > posvolume("length" = 30)."avg" within 1 bar;

def condition3 = RSI < AVG within 1 bar;

plot example = condition2 and condition3 is true within 1 bar;

#### majidg

##### Member
Hi Conmayne, you have two moving averages and the their inputs named the same, I believe they should be different, input and input1,....

#### majidg

##### Member
Ruby:
``````# ########################################################
# Positive Volume Index
# ########################################################

declare lower;

input AvgType = averageType.SIMPLE;
input Length = 50;

def index = compoundValue(1, index + if (volume > volume and close != 0) then 100 * (close - close) / close else 0, 100);

plot PVI = index;
plot AVG = MovingAverage(AvgType, index, Length);
PVI.SetDefaultColor(GetColor(5));
AVG.SetDefaultColor(color.yellow);``````
Hi, I have a question, when the Volume goes below Moving average, does it mean the momentum is decreasing and Bears are getting nervous?

#### conmayne

##### New member
VIP
Hi Conmayne, you have two moving averages and the their inputs named the same, I believe they should be different, input and input1,....

I adjusted That was a typo and thinkscript bug it does when adding that code to the scanner. My intentions are positive reversal of the AVG vol line (yellow) and and rsi that is under the Yellow line on the same study. I have no use for the PVI line(red)

#### conmayne

##### New member
VIP
Hi I've been trying to write code for this scanner. Specifically for when the 1. AVG has reversed direction and showing an inflection point positive and positive slope for at least 2 bars. I also want to incorporate rsi and scan for when rsi is under the avg given the first condition is true. IS IT POSSIBLE TO REPLACE PVI WITH RSI ON THE STUDY???

THIS IS WHAT IM TALKING ABOUT VISUALLY FOR THE AVG LINE

input AvgType = AverageType.EXPONENTIAL;
input Length = 30;
input priceclose = close;

def index = CompoundValue(1, index + if (volume > volume and priceclose != 0) then 100 * (priceclose - priceclose) / priceclose else 0, 100);

def PVI = index;
def AVG = MovingAverage(AvgType, index, Length);

def condition2 = posvolume("length" = 30)."avg" > posvolume("length" = 30)."avg" within 1 bar;

def condition3 = RSI < AVG within 1 bar; (OBVIOUSLY I KNOW THIS WONT WORK BUT THIS IS THE CONDITION I WANT TO CODE FOR INCLUDED IN MY SCANNER AND STUDY)

plot example = condition2 and condition3 is true within 1 bar;

UPDATE:

Solved my first problem it seems with this code:

input AvgType = AverageType.EXPONENTIAL;
input Length = 30;
input priceclose = close;

def index = CompoundValue(1, index + if (volume > volume and priceclose != 0) then 100 * (priceclose - priceclose) / priceclose else 0, 100);

def PVI = index;
def AVG = MovingAverage(AvgType, index, Length);

#def condition1 = Between(posvolume("length" = 30)."PVI", 0.99 * posvolume("length" = 30)."AVG", posvolume("length" = 30)."AVG") within 1 bar;
#def condition2 = posvolume("length" = 30)."PVI" > posvolume("length" = 30)."PVI" within 1 bar;

def condition3 = Avg > avg and avg < avg;

plot scan = condition3 is true within 1 bars ;

NOW:

Im trying to figure out how to use rsi in place of PVI. PVI is needed for the calcualtion of AVG it seems so thats my dilemna.