# Trying to convert Market Mode indicator to ThinkorSwim

T

#### TETRIZ

##### New member
I am working on my first script to convert a tradingview indicator. As a beginner, I have trouble understanding thinkscript. Appreciate your help.

This as close as I can get as a beginner after burning multiple days. The plotting is good enough. I have tested multiple tickers manually. However, the values are skewed by .1

Appreciate if one of the experts can extend the hand with big heart in fixing the skewed numbers. I am expecting the correction is needed in fold statement or compound value usage.

The script in Conversion:

Code:
``````#version = 0.1
#Ehler’s description of the method and indicator:
#There just has to be a better way to determine whether the market is in a cycle mode or trend mode other than #gazing at squiggly lines on your computer screen.
#One approach is to compare the peak swings of the cycle mode to the amplitude of the trend mode.
#We do this by capturing the peaks and averaging these peaks in a relatively long moving average. #Correspondingly, one can capture the valleys and also average the valleys in a relatively long moving average. #We then take a fraction of these averages as the thresholds between a trend mode and a cycle mode. If the trend #is above the upper threshold the
#market is in an uptrend. If the trend is below the lower threshold the market is in a downtrend. When the trend #falls between the two threshold levels the market is in a cycle mode.
#The setting of the fraction of the averaged peaks and valleys to be used to establish the thresholds is somewhat #subjective and can be adjusted to fit your trading style. Personally, we prefer to trade in the cycle mode and #therefore tend to set the thresholds relatively far apart.
#In this way one can stop swing trading when the market is clearly in a trend.
declare lower;
input MMPeriod = 20;# "Market mode period"
input Delta = 0.5; # "Delta"
input Fraction = 0.8; # "Fraction"
input LevelUpAveragePeriod = 10; # "Level up average period"
input LevelDownAveragePeriod = 10; # "Level down average period"

def Price = (high + low) / 2;
def MPI = Double.Pi;#3.1415926535897;
def bar_index = BarNumber();
def calc = 2.0 * MPI / MMPeriod;
def beta = Cos(calc / (2 * MPI) * 360);
def calc2 = 4.0 * MPI * Delta / MMPeriod;
def gamma   = 1.0 / Cos(calc2 / (4.0 * MPI * Delta) * 360);
def alpha   = gamma - Sqrt(gamma * gamma - 1.0);
def alphaUp = 2.0 / (1.0 + LevelUpAveragePeriod);
def alphaDn = 2.0 / (1.0 + LevelDownAveragePeriod);
def dPeriod = 2 * MMPeriod;

#coulld be something worng in using Compondvalue or fold statement
# original script
#if bar_index>MMPeriod
#   workbp := 0.5*(1.0-alpha)*(Price-Price)+beta*(1.0+alpha)*workbp-alpha*workbp
#    mean = mean+workbp[k]
#     inc:=k
# mean := mean/inc
# workapeak := workapeak
# if mean>0
#    workapeak:=workapeak+alphaUp*(mean-workapeak)
#workavaley:=workavaley
#if mean<0
#fractionUp := Fraction*workapeak
#fractionDn := Fraction*workavaley

def workbp  = 0.5 * (1.0 - alpha) * (Price - Price) + beta * (1.0 + alpha) * workbp - alpha * workbp;

def mean = (fold i = 0 to dPeriod-1 with p = workbp
do CompoundValue(40, workbp[i], workbp)) / dPeriod ;

def workapeak = if mean > 0 then workapeak + alphaUp * (mean - workapeak) else workapeak;

def workavaley = if mean < 0 then workavaley + alphaDn * (mean - workavaley) else workavaley;

def fractionUp = Fraction * workapeak;
def fractionDn = Fraction * workavaley;

plot meanp = mean;
meanp.SetPaintingStrategy(PaintingStrategy.LINE);
meanp.AssignValueColor(if (mean > fractionUp) then Color.DARK_GREEN else if (mean < fractionDn ) then Color.DARK_RED else Color.WHITE);
meanp.SetLineWeight(2);

plot fupP = fractionUp ;
fupP.AssignValueColor(CreateColor(157,9,173));
plot fdnP = fractionDn ;
fdnP.AssignValueColor(CreateColor(157,9,173));
def buy = crosses(mean,  fractionUp, CrossingDirection.ABOVE);
def sell = crosses(mean, fractionDn, CrossingDirection.BELOW);