Visual DMI (The 10x Bars) MTF Labels for ThinkorSwim

S

sierioiza

New member
My first post here. I tweaked the DMI script on TOS to basically do what John Carter's 10x bars do (yay me! almost no experience with thinkscript).

Here is what I got.

Code:
input length = 14;
input averageType = AverageType.WILDERS;

def hiDiff = high - high[1];
def loDiff = low[1] - low;

def plusDM = if hiDiff > loDiff and hiDiff > 0 then hiDiff else 0;
def minusDM =  if loDiff > hiDiff and loDiff > 0 then loDiff else 0;

def ATR = MovingAverage(averageType, TrueRange(high, close, low), length);
def "DI+" = 100 * MovingAverage(averageType, plusDM, length) / ATR;
def "DI-" = 100 * MovingAverage(averageType, minusDM, length) / ATR;

def DX = if ("DI+" + "DI-" > 0) then 100 * AbsValue("DI+" - "DI-") / ("DI+" + "DI-") else 0;
def ADX = MovingAverage(averageType, DX, length);

declare upper;
declare once_per_bar;

AssignPriceColor(if "DI+" > "DI-" and ADX > 20 then Color.GREEN
  else if "DI+" < "DI-" and ADX > 20 then Color.RED
  else Color.YELLOW);

The next thing I want to accomplish is to use this study to create multi-time frame labels. I have Mobius's MTF label for TTM_Squeeze.

input dStr =aggregationPeriod.DAY;


script MySqueeze{
def length  = 20;
def AtrMult = 2.0;
def SdMult  = 2.0;

input period = aggregationPeriod.DAY;;


def valueClose   =  close(period = period);
def valueHigh   =  high(period = period);
def valueLow   =  low(period = period);

def SD = StDev(valueClose, length);

def Avg = Average(valueClose, length);

def ATR = Average(TrueRange(valueHigh, valueClose, valueLow), length);

def SDup = Avg + (SdMult * SD);

def ATRup = Avg + (AtrMult * ATR);

plot Squeeze = if SDup < ATRup 

               then 1

               else 0;


}

def dSQ= MySqueeze(dStr);
#  AddLabel(yes, dStr, if dSQ
#  then  Color.RED else  Color.GREEN); # display label red if has squeeze
AddLabel(yes, if dStr == aggregationPeriod.MONTH then "M"
    else
    if dStr == aggregationPeriod.WEEK then "W"
    else
    if dStr == aggregationPeriod.FOUR_DAYS then "4D"
    else
    if dStr == aggregationPeriod.THREE_DAYS then "3D"
    else
    if dStr == aggregationPeriod.TWO_DAYS then "2D"
    else
    if dStr == aggregationPeriod.DAY then "D"
    else
    if dStr == aggregationPeriod.FOUR_HOURS then "4H"
    else
    if dStr == aggregationPeriod.TWO_HOURS then "2H"
    else
    if dStr == aggregationPeriod.HOUR then "1H"
    else
    if dStr == aggregationPeriod.THIRTY_MIN then "30m"
    else
    if dStr == aggregationPeriod.TWENTY_MIN then "20m"
    else
    if dStr == aggregationPeriod.FIFTEEN_MIN then "15m"
    else
    if dStr == aggregationPeriod.TEN_MIN then "10m"
    else
    if dStr == aggregationPeriod.FIVE_MIN then "5m"
    else
    if dStr == aggregationPeriod.FOUR_MIN then "4m"
    else
    if dStr  == aggregationPeriod.THREE_MIN then "3m"
    else
    if dStr == aggregationPeriod.TWO_MIN then "2m"
    else
    if dStr  == aggregationPeriod.MIN then "1m"
    else "",  if dSQ
then  Color.RED else  Color.GREEN);
I tried replacing his script MySqueeze {the whole script} with 10x bars script at the top, but TOS is asking for at least one plot, and the Squeeze labels only used 2 colors--I need three colors. It also showed error with dSQ, and I had no idea how it wanted me to correct it. If anyone could help me figure out how to use my 10x bars script to create MTF label study I would highly appreciate that. Thank you!
 
Last edited by a moderator:
T

tomsk

Well-known member
VIP
@sierioiza After a quick code review I see several issues with your study set.

Firstly it seems that you are attempting to build some sort of an MTF indicator using the script() function.
Assuming you want to use this feature to process secondary aggregations, take note that using the script() function to handle secondary aggregations do not work. You might like to read my post at

https://usethinkscript.com/threads/...dies-utilizing-script-for-secondary-aggs.969/
Then you might like to consider a restructure of your code logic in light of that

Second issue is with the variable "dStr". The data is static. You probably want to dynamically determine the chart aggregation and then run a comparison with your AddLabel() statement. Suggest you use GetAggregationPeriod() to achieve that. The rest of your AddLabel statement seems to be fine. Great job on that by the way.

Here then is the modified section, once you have loaded that into a chart study then set your chart to different aggregations and see it do its thing.

Code:
def dStr = GetAggregationPeriod();
AddLabel(yes, if dStr == aggregationPeriod.MONTH then "M"
              else if dStr == aggregationPeriod.WEEK then "W"
              else if dStr == aggregationPeriod.FOUR_DAYS then "4D"
              else if dStr == aggregationPeriod.THREE_DAYS then "3D"
              else if dStr == aggregationPeriod.TWO_DAYS then "2D"
              else if dStr == aggregationPeriod.DAY then "D"
              else if dStr == aggregationPeriod.FOUR_HOURS then "4H"
              else if dStr == aggregationPeriod.TWO_HOURS then "2H"
              else if dStr == aggregationPeriod.HOUR then "1H"
              else if dStr == aggregationPeriod.THIRTY_MIN then "30m"
              else if dStr == aggregationPeriod.TWENTY_MIN then "20m"
              else if dStr == aggregationPeriod.FIFTEEN_MIN then "15m"
              else if dStr == aggregationPeriod.TEN_MIN then "10m"
              else if dStr == aggregationPeriod.FIVE_MIN then "5m"
              else if dStr == aggregationPeriod.FOUR_MIN then "4m"
              else if dStr == aggregationPeriod.THREE_MIN then "3m"
              else if dStr == aggregationPeriod.TWO_MIN then "2m"
              else if dStr == aggregationPeriod.MIN then "1m"
              else "", Color.GREEN);
 
S

sierioiza

New member
Thanks for the response.

To address your first point, I do not need to create an MTF label using the script{} function. This was just what I saw in Mobius' Squeeze MTF label, so I thought this may be the correct way to do it. I am new to thinkscript, so it does not matter to me if I use the script{} function or not. I just want to get it done correctly so it works. I read through the thread you linked, and it is specifically about some indicators. I thought I would see a template of how to reference a study in an MTF label, but no luck (or at least I did not see it).

To address you second point, I have no idea what you are talking about (sorry), but I will do as directed. Thank you.

Here are my two questions that remain.

How do I reference my study in an MTF label (without the script{} function, to make it work correctly)? Do I put it at the top of the label code, under each aggregation period (that would be a very long script), or do I just mention it by name and TOS will know what to do with it? I also don't know what the script it the label would have to look like.

How do I create a label that changes three colors? All I have seen so far were labels with just two colors: red and green. My study paints bars into one of the three colors based on parameters of the study. I want the color of the label to reflect the color of the current bar on each aggregation period. I am willing to be taught and learn how to do it. You are also welcome to write the label code for my study the way it should be done, and I will learn from it.
 
T

tomsk

Well-known member
VIP
@sierioiza Let's start over - state precisely what you're attempting to do. Still not very clear so you'll need to be real precise. I'll offer whatever guidance I can
 
S

sierioiza

New member
How about a picture, I think that will make a lot of sense.



My study paints the bars as you see in the image--green, yellow, and red. I want to create MTF labels as you see in the picture. The names in the labels correspond to different time frames, and the color of each label corresponds to the color of the current bar in the time frame the label is for. I got the study part to paint the bars (like what you see in the picture). I don't know how to create the labels that will reference my study and be of the same color as the current bar in the time frame of the label (like you see in the picture). I hope that makes sense.
 
T

tomsk

Well-known member
VIP
@sierioiza You essentially want to create MTF labels for your study. First define your plot conditions as well as define all the aggregation periods you want to use. Once that is done, you can build your labels quite easily via a mega if-then-else condition
 
S

sierioiza

New member
Yes, MTF labels for my study.
How do I do what you said in your second sentence? Isn't my study defining my plot conditions already? Also, if If I write plot in my code, wouldn't that draw something on the chart? I think the def dStr = GetAggregationPeriod(); and the rest of the code you gave me already defines all the aggregation periods, correct? How do I do three colors? Could you give me a template of what this label should look like? I have no idea what the script should look like for this study's MTF label...
 
S

sierioiza

New member
OK, here is the best I got. It still shows errors. Help, please.

Code:
input length = 14;
input averageType = AverageType.WILDERS;

def hiDiff = high - high[1];
def loDiff = low[1] - low;

def plusDM = if hiDiff > loDiff and hiDiff > 0 then hiDiff else 0;
def minusDM =  if loDiff > hiDiff and loDiff > 0 then loDiff else 0;

def ATR = MovingAverage(averageType, TrueRange(high, close, low), length);
def "DI+" = 100 * MovingAverage(averageType, plusDM, length) / ATR;
def "DI-" = 100 * MovingAverage(averageType, minusDM, length) / ATR;

def DX = if ("DI+" + "DI-" > 0) then 100 * AbsValue("DI+" - "DI-") / ("DI+" + "DI-") else 0;
def ADX = MovingAverage(averageType, DX, length);

DefineGlobalColor("Bullish", Color.GREEN);
DefineGlobalColor("Sideways", Color.YELLOW);
DefineGlobalColor("Bearish", Color.RED);

def bullish = ("DI+" > "DI-") and (ADX > 20);
def bearish = ("DI+" < "DI-") and (ADX > 20);
def sideways = (ADX < 20);

def dStr = GetAggregationPeriod();
AddLabel(yes, if bullish then GlobalColor("Bullish") else if bearish then GlobalColor("Bearish") else GlobalColor("Sideways"),
    if dStr == aggregationPeriod.MONTH then "10x:M"
              else if dStr == aggregationPeriod.WEEK then "10x:W"
              else if dStr == aggregationPeriod.FOUR_DAYS then "10x:4D"
              else if dStr == aggregationPeriod.THREE_DAYS then "10x:3D"
              else if dStr == aggregationPeriod.TWO_DAYS then "10x:2D"
              else if dStr == aggregationPeriod.DAY then "10x:D"
              else if dStr == aggregationPeriod.FOUR_HOURS then "10x:4H"
              else if dStr == aggregationPeriod.TWO_HOURS then "10x:2H"
              else if dStr == aggregationPeriod.HOUR then "10x:1H"
              else if dStr == aggregationPeriod.THIRTY_MIN then "10x:30m"
              else if dStr == aggregationPeriod.TWENTY_MIN then "10x:20m"
              else if dStr == aggregationPeriod.FIFTEEN_MIN then "10x:15m"
              else if dStr == aggregationPeriod.TEN_MIN then "10x:10m"
              else if dStr == aggregationPeriod.FIVE_MIN then "10x:5m"
              else if dStr == aggregationPeriod.FOUR_MIN then "10x:4m"
              else if dStr == aggregationPeriod.THREE_MIN then "10x:3m"
              else if dStr == aggregationPeriod.TWO_MIN then "10x:2m"
              else "10x:1m"
  );
 
Last edited by a moderator:
horserider

horserider

Well-known member
VIP
I believe you need to define the plots for each agg period.
input or def period5 = aggregation.fiveminutes
input or def period10 = aggregation.tenminutes
and so on
then use agg period for each calculation
def hiDiff = high (period = period5) - high[1](period = period5);

Just an example from off top of my head. Hope it gives you the idea.
 
T

tomsk

Well-known member
VIP
@sierioiza Since you have 16 different aggregation periods, you literally have to define 16 sets of definitions to calculate the DMI values for each aggregation
I won't write your entire study for you but will give you a general approach

First define your aggregation periods like so

def agg1 = aggregationPeriod.WEEK;
def agg2 = aggregationPeriod.FOUR_DAYS;
def agg3 = aggregationPeriod.THREE_DAYS;
def agg4 = aggregationPeriod.TWO_DAYS;
def agg5 = aggregationPeriod.DAY;
def agg6 = aggregationPeriod.FOUR_HOURS;
def agg7 = aggregationPeriod.TWO_HOURS;
def agg8 = aggregationPeriod.HOUR;
def agg9 = aggregationPeriod.THIRTY_MIN;
def agg10 = aggregationPeriod.TWENTY_MIN;
def agg11 = aggregationPeriod.FIFTEEN_MIN;
def agg12 = aggregationPeriod.TEN_MIN;
def agg13 = aggregationPeriod.FIVE_MIN;
def agg14 = aggregationPeriod.FOUR_MIN;
def agg15 = aggregationPeriod.THREE_MIN;
def agg16 = aggregationPeriod.TWO_MIN;

Then FOR EACH aggregation you will need to have specific computations for each aggregation
I'll give you an example of what one set would look like for agg1.

def hiDiff01 = high(period = agg1) - high(period = agg1)[1];
def loDiff01 = low(period = agg1)[1] - low(period = agg1);

def plusDM01 = if hiDiff01 > loDiff01 and hiDiff01 > 0 then hiDiff01 else 0;
def minusDM01 = if loDiff01 > hiDiff01 and loDiff01 > 0 then loDiff01 else 0;

def ATR01 = MovingAverage(averageType, TrueRange(high(period = agg1), close(period = agg1), low(period = agg1)), length);
def "DI+01" = 100 * MovingAverage(averageType, plusDM01, length) / ATR01;
def "DI-01" = 100 * MovingAverage(averageType, minusDM01, length) / ATR01;

def DX01 = if ("DI+01" + "DI-01" > 0) then 100 * AbsValue("DI+01" - "DI-01") / ("DI+01" + "DI-01") else 0;
def ADX01 = MovingAverage(averageType, DX01, length);

def bullish01 = ("DI+01" > "DI-01") and (ADX01 > 20);
def bearish01 = ("DI+01" < "DI-01") and (ADX01 > 20);
def sideways01 = (ADX01 < 20);

You will now want to replicate that another 15 times to handle the computation for another 15 aggregations but using different variable names. You can see that this will become a rather complex exercise. Suggest you start with 2 or maybe 3 aggregations, then build your study slowly.

Once you are done then you'll have the conditions ready to plug in to your AddLabel() statement which will be another humongous if then else compound statement.

Best of luck!
 
markos

markos

Well-known member
VIP
@tomsk Thank you for the template. Now I REALLY have an appreciation for coders! At least, you gave us all something to follow.
 
T

tomsk

Well-known member
VIP
S

sierioiza

New member
@tomsk Thank, that is very helpful. Here is what I wrote for two time frames so far. The last bit only covers a label for one aggregation period, and I don't know how to create a label for the second time frame. I guess I could easily do 2 labels in one study, but isn't there a way to create one label study, then pick the time frame from a drop down menu? In other words: one study for one label, you load up 5 copies of them into your study list, and select a different time frame for each copy under the study settings (like when you can change your moving average length, but do that with aggregation periods). Or is that not possible any more? Also, are there any parts of this script that can be stated once and apply for all aggregation periods? Like maybe colors for labels?

input length = 14;
input averageType = AverageType.WILDERS;

def agg1 = aggregationPeriod.TWO_HOURS;

def hiDiff01 = high(period = agg1) - high(period = agg1)[1];
def loDiff01 = low(period = agg1)[1] - low(period = agg1);

def plusDM01 = if hiDiff01 > loDiff01 and hiDiff01 > 0 then hiDiff01 else 0;
def minusDM01 = if loDiff01 > hiDiff01 and loDiff01 > 0 then loDiff01 else 0;

def ATR01 = MovingAverage(averageType, TrueRange(high(period = agg1), close(period = agg1), low(period = agg1)), length);
def "DI+01" = 100 * MovingAverage(averageType, plusDM01, length) / ATR01;
def "DI-01" = 100 * MovingAverage(averageType, minusDM01, length) / ATR01;

def DX01 = if ("DI+01" + "DI-01" > 0) then 100 * AbsValue("DI+01" - "DI-01") / ("DI+01" + "DI-01") else 0;
def ADX01 = MovingAverage(averageType, DX01, length);

def bullish01 = ("DI+01" > "DI-01") and (ADX01 > 20);
def bearish01 = ("DI+01" < "DI-01") and (ADX01 > 20);
def sideways01 = (ADX01 < 20);

def agg2 = aggregationPeriod.FOUR_HOURS;

def hiDiff02 = high(period = agg2) - high(period = agg2)[1];
def loDiff02 = low(period = agg2)[1] - low(period = agg2);

def plusDM02 = if hiDiff02 > loDiff02 and hiDiff02 > 0 then hiDiff02 else 0;
def minusDM02 = if loDiff02 > hiDiff02 and loDiff02 > 0 then loDiff02 else 0;

def ATR02 = MovingAverage(averageType, TrueRange(high(period = agg2), close(period = agg2), low(period = agg2)), length);
def "DI+02" = 100 * MovingAverage(averageType, plusDM02, length) / ATR02;
def "DI-02" = 100 * MovingAverage(averageType, minusDM02, length) / ATR02;

def DX02 = if ("DI+02" + "DI-02" > 0) then 100 * AbsValue("DI+02" - "DI-02") / ("DI+02" + "DI-02") else 0;
def ADX02 = MovingAverage(averageType, DX02, length);

def bullish02 = ("DI+02" > "DI-02") and (ADX02 > 20);
def bearish02 = ("DI+02" < "DI-02") and (ADX02 > 20);
def sideways02 = (ADX02 < 20);

DefineGlobalColor("Bullish", Color.GREEN);
DefineGlobalColor("Sideways", Color.YELLOW);
DefineGlobalColor("Bearish", Color.RED);

def dStr = GetAggregationPeriod();
AddLabel(yes,if dStr == aggregationPeriod.TWO_HOURS then "10x:2H" else "",if bullish01 then GlobalColor("Bullish") else if bearish01 then GlobalColor("Bearish") else GlobalColor("Sideways"));
 
T

tomsk

Well-known member
VIP
@sierioiza To turn this into an actual study, let's define three aggregations, say 4HOURS, 2HOURS and HOUR.
I have chosen to implement aggregation definitions via user selectable input definitions rather than statically set those within the study

Here then is your COMPLETED study based on the embedded logic you provided. Please study this carefully so that you can learn from this.
It is sectionalized complete with comments so that it is very easy to follow the logic. I have used your global color definitions you had defined earlier. The choice of colors can be changed within the user interface

Other interested readers like @markos and others can use this as a reference template for any future MTF studies they wish to do.

There are several ways to define labels, probably the easiest for you is to define one label per aggregation. That is how I have implemented this study. Other methods to define labels include automatically determining within the logic of the code the next higher aggregation based upon your chart aggregation. Typically it uses a huge if-then-else construct which can get cumbersome and tedious for the uninitiated.

Once you get some experience with ThinkScript you can then venture to explore other advanced possibilities.
All the best
tomsk

Code:
# DMI MTF Labels
# tomsk
# 11.21.2019

input length = 14;
input averageType = AverageType.WILDERS;

# DEFINE AGGREGATION PERIODS

input agg1  = aggregationPeriod.FOUR_HOURS;
input agg2  = aggregationPeriod.TWO_HOURS;
input agg3  = aggregationPeriod.HOUR;

# COLOR DEFINITIONS

DefineGlobalColor("Bullish", Color.GREEN);
DefineGlobalColor("Bearish", Color.RED);
DefineGlobalColor("Neutral", Color.YELLOW);

# AGGREGATION 1 SPECIFIC

def hiDiff01 = high(period = agg1) - high(period = agg1)[1];
def loDiff01 = low(period = agg1)[1] - low(period = agg1);

def plusDM01 = if hiDiff01 > loDiff01 and hiDiff01 > 0 then hiDiff01 else 0;
def minusDM01 =  if loDiff01 > hiDiff01 and loDiff01 > 0 then loDiff01 else 0;

def ATR01 = MovingAverage(averageType, TrueRange(high(period = agg1), close(period = agg1), low(period = agg1)), length);
def "DI+01" = 100 * MovingAverage(averageType, plusDM01, length) / ATR01;
def "DI-01" = 100 * MovingAverage(averageType, minusDM01, length) / ATR01;

def DX01 = if ("DI+01" + "DI-01" > 0) then 100 * AbsValue("DI+01" - "DI-01") / ("DI+01" + "DI-01") else 0;
def ADX01 = MovingAverage(averageType, DX01, length);

def bullish01 = ("DI+01" > "DI-01") and (ADX01 > 20);
def bearish01 = ("DI+01" < "DI-01") and (ADX01 > 20);
def sideways01 = (ADX01 < 20);

# AGGREGATION 2 SPECIFIC

def hiDiff02 = high(period = agg2) - high(period = agg2)[1];
def loDiff02 = low(period = agg2)[1] - low(period = agg2);

def plusDM02 = if hiDiff02 > loDiff02 and hiDiff02 > 0 then hiDiff02 else 0;
def minusDM02 =  if loDiff02 > hiDiff02 and loDiff02 > 0 then loDiff02 else 0;

def ATR02 = MovingAverage(averageType, TrueRange(high(period = agg2), close(period = agg2), low(period = agg2)), length);
def "DI+02" = 100 * MovingAverage(averageType, plusDM02, length) / ATR02;
def "DI-02" = 100 * MovingAverage(averageType, minusDM02, length) / ATR02;

def DX02 = if ("DI+02" + "DI-02" > 0) then 100 * AbsValue("DI+02" - "DI-02") / ("DI+02" + "DI-02") else 0;
def ADX02 = MovingAverage(averageType, DX02, length);

def bullish02 = ("DI+02" > "DI-02") and (ADX02 > 20);
def bearish02 = ("DI+02" < "DI-02") and (ADX02 > 20);
def sideways02 = (ADX02 < 20);

# AGGREGATION 3 SPECIFIC

def hiDiff03 = high(period = agg3) - high(period = agg3)[1];
def loDiff03 = low(period = agg3)[1] - low(period = agg3);

def plusDM03 = if hiDiff03 > loDiff03 and hiDiff03 > 0 then hiDiff03 else 0;
def minusDM03 =  if loDiff03 > hiDiff03 and loDiff03 > 0 then loDiff03 else 0;

def ATR03 = MovingAverage(averageType, TrueRange(high(period = agg3), close(period = agg3), low(period = agg3)), length);
def "DI+03" = 100 * MovingAverage(averageType, plusDM03, length) / ATR03;
def "DI-03" = 100 * MovingAverage(averageType, minusDM03, length) / ATR03;

def DX03 = if ("DI+03" + "DI-03" > 0) then 100 * AbsValue("DI+03" - "DI-03") / ("DI+03" + "DI-03") else 0;
def ADX03 = MovingAverage(averageType, DX03, length);

def bullish03 = ("DI+03" > "DI-03") and (ADX03 > 20);
def bearish03 = ("DI+03" < "DI-03") and (ADX03 > 20);
def sideways03 = (ADX03 < 20);

# Display Labels for Selected Aggregation Periods

AddLabel(1, "DMI Multiple Time Frame", Color.CYAN);

AddLabel(1, "Agg1: " + (agg1/1000/60)+ " min", if bullish01 then GlobalColor("Bullish") else if bearish01 then GlobalColor("Bearish") else GlobalColor("Neutral"));
AddLabel(1, "Agg2: " + (agg2/1000/60)+ " min", if bullish02 then GlobalColor("Bullish") else if bearish02 then GlobalColor("Bearish") else GlobalColor("Neutral"));
AddLabel(1, "Agg3: " + (agg3/1000/60)+ " min", if bullish03 then GlobalColor("Bullish") else if bearish03 then GlobalColor("Bearish") else GlobalColor("Neutral"));

# End DMI MTF Label
 
Last edited:
D

dvorakm

New member
2019 Donor
VIP
This is great! Is there a way to display the labels on all timeframes? As is, it only displays on charts below the three programmed time-frames, for example the 2 hour label appears on the 1 hour, but not the 2 hour or 3 hour.
 
S

sierioiza

New member
Hi everyone,

Thank you for teaching me how to do this. It is extremely helpful to learn by actually seeing what it should look like. I programmed my label yesterday for three time frames, and I ran into the exact same problem as @dvorakm mentioned--no labels if you are not on or under the lowest aggregation period. So, I multiplied my label for just one aggregation period 18 times, and each had a different display text and different aggregation period. I ended up with 18 studies, but at least it works (would be nice to have all the labels under one study, though; it does not seem to work at the moment...). If anyone wants to use it I am providing the code below. The code in the first post ending after AssignPriceColor(); is the 10x bars indicator from Simpler Trading that John Carter sells for $397. The label is their MTF 10x bars label that also goes for $397. I was super excited that I saved myself almost $800, and I thank everyone who helped me on this thread. Now if I could only figure out their reversal arrows indicator... It has to do with price channel and a break of high of the low bar, and a break of low of the high bar. But that will be another thread if I decide to play with some more script. Here is the code for a sample label: you can copy and paste it for a new study, just change two things as instructed in the comments starting with #

#10x bars from Simpler Trading label
# sieioiza
#11/22/2019

input length = 14;
input averageType = AverageType.WILDERS;

#change aggregation period to what you need. start typing aggregation and TOS will give all the available choices
def agg1 = aggregationPeriod.FOUR_HOURS;

def hiDiff01 = high(period = agg1) - high(period = agg1)[1];
def loDiff01 = low(period = agg1)[1] - low(period = agg1);

def plusDM01 = if hiDiff01 > loDiff01 and hiDiff01 > 0 then hiDiff01 else 0;
def minusDM01 = if loDiff01 > hiDiff01 and loDiff01 > 0 then loDiff01 else 0;

def ATR01 = MovingAverage(averageType, TrueRange(high(period = agg1), close(period = agg1), low(period = agg1)), length);
def "DI+01" = 100 * MovingAverage(averageType, plusDM01, length) / ATR01;
def "DI-01" = 100 * MovingAverage(averageType, minusDM01, length) / ATR01;

def DX01 = if ("DI+01" + "DI-01" > 0) then 100 * AbsValue("DI+01" - "DI-01") / ("DI+01" + "DI-01") else 0;
def ADX01 = MovingAverage(averageType, DX01, length);

def bullish01 = ("DI+01" > "DI-01") and (ADX01 > 20);
def bearish01 = ("DI+01" < "DI-01") and (ADX01 > 20);
def sideways01 = (ADX01 < 20);

DefineGlobalColor("Bullish", Color.GREEN);
DefineGlobalColor("Sideways", Color.YELLOW);
DefineGlobalColor("Bearish", Color.RED);

#change the text in "" to reflect the time frame you chose at the top
AddLabel(yes, "10x:4H",if bullish01 then GlobalColor("Bullish") else if bearish01 then GlobalColor("Bearish") else GlobalColor("Sideways"));
 
T

tomsk

Well-known member
VIP
This is great! Is there a way to display the labels on all timeframes? As is, it only displays on charts below the three programmed time-frames, for example the 2 hour label appears on the 1 hour, but not the 2 hour or 3 hour.
There seems to be some misconceptions on the display of secondary aggregations. I have discussed this on some other threads in the forum but is worth mentioning it here again for anyone new to ThinkScript who may not have this understanding.

You can only reference HIGHER aggregations, but not a lower aggregation than the chart you’re on. For example, you can use weekly aggregation on the daily chart, but not daily aggregation on the weekly chart. You can't use anything less than the primary (chart) aggregation.

If you are on a daily chart, you won't be able to reference aggregations lower than a daily, e.g. 1HR, 2HR, etc. You can only view aggregations larger than a daily, e.g. WEEK, MONTH, etc. In other words, you can only go up, not down.
 
T

tomsk

Well-known member
VIP
@sierioiza Read my post above, hopefully you've learn a thing or two on aggregations. Once you abide by those rules, the sky's the limit. 18 aggregations? Seriously ...
 
S

sierioiza

New member
@tomsk What he is saying is that when you are not on the lowest or lower aggregation, the label does not show up AT ALL. Say, the label covers 1 hour, 2 hours, and 4 hours. Your chart at 1 hour or lower will show all three labels. However, if I go to 2 hours it will not show 1 hour (which is normal and to be expected), but neither will it show 2 hours or 4 hours. That is the problem with having multiple aggregations in one study at the moment. Unless I am missing something. That was the reason I created 18 labels one for each aggregation period.

I read through your post and will use what you wrote to organize my code better (or when multiple aggregations are showing up correctly again). Thank you!
 
T

tomsk

Well-known member
VIP
@tomsk What he is saying is that when you are not on the lowest or lower aggregation, the label does not show up AT ALL. Say, the label covers 1 hour, 2 hours, and 4 hours. Your chart at 1 hour or lower will show all three labels. However, if I go to 2 hours it will not show 1 hour (which is normal and to be expected), but neither will it show 2 hours or 4 hours. That is the problem with having multiple aggregations in one study at the moment. Unless I am missing something. That was the reason I created 18 labels one for each aggregation period.

I read through your post and will use what you wrote to organize my code better (or when multiple aggregations are showing up correctly again). Thank you!
Have fun, and remember whenever any MTF labels don't work as you'd expect, always go back to the rules as described
 

Similar threads

Top