Convert some indicators from MotiveWave platform to ThinkorSwim

  • Thread starter samiranadhikari
  • Start date
samiranadhikari

samiranadhikari

Member
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:
D

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 ;)



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:
A

Art

Member
@samiranadhikari do you mind sharing that swing trading indicator?
 
BenTen

BenTen

Administrative
Staff
VIP
@Art @otmane That's the Big Hand indicator. You can find it here.
 
  • Like
Reactions: Art
lowtrade

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


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

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


This is a 6 months chart and we could see the crossover.
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.
 
lowtrade

lowtrade

Member
VIP
@diazlaz Thank you for looking into it but it still not working. Please try with stock AXLA and you could see the difference between 3Mo and 6Mo chart.
 

Similar threads

Top