The Multi-10x MTF Labels Indicator for ThinkorSwim

sierioiza

New member
Ruby:
# 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 by a moderator:

Volatility Trading Range

VTR is a momentum indicator that shows if a stock is overbought or oversold based on its Weekly and Monthly average volatility trading range.

Download the indicator

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.
 

sierioiza

New member
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 #

Code:
#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"));
 

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.
 

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 ...
 

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!
 

tomsk

Well-known member
VIP
@sierioiza I looked at Simpler's 10x study some time ago, seems that they were looking at BOTH ADX/DMI and above average volumes for confirmation.
 

sierioiza

New member
@tomsk Yes, you are right, but that's only an add-on in my opinion. John does not always wait for the volume confirmation when ADX/DMI gives him enough information.
 

diazlaz

Well-known member
2019 Donor
VIP
thanks everyone for the ideas and contribution, spent some time developing the system out further, found it interesting.

In return, share your ideas and findings. let me know if you want any other time frames added to it.

CSCO
rdKbdM2.png


SPY
SxUB2wC.png


Ruby:
#Visual DMI (The 10x Bars) MTF Labels for ThinkorSwim
#Last Updated on Monday, November 25 2019 at 08:37:39 AM
#
#CREDITS
# John Carter's 10x bars, @sierioiza, @tomsk, @horserider, @markos
#
#CHANGELOG
# 2019.11.25 1.0 @diazlaz - Updated Port
# 2019.11.25 1.1 @diazlaz - Added Month Time frame
#
#DESCRIPTION
# For TSLA, we can see the 10X bars across various time frames,
# from the Weekly down to the 1-minute chart.  We can see at a
# glance that there is strong GREEN momentum across most time frames.
# Any short-term red sets up a buying opportunity back in the direction
# of the longer-term greens and vice versa.
#
#

# INPUTS

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

# CORE

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

AddLabel(yes, "10X System", COLOR.CYAN);


# AGGREGATION 1 - ONE MINUTE

def aM1 = aggregationPeriod.MIN;
def hiDiffM1 = high(period = aM1) - high(period = aM1)[1];
def loDiffM1 = low(period = aM1)[1] - low(period = aM1);

def plusDMM1 = if hiDiffM1 > loDiffM1 and hiDiffM1 > 0 then hiDiffM1 else 0;
def minusDMM1 =  if loDiffM1 > hiDiffM1 and loDiffM1 > 0 then loDiffM1 else 0;

def ATRM1 = MovingAverage(averageType, TrueRange(high(period = aM1), close(period = aM1), low(period = aM1)), length);
def "DI+M1" = 100 * MovingAverage(averageType, plusDMM1, length) / ATRM1;
def "DI-M1" = 100 * MovingAverage(averageType, minusDMM1, length) / ATRM1;

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

def bullishM1 = ("DI+M1" > "DI-M1") and (ADXM1 > 20);
def bearishM1 = ("DI+M1" < "DI-M1") and (ADXM1 > 20);
def sidewaysM1 = (ADXM1 < 20);

AddLabel(1, "1MIN", if bullishM1 then GlobalColor("Bullish") else
 if bearishM1 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 2 - TWO MINUTES

def aM2 = aggregationPeriod.TWO_MIN;
def hiDiffM2 = high(period = aM2) - high(period = aM2)[1];
def loDiffM2 = low(period = aM2)[1] - low(period = aM2);

def plusDMM2 = if hiDiffM2 > loDiffM2 and hiDiffM2 > 0 then hiDiffM2 else 0;
def minusDMM2 =  if loDiffM2 > hiDiffM2 and loDiffM2 > 0 then loDiffM2 else 0;

def ATRM2 = MovingAverage(averageType, TrueRange(high(period = aM2), close(period = aM2), low(period = aM2)), length);
def "DI+M2" = 100 * MovingAverage(averageType, plusDMM2, length) / ATRM2;
def "DI-M2" = 100 * MovingAverage(averageType, minusDMM2, length) / ATRM2;

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

def bullishM2 = ("DI+M2" > "DI-M2") and (ADXM2 > 20);
def bearishM2 = ("DI+M2" < "DI-M2") and (ADXM2 > 20);
def sidewaysM2 = (ADXM2 < 20);

AddLabel(1, "2MIN", if bullishM2 then GlobalColor("Bullish") else
 if bearishM2 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 3 - THREE MINUTES

def aM3 = aggregationPeriod.THREE_MIN;
def hiDiffM3 = high(period = aM3) - high(period = aM3)[1];
def loDiffM3 = low(period = aM3)[1] - low(period = aM3);

def plusDMM3 = if hiDiffM3 > loDiffM3 and hiDiffM3 > 0 then hiDiffM3 else 0;
def minusDMM3 =  if loDiffM3 > hiDiffM3 and loDiffM3 > 0 then loDiffM3 else 0;

def ATRM3 = MovingAverage(averageType, TrueRange(high(period = aM3), close(period = aM3), low(period = aM3)), length);
def "DI+M3" = 100 * MovingAverage(averageType, plusDMM3, length) / ATRM3;
def "DI-M3" = 100 * MovingAverage(averageType, minusDMM3, length) / ATRM3;

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

def bullishM3 = ("DI+M3" > "DI-M3") and (ADXM3 > 20);
def bearishM3 = ("DI+M3" < "DI-M3") and (ADXM3 > 20);
def sidewaysM3 = (ADXM3 < 20);

AddLabel(1, "3MIN", if bullishM3 then GlobalColor("Bullish") else
 if bearishM3 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 4 - FIVE MINUTES

def aM5 = aggregationPeriod.FIVE_MIN;
def hiDiffM5 = high(period = aM5) - high(period = aM5)[1];
def loDiffM5 = low(period = aM5)[1] - low(period = aM5);

def plusDMM5 = if hiDiffM5 > loDiffM5 and hiDiffM5 > 0 then hiDiffM5 else 0;
def minusDMM5 =  if loDiffM5 > hiDiffM5 and loDiffM5 > 0 then loDiffM5 else 0;

def ATRM5 = MovingAverage(averageType, TrueRange(high(period = aM5), close(period = aM5), low(period = aM5)), length);
def "DI+M5" = 100 * MovingAverage(averageType, plusDMM5, length) / ATRM5;
def "DI-M5" = 100 * MovingAverage(averageType, minusDMM5, length) / ATRM5;

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

def bullishM5 = ("DI+M5" > "DI-M5") and (ADXM5 > 20);
def bearishM5 = ("DI+M5" < "DI-M5") and (ADXM5 > 20);
def sidewaysM5 = (ADXM5 < 20);

AddLabel(1, "5MIN", if bullishM5 then GlobalColor("Bullish") else
 if bearishM5 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 5 - FIFTEEN MINUTES

def aM15 = aggregationPeriod.FIFTEEN_MIN;
def hiDiffM15 = high(period = aM15) - high(period = aM15)[1];
def loDiffM15 = low(period = aM15)[1] - low(period = aM15);

def plusDMM15 = if hiDiffM15 > loDiffM15 and hiDiffM15 > 0 then hiDiffM15 else 0;
def minusDMM15 =  if loDiffM15 > hiDiffM15 and loDiffM15 > 0 then loDiffM15 else 0;

def ATRM15 = MovingAverage(averageType, TrueRange(high(period = aM15), close(period = aM15), low(period = aM15)), length);
def "DI+M15" = 100 * MovingAverage(averageType, plusDMM15, length) / ATRM15;
def "DI-M15" = 100 * MovingAverage(averageType, minusDMM15, length) / ATRM15;

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

def bullishM15 = ("DI+M15" > "DI-M15") and (ADXM15 > 20);
def bearishM15 = ("DI+M15" < "DI-M15") and (ADXM15 > 20);
def sidewaysM15 = (ADXM15 < 20);

AddLabel(1, "15MIN", if bullishM15 then GlobalColor("Bullish") else
 if bearishM15 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 6 - THIRTY MINUTES

def aM30 = aggregationPeriod.THIRTY_MIN;
def hiDiffM30 = high(period = aM30) - high(period = aM30)[1];
def loDiffM30 = low(period = aM30)[1] - low(period = aM30);

def plusDMM30 = if hiDiffM30 > loDiffM30 and hiDiffM30 > 0 then hiDiffM30 else 0;
def minusDMM30 =  if loDiffM30 > hiDiffM30 and loDiffM30 > 0 then loDiffM30 else 0;

def ATRM30 = MovingAverage(averageType, TrueRange(high(period = aM30), close(period = aM30), low(period = aM30)), length);
def "DI+M30" = 100 * MovingAverage(averageType, plusDMM30, length) / ATRM30;
def "DI-M30" = 100 * MovingAverage(averageType, minusDMM30, length) / ATRM30;

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

def bullishM30 = ("DI+M30" > "DI-M30") and (ADXM30 > 20);
def bearishM30 = ("DI+M30" < "DI-M30") and (ADXM30 > 20);
def sidewaysM30 = (ADXM30 < 20);

AddLabel(1, "30MIN", if bullishM30 then GlobalColor("Bullish") else
 if bearishM30 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 7 - HOUR

def aH1 = aggregationPeriod.HOUR;
def hiDiffH1 = high(period = aH1) - high(period = aH1)[1];
def loDiffH1 = low(period = aH1)[1] - low(period = aH1);

def plusDMH1 = if hiDiffH1 > loDiffH1 and hiDiffH1 > 0 then hiDiffH1 else 0;
def minusDMH1 =  if loDiffH1 > hiDiffH1 and loDiffH1 > 0 then loDiffH1 else 0;

def ATRH1 = MovingAverage(averageType, TrueRange(high(period = aH1), close(period = aH1), low(period = aH1)), length);
def "DI+H1" = 100 * MovingAverage(averageType, plusDMH1, length) / ATRH1;
def "DI-H1" = 100 * MovingAverage(averageType, minusDMH1, length) / ATRH1;

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

def bullishH1 = ("DI+H1" > "DI-H1") and (ADXH1 > 20);
def bearishH1 = ("DI+H1" < "DI-H1") and (ADXH1 > 20);
def sidewaysH1 = (ADXH1 < 20);

AddLabel(1, "1HOUR", if bullishH1 then GlobalColor("Bullish") else
 if bearishH1 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 8 - TWO HOURS

def aH2 = aggregationPeriod.TWO_HOURS;
def hiDiffH2 = high(period = aH2) - high(period = aH2)[1];
def loDiffH2 = low(period = aH2)[1] - low(period = aH2);

def plusDMH2 = if hiDiffH2 > loDiffH2 and hiDiffH2 > 0 then hiDiffH2 else 0;
def minusDMH2 =  if loDiffH2 > hiDiffH2 and loDiffH2 > 0 then loDiffH2 else 0;

def ATRH2 = MovingAverage(averageType, TrueRange(high(period = aH2), close(period = aH2), low(period = aH2)), length);
def "DI+H2" = 100 * MovingAverage(averageType, plusDMH2, length) / ATRH2;
def "DI-H2" = 100 * MovingAverage(averageType, minusDMH2, length) / ATRH2;

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

def bullishH2 = ("DI+H2" > "DI-H2") and (ADXH2 > 20);
def bearishH2 = ("DI+H2" < "DI-H2") and (ADXH2 > 20);
def sidewaysH2 = (ADXH2 < 20);

AddLabel(1, "2HOUR", if bullishH2 then GlobalColor("Bullish") else
 if bearishH2 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 9 - FOUR HOURS

def aH4 = aggregationPeriod.FOUR_HOURS;
def hiDiffH4 = high(period = aH4) - high(period = aH4)[1];
def loDiffH4 = low(period = aH4)[1] - low(period = aH4);

def plusDMH4 = if hiDiffH4 > loDiffH4 and hiDiffH4 > 0 then hiDiffH4 else 0;
def minusDMH4 =  if loDiffH4 > hiDiffH4 and loDiffH4 > 0 then loDiffH4 else 0;

def ATRH4 = MovingAverage(averageType, TrueRange(high(period = aH4), close(period = aH4), low(period = aH4)), length);
def "DI+H4" = 100 * MovingAverage(averageType, plusDMH4, length) / ATRH4;
def "DI-H4" = 100 * MovingAverage(averageType, minusDMH4, length) / ATRH4;

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

def bullishH4 = ("DI+H4" > "DI-H4") and (ADXH4 > 20);
def bearishH4 = ("DI+H4" < "DI-H4") and (ADXH4 > 20);
def sidewaysH4 = (ADXH4 < 20);

AddLabel(1, "4HOUR", if bullishH4 then GlobalColor("Bullish") else
 if bearishH4 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 10 - ONE DAY

def aD1 = aggregationPeriod.DAY;
def hiDiffD1 = high(period = aD1) - high(period = aD1)[1];
def loDiffD1 = low(period = aD1)[1] - low(period = aD1);

def plusDMD1 = if hiDiffD1 > loDiffD1 and hiDiffD1 > 0 then hiDiffD1 else 0;
def minusDMD1 =  if loDiffD1 > hiDiffD1 and loDiffD1 > 0 then loDiffD1 else 0;

def ATRD1 = MovingAverage(averageType, TrueRange(high(period = aD1), close(period = aD1), low(period = aD1)), length);
def "DI+D1" = 100 * MovingAverage(averageType, plusDMD1, length) / ATRD1;
def "DI-D1" = 100 * MovingAverage(averageType, minusDMD1, length) / ATRD1;

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

def bullishD1 = ("DI+D1" > "DI-D1") and (ADXD1 > 20);
def bearishD1 = ("DI+D1" < "DI-D1") and (ADXD1 > 20);
def sidewaysD1 = (ADXD1 < 20);

AddLabel(1, "1DAY", if bullishD1 then GlobalColor("Bullish") else
 if bearishD1 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 11 - THREE DAYS

def aD3 = aggregationPeriod.THREE_DAYS;
def hiDiffD3 = high(period = aD3) - high(period = aD3)[1];
def loDiffD3 = low(period = aD3)[1] - low(period = aD3);

def plusDMD3 = if hiDiffD3 > loDiffD3 and hiDiffD3 > 0 then hiDiffD3 else 0;
def minusDMD3 =  if loDiffD3 > hiDiffD3 and loDiffD3 > 0 then loDiffD3 else 0;

def ATRD3 = MovingAverage(averageType, TrueRange(high(period = aD3), close(period = aD3), low(period = aD3)), length);
def "DI+D3" = 100 * MovingAverage(averageType, plusDMD3, length) / ATRD3;
def "DI-D3" = 100 * MovingAverage(averageType, minusDMD3, length) / ATRD3;

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

def bullishD3 = ("DI+D3" > "DI-D3") and (ADXD3 > 20);
def bearishD3 = ("DI+D3" < "DI-D3") and (ADXD3 > 20);
def sidewaysD3 = (ADXD3 < 20);

AddLabel(1, "3DAY", if bullishD3 then GlobalColor("Bullish") else
 if bearishD3 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 12 - ONE WEEK

def aW1 = aggregationPeriod.WEEK;
def hiDiffW1 = high(period = aW1) - high(period = aW1)[1];
def loDiffW1 = low(period = aW1)[1] - low(period = aW1);

def plusDMW1 = if hiDiffW1 > loDiffW1 and hiDiffW1 > 0 then hiDiffW1 else 0;
def minusDMW1 =  if loDiffW1 > hiDiffW1 and loDiffW1 > 0 then loDiffW1 else 0;

def ATRW1 = MovingAverage(averageType, TrueRange(high(period = aW1), close(period = aW1), low(period = aW1)), length);
def "DI+W1" = 100 * MovingAverage(averageType, plusDMW1, length) / ATRW1;
def "DI-W1" = 100 * MovingAverage(averageType, minusDMW1, length) / ATRW1;

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

def bullishW1 = ("DI+W1" > "DI-W1") and (ADXW1 > 20);
def bearishW1 = ("DI+W1" < "DI-W1") and (ADXW1 > 20);
def sidewaysW1 = (ADXW1 < 20);

AddLabel(1, "WEEK", if bullishW1 then GlobalColor("Bullish") else
 if bearishW1 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 13 - ONE MONTH

def aMM = aggregationPeriod.MONTH;
def hiDiffMM = high(period = aMM) - high(period = aMM)[1];
def loDiffMM = low(period = aMM)[1] - low(period = aMM);

def plusDMMM = if hiDiffMM > loDiffMM and hiDiffMM > 0 then hiDiffMM else 0;
def minusDMMM =  if loDiffMM > hiDiffMM and loDiffMM > 0 then loDiffMM else 0;

def ATRMM = MovingAverage(averageType, TrueRange(high(period = aMM), close(period = aMM), low(period = aMM)), length);
def "DI+MM" = 100 * MovingAverage(averageType, plusDMMM, length) / ATRMM;
def "DI-MM" = 100 * MovingAverage(averageType, minusDMMM, length) / ATRMM;

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

def bullishMM = ("DI+MM" > "DI-MM") and (ADXMM > 20);
def bearishMM = ("DI+MM" < "DI-MM") and (ADXMM > 20);
def sidewaysMM = (ADXMM < 20);

AddLabel(1, "MONTH", if bullishMM then GlobalColor("Bullish") else
 if bearishMM then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# END OF Visual DMI (The 10x Bars) MTF Labels for ThinkorSwim
 
Last edited:

tomsk

Well-known member
VIP
@diazlaz Very well done, might be worthwhile highlighting that this needs to be run on a 1 min chart, as those newer folks here may end up wondering how come it does not display. It's just a bummer that we can't use the script() function to handle all the different aggregations, sure would have simplified things.
 

diazlaz

Well-known member
2019 Donor
VIP
@diazlaz Very well done, might be worthwhile highlighting that this needs to be run on a 1 min chart, as those newer folks here may end up wondering how come it does not display. It's just a bummer that we can't use the script() function to handle all the different aggregations, sure would have simplified things.
thanks tomsk, for ur feedback great point :) - updated with timecheck, instructions and periods V1.2.
 

diazlaz

Well-known member
2019 Donor
VIP
Updated 1.2 - All 18 periods are now included.

Ruby:
#Visual DMI (The 10x Bars) MTF Labels for ThinkorSwim
#Last Updated on Monday, November 25 2019 at 09:34:43 AM
#
#CREDITS
# John Carter's 10x bars, @sierioiza, @tomsk, @horserider, @markos
#
#CHANGELOG
# 2019.11.25 1.0 @diazlaz - Updated Port
# 2019.11.25 1.1 @diazlaz - Added Month Time frame
# 2019.11.25 1.2 @diazlaz - Added additional timecheck on chart and instructions
# 2019.11.25 1.2 @diazlaz - Added additional aggregation periods (18 total)
# 2019.11.25 1.2 @diazlaz - Added additional periods 4MIN, 20MIN, 4DAYS
#
#DESCRIPTION
# For TSLA, we can see the 10X bars across various time frames,
# from the Weekly down to the 1-minute chart.  We can see at a
# glance that there is strong GREEN momentum across most time frames.
# Any short-term red sets up a buying opportunity back in the direction
# of the longer-term greens and vice versa.
#
#
#INSTRUCTIONS
# Set timeframe charts to 1 minute and 30 days.
#
#

# INPUTS

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

# CORE

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

# TIMEFRAME CHECK

def currentTimeFrame = getAggregationPeriod();
assert(currentTimeFrame == AggregationPeriod.MIN, "ERROR: timeFrame needs to be 1 minute");

# LABELS

AddLabel(yes, "10X System", COLOR.CYAN);


# AGGREGATION 1 - ONE MINUTE

def aM1 = aggregationPeriod.MIN;
def hiDiffM1 = high(period = aM1) - high(period = aM1)[1];
def loDiffM1 = low(period = aM1)[1] - low(period = aM1);

def plusDMM1 = if hiDiffM1 > loDiffM1 and hiDiffM1 > 0 then hiDiffM1 else 0;
def minusDMM1 =  if loDiffM1 > hiDiffM1 and loDiffM1 > 0 then loDiffM1 else 0;

def ATRM1 = MovingAverage(averageType, TrueRange(high(period = aM1), close(period = aM1), low(period = aM1)), length);
def "DI+M1" = 100 * MovingAverage(averageType, plusDMM1, length) / ATRM1;
def "DI-M1" = 100 * MovingAverage(averageType, minusDMM1, length) / ATRM1;

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

def bullishM1 = ("DI+M1" > "DI-M1") and (ADXM1 > 20);
def bearishM1 = ("DI+M1" < "DI-M1") and (ADXM1 > 20);
def sidewaysM1 = (ADXM1 < 20);

AddLabel(1, "1MIN", if bullishM1 then GlobalColor("Bullish") else
 if bearishM1 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 2 - TWO MINUTES

def aM2 = aggregationPeriod.TWO_MIN;
def hiDiffM2 = high(period = aM2) - high(period = aM2)[1];
def loDiffM2 = low(period = aM2)[1] - low(period = aM2);

def plusDMM2 = if hiDiffM2 > loDiffM2 and hiDiffM2 > 0 then hiDiffM2 else 0;
def minusDMM2 =  if loDiffM2 > hiDiffM2 and loDiffM2 > 0 then loDiffM2 else 0;

def ATRM2 = MovingAverage(averageType, TrueRange(high(period = aM2), close(period = aM2), low(period = aM2)), length);
def "DI+M2" = 100 * MovingAverage(averageType, plusDMM2, length) / ATRM2;
def "DI-M2" = 100 * MovingAverage(averageType, minusDMM2, length) / ATRM2;

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

def bullishM2 = ("DI+M2" > "DI-M2") and (ADXM2 > 20);
def bearishM2 = ("DI+M2" < "DI-M2") and (ADXM2 > 20);
def sidewaysM2 = (ADXM2 < 20);

AddLabel(1, "2MIN", if bullishM2 then GlobalColor("Bullish") else
 if bearishM2 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 3 - THREE MINUTES

def aM3 = aggregationPeriod.THREE_MIN;
def hiDiffM3 = high(period = aM3) - high(period = aM3)[1];
def loDiffM3 = low(period = aM3)[1] - low(period = aM3);

def plusDMM3 = if hiDiffM3 > loDiffM3 and hiDiffM3 > 0 then hiDiffM3 else 0;
def minusDMM3 =  if loDiffM3 > hiDiffM3 and loDiffM3 > 0 then loDiffM3 else 0;

def ATRM3 = MovingAverage(averageType, TrueRange(high(period = aM3), close(period = aM3), low(period = aM3)), length);
def "DI+M3" = 100 * MovingAverage(averageType, plusDMM3, length) / ATRM3;
def "DI-M3" = 100 * MovingAverage(averageType, minusDMM3, length) / ATRM3;

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

def bullishM3 = ("DI+M3" > "DI-M3") and (ADXM3 > 20);
def bearishM3 = ("DI+M3" < "DI-M3") and (ADXM3 > 20);
def sidewaysM3 = (ADXM3 < 20);

AddLabel(1, "3MIN", if bullishM3 then GlobalColor("Bullish") else
 if bearishM3 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 4 - FOUR MINUTES

def aM4 = aggregationPeriod.FOUR_MIN;
def hiDiffM4 = high(period = aM4) - high(period = aM4)[1];
def loDiffM4 = low(period = aM4)[1] - low(period = aM4);

def plusDMM4 = if hiDiffM4 > loDiffM4 and hiDiffM4 > 0 then hiDiffM4 else 0;
def minusDMM4 =  if loDiffM4 > hiDiffM4 and loDiffM4 > 0 then loDiffM4 else 0;

def ATRM4 = MovingAverage(averageType, TrueRange(high(period = aM4), close(period = aM4), low(period = aM4)), length);
def "DI+M4" = 100 * MovingAverage(averageType, plusDMM4, length) / ATRM4;
def "DI-M4" = 100 * MovingAverage(averageType, minusDMM4, length) / ATRM4;

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

def bullishM4 = ("DI+M4" > "DI-M4") and (ADXM4 > 20);
def bearishM4 = ("DI+M4" < "DI-M4") and (ADXM4 > 20);
def sidewaysM4 = (ADXM4 < 20);

AddLabel(1, "4MIN", if bullishM4 then GlobalColor("Bullish") else
 if bearishM4 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 5 - FIVE MINUTES

def aM5 = aggregationPeriod.FIVE_MIN;
def hiDiffM5 = high(period = aM5) - high(period = aM5)[1];
def loDiffM5 = low(period = aM5)[1] - low(period = aM5);

def plusDMM5 = if hiDiffM5 > loDiffM5 and hiDiffM5 > 0 then hiDiffM5 else 0;
def minusDMM5 =  if loDiffM5 > hiDiffM5 and loDiffM5 > 0 then loDiffM5 else 0;

def ATRM5 = MovingAverage(averageType, TrueRange(high(period = aM5), close(period = aM5), low(period = aM5)), length);
def "DI+M5" = 100 * MovingAverage(averageType, plusDMM5, length) / ATRM5;
def "DI-M5" = 100 * MovingAverage(averageType, minusDMM5, length) / ATRM5;

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

def bullishM5 = ("DI+M5" > "DI-M5") and (ADXM5 > 20);
def bearishM5 = ("DI+M5" < "DI-M5") and (ADXM5 > 20);
def sidewaysM5 = (ADXM5 < 20);

AddLabel(1, "5MIN", if bullishM5 then GlobalColor("Bullish") else
 if bearishM5 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 6 - TEN MINUTES

def aM10 = aggregationPeriod.TEN_MIN;
def hiDiffM10 = high(period = aM10) - high(period = aM10)[1];
def loDiffM10 = low(period = aM10)[1] - low(period = aM10);

def plusDMM10 = if hiDiffM10 > loDiffM10 and hiDiffM10 > 0 then hiDiffM10 else 0;
def minusDMM10 =  if loDiffM10 > hiDiffM10 and loDiffM10 > 0 then loDiffM10 else 0;

def ATRM10 = MovingAverage(averageType, TrueRange(high(period = aM10), close(period = aM10), low(period = aM10)), length);
def "DI+M10" = 100 * MovingAverage(averageType, plusDMM10, length) / ATRM10;
def "DI-M10" = 100 * MovingAverage(averageType, minusDMM10, length) / ATRM10;

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

def bullishM10 = ("DI+M10" > "DI-M10") and (ADXM10 > 20);
def bearishM10 = ("DI+M10" < "DI-M10") and (ADXM10 > 20);
def sidewaysM10 = (ADXM10 < 20);

AddLabel(1, "10MIN", if bullishM10 then GlobalColor("Bullish") else
 if bearishM10 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 7 - FIFTEEN MINUTES

def aM15 = aggregationPeriod.FIFTEEN_MIN;
def hiDiffM15 = high(period = aM15) - high(period = aM15)[1];
def loDiffM15 = low(period = aM15)[1] - low(period = aM15);

def plusDMM15 = if hiDiffM15 > loDiffM15 and hiDiffM15 > 0 then hiDiffM15 else 0;
def minusDMM15 =  if loDiffM15 > hiDiffM15 and loDiffM15 > 0 then loDiffM15 else 0;

def ATRM15 = MovingAverage(averageType, TrueRange(high(period = aM15), close(period = aM15), low(period = aM15)), length);
def "DI+M15" = 100 * MovingAverage(averageType, plusDMM15, length) / ATRM15;
def "DI-M15" = 100 * MovingAverage(averageType, minusDMM15, length) / ATRM15;

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

def bullishM15 = ("DI+M15" > "DI-M15") and (ADXM15 > 20);
def bearishM15 = ("DI+M15" < "DI-M15") and (ADXM15 > 20);
def sidewaysM15 = (ADXM15 < 20);

AddLabel(1, "15MIN", if bullishM15 then GlobalColor("Bullish") else
 if bearishM15 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 8 - TWENTY MINUTES

def aM20 = aggregationPeriod.TWENTY_MIN;
def hiDiffM20 = high(period = aM20) - high(period = aM20)[1];
def loDiffM20 = low(period = aM20)[1] - low(period = aM20);

def plusDMM20 = if hiDiffM20 > loDiffM20 and hiDiffM20 > 0 then hiDiffM20 else 0;
def minusDMM20 =  if loDiffM20 > hiDiffM20 and loDiffM20 > 0 then loDiffM20 else 0;

def ATRM20 = MovingAverage(averageType, TrueRange(high(period = aM20), close(period = aM20), low(period = aM20)), length);
def "DI+M20" = 100 * MovingAverage(averageType, plusDMM20, length) / ATRM20;
def "DI-M20" = 100 * MovingAverage(averageType, minusDMM20, length) / ATRM20;

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

def bullishM20 = ("DI+M20" > "DI-M20") and (ADXM20 > 20);
def bearishM20 = ("DI+M20" < "DI-M20") and (ADXM20 > 20);
def sidewaysM20 = (ADXM20 < 20);

AddLabel(1, "20MIN", if bullishM20 then GlobalColor("Bullish") else
 if bearishM20 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 9 - THIRTY MINUTES

def aM30 = aggregationPeriod.THIRTY_MIN;
def hiDiffM30 = high(period = aM30) - high(period = aM30)[1];
def loDiffM30 = low(period = aM30)[1] - low(period = aM30);

def plusDMM30 = if hiDiffM30 > loDiffM30 and hiDiffM30 > 0 then hiDiffM30 else 0;
def minusDMM30 =  if loDiffM30 > hiDiffM30 and loDiffM30 > 0 then loDiffM30 else 0;

def ATRM30 = MovingAverage(averageType, TrueRange(high(period = aM30), close(period = aM30), low(period = aM30)), length);
def "DI+M30" = 100 * MovingAverage(averageType, plusDMM30, length) / ATRM30;
def "DI-M30" = 100 * MovingAverage(averageType, minusDMM30, length) / ATRM30;

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

def bullishM30 = ("DI+M30" > "DI-M30") and (ADXM30 > 20);
def bearishM30 = ("DI+M30" < "DI-M30") and (ADXM30 > 20);
def sidewaysM30 = (ADXM30 < 20);

AddLabel(1, "30MIN", if bullishM30 then GlobalColor("Bullish") else
 if bearishM30 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 10 - HOUR

def aH1 = aggregationPeriod.HOUR;
def hiDiffH1 = high(period = aH1) - high(period = aH1)[1];
def loDiffH1 = low(period = aH1)[1] - low(period = aH1);

def plusDMH1 = if hiDiffH1 > loDiffH1 and hiDiffH1 > 0 then hiDiffH1 else 0;
def minusDMH1 =  if loDiffH1 > hiDiffH1 and loDiffH1 > 0 then loDiffH1 else 0;

def ATRH1 = MovingAverage(averageType, TrueRange(high(period = aH1), close(period = aH1), low(period = aH1)), length);
def "DI+H1" = 100 * MovingAverage(averageType, plusDMH1, length) / ATRH1;
def "DI-H1" = 100 * MovingAverage(averageType, minusDMH1, length) / ATRH1;

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

def bullishH1 = ("DI+H1" > "DI-H1") and (ADXH1 > 20);
def bearishH1 = ("DI+H1" < "DI-H1") and (ADXH1 > 20);
def sidewaysH1 = (ADXH1 < 20);

AddLabel(1, "1HOUR", if bullishH1 then GlobalColor("Bullish") else
 if bearishH1 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 11 - TWO HOURS

def aH2 = aggregationPeriod.TWO_HOURS;
def hiDiffH2 = high(period = aH2) - high(period = aH2)[1];
def loDiffH2 = low(period = aH2)[1] - low(period = aH2);

def plusDMH2 = if hiDiffH2 > loDiffH2 and hiDiffH2 > 0 then hiDiffH2 else 0;
def minusDMH2 =  if loDiffH2 > hiDiffH2 and loDiffH2 > 0 then loDiffH2 else 0;

def ATRH2 = MovingAverage(averageType, TrueRange(high(period = aH2), close(period = aH2), low(period = aH2)), length);
def "DI+H2" = 100 * MovingAverage(averageType, plusDMH2, length) / ATRH2;
def "DI-H2" = 100 * MovingAverage(averageType, minusDMH2, length) / ATRH2;

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

def bullishH2 = ("DI+H2" > "DI-H2") and (ADXH2 > 20);
def bearishH2 = ("DI+H2" < "DI-H2") and (ADXH2 > 20);
def sidewaysH2 = (ADXH2 < 20);

AddLabel(1, "2HOUR", if bullishH2 then GlobalColor("Bullish") else
 if bearishH2 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 12 - FOUR HOURS

def aH4 = aggregationPeriod.FOUR_HOURS;
def hiDiffH4 = high(period = aH4) - high(period = aH4)[1];
def loDiffH4 = low(period = aH4)[1] - low(period = aH4);

def plusDMH4 = if hiDiffH4 > loDiffH4 and hiDiffH4 > 0 then hiDiffH4 else 0;
def minusDMH4 =  if loDiffH4 > hiDiffH4 and loDiffH4 > 0 then loDiffH4 else 0;

def ATRH4 = MovingAverage(averageType, TrueRange(high(period = aH4), close(period = aH4), low(period = aH4)), length);
def "DI+H4" = 100 * MovingAverage(averageType, plusDMH4, length) / ATRH4;
def "DI-H4" = 100 * MovingAverage(averageType, minusDMH4, length) / ATRH4;

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

def bullishH4 = ("DI+H4" > "DI-H4") and (ADXH4 > 20);
def bearishH4 = ("DI+H4" < "DI-H4") and (ADXH4 > 20);
def sidewaysH4 = (ADXH4 < 20);

AddLabel(1, "4HOUR", if bullishH4 then GlobalColor("Bullish") else
 if bearishH4 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 13 - ONE DAY

def aD1 = aggregationPeriod.DAY;
def hiDiffD1 = high(period = aD1) - high(period = aD1)[1];
def loDiffD1 = low(period = aD1)[1] - low(period = aD1);

def plusDMD1 = if hiDiffD1 > loDiffD1 and hiDiffD1 > 0 then hiDiffD1 else 0;
def minusDMD1 =  if loDiffD1 > hiDiffD1 and loDiffD1 > 0 then loDiffD1 else 0;

def ATRD1 = MovingAverage(averageType, TrueRange(high(period = aD1), close(period = aD1), low(period = aD1)), length);
def "DI+D1" = 100 * MovingAverage(averageType, plusDMD1, length) / ATRD1;
def "DI-D1" = 100 * MovingAverage(averageType, minusDMD1, length) / ATRD1;

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

def bullishD1 = ("DI+D1" > "DI-D1") and (ADXD1 > 20);
def bearishD1 = ("DI+D1" < "DI-D1") and (ADXD1 > 20);
def sidewaysD1 = (ADXD1 < 20);

AddLabel(1, "1DAY", if bullishD1 then GlobalColor("Bullish") else
 if bearishD1 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 14 - TWO DAYS

def aD2 = aggregationPeriod.TWO_DAYS;
def hiDiffD2 = high(period = aD2) - high(period = aD2)[1];
def loDiffD2 = low(period = aD2)[1] - low(period = aD2);

def plusDMD2 = if hiDiffD2 > loDiffD2 and hiDiffD2 > 0 then hiDiffD2 else 0;
def minusDMD2 =  if loDiffD2 > hiDiffD2 and loDiffD2 > 0 then loDiffD2 else 0;

def ATRD2 = MovingAverage(averageType, TrueRange(high(period = aD2), close(period = aD2), low(period = aD2)), length);
def "DI+D2" = 100 * MovingAverage(averageType, plusDMD2, length) / ATRD2;
def "DI-D2" = 100 * MovingAverage(averageType, minusDMD2, length) / ATRD2;

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

def bullishD2 = ("DI+D2" > "DI-D2") and (ADXD2 > 20);
def bearishD2 = ("DI+D2" < "DI-D2") and (ADXD2 > 20);
def sidewaysD2 = (ADXD2 < 20);

AddLabel(1, "2DAY", if bullishD2 then GlobalColor("Bullish") else
 if bearishD2 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 15 - THREE DAYS

def aD3 = aggregationPeriod.THREE_DAYS;
def hiDiffD3 = high(period = aD3) - high(period = aD3)[1];
def loDiffD3 = low(period = aD3)[1] - low(period = aD3);

def plusDMD3 = if hiDiffD3 > loDiffD3 and hiDiffD3 > 0 then hiDiffD3 else 0;
def minusDMD3 =  if loDiffD3 > hiDiffD3 and loDiffD3 > 0 then loDiffD3 else 0;

def ATRD3 = MovingAverage(averageType, TrueRange(high(period = aD3), close(period = aD3), low(period = aD3)), length);
def "DI+D3" = 100 * MovingAverage(averageType, plusDMD3, length) / ATRD3;
def "DI-D3" = 100 * MovingAverage(averageType, minusDMD3, length) / ATRD3;

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

def bullishD3 = ("DI+D3" > "DI-D3") and (ADXD3 > 20);
def bearishD3 = ("DI+D3" < "DI-D3") and (ADXD3 > 20);
def sidewaysD3 = (ADXD3 < 20);

AddLabel(1, "3DAY", if bullishD3 then GlobalColor("Bullish") else
 if bearishD3 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 16 - FOUR DAYS

def aD4 = aggregationPeriod.FOUR_DAYS;
def hiDiffD4 = high(period = aD4) - high(period = aD4)[1];
def loDiffD4 = low(period = aD4)[1] - low(period = aD4);

def plusDMD4 = if hiDiffD4 > loDiffD4 and hiDiffD4 > 0 then hiDiffD4 else 0;
def minusDMD4 =  if loDiffD4 > hiDiffD4 and loDiffD4 > 0 then loDiffD4 else 0;

def ATRD4 = MovingAverage(averageType, TrueRange(high(period = aD4), close(period = aD4), low(period = aD4)), length);
def "DI+D4" = 100 * MovingAverage(averageType, plusDMD4, length) / ATRD4;
def "DI-D4" = 100 * MovingAverage(averageType, minusDMD4, length) / ATRD4;

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

def bullishD4 = ("DI+D4" > "DI-D4") and (ADXD4 > 20);
def bearishD4 = ("DI+D4" < "DI-D4") and (ADXD4 > 20);
def sidewaysD4 = (ADXD4 < 20);

AddLabel(1, "4DAY", if bullishD4 then GlobalColor("Bullish") else
 if bearishD4 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 17 - ONE WEEK

def aW1 = aggregationPeriod.WEEK;
def hiDiffW1 = high(period = aW1) - high(period = aW1)[1];
def loDiffW1 = low(period = aW1)[1] - low(period = aW1);

def plusDMW1 = if hiDiffW1 > loDiffW1 and hiDiffW1 > 0 then hiDiffW1 else 0;
def minusDMW1 =  if loDiffW1 > hiDiffW1 and loDiffW1 > 0 then loDiffW1 else 0;

def ATRW1 = MovingAverage(averageType, TrueRange(high(period = aW1), close(period = aW1), low(period = aW1)), length);
def "DI+W1" = 100 * MovingAverage(averageType, plusDMW1, length) / ATRW1;
def "DI-W1" = 100 * MovingAverage(averageType, minusDMW1, length) / ATRW1;

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

def bullishW1 = ("DI+W1" > "DI-W1") and (ADXW1 > 20);
def bearishW1 = ("DI+W1" < "DI-W1") and (ADXW1 > 20);
def sidewaysW1 = (ADXW1 < 20);

AddLabel(1, "WEEK", if bullishW1 then GlobalColor("Bullish") else
 if bearishW1 then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


# AGGREGATION 18 - ONE MONTH

def aMM = aggregationPeriod.MONTH;
def hiDiffMM = high(period = aMM) - high(period = aMM)[1];
def loDiffMM = low(period = aMM)[1] - low(period = aMM);

def plusDMMM = if hiDiffMM > loDiffMM and hiDiffMM > 0 then hiDiffMM else 0;
def minusDMMM =  if loDiffMM > hiDiffMM and loDiffMM > 0 then loDiffMM else 0;

def ATRMM = MovingAverage(averageType, TrueRange(high(period = aMM), close(period = aMM), low(period = aMM)), length);
def "DI+MM" = 100 * MovingAverage(averageType, plusDMMM, length) / ATRMM;
def "DI-MM" = 100 * MovingAverage(averageType, minusDMMM, length) / ATRMM;

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

def bullishMM = ("DI+MM" > "DI-MM") and (ADXMM > 20);
def bearishMM = ("DI+MM" < "DI-MM") and (ADXMM > 20);
def sidewaysMM = (ADXMM < 20);

AddLabel(1, "MONTH", if bullishMM then GlobalColor("Bullish") else
 if bearishMM then GlobalColor("Bearish")
 else GlobalColor("Neutral"));


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

markos

Well-known member
VIP
@diazlaz Because I use a longer time frame, could you add a 2 week? You could leave them commented out for those that don't want it.
 

diazlaz

Well-known member
2019 Donor
VIP
Updated to V1.4 - now includes input for sideways and generates statistics and percentage summary of the trends across all time frames.

xiHA2mG.png


h7VitMY.png


Ruby:
#Visual DMI (The 10x Bars) MTF Labels for ThinkorSwim
#Last Updated on Monday, November 25 2019 at 06:01:38 PM
#
#CREDITS
# John Carter's 10x bars, @sierioiza, @tomsk, @horserider, @markos
#
#CHANGELOG
# 2019.11.25 1.0 @diazlaz - Updated Port
# 2019.11.25 1.1 @diazlaz - Added Month Time frame
# 2019.11.25 1.2 @diazlaz - Added additional timecheck on chart and instructions
# 2019.11.25 1.2 @diazlaz - Added additional aggregation periods (18 total)
# 2019.11.25 1.2 @diazlaz - Added additional periods 4MIN, 20MIN, 2DAYS, 4DAYS
# 2019.11.25 1.3 @diazlaz - Added adxlength and showlabels inputs
# 2019.11.25 1.4 @diazlaz - Added states and stats labels

#
#DESCRIPTION
# For TSLA, we can see the 10X bars across various time frames,
# from the Weekly down to the 1-minute chart.  We can see at a
# glance that there is strong GREEN momentum across most time frames.
# Any short-term red sets up a buying opportunity back in the direction
# of the longer-term greens and vice versa.
#
#
#INSTRUCTIONS
# Set timeframe charts to 1 minute and 30 days.
#
#

#INPUTS
input length = 14; #DI+- length
input adxlength = 20; #adx sideways length
input averageType = AverageType.WILDERS;
input showlabels = yes; #show labels
input showstats = yes; #show stats

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

#TIMEFRAME CHECK
def currentTimeFrame = getAggregationPeriod();
assert(currentTimeFrame == AggregationPeriod.MIN, "ERROR: timeFrame needs to be 1 minute");

#LABELS
AddLabel(showlabels, "10X System V1.4", COLOR.CYAN);

# AGGREGATION 1 - ONE MINUTE

def aM1 = aggregationPeriod.MIN;
def hiDiffM1 = high(period = aM1) - high(period = aM1)[1];
def loDiffM1 = low(period = aM1)[1] - low(period = aM1);

def plusDMM1 = if hiDiffM1 > loDiffM1 and hiDiffM1 > 0 then hiDiffM1 else 0;
def minusDMM1 =  if loDiffM1 > hiDiffM1 and loDiffM1 > 0 then loDiffM1 else 0;

def ATRM1 = MovingAverage(averageType, TrueRange(high(period = aM1), close(period = aM1), low(period = aM1)), length);
def "DI+M1" = 100 * MovingAverage(averageType, plusDMM1, length) / ATRM1;
def "DI-M1" = 100 * MovingAverage(averageType, minusDMM1, length) / ATRM1;

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

def bullishM1 = ("DI+M1" > "DI-M1") and (ADXM1 > adxlength);
def bearishM1 = ("DI+M1" < "DI-M1") and (ADXM1 > adxlength);
def sidewaysM1 = (ADXM1 < adxlength);

def sStateM1 = if bullishM1 then 100 else if bearishM1 then -100 else 0;
def sPOSM1 = bullishM1;
def sNEGM1 = bearishM1;
def sSIDM1 = if sPOSM1 or sNEGM1 then 0 else sidewaysM1;

AddLabel(showlabels, "1MIN", if sStateM1 == 100 then GlobalColor("Bullish") else
if sStateM1 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));

# AGGREGATION 2 - TWO MINUTES

def aM2 = aggregationPeriod.TWO_MIN;
def hiDiffM2 = high(period = aM2) - high(period = aM2)[1];
def loDiffM2 = low(period = aM2)[1] - low(period = aM2);

def plusDMM2 = if hiDiffM2 > loDiffM2 and hiDiffM2 > 0 then hiDiffM2 else 0;
def minusDMM2 =  if loDiffM2 > hiDiffM2 and loDiffM2 > 0 then loDiffM2 else 0;

def ATRM2 = MovingAverage(averageType, TrueRange(high(period = aM2), close(period = aM2), low(period = aM2)), length);
def "DI+M2" = 100 * MovingAverage(averageType, plusDMM2, length) / ATRM2;
def "DI-M2" = 100 * MovingAverage(averageType, minusDMM2, length) / ATRM2;

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

def bullishM2 = ("DI+M2" > "DI-M2") and (ADXM2 > adxlength);
def bearishM2 = ("DI+M2" < "DI-M2") and (ADXM2 > adxlength);
def sidewaysM2 = (ADXM2 < adxlength);

def sStateM2 = if bullishM2 then 100 else if bearishM2 then -100 else 0;
def sPOSM2 = bullishM2;
def sNEGM2 = bearishM2;
def sSIDM2 = if sPOSM2 or sNEGM2 then 0 else sidewaysM2;

AddLabel(showlabels, "2MIN", if sStateM2 == 100 then GlobalColor("Bullish") else
if sStateM2 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));

# AGGREGATION 3 - THREE MINUTES

def aM3 = aggregationPeriod.THREE_MIN;
def hiDiffM3 = high(period = aM3) - high(period = aM3)[1];
def loDiffM3 = low(period = aM3)[1] - low(period = aM3);

def plusDMM3 = if hiDiffM3 > loDiffM3 and hiDiffM3 > 0 then hiDiffM3 else 0;
def minusDMM3 =  if loDiffM3 > hiDiffM3 and loDiffM3 > 0 then loDiffM3 else 0;

def ATRM3 = MovingAverage(averageType, TrueRange(high(period = aM3), close(period = aM3), low(period = aM3)), length);
def "DI+M3" = 100 * MovingAverage(averageType, plusDMM3, length) / ATRM3;
def "DI-M3" = 100 * MovingAverage(averageType, minusDMM3, length) / ATRM3;

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

def bullishM3 = ("DI+M3" > "DI-M3") and (ADXM3 > adxlength);
def bearishM3 = ("DI+M3" < "DI-M3") and (ADXM3 > adxlength);
def sidewaysM3 = (ADXM3 < adxlength);

def sStateM3 = if bullishM3 then 100 else if bearishM3 then -100 else 0;
def sPOSM3 = bullishM3;
def sNEGM3 = bearishM3;
def sSIDM3 = if sPOSM3 or sNEGM3 then 0 else sidewaysM3;

AddLabel(showlabels, "3MIN", if sStateM3 == 100 then GlobalColor("Bullish") else
if sStateM3 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));

# AGGREGATION 4 - FOUR MINUTES

def aM4 = aggregationPeriod.FOUR_MIN;
def hiDiffM4 = high(period = aM4) - high(period = aM4)[1];
def loDiffM4 = low(period = aM4)[1] - low(period = aM4);

def plusDMM4 = if hiDiffM4 > loDiffM4 and hiDiffM4 > 0 then hiDiffM4 else 0;
def minusDMM4 =  if loDiffM4 > hiDiffM4 and loDiffM4 > 0 then loDiffM4 else 0;

def ATRM4 = MovingAverage(averageType, TrueRange(high(period = aM4), close(period = aM4), low(period = aM4)), length);
def "DI+M4" = 100 * MovingAverage(averageType, plusDMM4, length) / ATRM4;
def "DI-M4" = 100 * MovingAverage(averageType, minusDMM4, length) / ATRM4;

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

def bullishM4 = ("DI+M4" > "DI-M4") and (ADXM4 > adxlength);
def bearishM4 = ("DI+M4" < "DI-M4") and (ADXM4 > adxlength);
def sidewaysM4 = (ADXM4 < adxlength);

def sStateM4 = if bullishM4 then 100 else if bearishM4 then -100 else 0;
def sPOSM4 = bullishM4;
def sNEGM4 = bearishM4;
def sSIDM4 = if sPOSM4 or sNEGM4 then 0 else sidewaysM4;

AddLabel(showlabels, "4MIN", if sStateM4 == 100 then GlobalColor("Bullish") else
if sStateM4 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));

# AGGREGATION 5 - FIVE MINUTES

def aM5 = aggregationPeriod.FIVE_MIN;
def hiDiffM5 = high(period = aM5) - high(period = aM5)[1];
def loDiffM5 = low(period = aM5)[1] - low(period = aM5);

def plusDMM5 = if hiDiffM5 > loDiffM5 and hiDiffM5 > 0 then hiDiffM5 else 0;
def minusDMM5 =  if loDiffM5 > hiDiffM5 and loDiffM5 > 0 then loDiffM5 else 0;

def ATRM5 = MovingAverage(averageType, TrueRange(high(period = aM5), close(period = aM5), low(period = aM5)), length);
def "DI+M5" = 100 * MovingAverage(averageType, plusDMM5, length) / ATRM5;
def "DI-M5" = 100 * MovingAverage(averageType, minusDMM5, length) / ATRM5;

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

def bullishM5 = ("DI+M5" > "DI-M5") and (ADXM5 > adxlength);
def bearishM5 = ("DI+M5" < "DI-M5") and (ADXM5 > adxlength);
def sidewaysM5 = (ADXM5 < adxlength);

def sStateM5 = if bullishM5 then 100 else if bearishM5 then -100 else 0;
def sPOSM5 = bullishM5;
def sNEGM5 = bearishM5;
def sSIDM5 = if sPOSM5 or sNEGM5 then 0 else sidewaysM5;

AddLabel(showlabels, "5MIN", if sStateM5 == 100 then GlobalColor("Bullish") else
if sStateM5 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));

# AGGREGATION 6 - TEN MINUTES

def aM10 = aggregationPeriod.TEN_MIN;
def hiDiffM10 = high(period = aM10) - high(period = aM10)[1];
def loDiffM10 = low(period = aM10)[1] - low(period = aM10);

def plusDMM10 = if hiDiffM10 > loDiffM10 and hiDiffM10 > 0 then hiDiffM10 else 0;
def minusDMM10 =  if loDiffM10 > hiDiffM10 and loDiffM10 > 0 then loDiffM10 else 0;

def ATRM10 = MovingAverage(averageType, TrueRange(high(period = aM10), close(period = aM10), low(period = aM10)), length);
def "DI+M10" = 100 * MovingAverage(averageType, plusDMM10, length) / ATRM10;
def "DI-M10" = 100 * MovingAverage(averageType, minusDMM10, length) / ATRM10;

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

def bullishM10 = ("DI+M10" > "DI-M10") and (ADXM10 > adxlength);
def bearishM10 = ("DI+M10" < "DI-M10") and (ADXM10 > adxlength);
def sidewaysM10 = (ADXM10 < adxlength);

def sStateM10 = if bullishM10 then 100 else if bearishM10 then -100 else 0;
def sPOSM10 = bullishM10;
def sNEGM10 = bearishM10;
def sSIDM10 = if sPOSM10 or sNEGM10 then 0 else sidewaysM10;

AddLabel(showlabels, "10MIN", if sStateM10 == 100 then GlobalColor("Bullish") else
if sStateM10 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));

# AGGREGATION 7 - FIFTEEN MINUTES

def aM15 = aggregationPeriod.FIFTEEN_MIN;
def hiDiffM15 = high(period = aM15) - high(period = aM15)[1];
def loDiffM15 = low(period = aM15)[1] - low(period = aM15);

def plusDMM15 = if hiDiffM15 > loDiffM15 and hiDiffM15 > 0 then hiDiffM15 else 0;
def minusDMM15 =  if loDiffM15 > hiDiffM15 and loDiffM15 > 0 then loDiffM15 else 0;

def ATRM15 = MovingAverage(averageType, TrueRange(high(period = aM15), close(period = aM15), low(period = aM15)), length);
def "DI+M15" = 100 * MovingAverage(averageType, plusDMM15, length) / ATRM15;
def "DI-M15" = 100 * MovingAverage(averageType, minusDMM15, length) / ATRM15;

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

def bullishM15 = ("DI+M15" > "DI-M15") and (ADXM15 > adxlength);
def bearishM15 = ("DI+M15" < "DI-M15") and (ADXM15 > adxlength);
def sidewaysM15 = (ADXM15 < adxlength);

def sStateM15 = if bullishM15 then 100 else if bearishM15 then -100 else 0;
def sPOSM15 = bullishM15;
def sNEGM15 = bearishM15;
def sSIDM15 = if sPOSM15 or sNEGM15 then 0 else sidewaysM15;

AddLabel(showlabels, "15MIN", if sStateM15 == 100 then GlobalColor("Bullish") else
if sStateM15 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));

# AGGREGATION 8 - TWENTY MINUTES

def aM20 = aggregationPeriod.TWENTY_MIN;
def hiDiffM20 = high(period = aM20) - high(period = aM20)[1];
def loDiffM20 = low(period = aM20)[1] - low(period = aM20);

def plusDMM20 = if hiDiffM20 > loDiffM20 and hiDiffM20 > 0 then hiDiffM20 else 0;
def minusDMM20 =  if loDiffM20 > hiDiffM20 and loDiffM20 > 0 then loDiffM20 else 0;

def ATRM20 = MovingAverage(averageType, TrueRange(high(period = aM20), close(period = aM20), low(period = aM20)), length);
def "DI+M20" = 100 * MovingAverage(averageType, plusDMM20, length) / ATRM20;
def "DI-M20" = 100 * MovingAverage(averageType, minusDMM20, length) / ATRM20;

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

def bullishM20 = ("DI+M20" > "DI-M20") and (ADXM20 > adxlength);
def bearishM20 = ("DI+M20" < "DI-M20") and (ADXM20 > adxlength);
def sidewaysM20 = (ADXM20 < adxlength);

def sStateM20 = if bullishM20 then 100 else if bearishM20 then -100 else 0;
def sPOSM20 = bullishM20;
def sNEGM20 = bearishM20;
def sSIDM20 = if sPOSM20 or sNEGM20 then 0 else sidewaysM20;

AddLabel(showlabels, "20MIN", if sStateM20 == 100 then GlobalColor("Bullish") else
if sStateM20 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));

# AGGREGATION 9 - THIRTY MINUTES

def aM30 = aggregationPeriod.THIRTY_MIN;
def hiDiffM30 = high(period = aM30) - high(period = aM30)[1];
def loDiffM30 = low(period = aM30)[1] - low(period = aM30);

def plusDMM30 = if hiDiffM30 > loDiffM30 and hiDiffM30 > 0 then hiDiffM30 else 0;
def minusDMM30 =  if loDiffM30 > hiDiffM30 and loDiffM30 > 0 then loDiffM30 else 0;

def ATRM30 = MovingAverage(averageType, TrueRange(high(period = aM30), close(period = aM30), low(period = aM30)), length);
def "DI+M30" = 100 * MovingAverage(averageType, plusDMM30, length) / ATRM30;
def "DI-M30" = 100 * MovingAverage(averageType, minusDMM30, length) / ATRM30;

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

def bullishM30 = ("DI+M30" > "DI-M30") and (ADXM30 > adxlength);
def bearishM30 = ("DI+M30" < "DI-M30") and (ADXM30 > adxlength);
def sidewaysM30 = (ADXM30 < adxlength);

def sStateM30 = if bullishM30 then 100 else if bearishM30 then -100 else 0;
def sPOSM30 = bullishM30;
def sNEGM30 = bearishM30;
def sSIDM30 = if sPOSM30 or sNEGM30 then 0 else sidewaysM30;

AddLabel(showlabels, "30MIN", if sStateM30 == 100 then GlobalColor("Bullish") else
if sStateM30 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));

# AGGREGATION 10 - HOUR

def aH1 = aggregationPeriod.HOUR;
def hiDiffH1 = high(period = aH1) - high(period = aH1)[1];
def loDiffH1 = low(period = aH1)[1] - low(period = aH1);

def plusDMH1 = if hiDiffH1 > loDiffH1 and hiDiffH1 > 0 then hiDiffH1 else 0;
def minusDMH1 =  if loDiffH1 > hiDiffH1 and loDiffH1 > 0 then loDiffH1 else 0;

def ATRH1 = MovingAverage(averageType, TrueRange(high(period = aH1), close(period = aH1), low(period = aH1)), length);
def "DI+H1" = 100 * MovingAverage(averageType, plusDMH1, length) / ATRH1;
def "DI-H1" = 100 * MovingAverage(averageType, minusDMH1, length) / ATRH1;

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

def bullishH1 = ("DI+H1" > "DI-H1") and (ADXH1 > adxlength);
def bearishH1 = ("DI+H1" < "DI-H1") and (ADXH1 > adxlength);
def sidewaysH1 = (ADXH1 < adxlength);

def sStateH1 = if bullishH1 then 100 else if bearishH1 then -100 else 0;
def sPOSH1 = bullishH1;
def sNEGH1 = bearishH1;
def sSIDH1 = if sPOSH1 or sNEGH1 then 0 else sidewaysH1;

AddLabel(showlabels, "1HOUR", if sStateH1 == 100 then GlobalColor("Bullish") else
if sStateH1 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));

# AGGREGATION 11 - TWO HOURS

def aH2 = aggregationPeriod.TWO_HOURS;
def hiDiffH2 = high(period = aH2) - high(period = aH2)[1];
def loDiffH2 = low(period = aH2)[1] - low(period = aH2);

def plusDMH2 = if hiDiffH2 > loDiffH2 and hiDiffH2 > 0 then hiDiffH2 else 0;
def minusDMH2 =  if loDiffH2 > hiDiffH2 and loDiffH2 > 0 then loDiffH2 else 0;

def ATRH2 = MovingAverage(averageType, TrueRange(high(period = aH2), close(period = aH2), low(period = aH2)), length);
def "DI+H2" = 100 * MovingAverage(averageType, plusDMH2, length) / ATRH2;
def "DI-H2" = 100 * MovingAverage(averageType, minusDMH2, length) / ATRH2;

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

def bullishH2 = ("DI+H2" > "DI-H2") and (ADXH2 > adxlength);
def bearishH2 = ("DI+H2" < "DI-H2") and (ADXH2 > adxlength);
def sidewaysH2 = (ADXH2 < adxlength);

def sStateH2 = if bullishH2 then 100 else if bearishH2 then -100 else 0;
def sPOSH2 = bullishH2;
def sNEGH2 = bearishH2;
def sSIDH2 = if sPOSH2 or sNEGH2 then 0 else sidewaysH2;

AddLabel(showlabels, "2HOUR", if sStateH2 == 100 then GlobalColor("Bullish") else
if sStateH2 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));

# AGGREGATION 12 - FOUR HOURS

def aH4 = aggregationPeriod.FOUR_HOURS;
def hiDiffH4 = high(period = aH4) - high(period = aH4)[1];
def loDiffH4 = low(period = aH4)[1] - low(period = aH4);

def plusDMH4 = if hiDiffH4 > loDiffH4 and hiDiffH4 > 0 then hiDiffH4 else 0;
def minusDMH4 =  if loDiffH4 > hiDiffH4 and loDiffH4 > 0 then loDiffH4 else 0;

def ATRH4 = MovingAverage(averageType, TrueRange(high(period = aH4), close(period = aH4), low(period = aH4)), length);
def "DI+H4" = 100 * MovingAverage(averageType, plusDMH4, length) / ATRH4;
def "DI-H4" = 100 * MovingAverage(averageType, minusDMH4, length) / ATRH4;

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

def bullishH4 = ("DI+H4" > "DI-H4") and (ADXH4 > adxlength);
def bearishH4 = ("DI+H4" < "DI-H4") and (ADXH4 > adxlength);
def sidewaysH4 = (ADXH4 < adxlength);

def sStateH4 = if bullishH4 then 100 else if bearishH4 then -100 else 0;
def sPOSH4 = bullishH4;
def sNEGH4 = bearishH4;
def sSIDH4 = if sPOSH4 or sNEGH4 then 0 else sidewaysH4;

AddLabel(showlabels, "4HOUR", if sStateH4 == 100 then GlobalColor("Bullish") else
if sStateH4 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));

# AGGREGATION 13 - ONE DAY

def aD1 = aggregationPeriod.DAY;
def hiDiffD1 = high(period = aD1) - high(period = aD1)[1];
def loDiffD1 = low(period = aD1)[1] - low(period = aD1);

def plusDMD1 = if hiDiffD1 > loDiffD1 and hiDiffD1 > 0 then hiDiffD1 else 0;
def minusDMD1 =  if loDiffD1 > hiDiffD1 and loDiffD1 > 0 then loDiffD1 else 0;

def ATRD1 = MovingAverage(averageType, TrueRange(high(period = aD1), close(period = aD1), low(period = aD1)), length);
def "DI+D1" = 100 * MovingAverage(averageType, plusDMD1, length) / ATRD1;
def "DI-D1" = 100 * MovingAverage(averageType, minusDMD1, length) / ATRD1;

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

def bullishD1 = ("DI+D1" > "DI-D1") and (ADXD1 > adxlength);
def bearishD1 = ("DI+D1" < "DI-D1") and (ADXD1 > adxlength);
def sidewaysD1 = (ADXD1 < adxlength);

def sStateD1 = if bullishD1 then 100 else if bearishD1 then -100 else 0;
def sPOSD1 = bullishD1;
def sNEGD1 = bearishD1;
def sSIDD1 = if sPOSD1 or sNEGD1 then 0 else sidewaysD1;

AddLabel(showlabels, "1DAY", if sStateD1 == 100 then GlobalColor("Bullish") else
if sStateD1 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));

# AGGREGATION 14 - TWO DAYS

def aD2 = aggregationPeriod.TWO_DAYS;
def hiDiffD2 = high(period = aD2) - high(period = aD2)[1];
def loDiffD2 = low(period = aD2)[1] - low(period = aD2);

def plusDMD2 = if hiDiffD2 > loDiffD2 and hiDiffD2 > 0 then hiDiffD2 else 0;
def minusDMD2 =  if loDiffD2 > hiDiffD2 and loDiffD2 > 0 then loDiffD2 else 0;

def ATRD2 = MovingAverage(averageType, TrueRange(high(period = aD2), close(period = aD2), low(period = aD2)), length);
def "DI+D2" = 100 * MovingAverage(averageType, plusDMD2, length) / ATRD2;
def "DI-D2" = 100 * MovingAverage(averageType, minusDMD2, length) / ATRD2;

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

def bullishD2 = ("DI+D2" > "DI-D2") and (ADXD2 > adxlength);
def bearishD2 = ("DI+D2" < "DI-D2") and (ADXD2 > adxlength);
def sidewaysD2 = (ADXD2 < adxlength);

def sStateD2 = if bullishD2 then 100 else if bearishD2 then -100 else 0;
def sPOSD2 = bullishD2;
def sNEGD2 = bearishD2;
def sSIDD2 = if sPOSD2 or sNEGD2 then 0 else sidewaysD2;

AddLabel(showlabels, "2DAY", if sStateD2 == 100 then GlobalColor("Bullish") else
if sStateD2 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));

# AGGREGATION 15 - THREE DAYS

def aD3 = aggregationPeriod.THREE_DAYS;
def hiDiffD3 = high(period = aD3) - high(period = aD3)[1];
def loDiffD3 = low(period = aD3)[1] - low(period = aD3);

def plusDMD3 = if hiDiffD3 > loDiffD3 and hiDiffD3 > 0 then hiDiffD3 else 0;
def minusDMD3 =  if loDiffD3 > hiDiffD3 and loDiffD3 > 0 then loDiffD3 else 0;

def ATRD3 = MovingAverage(averageType, TrueRange(high(period = aD3), close(period = aD3), low(period = aD3)), length);
def "DI+D3" = 100 * MovingAverage(averageType, plusDMD3, length) / ATRD3;
def "DI-D3" = 100 * MovingAverage(averageType, minusDMD3, length) / ATRD3;

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

def bullishD3 = ("DI+D3" > "DI-D3") and (ADXD3 > adxlength);
def bearishD3 = ("DI+D3" < "DI-D3") and (ADXD3 > adxlength);
def sidewaysD3 = (ADXD3 < adxlength);

def sStateD3 = if bullishD3 then 100 else if bearishD3 then -100 else 0;
def sPOSD3 = bullishD3;
def sNEGD3 = bearishD3;
def sSIDD3 = if sPOSD3 or sNEGD3 then 0 else sidewaysD3;

AddLabel(showlabels, "3DAY", if sStateD3 == 100 then GlobalColor("Bullish") else
if sStateD3 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));

# AGGREGATION 16 - FOUR DAYS

def aD4 = aggregationPeriod.FOUR_DAYS;
def hiDiffD4 = high(period = aD4) - high(period = aD4)[1];
def loDiffD4 = low(period = aD4)[1] - low(period = aD4);

def plusDMD4 = if hiDiffD4 > loDiffD4 and hiDiffD4 > 0 then hiDiffD4 else 0;
def minusDMD4 =  if loDiffD4 > hiDiffD4 and loDiffD4 > 0 then loDiffD4 else 0;

def ATRD4 = MovingAverage(averageType, TrueRange(high(period = aD4), close(period = aD4), low(period = aD4)), length);
def "DI+D4" = 100 * MovingAverage(averageType, plusDMD4, length) / ATRD4;
def "DI-D4" = 100 * MovingAverage(averageType, minusDMD4, length) / ATRD4;

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

def bullishD4 = ("DI+D4" > "DI-D4") and (ADXD4 > adxlength);
def bearishD4 = ("DI+D4" < "DI-D4") and (ADXD4 > adxlength);
def sidewaysD4 = (ADXD4 < adxlength);

def sStateD4 = if bullishD4 then 100 else if bearishD4 then -100 else 0;
def sPOSD4 = bullishD4;
def sNEGD4 = bearishD4;
def sSIDD4 = if sPOSD4 or sNEGD4 then 0 else sidewaysD4;

AddLabel(showlabels, "4DAY", if sStateD4 == 100 then GlobalColor("Bullish") else
if sStateD4 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));

# AGGREGATION 17 - ONE WEEK

def aW1 = aggregationPeriod.WEEK;
def hiDiffW1 = high(period = aW1) - high(period = aW1)[1];
def loDiffW1 = low(period = aW1)[1] - low(period = aW1);

def plusDMW1 = if hiDiffW1 > loDiffW1 and hiDiffW1 > 0 then hiDiffW1 else 0;
def minusDMW1 =  if loDiffW1 > hiDiffW1 and loDiffW1 > 0 then loDiffW1 else 0;

def ATRW1 = MovingAverage(averageType, TrueRange(high(period = aW1), close(period = aW1), low(period = aW1)), length);
def "DI+W1" = 100 * MovingAverage(averageType, plusDMW1, length) / ATRW1;
def "DI-W1" = 100 * MovingAverage(averageType, minusDMW1, length) / ATRW1;

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

def bullishW1 = ("DI+W1" > "DI-W1") and (ADXW1 > adxlength);
def bearishW1 = ("DI+W1" < "DI-W1") and (ADXW1 > adxlength);
def sidewaysW1 = (ADXW1 < adxlength);

def sStateW1 = if bullishW1 then 100 else if bearishW1 then -100 else 0;
def sPOSW1 = bullishW1;
def sNEGW1 = bearishW1;
def sSIDW1 = if sPOSW1 or sNEGW1 then 0 else sidewaysW1;

AddLabel(showlabels, "WEEK", if sStateW1 == 100 then GlobalColor("Bullish") else
if sStateW1 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));

# AGGREGATION 18 - ONE MONTH

def aMM = aggregationPeriod.MONTH;
def hiDiffMM = high(period = aMM) - high(period = aMM)[1];
def loDiffMM = low(period = aMM)[1] - low(period = aMM);

def plusDMMM = if hiDiffMM > loDiffMM and hiDiffMM > 0 then hiDiffMM else 0;
def minusDMMM =  if loDiffMM > hiDiffMM and loDiffMM > 0 then loDiffMM else 0;

def ATRMM = MovingAverage(averageType, TrueRange(high(period = aMM), close(period = aMM), low(period = aMM)), length);
def "DI+MM" = 100 * MovingAverage(averageType, plusDMMM, length) / ATRMM;
def "DI-MM" = 100 * MovingAverage(averageType, minusDMMM, length) / ATRMM;

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

def bullishMM = ("DI+MM" > "DI-MM") and (ADXMM > adxlength);
def bearishMM = ("DI+MM" < "DI-MM") and (ADXMM > adxlength);
def sidewaysMM = (ADXMM < adxlength);

def sStateMM = if bullishMM then 100 else if bearishMM then -100 else 0;
def sPOSMM = bullishMM;
def sNEGMM = bearishMM;
def sSIDMM = if sPOSMM or sNEGMM then 0 else sidewaysMM;

AddLabel(showlabels, "MONTH", if sStateMM == 100 then GlobalColor("Bullish") else
if sStateMM == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));


#STATS
def sPOS = sPOSM1 + sPOSM2 + sPOSM3 + sPOSM4 + sPOSM5 + sPOSM10 + sPOSM15 +
sPOSM20 + sPOSM30 + sPOSH1 + sPOSH2 + sPOSH4 + sPOSD1 + sPOSD2 + sPOSD3 +
sPOSD4 + sPOSW1 + sPOSMM;

def sNEG = sNEGM1 + sNEGM2 + sNEGM3 + sNEGM4 + sNEGM5 + sNEGM10 + sNEGM15 +
sNEGM20 + sNEGM30 + sNEGH1 + sNEGH2 + sNEGH4 + sNEGD1 + sNEGD2 + sNEGD3 +
sNEGD4 + sNEGW1 + sNEGMM;

def sSID = sSIDM1 + sSIDM2 + sSIDM3 + sSIDM4 + sSIDM5 + sSIDM10 + sSIDM15 +
sSIDM20 + sSIDM30 + sSIDH1 + sSIDH2 + sSIDH4 + sSIDD1 + sSIDD2 + sSIDD3 +
sSIDD4 + sSIDW1 + sSIDMM;

def sTOL = sPOS + sNEG + sSID;

AddLabel (showstats, "Bullish: " + sPOS + " (" +
  AsPercent(Round(sPOS / sTOL, 2)) + ")", GlobalColor("Bullish"));

AddLabel (showstats, "Bearish: " + sNEG + " (" +
  AsPercent(Round(sNEG / sTOL, 2)) + ")", GlobalColor("Bearish"));

AddLabel (showstats, "Neutral: " + sSID + " (" +
  AsPercent(Round(sSID / sTOL, 2)) + ")", GlobalColor("Neutral"));


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

tomsk

Well-known member
VIP
@diazlaz Here's another enhancement you might like to consider. Currently this study has about 18 different aggregations, which might be more than most people might want to see on their charts. Perhaps you might want to consider displaying a set of just 5 or maybe 6 aggregations and let the user choose whatever aggregations they wish to see. You can of course define a default set of your favorite timeframes in this regard. Since the aggregations are currently defined by a series of "def" statements, just change them to inputs. Should be a relatively quick turnaround.
 

diazlaz

Well-known member
2019 Donor
VIP
@diazlaz Here's another enhancement you might like to consider. Currently this study has about 18 different aggregations, which might be more than most people might want to see on their charts. Perhaps you might want to consider displaying a set of just 5 or maybe 6 aggregations and let the user choose whatever aggregations they wish to see. You can of course define a default set of your favorite timeframes in this regard. Since the aggregations are currently defined by a series of "def" statements, just change them to inputs. Should be a relatively quick turnaround.
will add it to the next release.
 

tomsk

Well-known member
VIP
That would certainly make it much easier for code maintenance and what's more more folks would feel compelled to use it, assuming of course they are DMI fans. ;)
 

diazlaz

Well-known member
2019 Donor
VIP
here is the Version 1.6 Release for Visual DMI (The 10x Bars) MTF Labels for ThinkorSwim

CHANGELOG

# 2019.11.25 1.5 @diazlaz - Added fix for NaN when data is not available
# such as monthly for UBER due to recent IPO.
# colors Label as Dark Gray when no data is available.
# handles count of stats when error is encountered.
# 2019.11.25 1.6 @diazlaz - Added inputs for label display all aggregation periods
# default display all set to yes.
# Note:
# STATS still calc all periods. if this is a ask to filter
# based on displayed aggregation periods please let me know
# and will incorporate this request.

Ruby:
#Visual DMI (The 10x Bars) MTF Labels for ThinkorSwim V1.6
#Last Updated on Monday, November 25 2019 at 09:51:52 PM
#
#CREDITS
# John Carter's 10x bars, @sierioiza, @tomsk, @horserider, @markos
#
#CHANGELOG
# 2019.11.25 1.0 @diazlaz - Updated Port
# 2019.11.25 1.1 @diazlaz - Added Month Time frame
# 2019.11.25 1.2 @diazlaz - Added additional timecheck on chart and instructions
# 2019.11.25 1.2 @diazlaz - Added additional aggregation periods (18 total)
# 2019.11.25 1.2 @diazlaz - Added additional periods 4MIN, 20MIN, 2DAYS, 4DAYS
# 2019.11.25 1.3 @diazlaz - Added adxlength and showlabels inputs
# 2019.11.25 1.4 @diazlaz - Added states and stats labels
# 2019.11.25 1.5 @diazlaz - Added fix for NaN when data is not available
#                           such as monthly for UBER due to recent IPO.
#                           colors Label as Dark Gray when no data is available.
#                           handles count of stats when error is encountered.
# 2019.11.25 1.6 @diazlaz - Added inputs for label display all aggregation periods
#                           default display all set to yes.
#                           Note:
#                            STATS still calc all periods. if this is a ask to filter
#                            based on displayed aggregation periods please let me know
#                            and will incorporate this request.
#
#DESCRIPTION
# For TSLA, we can see the 10X bars across various time frames,
# from the Weekly down to the 1-minute chart.  We can see at a
# glance that there is strong GREEN momentum across most time frames.
# Any short-term red sets up a buying opportunity back in the direction
# of the longer-term greens and vice versa.
#
#
#INSTRUCTIONS
# Set timeframe charts to 1 minute and 30 days.
#
#

#INPUTS
input length = 14; #DI+- length
input adxlength = 20; #ADX sideways length
input averageType = AverageType.WILDERS;
input showlabels = yes; #show labels
input showstats = yes; #show stats

input show1MIN = yes; #show ONE MINUTE label
input show2MIN = yes; #show TWO MINUTES label
input show3MIN = yes; #show THREE MINUTES label
input show4MIN = yes; #show FOUR MINUTES label
input show5MIN = yes; #show FIVE MINUTES label
input show10MIN = yes; #show TEN MINUTES label
input show15MIN = yes; #show FIFTEEN MINUTES label
input show20MIN = yes; #show TWENTY MINUTES label
input show30MIN = yes; #show THIRTY MINUTES label
input show1HOUR = yes; #show HOUR label
input show2HOUR = yes; #show TWO HOURS label
input show4HOUR = yes; #show FOUR HOURS label
input show1DAY = yes; #show ONE DAY label
input show2DAY = yes; #show TWO DAYS label
input show3DAY = yes; #show THREE DAYS label
input show4DAY = yes; #show FOUR DAYS label
input showWEEK = yes; #show ONE WEEK label
input showMONTH = yes; #show ONE MONTH label

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

#TIMEFRAME CHECK
def currentTimeFrame = getAggregationPeriod();
assert(currentTimeFrame == AggregationPeriod.MIN, "ERROR: timeFrame needs to be 1 minute");

#LABELS
AddLabel(showlabels, "10X System V1.6", COLOR.CYAN);


# AGGREGATION 1 - ONE MINUTE

def aM1 = aggregationPeriod.MIN;
def hiDiffM1 = high(period = aM1) - high(period = aM1)[1];
def loDiffM1 = low(period = aM1)[1] - low(period = aM1);

def plusDMM1 = if hiDiffM1 > loDiffM1 and hiDiffM1 > 0 then hiDiffM1 else 0;
def minusDMM1 =  if loDiffM1 > hiDiffM1 and loDiffM1 > 0 then loDiffM1 else 0;

def ATRM1 = MovingAverage(averageType, TrueRange(high(period = aM1), close(period = aM1), low(period = aM1)), length);
def "DI+M1" = 100 * MovingAverage(averageType, plusDMM1, length) / ATRM1;
def "DI-M1" = 100 * MovingAverage(averageType, minusDMM1, length) / ATRM1;

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

def bullishM1 = ("DI+M1" > "DI-M1") and (ADXM1 > adxlength);
def bearishM1 = ("DI+M1" < "DI-M1") and (ADXM1 > adxlength);
def sidewaysM1 = (ADXM1 < adxlength);

def sStateM1 = if bullishM1 then 100 else if bearishM1 then -100 else 0;
def sPOSM1 = if IsNan(sStateM1) then 0 else bullishM1;
def sNEGM1 = if IsNan(sStateM1) then 0 else bearishM1;
def sSIDM1 = if IsNan(sStateM1) then 0 else if sPOSM1 or sNEGM1 then 0 else sidewaysM1;

AddLabel(showlabels and show1MIN, "1MIN", if IsNan(sStateM1) then COLOR.DARK_GRAY else
if sStateM1 == 100 then GlobalColor("Bullish") else
if sStateM1 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));


# AGGREGATION 2 - TWO MINUTES

def aM2 = aggregationPeriod.TWO_MIN;
def hiDiffM2 = high(period = aM2) - high(period = aM2)[1];
def loDiffM2 = low(period = aM2)[1] - low(period = aM2);

def plusDMM2 = if hiDiffM2 > loDiffM2 and hiDiffM2 > 0 then hiDiffM2 else 0;
def minusDMM2 =  if loDiffM2 > hiDiffM2 and loDiffM2 > 0 then loDiffM2 else 0;

def ATRM2 = MovingAverage(averageType, TrueRange(high(period = aM2), close(period = aM2), low(period = aM2)), length);
def "DI+M2" = 100 * MovingAverage(averageType, plusDMM2, length) / ATRM2;
def "DI-M2" = 100 * MovingAverage(averageType, minusDMM2, length) / ATRM2;

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

def bullishM2 = ("DI+M2" > "DI-M2") and (ADXM2 > adxlength);
def bearishM2 = ("DI+M2" < "DI-M2") and (ADXM2 > adxlength);
def sidewaysM2 = (ADXM2 < adxlength);

def sStateM2 = if bullishM2 then 100 else if bearishM2 then -100 else 0;
def sPOSM2 = if IsNan(sStateM2) then 0 else bullishM2;
def sNEGM2 = if IsNan(sStateM2) then 0 else bearishM2;
def sSIDM2 = if IsNan(sStateM2) then 0 else if sPOSM2 or sNEGM2 then 0 else sidewaysM2;

AddLabel(showlabels and show2MIN, "2MIN", if IsNan(sStateM2) then COLOR.DARK_GRAY else
if sStateM2 == 100 then GlobalColor("Bullish") else
if sStateM2 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));


# AGGREGATION 3 - THREE MINUTES

def aM3 = aggregationPeriod.THREE_MIN;
def hiDiffM3 = high(period = aM3) - high(period = aM3)[1];
def loDiffM3 = low(period = aM3)[1] - low(period = aM3);

def plusDMM3 = if hiDiffM3 > loDiffM3 and hiDiffM3 > 0 then hiDiffM3 else 0;
def minusDMM3 =  if loDiffM3 > hiDiffM3 and loDiffM3 > 0 then loDiffM3 else 0;

def ATRM3 = MovingAverage(averageType, TrueRange(high(period = aM3), close(period = aM3), low(period = aM3)), length);
def "DI+M3" = 100 * MovingAverage(averageType, plusDMM3, length) / ATRM3;
def "DI-M3" = 100 * MovingAverage(averageType, minusDMM3, length) / ATRM3;

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

def bullishM3 = ("DI+M3" > "DI-M3") and (ADXM3 > adxlength);
def bearishM3 = ("DI+M3" < "DI-M3") and (ADXM3 > adxlength);
def sidewaysM3 = (ADXM3 < adxlength);

def sStateM3 = if bullishM3 then 100 else if bearishM3 then -100 else 0;
def sPOSM3 = if IsNan(sStateM3) then 0 else bullishM3;
def sNEGM3 = if IsNan(sStateM3) then 0 else bearishM3;
def sSIDM3 = if IsNan(sStateM3) then 0 else if sPOSM3 or sNEGM3 then 0 else sidewaysM3;

AddLabel(showlabels and show3MIN, "3MIN", if IsNan(sStateM3) then COLOR.DARK_GRAY else
if sStateM3 == 100 then GlobalColor("Bullish") else
if sStateM3 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));


# AGGREGATION 4 - FOUR MINUTES

def aM4 = aggregationPeriod.FOUR_MIN;
def hiDiffM4 = high(period = aM4) - high(period = aM4)[1];
def loDiffM4 = low(period = aM4)[1] - low(period = aM4);

def plusDMM4 = if hiDiffM4 > loDiffM4 and hiDiffM4 > 0 then hiDiffM4 else 0;
def minusDMM4 =  if loDiffM4 > hiDiffM4 and loDiffM4 > 0 then loDiffM4 else 0;

def ATRM4 = MovingAverage(averageType, TrueRange(high(period = aM4), close(period = aM4), low(period = aM4)), length);
def "DI+M4" = 100 * MovingAverage(averageType, plusDMM4, length) / ATRM4;
def "DI-M4" = 100 * MovingAverage(averageType, minusDMM4, length) / ATRM4;

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

def bullishM4 = ("DI+M4" > "DI-M4") and (ADXM4 > adxlength);
def bearishM4 = ("DI+M4" < "DI-M4") and (ADXM4 > adxlength);
def sidewaysM4 = (ADXM4 < adxlength);

def sStateM4 = if bullishM4 then 100 else if bearishM4 then -100 else 0;
def sPOSM4 = if IsNan(sStateM4) then 0 else bullishM4;
def sNEGM4 = if IsNan(sStateM4) then 0 else bearishM4;
def sSIDM4 = if IsNan(sStateM4) then 0 else if sPOSM4 or sNEGM4 then 0 else sidewaysM4;

AddLabel(showlabels and show4MIN, "4MIN", if IsNan(sStateM4) then COLOR.DARK_GRAY else
if sStateM4 == 100 then GlobalColor("Bullish") else
if sStateM4 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));


# AGGREGATION 5 - FIVE MINUTES

def aM5 = aggregationPeriod.FIVE_MIN;
def hiDiffM5 = high(period = aM5) - high(period = aM5)[1];
def loDiffM5 = low(period = aM5)[1] - low(period = aM5);

def plusDMM5 = if hiDiffM5 > loDiffM5 and hiDiffM5 > 0 then hiDiffM5 else 0;
def minusDMM5 =  if loDiffM5 > hiDiffM5 and loDiffM5 > 0 then loDiffM5 else 0;

def ATRM5 = MovingAverage(averageType, TrueRange(high(period = aM5), close(period = aM5), low(period = aM5)), length);
def "DI+M5" = 100 * MovingAverage(averageType, plusDMM5, length) / ATRM5;
def "DI-M5" = 100 * MovingAverage(averageType, minusDMM5, length) / ATRM5;

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

def bullishM5 = ("DI+M5" > "DI-M5") and (ADXM5 > adxlength);
def bearishM5 = ("DI+M5" < "DI-M5") and (ADXM5 > adxlength);
def sidewaysM5 = (ADXM5 < adxlength);

def sStateM5 = if bullishM5 then 100 else if bearishM5 then -100 else 0;
def sPOSM5 = if IsNan(sStateM5) then 0 else bullishM5;
def sNEGM5 = if IsNan(sStateM5) then 0 else bearishM5;
def sSIDM5 = if IsNan(sStateM5) then 0 else if sPOSM5 or sNEGM5 then 0 else sidewaysM5;

AddLabel(showlabels and show5MIN, "5MIN", if IsNan(sStateM5) then COLOR.DARK_GRAY else
if sStateM5 == 100 then GlobalColor("Bullish") else
if sStateM5 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));


# AGGREGATION 6 - TEN MINUTES

def aM10 = aggregationPeriod.TEN_MIN;
def hiDiffM10 = high(period = aM10) - high(period = aM10)[1];
def loDiffM10 = low(period = aM10)[1] - low(period = aM10);

def plusDMM10 = if hiDiffM10 > loDiffM10 and hiDiffM10 > 0 then hiDiffM10 else 0;
def minusDMM10 =  if loDiffM10 > hiDiffM10 and loDiffM10 > 0 then loDiffM10 else 0;

def ATRM10 = MovingAverage(averageType, TrueRange(high(period = aM10), close(period = aM10), low(period = aM10)), length);
def "DI+M10" = 100 * MovingAverage(averageType, plusDMM10, length) / ATRM10;
def "DI-M10" = 100 * MovingAverage(averageType, minusDMM10, length) / ATRM10;

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

def bullishM10 = ("DI+M10" > "DI-M10") and (ADXM10 > adxlength);
def bearishM10 = ("DI+M10" < "DI-M10") and (ADXM10 > adxlength);
def sidewaysM10 = (ADXM10 < adxlength);

def sStateM10 = if bullishM10 then 100 else if bearishM10 then -100 else 0;
def sPOSM10 = if IsNan(sStateM10) then 0 else bullishM10;
def sNEGM10 = if IsNan(sStateM10) then 0 else bearishM10;
def sSIDM10 = if IsNan(sStateM10) then 0 else if sPOSM10 or sNEGM10 then 0 else sidewaysM10;

AddLabel(showlabels and show10MIN, "10MIN", if IsNan(sStateM10) then COLOR.DARK_GRAY else
if sStateM10 == 100 then GlobalColor("Bullish") else
if sStateM10 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));


# AGGREGATION 7 - FIFTEEN MINUTES

def aM15 = aggregationPeriod.FIFTEEN_MIN;
def hiDiffM15 = high(period = aM15) - high(period = aM15)[1];
def loDiffM15 = low(period = aM15)[1] - low(period = aM15);

def plusDMM15 = if hiDiffM15 > loDiffM15 and hiDiffM15 > 0 then hiDiffM15 else 0;
def minusDMM15 =  if loDiffM15 > hiDiffM15 and loDiffM15 > 0 then loDiffM15 else 0;

def ATRM15 = MovingAverage(averageType, TrueRange(high(period = aM15), close(period = aM15), low(period = aM15)), length);
def "DI+M15" = 100 * MovingAverage(averageType, plusDMM15, length) / ATRM15;
def "DI-M15" = 100 * MovingAverage(averageType, minusDMM15, length) / ATRM15;

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

def bullishM15 = ("DI+M15" > "DI-M15") and (ADXM15 > adxlength);
def bearishM15 = ("DI+M15" < "DI-M15") and (ADXM15 > adxlength);
def sidewaysM15 = (ADXM15 < adxlength);

def sStateM15 = if bullishM15 then 100 else if bearishM15 then -100 else 0;
def sPOSM15 = if IsNan(sStateM15) then 0 else bullishM15;
def sNEGM15 = if IsNan(sStateM15) then 0 else bearishM15;
def sSIDM15 = if IsNan(sStateM15) then 0 else if sPOSM15 or sNEGM15 then 0 else sidewaysM15;

AddLabel(showlabels and show15MIN, "15MIN", if IsNan(sStateM15) then COLOR.DARK_GRAY else
if sStateM15 == 100 then GlobalColor("Bullish") else
if sStateM15 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));


# AGGREGATION 8 - TWENTY MINUTES

def aM20 = aggregationPeriod.TWENTY_MIN;
def hiDiffM20 = high(period = aM20) - high(period = aM20)[1];
def loDiffM20 = low(period = aM20)[1] - low(period = aM20);

def plusDMM20 = if hiDiffM20 > loDiffM20 and hiDiffM20 > 0 then hiDiffM20 else 0;
def minusDMM20 =  if loDiffM20 > hiDiffM20 and loDiffM20 > 0 then loDiffM20 else 0;

def ATRM20 = MovingAverage(averageType, TrueRange(high(period = aM20), close(period = aM20), low(period = aM20)), length);
def "DI+M20" = 100 * MovingAverage(averageType, plusDMM20, length) / ATRM20;
def "DI-M20" = 100 * MovingAverage(averageType, minusDMM20, length) / ATRM20;

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

def bullishM20 = ("DI+M20" > "DI-M20") and (ADXM20 > adxlength);
def bearishM20 = ("DI+M20" < "DI-M20") and (ADXM20 > adxlength);
def sidewaysM20 = (ADXM20 < adxlength);

def sStateM20 = if bullishM20 then 100 else if bearishM20 then -100 else 0;
def sPOSM20 = if IsNan(sStateM20) then 0 else bullishM20;
def sNEGM20 = if IsNan(sStateM20) then 0 else bearishM20;
def sSIDM20 = if IsNan(sStateM20) then 0 else if sPOSM20 or sNEGM20 then 0 else sidewaysM20;

AddLabel(showlabels and show20MIN, "20MIN", if IsNan(sStateM20) then COLOR.DARK_GRAY else
if sStateM20 == 100 then GlobalColor("Bullish") else
if sStateM20 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));


# AGGREGATION 9 - THIRTY MINUTES

def aM30 = aggregationPeriod.THIRTY_MIN;
def hiDiffM30 = high(period = aM30) - high(period = aM30)[1];
def loDiffM30 = low(period = aM30)[1] - low(period = aM30);

def plusDMM30 = if hiDiffM30 > loDiffM30 and hiDiffM30 > 0 then hiDiffM30 else 0;
def minusDMM30 =  if loDiffM30 > hiDiffM30 and loDiffM30 > 0 then loDiffM30 else 0;

def ATRM30 = MovingAverage(averageType, TrueRange(high(period = aM30), close(period = aM30), low(period = aM30)), length);
def "DI+M30" = 100 * MovingAverage(averageType, plusDMM30, length) / ATRM30;
def "DI-M30" = 100 * MovingAverage(averageType, minusDMM30, length) / ATRM30;

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

def bullishM30 = ("DI+M30" > "DI-M30") and (ADXM30 > adxlength);
def bearishM30 = ("DI+M30" < "DI-M30") and (ADXM30 > adxlength);
def sidewaysM30 = (ADXM30 < adxlength);

def sStateM30 = if bullishM30 then 100 else if bearishM30 then -100 else 0;
def sPOSM30 = if IsNan(sStateM30) then 0 else bullishM30;
def sNEGM30 = if IsNan(sStateM30) then 0 else bearishM30;
def sSIDM30 = if IsNan(sStateM30) then 0 else if sPOSM30 or sNEGM30 then 0 else sidewaysM30;

AddLabel(showlabels and show30MIN, "30MIN", if IsNan(sStateM30) then COLOR.DARK_GRAY else
if sStateM30 == 100 then GlobalColor("Bullish") else
if sStateM30 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));


# AGGREGATION 10 - HOUR

def aH1 = aggregationPeriod.HOUR;
def hiDiffH1 = high(period = aH1) - high(period = aH1)[1];
def loDiffH1 = low(period = aH1)[1] - low(period = aH1);

def plusDMH1 = if hiDiffH1 > loDiffH1 and hiDiffH1 > 0 then hiDiffH1 else 0;
def minusDMH1 =  if loDiffH1 > hiDiffH1 and loDiffH1 > 0 then loDiffH1 else 0;

def ATRH1 = MovingAverage(averageType, TrueRange(high(period = aH1), close(period = aH1), low(period = aH1)), length);
def "DI+H1" = 100 * MovingAverage(averageType, plusDMH1, length) / ATRH1;
def "DI-H1" = 100 * MovingAverage(averageType, minusDMH1, length) / ATRH1;

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

def bullishH1 = ("DI+H1" > "DI-H1") and (ADXH1 > adxlength);
def bearishH1 = ("DI+H1" < "DI-H1") and (ADXH1 > adxlength);
def sidewaysH1 = (ADXH1 < adxlength);

def sStateH1 = if bullishH1 then 100 else if bearishH1 then -100 else 0;
def sPOSH1 = if IsNan(sStateH1) then 0 else bullishH1;
def sNEGH1 = if IsNan(sStateH1) then 0 else bearishH1;
def sSIDH1 = if IsNan(sStateH1) then 0 else if sPOSH1 or sNEGH1 then 0 else sidewaysH1;

AddLabel(showlabels and show1HOUR, "1HOUR", if IsNan(sStateH1) then COLOR.DARK_GRAY else
if sStateH1 == 100 then GlobalColor("Bullish") else
if sStateH1 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));


# AGGREGATION 11 - TWO HOURS

def aH2 = aggregationPeriod.TWO_HOURS;
def hiDiffH2 = high(period = aH2) - high(period = aH2)[1];
def loDiffH2 = low(period = aH2)[1] - low(period = aH2);

def plusDMH2 = if hiDiffH2 > loDiffH2 and hiDiffH2 > 0 then hiDiffH2 else 0;
def minusDMH2 =  if loDiffH2 > hiDiffH2 and loDiffH2 > 0 then loDiffH2 else 0;

def ATRH2 = MovingAverage(averageType, TrueRange(high(period = aH2), close(period = aH2), low(period = aH2)), length);
def "DI+H2" = 100 * MovingAverage(averageType, plusDMH2, length) / ATRH2;
def "DI-H2" = 100 * MovingAverage(averageType, minusDMH2, length) / ATRH2;

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

def bullishH2 = ("DI+H2" > "DI-H2") and (ADXH2 > adxlength);
def bearishH2 = ("DI+H2" < "DI-H2") and (ADXH2 > adxlength);
def sidewaysH2 = (ADXH2 < adxlength);

def sStateH2 = if bullishH2 then 100 else if bearishH2 then -100 else 0;
def sPOSH2 = if IsNan(sStateH2) then 0 else bullishH2;
def sNEGH2 = if IsNan(sStateH2) then 0 else bearishH2;
def sSIDH2 = if IsNan(sStateH2) then 0 else if sPOSH2 or sNEGH2 then 0 else sidewaysH2;

AddLabel(showlabels and show2HOUR, "2HOUR", if IsNan(sStateH2) then COLOR.DARK_GRAY else
if sStateH2 == 100 then GlobalColor("Bullish") else
if sStateH2 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));


# AGGREGATION 12 - FOUR HOURS

def aH4 = aggregationPeriod.FOUR_HOURS;
def hiDiffH4 = high(period = aH4) - high(period = aH4)[1];
def loDiffH4 = low(period = aH4)[1] - low(period = aH4);

def plusDMH4 = if hiDiffH4 > loDiffH4 and hiDiffH4 > 0 then hiDiffH4 else 0;
def minusDMH4 =  if loDiffH4 > hiDiffH4 and loDiffH4 > 0 then loDiffH4 else 0;

def ATRH4 = MovingAverage(averageType, TrueRange(high(period = aH4), close(period = aH4), low(period = aH4)), length);
def "DI+H4" = 100 * MovingAverage(averageType, plusDMH4, length) / ATRH4;
def "DI-H4" = 100 * MovingAverage(averageType, minusDMH4, length) / ATRH4;

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

def bullishH4 = ("DI+H4" > "DI-H4") and (ADXH4 > adxlength);
def bearishH4 = ("DI+H4" < "DI-H4") and (ADXH4 > adxlength);
def sidewaysH4 = (ADXH4 < adxlength);

def sStateH4 = if bullishH4 then 100 else if bearishH4 then -100 else 0;
def sPOSH4 = if IsNan(sStateH4) then 0 else bullishH4;
def sNEGH4 = if IsNan(sStateH4) then 0 else bearishH4;
def sSIDH4 = if IsNan(sStateH4) then 0 else if sPOSH4 or sNEGH4 then 0 else sidewaysH4;

AddLabel(showlabels and show4HOUR, "4HOUR", if IsNan(sStateH4) then COLOR.DARK_GRAY else
if sStateH4 == 100 then GlobalColor("Bullish") else
if sStateH4 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));


# AGGREGATION 13 - ONE DAY

def aD1 = aggregationPeriod.DAY;
def hiDiffD1 = high(period = aD1) - high(period = aD1)[1];
def loDiffD1 = low(period = aD1)[1] - low(period = aD1);

def plusDMD1 = if hiDiffD1 > loDiffD1 and hiDiffD1 > 0 then hiDiffD1 else 0;
def minusDMD1 =  if loDiffD1 > hiDiffD1 and loDiffD1 > 0 then loDiffD1 else 0;

def ATRD1 = MovingAverage(averageType, TrueRange(high(period = aD1), close(period = aD1), low(period = aD1)), length);
def "DI+D1" = 100 * MovingAverage(averageType, plusDMD1, length) / ATRD1;
def "DI-D1" = 100 * MovingAverage(averageType, minusDMD1, length) / ATRD1;

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

def bullishD1 = ("DI+D1" > "DI-D1") and (ADXD1 > adxlength);
def bearishD1 = ("DI+D1" < "DI-D1") and (ADXD1 > adxlength);
def sidewaysD1 = (ADXD1 < adxlength);

def sStateD1 = if bullishD1 then 100 else if bearishD1 then -100 else 0;
def sPOSD1 = if IsNan(sStateD1) then 0 else bullishD1;
def sNEGD1 = if IsNan(sStateD1) then 0 else bearishD1;
def sSIDD1 = if IsNan(sStateD1) then 0 else if sPOSD1 or sNEGD1 then 0 else sidewaysD1;

AddLabel(showlabels and show1DAY, "1DAY", if IsNan(sStateD1) then COLOR.DARK_GRAY else
if sStateD1 == 100 then GlobalColor("Bullish") else
if sStateD1 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));


# AGGREGATION 14 - TWO DAYS

def aD2 = aggregationPeriod.TWO_DAYS;
def hiDiffD2 = high(period = aD2) - high(period = aD2)[1];
def loDiffD2 = low(period = aD2)[1] - low(period = aD2);

def plusDMD2 = if hiDiffD2 > loDiffD2 and hiDiffD2 > 0 then hiDiffD2 else 0;
def minusDMD2 =  if loDiffD2 > hiDiffD2 and loDiffD2 > 0 then loDiffD2 else 0;

def ATRD2 = MovingAverage(averageType, TrueRange(high(period = aD2), close(period = aD2), low(period = aD2)), length);
def "DI+D2" = 100 * MovingAverage(averageType, plusDMD2, length) / ATRD2;
def "DI-D2" = 100 * MovingAverage(averageType, minusDMD2, length) / ATRD2;

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

def bullishD2 = ("DI+D2" > "DI-D2") and (ADXD2 > adxlength);
def bearishD2 = ("DI+D2" < "DI-D2") and (ADXD2 > adxlength);
def sidewaysD2 = (ADXD2 < adxlength);

def sStateD2 = if bullishD2 then 100 else if bearishD2 then -100 else 0;
def sPOSD2 = if IsNan(sStateD2) then 0 else bullishD2;
def sNEGD2 = if IsNan(sStateD2) then 0 else bearishD2;
def sSIDD2 = if IsNan(sStateD2) then 0 else if sPOSD2 or sNEGD2 then 0 else sidewaysD2;

AddLabel(showlabels and show2DAY, "2DAY", if IsNan(sStateD2) then COLOR.DARK_GRAY else
if sStateD2 == 100 then GlobalColor("Bullish") else
if sStateD2 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));


# AGGREGATION 15 - THREE DAYS

def aD3 = aggregationPeriod.THREE_DAYS;
def hiDiffD3 = high(period = aD3) - high(period = aD3)[1];
def loDiffD3 = low(period = aD3)[1] - low(period = aD3);

def plusDMD3 = if hiDiffD3 > loDiffD3 and hiDiffD3 > 0 then hiDiffD3 else 0;
def minusDMD3 =  if loDiffD3 > hiDiffD3 and loDiffD3 > 0 then loDiffD3 else 0;

def ATRD3 = MovingAverage(averageType, TrueRange(high(period = aD3), close(period = aD3), low(period = aD3)), length);
def "DI+D3" = 100 * MovingAverage(averageType, plusDMD3, length) / ATRD3;
def "DI-D3" = 100 * MovingAverage(averageType, minusDMD3, length) / ATRD3;

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

def bullishD3 = ("DI+D3" > "DI-D3") and (ADXD3 > adxlength);
def bearishD3 = ("DI+D3" < "DI-D3") and (ADXD3 > adxlength);
def sidewaysD3 = (ADXD3 < adxlength);

def sStateD3 = if bullishD3 then 100 else if bearishD3 then -100 else 0;
def sPOSD3 = if IsNan(sStateD3) then 0 else bullishD3;
def sNEGD3 = if IsNan(sStateD3) then 0 else bearishD3;
def sSIDD3 = if IsNan(sStateD3) then 0 else if sPOSD3 or sNEGD3 then 0 else sidewaysD3;

AddLabel(showlabels and show3DAY, "3DAY", if IsNan(sStateD3) then COLOR.DARK_GRAY else
if sStateD3 == 100 then GlobalColor("Bullish") else
if sStateD3 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));


# AGGREGATION 16 - FOUR DAYS

def aD4 = aggregationPeriod.FOUR_DAYS;
def hiDiffD4 = high(period = aD4) - high(period = aD4)[1];
def loDiffD4 = low(period = aD4)[1] - low(period = aD4);

def plusDMD4 = if hiDiffD4 > loDiffD4 and hiDiffD4 > 0 then hiDiffD4 else 0;
def minusDMD4 =  if loDiffD4 > hiDiffD4 and loDiffD4 > 0 then loDiffD4 else 0;

def ATRD4 = MovingAverage(averageType, TrueRange(high(period = aD4), close(period = aD4), low(period = aD4)), length);
def "DI+D4" = 100 * MovingAverage(averageType, plusDMD4, length) / ATRD4;
def "DI-D4" = 100 * MovingAverage(averageType, minusDMD4, length) / ATRD4;

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

def bullishD4 = ("DI+D4" > "DI-D4") and (ADXD4 > adxlength);
def bearishD4 = ("DI+D4" < "DI-D4") and (ADXD4 > adxlength);
def sidewaysD4 = (ADXD4 < adxlength);

def sStateD4 = if bullishD4 then 100 else if bearishD4 then -100 else 0;
def sPOSD4 = if IsNan(sStateD4) then 0 else bullishD4;
def sNEGD4 = if IsNan(sStateD4) then 0 else bearishD4;
def sSIDD4 = if IsNan(sStateD4) then 0 else if sPOSD4 or sNEGD4 then 0 else sidewaysD4;

AddLabel(showlabels and show4DAY, "4DAY", if IsNan(sStateD4) then COLOR.DARK_GRAY else
if sStateD4 == 100 then GlobalColor("Bullish") else
if sStateD4 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));


# AGGREGATION 17 - ONE WEEK

def aW1 = aggregationPeriod.WEEK;
def hiDiffW1 = high(period = aW1) - high(period = aW1)[1];
def loDiffW1 = low(period = aW1)[1] - low(period = aW1);

def plusDMW1 = if hiDiffW1 > loDiffW1 and hiDiffW1 > 0 then hiDiffW1 else 0;
def minusDMW1 =  if loDiffW1 > hiDiffW1 and loDiffW1 > 0 then loDiffW1 else 0;

def ATRW1 = MovingAverage(averageType, TrueRange(high(period = aW1), close(period = aW1), low(period = aW1)), length);
def "DI+W1" = 100 * MovingAverage(averageType, plusDMW1, length) / ATRW1;
def "DI-W1" = 100 * MovingAverage(averageType, minusDMW1, length) / ATRW1;

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

def bullishW1 = ("DI+W1" > "DI-W1") and (ADXW1 > adxlength);
def bearishW1 = ("DI+W1" < "DI-W1") and (ADXW1 > adxlength);
def sidewaysW1 = (ADXW1 < adxlength);

def sStateW1 = if bullishW1 then 100 else if bearishW1 then -100 else 0;
def sPOSW1 = if IsNan(sStateW1) then 0 else bullishW1;
def sNEGW1 = if IsNan(sStateW1) then 0 else bearishW1;
def sSIDW1 = if IsNan(sStateW1) then 0 else if sPOSW1 or sNEGW1 then 0 else sidewaysW1;

AddLabel(showlabels and showWEEK, "WEEK", if IsNan(sStateW1) then COLOR.DARK_GRAY else
if sStateW1 == 100 then GlobalColor("Bullish") else
if sStateW1 == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));


# AGGREGATION 18 - ONE MONTH

def aMM = aggregationPeriod.MONTH;
def hiDiffMM = high(period = aMM) - high(period = aMM)[1];
def loDiffMM = low(period = aMM)[1] - low(period = aMM);

def plusDMMM = if hiDiffMM > loDiffMM and hiDiffMM > 0 then hiDiffMM else 0;
def minusDMMM =  if loDiffMM > hiDiffMM and loDiffMM > 0 then loDiffMM else 0;

def ATRMM = MovingAverage(averageType, TrueRange(high(period = aMM), close(period = aMM), low(period = aMM)), length);
def "DI+MM" = 100 * MovingAverage(averageType, plusDMMM, length) / ATRMM;
def "DI-MM" = 100 * MovingAverage(averageType, minusDMMM, length) / ATRMM;

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

def bullishMM = ("DI+MM" > "DI-MM") and (ADXMM > adxlength);
def bearishMM = ("DI+MM" < "DI-MM") and (ADXMM > adxlength);
def sidewaysMM = (ADXMM < adxlength);

def sStateMM = if bullishMM then 100 else if bearishMM then -100 else 0;
def sPOSMM = if IsNan(sStateMM) then 0 else bullishMM;
def sNEGMM = if IsNan(sStateMM) then 0 else bearishMM;
def sSIDMM = if IsNan(sStateMM) then 0 else if sPOSMM or sNEGMM then 0 else sidewaysMM;

AddLabel(showlabels and showMONTH, "MONTH", if IsNan(sStateMM) then COLOR.DARK_GRAY else
if sStateMM == 100 then GlobalColor("Bullish") else
if sStateMM == -100 then GlobalColor("Bearish")
else GlobalColor("Neutral"));

#STATS
def sPOS = sPOSM1 + sPOSM2 + sPOSM3 + sPOSM4 + sPOSM5 + sPOSM10 + sPOSM15 +
sPOSM20 + sPOSM30 + sPOSH1 + sPOSH2 + sPOSH4 + sPOSD1 + sPOSD2 + sPOSD3 +
sPOSD4 + sPOSW1 + sPOSMM;

def sNEG = sNEGM1 + sNEGM2 + sNEGM3 + sNEGM4 + sNEGM5 + sNEGM10 + sNEGM15 +
sNEGM20 + sNEGM30 + sNEGH1 + sNEGH2 + sNEGH4 + sNEGD1 + sNEGD2 + sNEGD3 +
sNEGD4 + sNEGW1 + sNEGMM;

def sSID = sSIDM1 + sSIDM2 + sSIDM3 + sSIDM4 + sSIDM5 + sSIDM10 + sSIDM15 +
sSIDM20 + sSIDM30 + sSIDH1 + sSIDH2 + sSIDH4 + sSIDD1 + sSIDD2 + sSIDD3 +
sSIDD4 + sSIDW1 + sSIDMM;

def sTOL = sPOS + sNEG + sSID;

AddLabel (showstats, "Bullish: " + sPOS + " (" +
  AsPercent(Round(sPOS / sTOL, 2)) + ")", GlobalColor("Bullish"));

AddLabel (showstats, "Bearish: " + sNEG + " (" +
  AsPercent(Round(sNEG / sTOL, 2)) + ")", GlobalColor("Bearish"));

AddLabel (showstats, "Neutral: " + sSID + " (" +
  AsPercent(Round(sSID / sTOL, 2)) + ")", GlobalColor("Neutral"));


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

New Indicator: Buy the Dip

Check out our Buy the Dip indicator and see how it can help you find profitable swing trading ideas. Scanner, watchlist columns, and add-ons are included.

Download the indicator

Similar threads

Top