Convert some indicators from MotiveWave platform to ThinkorSwim

I would love to see thinkscript for RSI bars as described in the following code for motivewave.

Code:
//input = price, user defined, default is closing price
//method = moving average (ma), user defined, default is SMMA
//period = user defined, default is 14
//top = user defined, default is 70
//bottom = user defined, default is 30
//MT = more than, LT = less than
//index = current bar number
diff = price - price[index-1];
up = 0, down = 0;
if (diff MT 0) up = diff;
else down = abs(diff);
avgUp = ma(method, index, period, UP);
avgDown = ma(method, index, period, DOWN);
RS = avgUp / avgDown;
RSI = 100 - ( 100 / (1 + RS));
//signals
setBarColor(RSI,top,bottom);
buy = crossedAbove(RSI, top);
sell = crossedBelow(RSI, bottom);

I would love to see thinkscript for the following code for vidya 2 indicator (motivewave). I found this indicator very useful.

Code:
//input = user defined, default is close
//period = user defined, default is 20
//alpha1 = user defined, default is .2
//alpha2 = user defined, default is .04
//prev = previous, index = current bar number
//std = stdDev = standard deviation
stdDev = std(index, period, input);
avStdDev = ma(method, index, period, stdDev);
prevVidya1 = isNull(price, vidya1[index-1]); //feedback
prevVidya2 = isNull(price, vidya2[index-1]); //feedback
ratio = stdDev/avStdDev;
Plot1: vidya1 = (alpha1 * ratio * price) + ((1-(alpha1*ratio)) * prevVidya1);
Plot2: vidya2 = (alpha2 * ratio * price) + ((1-(alpha2*ratio)) * prevVidya2);
//Signals
buy = crossedAbove(VIDYA1, VIDYA2);
sell = crossedBelow(VIDYA1, VIDYA2);

VIDYA2 Indicator by Chande and Kroll are types of Adaptive Moving Averages that use Standard Deviation and feedback in their calculation. Two paths are plotted with different alpha values.
 
Last edited by a moderator:

diazlaz

Well-known member
2019 Donor
VIP
@samiranadhikari here is the VIDYA2 Indicator - let me know how it is and if any changes are needed. Let us know how you typically use it ;)

T15gzbL.png


Ruby:
#VIDYA2 Indicator by Chande and Kroll
#
# @samiranadhikari request
#
# 2019.12.08 @diazlaz - initial port/interpretation.
# 2020.05.24 @diazlaz - fixed stdev caching isNaN on smaller amount of bars.
#
#Adaptive Moving Averages that use Standard Deviation and feedback in their calculation.
#Two paths are plotted with different alpha values.
#

declare upper;

#INPUTS

input price = close;
input period = 20;
input alpha1 = 0.2;
input alpha2 = 0.04;

#LOGIC
def stdDev = CompoundValue(1,StDev(price, period),close);
def avStdDev = Average(stdDev, period);
def ratio = stdDev/avStdDev;
def vidya1 = (alpha1 * ratio * price) + ((1-(alpha1*ratio)) * vidya1[1]);
def vidya2 = (alpha2 * ratio * price) + ((1-(alpha2*ratio)) * vidya2[1]);

def buy = Crosses(VIDYA1, VIDYA2, CrossingDirection.ABOVE);
def sell = Crosses(VIDYA1, VIDYA2, CrossingDirection.BELOW);
def sState = if buy then 100 else if sell then -100 else sState[1];

#PLOTS
input showPlots = yes;

plot pVidya1 = vidya1;
pVidya1.SetHiding(!showPlots);

plot pVidya2 = vidya2;
pVidya2.SetHiding(!showPlots);

# ARROWS
input showArrows = yes;
plot pUP = sState crosses above 0;
pUP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
pUP.SetDefaultColor(Color.GREEN);
pUP.SetLineWeight(2);

plot pDown = sState crosses below 0;
pDown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
pDown.SetDefaultColor(Color.RED);
pDown.SetLineWeight(2);

#COLORBARS
input showColorBars = yes;
AssignPriceColor(if showColorBars then
if IsNaN(sState) then Color.GRAY else
if sState > 0 then Color.GREEN else
  Color.RED
else
  COLOR.CURRENT
);

#LABELS
input showLabels = yes;

AddLabel(showLabels,"VIDYA2->", COLOR.CYAN);
AddLabel(showLabels, "Buy",
if isNaN(sState) then COLOR.DARK_GRAY else
If sState == 100 then COLOR.GREEN else COLOR.DARK_GRAY);

AddLabel(showLabels, "Sell",
if isNaN(sState) then COLOR.DARK_GRAY else
If sState == -100 then COLOR.RED else COLOR.DARK_GRAY);

#END of VIDYA2 Port from MotiveWave

Is this just the regular RSI in TOS painted as a Histogram Line? - do you have a screenshot of how is rendered in motivewave?
 
Last edited:

lowtrade

Member
VIP
@diazlaz @BenTen I hope you could help me with this since I don't know how to fix this problem.

I'm getting two different crossover points using 3 Months/1Day chart Vs 6 Months/1 Day chart for this indicator. I was told, we need to use the CompoundValue() statement to initialize each line at the value of the close for the first bar on the chart to fix it but I don't know how to do it.

This is a 3 months chart
Ovmc34j.jpg


This is a 6 months chart and we could see the crossover.
sq7DXEE.jpg
 

diazlaz

Well-known member
2019 Donor
VIP
@diazlaz @BenTen I hope you could help me with this since I don't know how to fix this problem.

I'm getting two different crossover points using 3 Months/1Day chart Vs 6 Months/1 Day chart for this indicator. I was told, we need to use the CompoundValue() statement to initialize each line at the value of the close for the first bar on the chart to fix it but I don't know how to do it.

This is a 3 months chart
Ovmc34j.jpg


This is a 6 months chart and we could see the crossover.
sq7DXEE.jpg
thanks for finding this.

here is the fix: please replace this line:

def stdDev = StDev(price, period);

with

def stdDev = CompoundValue(1,StDev(price, period),close);

I updated the main code above.

Thanks!
Laz.
 

josvil

New member
VIP
Good afternoon,

I'm Newbie here.
I'm very glad to joined this forum, i found a lot of useful indicators for a very good price. These programmers are awesome.

I would like to be able to use the VIDYA2 as a scanner, I tried to use it and it and no matching symbols always shows.
I would like to scan results for:
1. When the TIMEFRAME (TF) is in BUY OR SELL mode
2. When the BUY or UP arrow FIRE UP (moving averages crosses).

Thanks in advance guys,

Regards,

Jose
 
Last edited:

rad14733

Well-known member
VIP
@josvil All you need to do is reference the VIDYA2 Study and select the proper plot, pUp or pDown, is true for the Buy and Sell firing... The code would need to be modified for extended Buy or Sell mode, however...
 

josvil

New member
VIP
here's the SCRIPT.

#VIDYA2 Indicator by Chande and Kroll
#
# @samiranadhikari request
#
# 2019.12.08 @diazlaz - initial port/interpretation.
# 2020.05.24 @diazlaz - fixed stdev caching isNaN on smaller amount of bars.
#
#Adaptive Moving Averages that use Standard Deviation and feedback in their calculation.
#Two paths are plotted with different alpha values.
#

declare upper;

#INPUTS

input price = close;
input period = 20;
input alpha1 = 0.2;
input alpha2 = 0.04;

#LOGIC
def stdDev = CompoundValue(1,StDev(price, period),close);
def avStdDev = Average(stdDev, period);
def ratio = stdDev/avStdDev;
def vidya1 = (alpha1 * ratio * price) + ((1-(alpha1*ratio)) * vidya1[1]);
def vidya2 = (alpha2 * ratio * price) + ((1-(alpha2*ratio)) * vidya2[1]);

def buy = Crosses(VIDYA1, VIDYA2, CrossingDirection.ABOVE);
def sell = Crosses(VIDYA1, VIDYA2, CrossingDirection.BELOW);
def sState = if buy then 100 else if sell then -100 else sState[1];

#PLOTS
input showPlots = yes;

plot pVidya1 = vidya1;
pVidya1.SetHiding(!showPlots);

plot pVidya2 = vidya2;
pVidya2.SetHiding(!showPlots);

# ARROWS
input showArrows = yes;
plot pUP = sState crosses above 0;
pUP.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
pUP.SetDefaultColor(Color.GREEN);
pUP.SetLineWeight(2);

plot pDown = sState crosses below 0;
pDown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
pDown.SetDefaultColor(Color.RED);
pDown.SetLineWeight(2);

#COLORBARS
input showColorBars = yes;
AssignPriceColor(if showColorBars then
if IsNaN(sState) then Color.GRAY else
if sState > 0 then Color.GREEN else
Color.RED
else
COLOR.CURRENT
);

#LABELS
input showLabels = yes;

AddLabel(showLabels,"VIDYA2->", COLOR.CYAN);
AddLabel(showLabels, "Buy",
if isNaN(sState) then COLOR.DARK_GRAY else
If sState == 100 then COLOR.GREEN else COLOR.DARK_GRAY);

AddLabel(showLabels, "Sell",
if isNaN(sState) then COLOR.DARK_GRAY else
If sState == -100 then COLOR.RED else COLOR.DARK_GRAY);

#END of VIDYA2 Port from MotiveWave
 

MerryDay

Administrative
Staff member
Staff
VIP
@josvil you are correct, the scan does not return any equities.
My guess is that the recursion of the variable sState does not allow it to be used in scan on the TOS platform. If there is a work-around, it is above my skill level.
 
Last edited:

Similar threads

Top