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.

Trading view script: https://www.tradingview.com/script/fE4pYynE-Market-Mode/

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
#credits: https://www.tradingview.com/script/fE4pYynE-Market-Mode/
#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[2])+beta*(1.0+alpha)*workbp[1]-alpha*workbp[2]
  #    mean = mean+workbp[k]
   #     inc:=k
   # mean := mean/inc
   # workapeak := workapeak[1]
   # if mean>0
    #    workapeak:=workapeak[1]+alphaUp*(mean-workapeak[1])
    #workavaley:=workavaley[1]
    #if mean<0
     #   workavaley:=workavaley[1]+alphaDn*(mean-workavaley[1])
    #fractionUp := Fraction*workapeak
    #fractionDn := Fraction*workavaley

def workbp  = 0.5 * (1.0 - alpha) * (Price - Price[2]) + beta * (1.0 + alpha) * workbp[1] - alpha * workbp[2];

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[1] + alphaUp * (mean - workapeak[1]) else workapeak[1];

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

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

plot buyP = if buy then fractionup else Double.NaN;
buyp.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
buyp.AssignValueColor(COlor.LIME);

plot sellP = if sell then fractionDn else Double.NaN;
sellP.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
sellP.AssignValueColor(COlor.LIGHT_ORANGE);
 
Last edited:

Similar threads

Top