# Indicator for TOS
#//@technicalwhale
#strategy("Trading BUY/SELL Signal",overlay=true)
# Converted and mod by Sam4Cok@Samer800 - 07/2024
input colorBars = no;
input higherResolution = AggregationPeriod.THIRTY_MIN; # "Resolution"
input cciLength = 10; # "CCI"
input atrLength = 2; # "ATR"
input atrMultiplier = 1.0;
input signalFilterType = {Default "Higher Timeframe", "Trend Direction", "No Filter"};
input showTpSlArea = yes;
input tpMultiplier = 4.0; # "Take Profit"
input slMultiplier = 3.0; # "Stop Loss"
def na = Double.NaN;
def last = IsNaN(close);
def sigHTF = signalFilterType == signalFilterType."Higher Timeframe";
def sigTrend = signalFilterType==signalFilterType."Trend Direction";
def current = GetAggregationPeriod();
def tf = Max(higherResolution, current);
def lastTF = IsNaN(close(Period = tf));
def h = high;
def c = close;
def l = low;
def hTF = high(Period = tf)[1];
def cTF = close(Period = tf)[1];
def lTF = low(Period = tf)[1];
script f_cci {
input price = close;
input length = 14;
def avgSrc = Average(price, length);
def linDev = LinDev(price, length);
def CCI = if !linDev then 0 else (price - avgSrc) / linDev / 0.015;
plot out = if IsNaN(CCI) then 0 else CCI;
}
script calcx {
input h = high;
input c = close;
input l = low;
input thisCCI = close;
input atrLength = 2;
input atrMultiplier = 1;
def tr1 = TrueRange(h, c, l);
def tr = if IsNaN(tr1[1]) then h - l else tr1;
def lastCCI = if IsNaN(thisCCI[1]) then 0 else thisCCI[1];
def nATR = WMA(tr, atrLength) * atrMultiplier;
def buDn = h + nATR;
def buUp = l - nATR;
def bufferUp;
def bufferDn;
def preUp = CompoundValue(1, if !bufferUp[1] then buUp else bufferUp[1], buUp);
def preDn = CompoundValue(1, if !bufferDn[1] then buDn else bufferDn[1], buDn);
def bufferUp1 = if (thisCCI >= 0 and lastCCI < 0) then preDn else buUp;
def bufferDn1 = if (thisCCI <= 0 and lastCCI > 0) then preUp else buDn;
if (thisCCI >= 0) {
bufferUp = if (bufferUp1 < preUp) then preUp else bufferUp1;
bufferDn = bufferDn1;
} else {
bufferUp = bufferUp1;
bufferDn = if (bufferDn1 > preDn) then preDn else bufferDn1;
}
def calcx = if thisCCI >= 0 then bufferUp else
if thisCCI <= 0 then bufferDn else calcx[1];
plot x = if IsNaN(calcx) then c else calcx;
}
def thisCCI = f_cci(c, cciLength);
def thisCCItf = f_cci(cTF, cciLength);
def tempx = calcx(h, c, l, thisCCI, atrLength, atrMultiplier);
def tempxTF = calcx(hTF, cTF, lTF, thisCCItf, atrLength, atrMultiplier);
def calcswap = if IsNaN(calcswap[1]) then 0 else
if tempx > tempx[1] then 1 else
if tempx < tempx[1] then -1 else calcswap[1];
def calcswapTF = if IsNaN(calcswapTF[1]) then 0 else
if tempxTF > tempxTF[1] then 1 else
if tempxTF < tempxTF[1] then -1 else calcswapTF[1];
def tempswap = if IsNaN(calcswap) then 0 else calcswap;
def tempswapTF = if IsNaN(calcswapTF) then 0 else calcswapTF;
def swap2 = if tempswap > 0 then 1 else 0;
def swap2TF = if tempswapTF > 0 then 1 else 0;
#/display current timeframe's Trend
plot tempxLine = if !last and tempx then tempx else na;
plot tempxLineTF = if !last and tempxTF then tempxTF else na;
tempxLine.SetLineWeight(2);
tempxLine.AssignValueColor(if swap2 then Color.CYAN else Color.MAGENTA);
tempxLineTF.SetLineWeight(2);
tempxLineTF.AssignValueColor(if swap2TF then Color.GREEN else Color.RED);
#--Signals
script tpsl {
input condition = close;
input opposite_condition = no;
input islong = yes;
input tpMult = 4;
input slMult = 2;
input showTpSl = yes;
def na = Double.NaN;
def last = IsNaN(open);
def level = if !last[-1] then open[-1] else level[1];
def atr = ATR(Length = 200);
def atrTP = atr * tpMult;
def atrSL = atr * slMult;
def tp_area;
def sl_area;
def entry;
def reached1;
def reached;
def inTrade;
if condition and !inTrade[1] {
inTrade = yes;
reached1 = no;
entry = level;
if islong {
tp_area = if !showTpSl then na else level + atrTP;
sl_area = if !showTpSl then na else level - atrSL;
} else {
tp_area = if !showTpSl then na else level - atrTP;
sl_area = if !showTpSl then na else level + atrSL;
}
} else if inTrade[1] {
reached1 = if opposite_condition then yes else reached[1];
inTrade = if reached1 then no else inTrade[1];
entry = if !showTpSl then na else entry[1];
tp_area = if !showTpSl then na else if reached1 then close else tp_area[1];
sl_area = if !showTpSl then na else if reached1 then close else sl_area[1];
} else {
reached1 = yes;
entry = na;
tp_area = na;
sl_area = na;
inTrade = no;
}
if islong and inTrade {
reached = if (high > tp_area) or (low < sl_area) then yes else reached1;
} else if !islong and inTrade {
reached = if (low < tp_area) or (high > sl_area) then yes else reached1;
} else {
reached = yes;
}
def wins = if barNumber() < 1 then 0 else
if islong and (!inTrade and inTrade[1]) then
if (tp_area - entry) > 0 then wins[1] + 1 else wins[1] else
if !islong and (!inTrade and inTrade[1]) then
if (entry - tp_area) > 0 then wins[1] + 1 else wins[1] else wins[1];
plot enter = if !reached then entry else na;
plot tp = if !reached then tp_area else na;
plot sl = if !reached then sl_area else na;
plot reach = !inTrade[1];
plot win = wins;
}
def filterUp = if sigHTF then (tempxLine > tempxLineTF) else
if sigTrend then (swap2 and swap2TF) else yes;
def filterDn = if sigHTF then (tempxLine < tempxLineTF) else
if sigTrend then (!swap2 and !swap2TF) else yes;
def SigUp = !swap2[1] and swap2 and filterUp;
def SigDn = swap2[1] and !swap2 and filterDn;
def os = if SigUp then 1 else if SigDn then 0 else os[1];
def ts_reset = (SigUp or SigDn);
def bull_Entry = tpsl(SigUp, SigDn, yes, tpMultiplier, slMultiplier, showTpSlArea).enter;
def bull_TP = tpsl(SigUp, SigDn, yes, tpMultiplier, slMultiplier, showTpSlArea).tp;
def bull_SL = tpsl(SigUp, SigDn, yes, tpMultiplier, slMultiplier, showTpSlArea).sl;
def bull_reached = tpsl(SigUp, SigDn, yes, tpMultiplier, slMultiplier, showTpSlArea).reach;
def bull_Win = tpsl(SigUp, SigDn, yes, tpMultiplier, slMultiplier, showTpSlArea).win;
def bear_Entry = tpsl(SigDn, SigUp, no, tpMultiplier, slMultiplier, showTpSlArea).enter;
def bear_TP = tpsl(SigDn, SigUp, no, tpMultiplier, slMultiplier, showTpSlArea).tp;
def bear_SL = tpsl(SigDn, SigUp, no, tpMultiplier, slMultiplier, showTpSlArea).sl;
def bear_reached = tpsl(SigDn, SigUp, no, tpMultiplier, slMultiplier, showTpSlArea).reach;
def bear_Win = tpsl(SigDn, SigUp, no, tpMultiplier, slMultiplier, showTpSlArea).win;
plot entryBull = if bull_Entry then bull_Entry else na;
plot tpBull = if bull_TP then bull_TP else na;
plot slBull = if bull_SL then bull_SL else na;
plot entryBear = if bear_Entry then bear_Entry else na;
plot tpBear = if bear_TP then bear_TP else na;
plot slBear = if bear_SL then bear_SL else na;
tpBear.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
slBear.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
entryBear.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
entryBull.SetDefaultColor(Color.DARK_GRAY);
entryBear.SetDefaultColor(Color.DARK_GRAY);
tpBull.SetDefaultColor(Color.DARK_GREEN);
tpBear.SetDefaultColor(Color.DARK_GREEN);
slBull.SetDefaultColor(Color.DARK_RED);
slBear.SetDefaultColor(Color.DARK_RED);
AddCloud(tpBull, entryBull, Color.DARK_GREEN);
AddCloud(entryBull, slBull, Color.DARK_RED);
AddCloud(entryBear, tpBear, Color.DARK_GREEN);
AddCloud(slBear, entryBear, Color.DARK_RED);
AddChartBubble(SigUp and bull_reached, low, "B", Color.GREEN, no);
AddChartBubble(SigDn and bear_reached, high, "S", Color.RED);
def cnt = if barNumber() < 1 then 0 else
if SigUp and bull_reached then cnt[1] + 1 else
if SigDn and bear_reached then cnt[1] + 1 else cnt[1];
def totWins = bull_Win + bear_Win;
def winRate = totWins / cnt;
AddLabel(showTpSlArea, "TotalTrade(" + cnt + ")", color.WHITE);
AddLabel(showTpSlArea, "TotalWins(" + totWins + ") " + AsPercent(winRate),
if winRate>0.5 then Color.GREEN else Color.RED);
# bar Color
def colUp = if sigHTF then (tempxLine > tempxLineTF) else
if sigTrend then (swap2 and swap2TF) else os;
def colDn = if sigHTF then (tempxLine < tempxLineTF) else
if sigTrend then (!swap2 and !swap2TF) else !os;
AssignPriceColor(if !colorBars then Color.CURRENT else
if colUp then Color.DARK_GREEN else
if colDn then Color.DARK_RED else Color.GRAY);
#-- End of CODE