Profit Maximizer For ThinkOrSwim

Author states:
This combines the MOST and SuperTrend Indicators. They are very good at trend following systems but conversely their performance is not bright in sideways market conditions like most of the other indicators.

Profit Maximizer - PMax tries to solve this problem. PMax combines the powerful sides of MOST (Moving Average Trend Changer) and SuperTrend (ATR price detection) in one indicator. It reduces the number of false signals in sideways and give more reliable trade signals.

PMax is easy to determine the trend and can be used in any type of markets and instruments. It does not repaint.

The first parameter in the PMax indicator set by the three parameters is the period/length of ATR.

The second Parameter is the Multiplier of ATR which would be useful to set the value of distance from the built in Moving Average.

The most important parameter is the Moving Average Length and type.
PMax will be much sensitive to trend movements if Moving Average Length is smaller. And vice versa, will be less sensitive when it is longer.

As the period increases it will become less sensitive to little trends and price actions.
LteLQgA.png


Hi @samer800

Thanks for your amazing help in converting the pine script inductors to think script. Can you please me with this one?

Regards

https://www.tradingview.com/script/sU9molfV/
 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Hi @samer800

Thanks for your amazing help in converting the pine script inductors to think script. Can you please me with this one?

Regards

https://www.tradingview.com/script/sU9molfV/
check the below:

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https:
#// © KivancOzbilgic
#//developer: @KivancOzbilgic
#//author: @KivancOzbilgic
#study("Profit Maximizer","PMax", overlay=true, format=format.price, precision=2, resolution="")
# Converted and mod by Sam4Cok@Samer800    - 05/2024
input colorBars = yes;
input timeframe = {default "Chart", "Manual"};
input manualTimeframe = AggregationPeriod.FIFTEEN_MIN;
input MovAvgType = { "SMA",default "EMA", "WMA", "DEMA", "TMA", "VAR", "WWMA", "ZLEMA", "TSF", "HULL"};
input Source = FundamentalType.HL2;     # "Source"
input atrLength = 10;          # "ATR Length"
input atrMultiplier = 3.0;     # "ATR Multiplier"
input movAvgLength = 10;       # "Moving Average Length"
input ChangeAtrCalculationMethod = yes ; # "Change ATR Calculation Method ?"
input NormalizeAtr = no ;      # "Normalize ATR ?"
input ShowMovingAverage = yes; # "Show Moving Average?"
input showMovAvgCrossingSignals = yes;    # "Show Crossing Signals?"
input showPriceCrossingSignals = no;      #  "Show Price/Pmax Crossing Signals?"
input highlighterOnOff = yes;  # "Highlighter On/Off ?"

def na = Double.NaN;
def last = IsNaN(close);
def ohlc = if highlighterOnOff then ohlc4 else na;
def src; def tr;
switch (timeframe) {
case "Manual":
    src = Fundamental(FundamentalType = Source, Period = manualTimeframe);
    tr = TrueRange(high(Period = manualTimeframe), close(Period = manualTimeframe), low(Period = manualTimeframe));
default :
    src = Fundamental(FundamentalType = Source);
    tr = TrueRange(high, close, low);
}

#-- Colors
DefineGlobalColor("exUp", CreateColor(0, 230, 118));
DefineGlobalColor("Up",  CreateColor(178, 223, 219));
DefineGlobalColor("exDn", CreateColor(239, 83, 80));
DefineGlobalColor("Dn",  CreateColor(255, 205, 210));
DefineGlobalColor("macd",  CreateColor(66, 107, 230));
DefineGlobalColor("signal",  CreateColor(230, 0, 0));
#--- Functions
#pine_linreg(src, len, offset=0) =>
script linreg {
    input src = close;
    input len = 100;
    input offset = 0;
    def na = Double.NaN;
    def bar_index = IsNaN(close);
    def x_sum = if bar_index then na else
                fold i = 0 to len with p do
                 p + i;
    def xx_sum = if bar_index then na else
                fold ii = 0 to len with pp do
                 pp + ii * ii;
    def y_sum = Sum(src, len);
    def xy_sum = fold j = 0 to len with q do
                  q  + j * GetValue(src, len - j - 1);
    def slope = (len * xy_sum - x_sum * y_sum) / (len * xx_sum - x_sum * x_sum);
    def intercept = (y_sum - slope * x_sum) / len;
    def linreg = intercept + slope * (len - offset - 1);
    plot out = linreg;
}
script f_var {
    input src = close;
    input length = 20;
    def alpha = 2 / (length + 1);
    def ud1 = if src > src[1] then src - src[1] else 0;
    def dd1 = if src < src[1] then src[1] - src else 0;
    def UD  = Sum(ud1, 9);
    def DD  = Sum(dd1, 9);
    def vCMO = (UD - DD) / (UD + DD);
    def CMO = if IsNaN(vCMO) then 0.0 else AbsValue(vCMO);
    def VAR = (alpha * CMO * src) + (1 - alpha * CMO) * If(IsNaN(VAR[1]), 0, VAR[1]);
    plot out = if length == 1 then src else VAR;
}
#Wwma_Func(src, length) =>
script Wwma_Func {
    input src = close;
    input length = 2;
    def alpha = 1 / length;
    def WWMA = alpha * src + (1 - alpha) * If(IsNaN(WWMA[1]), 0, WWMA[1]);
    plot return = WWMA;
}
#Zlema_Func(src, length) =>
script Zlema_Func {
    input src = close;
    input length = 2;
    def zxLag = if length / 2 == Round(length / 2, 0) then length / 2 else (length - 1) / 2;
    def zxEMAData = src + (src - src[zxLag]);
    def ZLEMA = ExpAverage(zxEMAData, length);
    plot return = ZLEMA;
}
#Tsf_Func(src, length) =>
script Tsf_Func {
    input src = close;
    input length = 2;
    def lrc = Inertia(src, length);
    def lrc1 = linreg(src, length, 1);
    def lrs = lrc - lrc1;
    def TSF = Inertia(src, length) + lrs;
    plot retur = TSF;
}
#ma(src, length, type) =>
script getMA {
    input src = close;
    input length = 80;
    input type   = "SMA";
    def ma =
     if type == "SMA"   then Average(src, length) else
     if type == "EMA"   then ExpAverage(src, length) else
     if type == "WMA"   then WMA(src, length) else
     if type == "DEMA"  then DEMA(src, length) else
     if type == "TMA"   then MovAvgTriangular(src, length) else
     if type == "VAR"   then f_var(src, length) else
     if type == "WWMA"  then WWMA_Func(src, length) else
     if type == "ZLEMA" then Zlema_Func(src, length) else
     if type == "TSF"   then Tsf_Func(src, length) else
     if type == "HULL"  then HullMovingAvg(src, length) else Average(src, length);
    plot result = ma;
}
def atr2 = Average(tr, atrLength);
def atr1 = WildersAverage(tr, atrLength);
def nATR = atrMultiplier * (if ChangeAtrCalculationMethod then atr1 else atr2);

def longStop; def shortStop;
def MAvg = getMA(src, movAvgLength, MovAvgType);
def longStop1 = if NormalizeAtr then MAvg - nATR / src else MAvg - nATR;
def longStopPrev = if (isNaN(longStop[1]) or !longStop[1]) then longStop1 else longStop[1];
    longStop = if MAvg > longStopPrev then Max(longStop1, longStopPrev) else longStop1;
def shortStop1 = if NormalizeAtr then MAvg + nATR / src else MAvg + nATR;
def shortStopPrev = if isNaN(shortStop[1]) or !shortStop[1] then shortStop1 else shortStop[1];
    shortStop = if MAvg < shortStopPrev then Min(shortStop1, shortStopPrev) else shortStop1;
def dir;
def dir1 = if isNaN(dir[1]) or !dir[1] then 1 else dir[1];
    dir = if dir1 ==-1 and MAvg > shortStopPrev then 1 else
          if dir1 == 1 and MAvg < longStopPrev then -1 else dir1;
def PMax = if dir == 1 then longStop else shortStop;

#-- Signals
def buySignalk   = if showMovAvgCrossingSignals and !NormalizeAtr then (MAvg Crosses Above PMax) else 0;
def sellSignallk = if showMovAvgCrossingSignals and !NormalizeAtr then (MAvg Crosses Below PMax) else 0;
def buySignalc   = if showPriceCrossingSignals then (src  Crosses Above PMax) else 0;
def sellSignallc = if showPriceCrossingSignals then (src  Crosses Below PMax) else 0;

def touchUp = if isNaN(touchUp[1]) then 0 else if touchUp[1] > 10 then 0 else
              if (dir!=dir[1]) then 0 else
              if (src Crosses Above PMax) and dir<0 then touchUp[1] + 1 else touchUp[1];
def touchDn = if isNaN(touchDn[1]) then 0 else if touchDn[1] > 10 then 0 else
              if (dir!=dir[1]) then 0 else
              if (src Crosses Below PMax) and dir>0 then touchDn[1] + 1 else touchDn[1];

#-- plot
plot MovAvgLine = if ShowMovingAverage then MAvg else na; # "Moving Avg Line"
plot pALLUp = if dir>0 then PMax else na; # "PMax",
plot pALLDn = if dir<0 then PMax else na; # "PMax",
pALLUp.SetLineWeight(2);
pALLDn.SetLineWeight(2);
MovAvgLine.AssignValueColor(if MAvg>PMax then Color.CYAN else Color.MAGENTA);
pALLup.AssignValueColor(if touchDn > 0 then Color.DARK_GREEN else Color.GREEN);
pALLDn.AssignValueColor(if touchUp > 0 then Color.DARK_RED else Color.LIGHT_RED);

#--Cloud
AddCloud(ohlc, pALLUp, Color.DARK_GREEN);
AddCloud(pALLDn, ohlc, Color.DARK_RED);

#--Bubbles
AddchartBubble(buySignalk, PMax, "B", Color.GREEN, no);
AddchartBubble(sellSignallk, PMax, "S", Color.RED);

AddchartBubble(touchUp!=touchUp[1] and touchUp==1, low, "B", Color.DARK_GREEN, no);
AddchartBubble(touchDn!=touchDn[1] and touchDn==1, high, "S", Color.DARK_RED);


#-- end of code
 
does this repaint or reset? some of the scripts I find on here don't repaint, but other ones repaint frequently.
 
does this repaint or reset? some of the scripts I find on here don't repaint, but other ones repaint frequently.

This is a version of Supertrend. Supertrends do not repaint.

Repainting indicators on the forum are prefixed with "repaints"

Most of the indicators on the forum will update on the current candle with every tick until the bar closes.
This is not considered repainting.

Additionally, this script has the ability to overlay a higher timeframe.
If you choose the MTF version then, of course, it will repaint.
It is not possible to predict what the final 1-hour candle trend will be on a 5-min chart. Therefore, the 12 5-min candles that comprise that 1-hour with repaint with every tick until the 1-hour candle closes.
 
Last edited by a moderator:

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
483 Online
Create Post

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top