Mix1 : Ema Cross + Trend Channel For ThinkOrSwim

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

hi forums, i've following a nice system for swing trades, i hope could be possible to convert it in thinkscript, thank you in advance

here is the code:

https://www.tradingview.com/v/YflOVb17/


// This source code is subject to these terms:
// Attribution-NonCommercial 4.0 International (CC BY-NC 4.0)
// https://creativecommons.org/licenses/by-nc/4.0/
// You are free to:
// Share, copy and redistribute this script
// Adapt, transform and build on this script
// Under the following terms:
// Non-commercial: You cannot sell my indicator. You can't sell my work.
// Attribution: If you post part of my code, you must give me proper credit
// © gu5tavo71 (Gustavo Cardelle)
//@version=5
indicator(title = 'Mix1 : Ema Cross + Trend Channel [Gu5]', shorttitle = 'Mix1', overlay = true)
// Indicator to combines:
// Trend Channel[Gu5] (by @gu5tavo71) +
// EMA's cross (26, 50 ) +
// Golden Cross (50, 200)
// Actual Study Version: @gu5tavo71
// v2.4.19, 2022.09.18
// Project #101
// This script was written using open source code from another authors:
// @PineCoders, Built-in Library, and Community Scripts
// And using the recommendations from the Pine Script™ User Manual's Style Guide
// Disclaimer: I am not a financial advisor.
// For purpose educate only. Use at your own risk.
// ——————————— <constant_declarations> {
O_MALEN1 = 'EMA'
O_MALEN2 = 'SMA'
O_POSSEL1 = 'Only Long'
O_POSSEL2 = 'Only Short'
O_POSSEL3 = 'Both'
O_TYPES1 = 'Strategy 1'
O_TYPES2 = 'Strategy 2'
O_START = timestamp ( '2020-01-01 00:00 GMT-3' )
O_END = timestamp ( '2099-12-31 00:00 GMT-3' )
C_GREEN = #006400 //Green
C_GREENLIGHT = #388e3c //Green Light
C_RED = #8B0000 //Red
C_REDLIGHT = #b71c1c //Red Light
//}
// ——————————— <inputs> {
// | | | | |
i_maSrc = input.source (close, 'MA Source' , group = 'EMAs')
i_maFast1 = input.int (26, 'EMA Fast' , group = 'EMAs')
i_maFast2 = input.int (50, 'EMA Medium' , group = 'EMAs')
i_maLen = input.int (200, 'MA Trend' , group = 'Trend Channel')
i_maLenSel = input.string (O_MALEN2, 'MA Type' , group = 'Trend Channel',
options = [O_MALEN1, O_MALEN2],
tooltip = 'EMA or SMA')
i_htf = input.timeframe ('', 'Select Higher Timeframe' , group = 'Trend Channel',
tooltip = 'Only for MA Trend' )
i_rangeLen = input.float (0.618, 'Channel Range Length' , group = 'Trend Channel',
tooltip = 'ATR of the MA Trend')
//<stop loss>
i_slOn = input.bool (false, '■ Stop Loss On/Off' , group = 'Stop Loss')
i_sl = input.float (2.618, 'SL %' , group = 'Stop Loss', step = 0.1)
i_periodSw = input.bool (false, '■ Period On/Off' , group = 'Period')
//<period>
i_periodStar = input.time (O_START, 'Start Time' , group = 'Period')
i_periodEnd = input.time (O_END, 'End Time' , group = 'Period')
i_posSel = input.string (O_POSSEL3, 'Position Type' , group = 'Strategy',
options = [O_POSSEL1, O_POSSEL2, O_POSSEL3],
tooltip = "Only Long, Only shortE or Both")
i_typeS = input.string (O_TYPES2, 'Strategy Type' , group = 'Strategy',
options = [O_TYPES1, O_TYPES2],
tooltip = "Strategy 1:\n
Long, when the price (close) crosses the ema.\n
Strategy 2:\n
Long, only when ema goes up")
i_barColOn = input.bool (true, '■ Bar Color On/Off' , group = 'Display')
i_alertOn = input.bool (true, '■ Alert On/Off' , group = 'Display')
i_channelOn = input.bool (false, '■ Channel Range On/Off' , group = 'Display',
tooltip = 'If the price (close) is over than the channel, the trend is bullish. If the price is under, bearish. And if the price is in the channel, it is in range')
i_goldenOn = input.bool (false, '■ Golden Cross On/Off' )
//}
// ——————————— <calculations> {
//<set initial values>
condition = 0.0
maFast1 = ta.ema(i_maSrc, i_maFast1)
maFast2 = ta.ema(i_maSrc, i_maFast2)
maDir = maFast1 > maFast2 ? 1 : -1
maTrend = request.security(syminfo.tickerid, i_htf,
i_maLenSel == "SMA" ? ta.sma(close, i_maLen)[1] : ta.ema(close, i_maLen)[1],
lookahead = barmerge.lookahead_on) //No repaint
maTrendDir = i_maSrc >= maTrend ? 1 : -1
rangeAtr = ta.atr(i_maLen) * i_rangeLen
rangeTop = maTrend + rangeAtr
rangeBot = maTrend - rangeAtr
rangeCh = (open <= rangeTop or close <= rangeTop) and
(open >= rangeBot or close >= rangeBot)
trendDir = i_typeS == 'Strategy 1' ?
rangeCh ? 0 :
maTrendDir == 1 and maDir == 1 and maTrend > maFast2 ? 0 :
maTrendDir == -1 and maDir == -1 and maTrend < maFast2 ? 0 :
maTrendDir == 1 and maDir == 1 ? 1 :
maTrendDir == -1 and maDir == -1 ? -1 : 0 :
rangeCh ? 0 :
maTrendDir == 1 and maDir == 1 ? 1 :
maTrendDir == -1 and maDir == -1 ? -1 : 0
GCross = i_goldenOn ? ta.crossover (maFast2, maTrend) : na
DCross = i_goldenOn ? ta.crossunder(maFast2, maTrend) : na
period = i_periodSw ?
time >= i_periodStar and time <= i_periodEnd : true
//<rules>
entryLong = i_posSel != 'Only Short' and
trendDir == 1 and
period
entryShort = i_posSel != 'Only Long' and
trendDir == -1 and
period
exitLong = i_posSel != 'Only Short' and (
trendDir != 1 or
maDir == -1) and
condition[1] == 1 and
period
exitShort = i_posSel != 'Only Long' and (
trendDir != -1 or
maDir == 1) and
condition[1] == -1 and
period
//<stop loss>
slEntry = i_slOn ?
close * i_sl / 100 : na
slTop = close + slEntry
slBot = close - slEntry
slTopBuff = ta.valuewhen(condition[1] != 1 and entryLong, slBot, 0)
slBotBuff = ta.valuewhen(condition[1] != -1 and entryShort, slTop, 0)
slLine = condition[1] == -1 and entryLong ? slTopBuff :
condition[1] == 1 and entryShort ? slBotBuff :
condition[1] == 1 or entryLong ? slTopBuff :
condition[1] == -1 or entryShort ? slBotBuff : na
slLong = condition[1] == 1 and ta.crossunder(close, slLine) or high > slLine and low < slLine
slShort = condition[1] == -1 and ta.crossover (close, slLine) or high > slLine and low < slLine
//<conditions>
condition := condition[1] != 1 and entryLong ? 1 :
condition[1] != -1 and entryShort ? -1 :
condition[1] == 1 and slLong ? 0 :
condition[1] == -1 and slShort ? 0 :
condition[1] != 0 and exitLong ? 0 :
condition[1] != 0 and exitShort ? 0 : nz(condition[1])
longE = condition[1] != 1 and condition == 1
shortE = condition[1] != -1 and condition == -1
longX = condition[1] == 1 and exitLong and not slLong
shortX = condition[1] == -1 and exitShort and not slShort
longSL = condition[1] == 1 and slLong
shortSL = condition[1] == -1 and slShort
//<color>
c_emas = longX ? color.new(color.orange, 99) :
shortX ? color.new(color.orange, 99) :
trendDir == 1 and maDir == 1 ? color.new(C_GREEN, 99) :
trendDir == -1 and maDir == -1 ? color.new(C_RED, 99) :
color.new(color.orange, 99)
c_maFill = longX ? color.new(color.orange, 70) :
shortX ? color.new(color.orange, 70) :
trendDir == 1 and maDir == 1 ? color.new(C_GREEN, 70) :
trendDir == -1 and maDir == -1 ? color.new(C_RED, 70) :
color.new(color.orange, 70)
c_maTrend = trendDir == 0 ? color.new(color.orange, 0) :
trendDir == 1 and maTrend[1] < maTrend ? color.new(C_GREEN, 0) :
trendDir == 1 and maTrend[1] >= maTrend ? color.new(C_GREENLIGHT, 0) :
trendDir == -1 and maTrend[1] < maTrend ? color.new(C_REDLIGHT, 0) :
trendDir == -1 and maTrend[1] >= maTrend ? color.new(C_RED, 0) : na
c_ch = trendDir == 0 ? color.new(color.orange, 75) :
trendDir == 1 ? color.new(C_GREEN, 75) :
trendDir == -1 ? color.new(C_RED, 75) : na
c_slLineUp = ta.rising (slLine, 1)
c_slLineDn = ta.falling(slLine, 1)
c_slLine = c_slLineUp ? na :
c_slLineDn ? na :
color.new(C_RED, 0)
c_barCol = trendDir == 0 ? color.new(color.orange, 0) :
trendDir == 1 and open <= close ? color.new(C_GREEN, 0) :
trendDir == 1 and open > close ? color.new(C_GREENLIGHT, 0) :
trendDir == -1 and open >= close ? color.new(C_RED, 0) :
trendDir == -1 and open < close ? color.new(C_REDLIGHT, 0) :
color.new(color.orange, 0)
//}
// ——————————— <plots> {
p_maFast1 = plot(
maFast1,
title = 'EMA Fast 1',
color = c_emas,
linewidth = 1)
p_maFast2 = plot(
maFast2,
title = 'EMA Fast 2',
color = c_emas,
linewidth = 2)
fill(
p_maFast1, p_maFast2,
title = 'EMAs Fill',
color = c_maFill)
plot(
maTrend,
title = 'SMA Trend',
color = c_maTrend,
linewidth = 3)
p_chTop = plot(
i_channelOn ? rangeTop : na,
title = 'Top Channel',
color = c_maTrend,
linewidth = 1)
p_chBot = plot(
i_channelOn ? rangeBot : na,
title = 'Bottom Channel',
color = c_maTrend,
linewidth = 1)
fill(
p_chTop, p_chBot,
title = 'Channel',
color = c_ch)
plot(
i_slOn and condition != 0 ? slLine : na,
title = 'Stop Loss Line',
color = c_slLine,
linewidth = 1,
style = plot.style_linebr)
//}
// ——————————— <alerts> {
plotshape(
i_alertOn and longE ? high : na,
title = 'Long',
text = 'Long',
textcolor = color.white,
color = color.new(C_GREEN, 0),
style = shape.labelup,
size = size.normal,
location = location.belowbar)
plotshape(
i_alertOn and shortE ? low : na,
title = 'Short',
text = 'Short',
textcolor = color.white,
color = color.new(C_RED, 0),
style = shape.labeldown,
size = size.normal,
location = location.abovebar)
plotshape(
i_alertOn and (longX or shortX) ? close : na,
title = 'Close',
text = 'Close',
textcolor = color.new(color.yellow, 0),
color = color.new(color.yellow, 0),
style = shape.xcross,
size = size.small,
location = location.absolute)
//<stop loss>
plotshape(
i_alertOn and (longSL or shortSL) ? slLine : na,
title = 'Stop Loss',
text = 'SL',
textcolor = color.new(color.yellow, 0),
color = color.new(color.yellow, 0),
style = shape.xcross,
size = size.small,
location = location.absolute)
plotshape(
i_alertOn and i_goldenOn and GCross ? maTrend : na,
title = 'Golden Cross',
text = 'Golden\nCross',
textcolor = color.white,
color = color.new(color.orange, 0),
style = shape.labelup,
size = size.normal,
location = location.absolute)
plotshape(
i_alertOn and i_goldenOn and DCross ? maTrend : na,
title = 'Death Cross',
text = 'Death\nCross',
textcolor = color.white,
color = color.new(color.orange, 0),
style = shape.labeldown,
size = size.normal,
location = location.absolute)
barcolor(i_barColOn ? c_barCol : na)
bgcolor(
i_periodSw and not period ? color.new(color.gray, 90) : na,
title = 'Session')
alertcondition(
longE or shortE or longX or shortX or longSL or shortSL,
title = "Any Alert",
message = "Any Alert")
alertcondition(
longE,
title = "Buy",
message = "Buy")
alertcondition(
shortE,
title = "Sell",
message = "Sell")
alertcondition(
longX,
title = "Buy Close",
message = "Buy Close")
alertcondition(
shortX,
title = "Sell Close",
message = "Sell Close")
alertcondition(
longSL,
title = "Stop Loss Long",
message = "Stop Loss Long")
alertcondition(
shortSL,
title = "Stop Loss Short",
message = "Stop Loss Short")
//}
find below.

CSS:
#// This source code is subject to these terms:
#// Attribution-NonCommercial 4.0 International (CC BY-NC 4.0)
#// https://creativecommons.org/licenses/by-nc/4.0/
#// You are free to:
#// Share, copy and redistribute this script
#// Adapt, transform and build on this script
#// Under the following terms:
#// Non-commercial: You cannot sell my indicator. You can't sell my work.
#// Attribution: If you post part of my code, you must give me proper credit
#// © gu5tavo71 (Gustavo Cardelle)
#// Indicator to combines:
#//           Trend Channel[Gu5] (by @gu5tavo71) +
#//           EMA's cross  (26, 50 ) +
#//           Golden Cross (50, 200)
#// Actual Study Version: @gu5tavo71
#// v2.4.19, 2022.09.18
#// Project #101
#// This script was written using open source code from another authors:
#// @PineCoders, Built-in Library, and Community Scripts
#// And using the recommendations from the Pine Script™ User Manual's Style Guide
#// Disclaimer: I am not a financial advisor.
#//             For purpose educate only. Use at your own risk.
#indicator(title = 'Mix1 : Ema Cross + Trend Channel [Gu5]', shorttitle = 'Mix1', overlay = true)
# Converted and mod by Sam4Cok@Samer800    - 06/2023
#// ——————————— <inputs> {

input maSrc      = close;        # 'MA Source'
input MovAvgFast1    = 26;       # 'EMA Fast'
input MovAvgFast2    = 50;       # 'EMA Medium'
input trendLength    = 200;      # 'MA Trend'
input atrLength      = 200;
input atrMultiplier  = 0.618;   # 'Channel Range Length'
input MovAvgType     = AverageType.SIMPLE;    # 'MA Type'
input useChartTimeframe = {default "Yes", "No"};    # 'Select Higher Timeframe'
input manualTimeframe = AggregationPeriod.FIFTEEN_MIN;
input ShowStopLoss    = no;      # 'Stop Loss On/Off'
input StopLossPercent = 2.618;   # 'SL %'
input PositionType    = {"Only Long", "Only Short", default "Both"};    # 'Position Type'
input StrategyType    = {"Strategy 1", default "Strategy 2"};           # 'Strategy Type'
input barColor   = yes;            # 'Bar Color On/Off'
input SignalsOn  = yes;            # 'Alert On/Off'
input ChannelRangeOn  = no;        # 'Channel Range On/Off'
input GoldenCrossOn   = no;        # 'Golden Cross On/Off'


def na = Double.NaN;
def strat1 = StrategyType == StrategyType."Strategy 1";
def long = PositionType == PositionType."Only Long";
def short = PositionType == PositionType."Only Short";

def c;
switch (useChartTimeframe) {
case "Yes" :
    c = close;
case "No" :
    c = close(Period = manualTimeframe);
}
#// ——————————— <calculations> {

def condition;
def condition1   = if isNaN(condition[1]) then 0 else condition[1];
def maFast1      = ExpAverage(maSrc, MovAvgFast1);
def maFast2      = ExpAverage(maSrc, MovAvgFast2);
def MovAvgMTF    = MovingAverage(MovAvgType, c, trendLength)[1];
def maDir        = if maFast1 > maFast2 then 1 else -1;
def maTrend      =  MovAvgMTF;#//No repaint
def maTrendDir   = if maSrc >= maTrend then 1 else -1;
def rangeAtr     = ATR(Length = atrLength) * atrMultiplier;
def rangeTop     = maTrend + rangeAtr;
def rangeBot     = maTrend - rangeAtr;
def rangeCh      = (open <= rangeTop or close <= rangeTop) and
                   (open >= rangeBot or close >= rangeBot);
def trendDir = if strat1 then
               if rangeCh then 0 else
               if maTrendDir ==  1 and maDir ==  1 and maTrend > maFast2 then 0 else
               if maTrendDir == -1 and maDir == -1 and maTrend < maFast2 then 0 else
               if maTrendDir ==  1 and maDir ==  1                       then 1 else
               if maTrendDir == -1 and maDir == -1                       then -1 else 0 else
               if rangeCh                                                then 0 else
               if maTrendDir ==  1 and maDir ==  1                       then 1 else
               if maTrendDir == -1 and maDir == -1                       then -1 else 0;
def GCross       = if GoldenCrossOn then Crosses(maFast2, maTrend, CrossingDirection.ABOVE) else na;
def DCross       = if GoldenCrossOn then Crosses(maFast2, maTrend, CrossingDirection.BELOW) else na;
#//<rules>
def entryLong    = !short and trendDir ==  1;
def entryShort   = !long  and trendDir == -1;
def exitLong     = !short and (trendDir !=  1 or maDir == -1) and condition1 ==  1;
def exitShort    = !long  and (trendDir != -1 or maDir ==  1) and condition1 == -1;
#//<stop loss>
def slEntry      = if ShowStopLoss then close * StopLossPercent / 100 else na;
def slTop        = if isNaN(slEntry) then 0 else close + slEntry;
def slBot        = if isNaN(slEntry) then 0 else close - slEntry;

def slTopBuff    = if (condition1 !=  1 and entryLong) then slBot else slTopBuff[1];
def slBotBuff    = if (condition1 != -1 and entryShort) then slTop else slBotBuff[1];

def slLine       = if condition1 == -1 and entryLong  then slTopBuff else
                   if condition1 ==  1 and entryShort then slBotBuff else
                   if condition1 ==  1  or entryLong  then slTopBuff else
                   if condition1 == -1  or entryShort then slBotBuff else 0;
def longCross = if isNaN(slLine) then 0 else (close < slLine) and  (close[1] >= slLine[1]);
def shortCross = if isNaN(slLine) then 0 else (close > slLine) and  (close[1] <= slLine[1]);

def slLong       = condition1 ==  1 and longCross or high > slLine and low < slLine;
def slShort      = condition1 == -1 and shortCross or high > slLine and low < slLine;

#//<conditions>
condition   = if condition1 !=  1 and entryLong  then  1 else
              if condition1 != -1 and entryShort then -1 else
              if condition1 ==  1 and slLong     then  0 else
              if condition1 == -1 and slShort    then  0 else
              if condition1 !=  0 and exitLong   then  0 else
              if condition1 !=  0 and exitShort  then  0 else condition1;

def longE   = condition[1] !=  1 and condition ==  1;
def shortE  = condition[1] != -1 and condition == -1;
def longX   = condition[1] ==  1 and exitLong  and !slLong;
def shortX  = condition[1] == -1 and exitShort and !slShort;
def longSL  = condition[1] ==  1 and slLong;
def shortSL = condition[1] == -1 and slShort;
#//<color>
def c_maFill  = if longX then 0 else if shortX then 0 else
                if trendDir ==  1 and maDir ==  1 then 1 else
                if trendDir == -1 and maDir == -1 then -1 else 0;
def c_maTrend = if trendDir ==  0 then 0 else
                if trendDir ==  1 and maTrend[1]  < maTrend then 2 else
                if trendDir ==  1 and maTrend[1] >= maTrend then 1 else
                if trendDir == -1 and maTrend[1]  < maTrend then -1 else
                if trendDir == -1 and maTrend[1] >= maTrend then -2 else na;
def c_ch      = if trendDir ==  1 then  1 else
                if trendDir == -1 then -1 else 0;
#                if trendDir == -1 then -1 else na;
def c_slLineUp   = slLine > slLine[1];
def c_slLineDn   = slLine < slLine[1];
def c_slLine     = if c_slLineUp then na else if c_slLineDn then na else 1;

#-- BAr color
AssignPriceColor(if !barColor then Color.CURRENT else
                 if trendDir ==  0 then Color.GRAY else
                 if trendDir ==  1 and open <= close then Color.GREEN else
                 if trendDir ==  1 and open >  close then Color.DARK_GREEN else
                 if trendDir == -1 and open >= close then Color.RED else
                 if trendDir == -1 and open <  close then Color.DARK_RED else Color.GRAY);

#// ——————————— <plots> {
def p_maFast1    = maFast1;# 'EMA Fast 1'
def p_maFast2    = maFast2;# 'EMA Fast 2'
AddCloud(if c_maFill==0 then na else p_maFast1, p_maFast2, Color.DARK_GREEN, Color.DARK_RED);
AddCloud(if c_maFill==0 then p_maFast1 else na, p_maFast2, Color.DARK_GRAY, Color.DARK_GRAy);

plot smaTrend = maTrend; # 'SMA Trend'
smaTrend.SetLineWeight(2);
smaTrend.AssignValueColor(if c_maTrend==2 then Color.GREEN else
                          if c_maTrend==1 then Color.DARK_GREEN else
                          if c_maTrend==-1 then Color.DARK_RED else
                          if c_maTrend==-2 then Color.RED else color.GRAY);
plot p_chTop = if ChannelRangeOn then rangeTop else na;    # 'Top Channel'
plot p_chBot = if ChannelRangeOn then rangeBot else na;    # 'Bottom Channel'
p_chTop.AssignValueColor(if c_maTrend==2 then Color.GREEN else
                          if c_maTrend==1 then Color.DARK_GREEN else
                          if c_maTrend==-1 then Color.DARK_RED else
                          if c_maTrend==-2 then Color.RED else color.GRAY);
p_chBot.AssignValueColor(if c_maTrend==2 then Color.GREEN else
                          if c_maTrend==1 then Color.DARK_GREEN else
                          if c_maTrend==-1 then Color.DARK_RED else
                          if c_maTrend==-2 then Color.RED else color.GRAY);

AddCloud(if c_ch<=0 then na else p_chTop, p_chBot,Color.DARK_GREEN);
AddCloud(if c_ch>=0 then na else p_chTop, p_chBot,Color.DARK_RED);
AddCloud(if c_ch!=0 then na else p_chTop, p_chBot,Color.DARK_GRAY);

plot stopLossLine = if isNaN(c_slLine) or slLine==0 then na else
                    if ShowStopLoss and condition != 0 then slLine else na;    # 'Stop Loss Line'
stopLossLine.SetDefaultColor(Color.MAGENTA);
stopLossLine.SetPaintingStrategy(PaintingStrategy.DASHES);
#// ——————————— <alerts> {
AddChartBubble(SignalsOn and longE, low, "Long", Color.GREEN, no);
AddChartBubble(SignalsOn and shortE, high, "Short", Color.RED, yes);

plot Exit = if SignalsOn and (longX or shortX) then close else na;
Exit.SetPaintingStrategy(PaintingStrategy.POINTS);
Exit.SetDefaultColor(Color.YELLOW);
Exit.SetLineWeight(2);
#//<stop loss>
AddChartBubble(SignalsOn and (longSL or shortSL), slLine, "SL", Color.YELLOW, if longSL then yes else no);

plot GoldCrossUp = if SignalsOn and GoldenCrossOn and (GCross or DCross) then close else na;
GoldCrossUp.SetLineWeight(2);
GoldCrossUp.SetPaintingStrategy(PaintingStrategy.SQUARES);
GoldCrossUp.AssignValueColor( if GCross then Color.CYAN else Color.MAGENTA);


#-- END of CODE
 
Last edited:
find below.

CSS:
#// This source code is subject to these terms:
#// Attribution-NonCommercial 4.0 International (CC BY-NC 4.0)
#// https://creativecommons.org/licenses/by-nc/4.0/
#// You are free to:
#// Share, copy and redistribute this script
#// Adapt, transform and build on this script
#// Under the following terms:
#// Non-commercial: You cannot sell my indicator. You can't sell my work.
#// Attribution: If you post part of my code, you must give me proper credit
#// © gu5tavo71 (Gustavo Cardelle)
#// Indicator to combines:
#//           Trend Channel[Gu5] (by @gu5tavo71) +
#//           EMA's cross  (26, 50 ) +
#//           Golden Cross (50, 200)
#// Actual Study Version: @gu5tavo71
#// v2.4.19, 2022.09.18
#// Project #101
#// This script was written using open source code from another authors:
#// @PineCoders, Built-in Library, and Community Scripts
#// And using the recommendations from the Pine Script™ User Manual's Style Guide
#// Disclaimer: I am not a financial advisor.
#//             For purpose educate only. Use at your own risk.
#indicator(title = 'Mix1 : Ema Cross + Trend Channel [Gu5]', shorttitle = 'Mix1', overlay = true)
# Converted and mod by Sam4Cok@Samer800    - 06/2023
#// ——————————— <inputs> {

input maSrc      = close;        # 'MA Source'
input MovAvgFast1    = 26;       # 'EMA Fast'
input MovAvgFast2    = 50;       # 'EMA Medium'
input trendLength    = 200;      # 'MA Trend'
input atrLength      = 200;
input atrMultiplier  = 0.618;   # 'Channel Range Length'
input MovAvgType     = AverageType.SIMPLE;    # 'MA Type'
input useChartTimeframe = {default "Yes", "No"};    # 'Select Higher Timeframe'
input manualTimeframe = AggregationPeriod.FIFTEEN_MIN;
input ShowStopLoss    = no;      # 'Stop Loss On/Off'
input StopLossPercent = 2.618;   # 'SL %'
input PositionType    = {"Only Long", "Only Short", default "Both"};    # 'Position Type'
input StrategyType    = {"Strategy 1", default "Strategy 2"};           # 'Strategy Type'
input barColor   = yes;            # 'Bar Color On/Off'
input SignalsOn  = yes;            # 'Alert On/Off'
input ChannelRangeOn  = no;        # 'Channel Range On/Off'
input GoldenCrossOn   = no;        # 'Golden Cross On/Off'


def na = Double.NaN;
def strat1 = StrategyType == StrategyType."Strategy 1";
def long = PositionType == PositionType."Only Long";
def short = PositionType == PositionType."Only Short";

def c;
switch (useChartTimeframe) {
case "Yes" :
    c = close;
case "No" :
    c = close(Period = manualTimeframe);
}
#// ——————————— <calculations> {

def condition;
def condition1 = if isNaN(condition[1]) then 0 else condition[1];
def maFast1      = ExpAverage(maSrc, MovAvgFast1);
def maFast2      = ExpAverage(maSrc, MovAvgFast2);
def MovAvgMTF    = MovingAverage(MovAvgType, c, trendLength);
def maDir        = if maFast1 > maFast2 then 1 else -1;
def maTrend      =  MovAvgMTF[1];#//No repaint
def maTrendDir   = if maSrc >= maTrend then 1 else -1;
def rangeAtr     = ATR(Length = atrLength) * atrMultiplier;
def rangeTop     = maTrend + rangeAtr;
def rangeBot     = maTrend - rangeAtr;
def rangeCh      = (open <= rangeTop or close <= rangeTop) and
                   (open >= rangeBot or close >= rangeBot);
def trendDir = if strat1 then
               if rangeCh then 0 else
               if maTrendDir ==  1 and maDir ==  1 and maTrend > maFast2 then 0 else
               if maTrendDir == -1 and maDir == -1 and maTrend < maFast2 then 0 else
               if maTrendDir ==  1 and maDir ==  1                       then 1 else
               if maTrendDir == -1 and maDir == -1                       then -1 else 0 else
               if rangeCh                                                then 0 else
               if maTrendDir ==  1 and maDir ==  1                       then 1 else
               if maTrendDir == -1 and maDir == -1                       then -1 else 0;
def GCross       = if GoldenCrossOn then Crosses(maFast2, maTrend, CrossingDirection.ABOVE) else na;
def DCross       = if GoldenCrossOn then Crosses(maFast2, maTrend, CrossingDirection.BELOW) else na;
#//<rules>
def entryLong    = !short and trendDir ==  1;
def entryShort   = !long  and trendDir == -1;
def exitLong     = !short and (trendDir !=  1 or maDir == -1) and condition1 ==  1;
def exitShort    = !long  and (trendDir != -1 or maDir ==  1) and condition1 == -1;
#//<stop loss>
def slEntry      = if ShowStopLoss then close * rangeAtr * StopLossPercent / 100 else 0;
def slTop        = close + slEntry;
def slBot        = close - slEntry;
def slTopBuff    = if (condition1 != 1 and entryLong) then slBot else slTopBuff[1];
def slBotBuff    = if (condition1 != -1 and entryShort) then slTop else slBotBuff[1];

def slLine       = if condition1 == -1 and entryLong  then slTopBuff else
                   if condition1 ==  1 and entryShort then slBotBuff else
                   if condition1 ==  1  or entryLong  then slTopBuff else
                   if condition1 == -1  or entryShort then slBotBuff else 0;
def slLong       = condition1 ==  1 and (close crosses below slLine) or high > slLine and low < slLine;
def slShort      = condition1 == -1 and (close crosses above slLine) or high > slLine and low < slLine;
#//<conditions>
condition   = if condition1 !=  1 and entryLong  then  1 else
              if condition1 != -1 and entryShort then -1 else
              if condition1 ==  1 and slLong     then  0 else
              if condition1 == -1 and slShort    then  0 else
              if condition1 !=  0 and exitLong   then  0 else
              if condition1 !=  0 and exitShort  then  0 else condition1;

def longE   = condition[1] !=  1 and condition ==  1;
def shortE  = condition[1] != -1 and condition == -1;
def longX   = condition[1] ==  1 and exitLong  and !slLong;
def shortX  = condition[1] == -1 and exitShort and !slShort;
def longSL  = condition[1] ==  1 and slLong;
def shortSL = condition[1] == -1 and slShort;
#//<color>
def c_maFill  = if longX then 0 else if shortX then 0 else
                if trendDir ==  1 and maDir ==  1 then 1 else
                if trendDir == -1 and maDir == -1 then -1 else 0;
def c_maTrend = if trendDir ==  0 then 0 else
                if trendDir ==  1 and maTrend[1]  < maTrend then 2 else
                if trendDir ==  1 and maTrend[1] >= maTrend then 1 else
                if trendDir == -1 and maTrend[1]  < maTrend then -1 else
                if trendDir == -1 and maTrend[1] >= maTrend then -2 else na;
def c_ch      = if trendDir ==  0 then 0 else
                if trendDir ==  1 then 1 else
                if trendDir == -1 then -1 else na;
def c_slLineUp   = slLine > slLine[1];
def c_slLineDn   = slLine < slLine[1];
def c_slLine     = if c_slLineUp then na else if c_slLineDn then na else 1;

AssignPriceColor(if !barColor then Color.CURRENT else
                 if trendDir ==  0 then Color.GRAY else
                 if trendDir ==  1 and open <= close then Color.GREEN else
                 if trendDir ==  1 and open >  close then Color.DARK_GREEN else
                 if trendDir == -1 and open >= close then Color.RED else
                 if trendDir == -1 and open <  close then Color.DARK_RED else Color.GRAY);

#// ——————————— <plots> {
def p_maFast1    = maFast1;# 'EMA Fast 1'
def p_maFast2    = maFast2;# 'EMA Fast 2'
AddCloud(if c_maFill==0 then na else p_maFast1, p_maFast2, Color.DARK_GREEN, Color.DARK_RED);
AddCloud(if c_maFill!=0 then na else p_maFast1, p_maFast2, Color.DARK_GRAY, Color.DARK_GRAy);

plot smaTrend = maTrend; # 'SMA Trend'
smaTrend.SetLineWeight(2);
smaTrend.AssignValueColor(if c_maTrend==2 then Color.GREEN else
                          if c_maTrend==1 then Color.DARK_GREEN else
                          if c_maTrend==-1 then Color.DARK_RED else
                          if c_maTrend==-2 then Color.RED else color.GRAY);
plot p_chTop = if ChannelRangeOn then rangeTop else na;    # 'Top Channel'
plot p_chBot = if ChannelRangeOn then rangeBot else na;    # 'Bottom Channel'
p_chTop.AssignValueColor(if c_maTrend==2 then Color.GREEN else
                          if c_maTrend==1 then Color.DARK_GREEN else
                          if c_maTrend==-1 then Color.DARK_RED else
                          if c_maTrend==-2 then Color.RED else color.GRAY);
p_chBot.AssignValueColor(if c_maTrend==2 then Color.GREEN else
                          if c_maTrend==1 then Color.DARK_GREEN else
                          if c_maTrend==-1 then Color.DARK_RED else
                          if c_maTrend==-2 then Color.RED else color.GRAY);

AddCloud(if c_ch<=0 then na else p_chTop, p_chBot,Color.DARK_GREEN);
AddCloud(if c_ch>=0 then na else p_chTop, p_chBot,Color.DARK_RED);
AddCloud(if c_ch!=0 then na else p_chTop, p_chBot,Color.DARK_GRAY);

plot stopLossLine = if isNaN(c_slLine) or slLine==0 then na else
                    if ShowStopLoss and condition != 0 then slLine else na;    # 'Stop Loss Line'
stopLossLine.SetDefaultColor(Color.MAGENTA);
stopLossLine.SetPaintingStrategy(PaintingStrategy.DASHES);
#// ——————————— <alerts> {
AddChartBubble(SignalsOn and longE, low, "Long", Color.GREEN, no);
AddChartBubble(SignalsOn and shortE, high, "Short", Color.RED, yes);

plot Exit = if SignalsOn and (longX or shortX) then close else na;
Exit.SetPaintingStrategy(PaintingStrategy.POINTS);
Exit.SetDefaultColor(Color.WHITE);
Exit.SetLineWeight(2);
#//<stop loss>
AddChartBubble(SignalsOn and (longSL or shortSL), slLine, "SL", Color.YELLOW, if longSL then yes else no);

plot GoldCrossUp = if SignalsOn and GoldenCrossOn and (GCross or DCross) then close else na;
GoldCrossUp.SetLineWeight(2);
GoldCrossUp.SetPaintingStrategy(PaintingStrategy.SQUARES);
GoldCrossUp.AssignValueColor( if GCross then Color.CYAN else Color.MAGENTA);


#-- END of CODE
@samer800 you are the best! thank you very much! really appreciate your time!

do you think it is possible to show it less signal, it is to cloudy on the chart. thank you in advance

sorry to bother to much, what i mean with the signal is keep repeating bar by bar, do you think it's possible to just get the real signal but not repeating bar by bar. thank you in advance
 
Last edited by a moderator:
@samer800 you are the best! thank you very much! really appreciate your time!

do you think it is possible to show it less signal, it is to cloudy on the chart. thank you in advance

sorry to bother to much, what i mean with the signal is keep repeating bar by bar, do you think it's possible to just get the real signal but not repeating bar by bar. thank you in advance
check now, I updated the same code.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
417 Online
Create Post

Similar threads

Similar threads

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