#// 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