Trend Magic for ToS

NPTechs

New member
Platform
  1. Thinkorswim
Code:
# Trend Magic Indicator based on https://ninjatraderecosystem.com/user-app-share-download/trend-magic/
# converted to ToS
# v1.0 NPTechs

input cciPeriod = 20;
input atrPeriod = 14;
input atrMult = 1.0;

def cciVal = CCI(cciPeriod);
def atrVal = ATR(atrPeriod);
def upTrend = low - atrVal * atrMult;
def downTrend = high + atrVal * atrMult;
def Trend =    if (cciVal >= 0) and upTrend < Trend[1] then Trend[1]
               else if (cciVal >= 0) and upTrend >= Trend[1] then upTrend
               else if (cciVal < 0) and (downTrend > Trend[1]) then Trend[1]
            else downTrend;
def iTrend = if Trend>Trend[1] then 1 else if Trend < Trend[1] then -1 else iTrend[1];
plot tmline = Trend;
tmline.AssignValueColor(if iTrend > 0 then Color.GREEN else Color.RED);
tmline.SetLineWeight(2);
 
Last edited:

NPTechs

New member
uc
 

NPTechs

New member
Code:
# Trend Magic Indicator based on https://ninjatraderecosystem.com/user-app-share-download/trend-magic/
# converted to ToS
# v1.0 NPTechs
# v1.1 Added buy/sell arrows, paint bars

input cciPeriod = 20;
input atrPeriod = 14;
input atrMult = 1.0;
input HideArrows = no;
input PaintBars = Yes;

def cciVal = CCI(cciPeriod);
def atrVal = ATR(atrPeriod);
def upTrend = low - atrVal * atrMult;
def downTrend = high + atrVal * atrMult;
def Trend = if (cciVal >= 0) and upTrend < Trend[1] then Trend[1]
               else if (cciVal >= 0) and upTrend >= Trend[1] then upTrend
               else if (cciVal < 0) and (downTrend > Trend[1]) then Trend[1]
            else downTrend;
def iTrend = if Trend>Trend[1] then 1 else if Trend < Trend[1] then -1 else iTrend[1];
plot tmline = Trend;
tmline.AssignValueColor(if iTrend > 0 then Color.GREEN else Color.RED);
tmline.SetLineWeight(2);

plot buy = if iTrend[1] == -1 and iTrend == 1 and !HideArrows then Trend else Double.NaN;
buy.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
buy.SetDefaultColor(Color.GREEN);
buy.SetLineWeight(3);

plot sell = if iTrend[1] == 1 and iTrend == -1 and !HideArrows then  Trend else Double.NaN;
sell.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
sell.SetDefaultColor(Color.RED);
sell.SetLineWeight(3);

Alert(buy, "TM Buy", Alert.BAR, Sound.Ding);
Alert(sell, "TM SELL", Alert.BAR, Sound.Ding);

AssignPriceColor (if !PaintBars then Color.CURRENT else if iTrend > 0 then Color.GREEN else Color.RED);
 
Top