Trend Magic MultiTime Frame Indicator for ThinkorSwim

H

horserider

Active member
Warehouse
Updated Trend Magic indicator to include 3 user selected time frames.

Code:
# Trend Magic MultiTime Frame
#Update to original Trend Magic adding multiTime Frames
#Horserider 8/8/2019


input agg = AggregationPeriod.FIFTEEN_MIN;
def c = close(period = agg);
def h = high(period = agg);
def l = low(period = agg);
def pricedata = hl2(period = agg);

DefineGlobalColor("TrendUp", CreateColor(0, 254, 30));
DefineGlobalColor("TrendDown", CreateColor(255, 3, 2));


input lengthCCI = 50;
input lengthATR = 5;
input AtrFactor = 0.7;

def ATRcci = Average(TrueRange(h, c, l), lengthATR) * AtrFactor;
def price = c + l + h;
def linDev = LinDev(price, lengthCCI);
def CCI = if linDev == 0
          then 0
          else (price - Average(price, lengthCCI)) / linDev / 0.015;
def MT1 = if CCI > 0
          then Max(MT1[1], pricedata - ATRcci)
          else Min(MT1[1], pricedata + ATRcci);
plot data = MT1;
data.AssignValueColor(if c < MT1 then Color.RED else Color.GREEN);

# Agg Period 2


input agg2 = AggregationPeriod.DAY;
def c2 = close(period = agg2);
def h2 = high(period = agg2);
def l2 = low(period = agg2);
def pricedata2 = hl2(period = agg2);

DefineGlobalColor("TrendUp", CreateColor(0, 254, 30));
DefineGlobalColor("TrendDown", CreateColor(255, 3, 2));


input lengthCCI2 = 50;
input lengthATR2 = 5;
input AtrFactor2 = 0.7;

def ATRcci2 = Average(TrueRange(h2, c2, l), lengthATR2) * AtrFactor2;
def price2 = c2 + l2 + h2;
def linDev2 = LinDev(price2, lengthCCI2);
def CCI2 = if linDev2 == 0
          then 0
          else (price2 - Average(price2, lengthCCI2)) / linDev2 / 0.015;
def MT2 = if CCI2 > 0
          then Max(MT2[1], pricedata2 - ATRcci2)
          else Min(MT2[1], pricedata2 + ATRcci2);
plot data2 = MT2;
data2.AssignValueColor(if c2 < MT2 then Color.RED else Color.GREEN);
DefineGlobalColor("TrendUp", CreateColor(0, 254, 30));
DefineGlobalColor("TrendDown", CreateColor(255, 3, 2));

# Agg Period 3


input agg3 = AggregationPeriod.DAY;
def c3 = close(period = agg3);
def h3 = high(period = agg3);
def l3 = low(period = agg3);
def pricedata3 = hl2(period = agg3);

DefineGlobalColor("TrendUp", CreateColor(0, 254, 30));
DefineGlobalColor("TrendDown", CreateColor(255, 3, 2));


input lengthCCI3 = 50;
input lengthATR3 = 5;
input AtrFactor3 = 0.7;

def ATRcci3 = Average(TrueRange(h3, c3, l), lengthATR3) * AtrFactor3;
def price3 = c3 + l3 + h3;
def linDev3 = LinDev(price3, lengthCCI3);
def CCI3 = if linDev3 == 0
          then 0
          else (price3 - Average(price3, lengthCCI3)) / linDev3 / 0.015;
def MT3 = if CCI3 > 0
          then Max(MT3[1], pricedata3 - ATRcci3)
          else Min(MT3[1], pricedata3 + ATRcci3);
plot data3 = MT3;
data3.AssignValueColor(if c3 < MT3 then Color.RED else Color.GREEN);
DefineGlobalColor("TrendUp", CreateColor(0, 254, 30));
DefineGlobalColor("TrendDown", CreateColor(255, 3, 2));
 
Last edited by a moderator:
Shinthus

Shinthus

Member
VIP
Updated Trend Magic indicator to include 3 user selected time frames.

Code:
# Trend Magic MultiTime Frame
#Update to original Trend Magic adding multiTime Frames
#Horserider 8/8/2019


input agg = AggregationPeriod.FIFTEEN_MIN;
def c = close(period = agg);
def h = high(period = agg);
def l = low(period = agg);
def pricedata = hl2(period = agg);

DefineGlobalColor("TrendUp", CreateColor(0, 254, 30));
DefineGlobalColor("TrendDown", CreateColor(255, 3, 2));


input lengthCCI = 50;
input lengthATR = 5;
input AtrFactor = 0.7;

def ATRcci = Average(TrueRange(h, c, l), lengthATR) * AtrFactor;
def price = c + l + h;
def linDev = LinDev(price, lengthCCI);
def CCI = if linDev == 0
          then 0
          else (price - Average(price, lengthCCI)) / linDev / 0.015;
def MT1 = if CCI > 0
          then Max(MT1[1], pricedata - ATRcci)
          else Min(MT1[1], pricedata + ATRcci);
plot data = MT1;
data.AssignValueColor(if c < MT1 then Color.RED else Color.GREEN);

# Agg Period 2


input agg2 = AggregationPeriod.DAY;
def c2 = close(period = agg2);
def h2 = high(period = agg2);
def l2 = low(period = agg2);
def pricedata2 = hl2(period = agg2);

DefineGlobalColor("TrendUp", CreateColor(0, 254, 30));
DefineGlobalColor("TrendDown", CreateColor(255, 3, 2));


input lengthCCI2 = 50;
input lengthATR2 = 5;
input AtrFactor2 = 0.7;

def ATRcci2 = Average(TrueRange(h2, c2, l), lengthATR2) * AtrFactor2;
def price2 = c2 + l2 + h2;
def linDev2 = LinDev(price2, lengthCCI2);
def CCI2 = if linDev2 == 0
          then 0
          else (price2 - Average(price2, lengthCCI2)) / linDev2 / 0.015;
def MT2 = if CCI2 > 0
          then Max(MT2[1], pricedata2 - ATRcci2)
          else Min(MT2[1], pricedata2 + ATRcci2);
plot data2 = MT2;
data2.AssignValueColor(if c2 < MT2 then Color.RED else Color.GREEN);
DefineGlobalColor("TrendUp", CreateColor(0, 254, 30));
DefineGlobalColor("TrendDown", CreateColor(255, 3, 2));

# Agg Period 3


input agg3 = AggregationPeriod.DAY;
def c3 = close(period = agg3);
def h3 = high(period = agg3);
def l3 = low(period = agg3);
def pricedata3 = hl2(period = agg3);

DefineGlobalColor("TrendUp", CreateColor(0, 254, 30));
DefineGlobalColor("TrendDown", CreateColor(255, 3, 2));


input lengthCCI3 = 50;
input lengthATR3 = 5;
input AtrFactor3 = 0.7;

def ATRcci3 = Average(TrueRange(h3, c3, l), lengthATR3) * AtrFactor3;
def price3 = c3 + l3 + h3;
def linDev3 = LinDev(price3, lengthCCI3);
def CCI3 = if linDev3 == 0
          then 0
          else (price3 - Average(price3, lengthCCI3)) / linDev3 / 0.015;
def MT3 = if CCI3 > 0
          then Max(MT3[1], pricedata3 - ATRcci3)
          else Min(MT3[1], pricedata3 + ATRcci3);
plot data3 = MT3;
data3.AssignValueColor(if c3 < MT3 then Color.RED else Color.GREEN);
DefineGlobalColor("TrendUp", CreateColor(0, 254, 30));
DefineGlobalColor("TrendDown", CreateColor(255, 3, 2));
Nice. Reminds me of a certain strategy... lol
 
H

horserider

Active member
Warehouse
@tenacity11 You are welcome. Hope it helps your trading.
 
T

thinky

Member
On SPY it is plotting a green line at 0, which makes the chart very compressed. Tried with and without pre-market data.

Wouldn't plot with other aggregation values and chart time frames.

 
H

horserider

Active member
Warehouse
@thinky How did you do that ? Sorry without more information I cannot help. Only suggestion is make sure your chart time frame is equal or less than lowest aggregation in the study.
 
T

thinky

Member
When I scrolled the chart to the right, away from the 0 line at the beginning, it plotted correctly.

Somehow it is plotting '0' and messing with the price axis auto setting. In this example used the 5D 5m time frame and the indicator default aggregation values.

 
H

horserider

Active member
Warehouse
@thinky Did you uncheck "fit studies"
 
markos

markos

Well-known member
VIP
Warehouse
@thinky 1. some studies have what's called a pre-fetch. 2. It's possible that one of your other studies doesn't play nice with this one. Take notes and pull studies out one at a time. 3. Also, match Horserider's chart agg: 10D5Min.
I noticed that the example at top only has the current study applied. 🤷‍♂️
 
T

thinky

Member
The indicator works with 10D 5m with Auto/Fit checked, but not with 5D 5m with Auto/Fit checked. So must uncheck Auto/Fit to use 5D 5m.

Is it because the longer aggregation data (agg2/3: Day) feeding values that make the plot behave this way (0 value, extreme ranges).

It is a weird behavior, the difference is 10D vs 5D (both screenshots with Auto/Fit checked):



 
Last edited:
markos

markos

Well-known member
VIP
Warehouse
@thinky we've been down this road before, haven't we?
Please go to Tutorials and Click on Super Secret Phone Number.
Someone at that number will be able to tell you why.
It's the ToS Advanced Option Dept. and they have the knowledge to work on all the peculiar problems.

Please let the group here know what you find out. Markos
 
B

Billions

Member
VIP
@horserider is this the most current code for (and do I need to alter anything
#Trend Magic Indicator
#Revised with Auto Aggregation and Global Colors
#Originally posted by horserider

def agg = getAggregationPeriod();
def c = close(period = agg);
def h = high(period = agg);
def l = low(period = agg);
def pricedata = hl2(period = agg);
DefineGlobalColor("TrendUp", CreateColor(0, 254, 30));
DefineGlobalColor("TrendDown", CreateColor(255, 3, 2));
input lengthCCI = 50;
input lengthATR = 5;
input AtrFactor = 0.7;
def ATRcci = Average(TrueRange(h, c, l), lengthATR) * AtrFactor;
def price = c + l + h;
def linDev = LinDev(price, lengthCCI);
def CCI = if linDev == 0
then 0
else (price - Average(price, lengthCCI)) / linDev / 0.015;
def MT1 = if CCI > 0
then Max(MT1[1], pricedata - ATRcci)
else Min(MT1[1], pricedata + ATRcci);
plot data = MT1;
data.AssignValueColor(if c < MT1 then GlobalColor("TrendDown") else GlobalColor("TrendUp"));
 
H

horserider

Active member
Warehouse
@Shinthus May look similiar but based on different indicators. And different lengths is not the same as different aggregations. So may look similar but big differences.
 
H

horserider

Active member
Warehouse
@Billions That is the original study with a change to make the study match the chart time frame. I newest code is the multitime frame in this thread. Depends what you want. I recommend the multitime frame but it takes a bit of effort to first set the time frames like you want. But is pretty easy just a few clicks. The single time frame with aggregation user set in the thread where the Trend Magic was first posted is also better in my opinion. Being able to set the aggregation has an advantage.
 
B

Billions

Member
VIP
@Billions That is the original study with a change to make the study match the chart time frame. I newest code is the multitime frame in this thread. Depends what you want. I recommend the multitime frame but it takes a bit of effort to first set the time frames like you want. But is pretty easy just a few clicks. The single time frame with aggregation user set in the thread where the Trend Magic was first posted is also better in my opinion. Being able to set the aggregation has an advantage.
I’ll hve to check it out thanks
 
B

Billions

Member
VIP
Can someone help need to code this for mobile I don't know if it can be done:
def agg = getAggregationPeriod();
def c = close(period = agg);
def h = high(period = agg);
def l = low(period = agg);
def pricedata = hl2(period = agg);
DefineGlobalColor("TrendUp", CreateColor(0, 254, 30));
DefineGlobalColor("TrendDown", CreateColor(255, 3, 2));
input lengthCCI = 50;
input lengthATR = 5;
input AtrFactor = 0.7;
def ATRcci = Average(TrueRange(h, c, l), lengthATR) * AtrFactor;
def price = c + l + h;
def linDev = LinDev(price, lengthCCI);
def CCI = if linDev == 0
then 0
else (price - Average(price, lengthCCI)) / linDev / 0.015;
def MT1 = if CCI > 0
then Max(MT1[1], pricedata - ATRcci)
else Min(MT1[1], pricedata + ATRcci);
plot data = MT1;
data.AssignValueColor(if c < MT1 then GlobalColor("TrendDown") else GlobalColor("TrendUp"));
 

Top