#// This source code is subject to the terms of the Mozilla Public License 2.0 at https:/
#// © loxx
#indicator("ATR-Adaptive, Smoothed Laguerre RSI [Loxx]", shorttitle = "ATRASLRSI [Loxx]"
# created by Sam4Cok@Samer800 - 06/2024 - request from useThinkScript.com member
input Source = Close;
input SmoothingPeriod = 5;#, "Source Smoothing Period"
input atrLength = 15;#, "ATR Period"
input UpperBoundary = 85;#, "Upper Boundary"
input LowerBoundary = 15;#, "Lower Boundary"
def na = Double.NaN;
def last = IsNaN(close);
script lagfiltosc {
input src = close;
input per = 15;
def _gamma = 1.0 - 10.0 / (per + 9.0);
def L0 = CompoundValue(1, (1 - _gamma) * src + _gamma * L0[1], 0);
def L1 = CompoundValue(1, -_gamma * L0 + L0[1] + _gamma * L1[1], 0);
def L2 = CompoundValue(1, -_gamma * L1 + L1[1] + _gamma * L2[1], 0);
def L3 = CompoundValue(1, -_gamma * L2 + L2[1] + _gamma * L3[1], 0);
def CU1 = if (L0 >= L1) then L0 - L1 else 0;
def CD1 = if (L0 >= L1) then 0 else L1 - L0;
def CU2 = if (L1 >= L2) then CU1 + L1 - L2 else CU1;
def CD2 = if (L1 >= L2) then CD1 else CD1 + L2 - L1;
def CU = if (L2 >= L3) then CU2 + L2 - L3 else CU2;
def CD = if (L2 >= L3) then CD2 else CD2 + L3 - L2;
def out = if (CU + CD != 0) then CU / (CU + CD) else 0;
plot return = out;
}
def atr = ATR(Length = atrLength);
def prevMax = fold j = 2 to atrLength + 1 with p =atr do
if p < atr[j] then atr[j] else p;
def prevMin = fold k = 2 to atrLength + 1 with q =atr do
if q > atr[k] then atr[k] else q;
def srcout = ExpAverage(Source, SmoothingPeriod);
def _max = if prevMax > atr then prevMax else atr;
def _min = if prevMin < atr then prevMin else atr;
def _coeff = if (_min != _max) then 1 - (atr - _min) / (_max - _min) else 0.5;
def val = lagfiltosc(srcout, atrLength * (_coeff + 0.75));
def plval = val * 100;
def plUp = if last then na else UpperBoundary;
def plDn = if last then na else LowerBoundary;
#// https://www.tradingview.com/v/uXB6f9Y5/
#// © traderharikrishna
#indicator("SARMACD")
input averageType = AverageType.EXPONENTIAL;
input fastLength = 12;
input slowLength = 26;
input macdLength = 9;
def sar = ParabolicSAR();
def cross = Crosses(Source, sar);
def sa = if cross then Source else sa[1];
def dir = if sar < Source then 1 else -1;
def m = MovingAverage(averageType, Source, fastLength) - MovingAverage(averageType, Source, slowLength);
def s = MovingAverage(averageType, m, macdLength);
def h = m - s;
def SigLine = s;
def Hist = if m then m else Hist[1];
# Filter
def volFilter = volume >= Average(volume, 2);
#/ Entry/Exit conditions
input labelOptions = {default "Summary", "Detailed", "Don't Show"};
input signalOptions = {default "Enter Long/Short", "Exit Long/Short", "Both Entry/Exit", "Don't Show Signals"};
input tradeDirection = {default "Both", "Long", "Short"}; # "Trading Direction"
input TPSLCondition = {default "Percentage", "ATR"};
input lotSize = 15;
input takeProfitFactor = 3.5; # "Take Profit"
input stopLossFactor = 2.5; # "Stop Loss"
def full = labelOptions == labelOptions."Detailed";
def summ = labelOptions == labelOptions."Summary" or full;
def allSig = signalOptions == signalOptions."Both Entry/Exit";
def enter = signalOptions == signalOptions."Enter Long/Short" or allSig;
def exit = signalOptions == signalOptions."Exit Long/Short" or allSig;
def long = tradeDirection == tradeDirection."Long" or tradeDirection == tradeDirection."Both";
def short = tradeDirection == tradeDirection."Short" or tradeDirection == tradeDirection."Both";
def nATR = TPSLCondition == TPSLCondition."ATR";
def nPer = TPSLCondition == TPSLCondition."Percentage";
def tpBuy;
def slBuy;
def tpSell;
def slSell;
Switch (TPSLCondition) {
Case "Percentage" :
tpBuy = (open * (1 + takeProfitFactor / 1000));
slBuy = (open * (1 - stopLossFactor / 1000));
tpSell = (open * (1 - takeProfitFactor / 1000));
slSell = (open * (1 + stopLossFactor / 1000));
Default :
tpBuy = (open + atr * takeProfitFactor);
slBuy = (open - atr * stopLossFactor);
tpSell = (open - atr * takeProfitFactor);
slSell = (open + atr * stopLossFactor);
}
#// Strategy logic
def inTrades;
def entryLong;
def closeLong;
def entryShort;
def closeShort;
def win;
def los;
def tpLvlLong;
def slLvlLong;
def tpLvlShort;
def slLvlShort;
def Trade = CompoundValue(1, inTrades[1], 0);
def crossUp = (plval > plDn) and (plval[1] <= plDn[1]);
def crossDn = (plval < plUp) and (plval[1] >= plUp[1]);
def LongEntry = barNumber()>0 and Hist > SigLine and crossUp and volFilter and close > sa;
def ShortEntry = barNumber()>0 and Hist < SigLine and crossDn and volFilter and close < sa;
def longCond = long and LongEntry;
def shotCond = short and ShortEntry;
def exitLong = ((high >= tpLvlLong[1]) or (close < slLvlLong[1]));
def exitShort = ((low <= tpLvlShort[1]) or (close > slLvlShort[1]));
if longCond[1] and !Trade {
inTrades = 1;
entryLong = open;
tpLvlLong = tpBuy;
slLvlLong = slBuy;
tpLvlShort = na;
entryShort = na;
slLvlShort = na;
closeLong = 0;
closeShort = 0;
win = 0;
los = 0;
} else if exitLong and Trade > 0 {
inTrades = 0;
entryLong = na;
entryShort = na;
tpLvlLong = na;
slLvlLong = na;
tpLvlShort = tpLvlShort[1];
slLvlShort = slLvlShort[1];
closeLong = (if (high >= tpLvlLong[1]) then tpLvlLong[1] else slLvlLong[1]) - entryLong[1];
win = if closeLong > 0 then 1 else 0;
los = if closeLong > 0 then 0 else 1;
closeShort = 0;
} else if shotCond[1] and !Trade {
inTrades = -1;
entryLong = na;
entryShort = open;
tpLvlLong = na;
slLvlLong = na;
tpLvlShort = tpSell;
slLvlShort = slSell;
closeLong = closeLong[1];
closeShort = 0;
win = 0;
los = 0;
} else if exitShort and Trade < 0 {
inTrades = 0;
entryLong = na;
entryShort = na;
tpLvlLong = tpLvlLong[1];
slLvlLong = slLvlLong[1];
tpLvlShort = na;
slLvlShort = na;
closeLong = na;
closeShort = entryShort[1] - (if (low <= tpLvlShort[1]) then tpLvlShort[1] else slLvlShort[1]);
win = if closeShort > 0 then -1 else 0;
los = if closeShort > 0 then 0 else -1;
} else {
inTrades = Trade;
entryLong = entryLong[1];
entryShort = entryShort[1];
tpLvlLong = tpLvlLong[1];
slLvlLong = slLvlLong[1];
tpLvlShort = tpLvlShort[1];
slLvlShort = slLvlShort[1];
closeLong = 0;
closeShort = 0;
win = 0;
los = 0;
}
def longSig = inTrades > 0 and !inTrades[1];
def shortSig = inTrades < 0 and !inTrades[1];
def exitL = !inTrades and inTrades[1] > 0;
def exitS = !inTrades and inTrades[1] < 0;
def extLloc = (entryLong[1] + closeLong);
def extSloc = (entryShort[1] - closeShort);
def enterLong = if !last and entryLong then entryLong else na;
def entrShort = if !last and entryShort then entryShort else na;
def TPlong = if tpLvlLong and inTrades > 0 then tpLvlLong else na;
def SLlong = if slLvlLong and inTrades > 0 then slLvlLong else na;
def TPshort = if tpLvlShort and inTrades < 0 then tpLvlShort else na;
def SLshort = if slLvlShort and inTrades < 0 then slLvlShort else na;
plot exitPointL = if exitL[-1] then extLloc[-1] else na;
plot exitPointS = if exitS[-1] then extSloc[-1] else na;
exitPointL.SetPaintingStrategy(PaintingStrategy.SQUARES);
exitPointS.SetPaintingStrategy(PaintingStrategy.SQUARES);
exitPointL.AssignValueColor(if high > TPlong[-1] then Color.CYAN else Color.MAGENTA);
exitPointS.AssignValueColor(if low < TPshort[-1] then Color.CYAN else Color.MAGENTA);
AddCloud(enterLong, SLlong, Color.DARK_RED, Color.DARK_RED, yes);
AddCloud(TPlong, enterLong, Color.DARK_GREEN, Color.DARK_GREEN, yes);
AddCloud(SLshort, entrShort, Color.DARK_RED, Color.DARK_RED, yes);
AddCloud(entrShort, TPshort, Color.PLUM, Color.DARK_RED, yes);
def totLong = TotalSum(if exitL then 1 else 0);
def totShort = TotalSum(if exitS then 1 else 0);
def tottrd = totLong + totShort;
def WinL = TotalSum(win>0); #TotalSum(if tradeL > 0 then 1 else 0);
def WinS = TotalSum(win<0); #if tradeS > 0 then 1 else 0);
def totWin = (WinL + WinS);
def winRt = Round(totWin / tottrd * 100, 1);
def winRtL = Round(WinL / totLong * 100, 1);
def winRtS = Round(WinS / totShort * 100, 1);
def amtL = TotalSum(if exitL then closeLong else 0) * lotSize;
def amtS = TotalSum(if exitS then closeShort else 0) * lotSize;
def pl = RoundDown(amtL + amtS, 2);
def plL = RoundDown(amtL, 2);
def plS = RoundDown(amtS, 2);
AddChartBubble(enter and shotCond[1] and !Trade, high, "S", Color.RED);
AddChartBubble(enter and longCond[1] and !Trade, low, "L", Color.GREEN, no);
AddLabel(summ, "WinRate(" + winRt + "%, " + tottrd + ")", if winRt >= 50 then Color.GREEN else Color.RED);
AddLabel(full and long, "Long(" + winRtL + "%, " + totLong + ")", if winRtL >= 50 then Color.GREEN else Color.RED);
AddLabel(full and short, "Short(" + winRtS + "%, " + totShort + ")", if winRtS >= 50 then Color.GREEN else Color.RED);
AddLabel(full and long, "AmtLong($" + plL + ")", if amtL > 0 then Color.GREEN else Color.RED);
AddLabel(full and short, "AmtShort($" + plS + ")", if amtS > 0 then Color.GREEN else Color.RED);
AddLabel(summ, "P/L($" + pl + ")", if pl > 0 then Color.GREEN else if pl < 0 then Color.RED else Color.GRAY);
#-- END of CODE