Accessing items in MACD.Diff

Peppermshrimp

New member
VIP
Is there a way to reference 4 different ‘trends’ in MACD.Diff, other than just to change the colors? I am referring to ‘Negative and Down’, ‘Negative and Up’, ‘Positive and Down’, and ‘Positive and Up’. (see images below) I would like to set up a Strategy that puts an up arrow on the chart when these switch between ‘Negative and Down’ to ‘Negative and Up’, and puts a down arrow on the chart when they switch from ‘Positive and Down’ to ‘Positive and Up’. The only thinkscript I can find with respect to these 4 trends is when the color is being defined.

macd.png
macd arrows.png
 
Last edited:
Solution
Is there a way to reference 4 different ‘trends’ in MACD.Diff, other than just to change the colors? I am referring to ‘Negative and Down’, ‘Negative and Up’, ‘Positive and Down’, and ‘Positive and Up’. (see images below) I would like to set up a Strategy that puts an up arrow on the chart when these switch between ‘Negative and Down’ to ‘Negative and Up’, and puts a down arrow on the chart when they switch from ‘Positive and Down’ to ‘Positive and Up’. The only thinkscript I can find with respect to these 4 trends is when the color is being defined.


this is a strategy, not a study. Must be saved in the STRATEGY tab to the right of the STUDY tab
for the upper chart

arrows from MACD histogram colors

changes in the histogram...
here is the code for the MACD Histogram
Code:
declare lower;
input APC =0;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;
input Arrows = no;
plot Diff = MACD(fastLength, slowLength, MACDLength, averageType).Diff;
plot UpSignal = if Diff crosses above 0 then 0 else Double.NaN;
plot DownSignal = if Diff crosses below 0 then 0 else Double.NaN;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
DefineGlobalColor("Positive and Up", Color.GREEN);
DefineGlobalColor("Positive and Down", Color.DARK_GREEN);
DefineGlobalColor("Negative and Down", Color.Dark_RED);
DefineGlobalColor("Negative and Up", Color.RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Globalcolor("Positive and Up") else Globalcolor("Positive and Down") else if Diff < Diff[1] then Globalcolor("Negative and Down") else Globalcolor("Negative and Up"));
AssignPriceColor(if APC ==1 && Diff >=0 && Diff > Diff[1] then Globalcolor(“Positive and Up”) else
if APC ==1 && Diff>= 0 && Diff  < Diff[1] then Globalcolor(“Positive and Down”) else
if APC ==1 && Diff < 0 && Diff < Diff[1] then Globalcolor(“Negative and Down”) else
If APC ==1 && Diff < 0 && Diff > Diff[1] then GlobalColor(“Negative and Up”) else Color.Current);
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
Def PaD = if Diff>= 0 && Diff  < Diff[1] then 1 else 0;
Def PaU = if Diff>= 0 && Diff  > Diff[1] then 1 else 0;
Def NaD =  if Diff< 0 && Diff  < Diff[1] then 1 else 0;
Def NaU = if Diff< 0 && Diff  > Diff[1] then 1 else 0;
Plot PaDArrow = if PaD ==1 && PaD[1] ==0 then Diff else Double.NaN;
Plot PaUArrow = if PaU ==1 && PaU[1] ==0 then Diff else Double.NaN;
Plot NaDArrow = if NaD ==1 && NaD[1] ==0 then Diff else Double.NaN;
Plot NaUArrow = if NaU ==1 && NaU[1] ==0 then Diff else Double.NaN;
PaDArrow.SetHiding(!Arrows);
PaDArrow.SetLineWeight(2);
PaUArrow.SetHiding(!Arrows);
PaUArrow.SetLineWeight(2);
NaDArrow.SetHiding(!Arrows);
NaDArrow.SetLineWeight(2);
NaUArrow.SetHiding(!Arrows);
NaUArrow.SetLineWeight(2);
PaDArrow.SetDefaultColor(GlobalColor(“Positive and Down”));
PaDArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
PaUArrow.SetDefaultColor(GlobalColor(“Positive and Up”));
PaUArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
NaDArrow.SetDefaultColor(GlobalColor(“Negative and Down”));
NaDArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
NaUArrow.SetDefaultColor(GlobalColor(“Negative and Up”));
NaUArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
 

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

Is there a way to reference 4 different ‘trends’ in MACD.Diff, other than just to change the colors? I am referring to ‘Negative and Down’, ‘Negative and Up’, ‘Positive and Down’, and ‘Positive and Up’. (see images below) I would like to set up a Strategy that puts an up arrow on the chart when these switch between ‘Negative and Down’ to ‘Negative and Up’, and puts a down arrow on the chart when they switch from ‘Positive and Down’ to ‘Positive and Up’. The only thinkscript I can find with respect to these 4 trends is when the color is being defined.


this is a strategy, not a study. Must be saved in the STRATEGY tab to the right of the STUDY tab
for the upper chart

arrows from MACD histogram colors

changes in the histogram colors don't always match price action

this uses the rules that determine MACD histogram colors, and builds on them to determine buy and sell signals.
..a buy is when red changes to dark_red (Diff_color_num = 1)
..a sell is when green changes to dark_green (Diff_color_num = -1)

to reduce excess signals, i added a rule, to have x bars in a row with same color after a color change. (default is consecutive_bars = 2)
..find all buy and sell signals (arrows visible with test3_signals = yes)
..create a trade signal, based on the first consecutive signal (visible with test2_trade = yes)
.. addorders() use trade signal


Code:
# macd_histo_colors_arrows_strat

#https://usethinkscript.com/threads/accessing-items-in-macd-diff.17075/
#Accessing items in MACD.Diff
#Peppermshrimp  11/2

#Is there a way to reference 4 different ‘trends’ in MACD.Diff, other than just to change the colors? I am referring to ‘Negative and Down’, ‘Negative and Up’, ‘Positive and Down’, and ‘Positive and Up’. (see images below) I would like to set up a Strategy that puts an up arrow on the chart when these switch between ‘Negative and Down’ to ‘Negative and Up’, and puts a down arrow on the chart when they switch from ‘Positive and Down’ to ‘Positive and Up’. The only thinkscript I can find with respect to these 4 trends is when the color is being defined.

def na = double.nan;
def bn = barnumber();

def ref1 = if bn == 1 then close else ref1[1];

# macd
# TD Ameritrade IP Company, Inc. (c) 2007-2023
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;

def Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
def Avg = MovingAverage(averageType, Value, MACDLength);
def Diff = Value - Avg;

# create # 's for diff colors ,  -2 to 2
# all color transistitions
#def Diff_color_num =
#      if Diff > 0 and Diff > Diff[1] then 2
# else if Diff < 0 and Diff < Diff[1] then -2
# else if Diff > 0 and Diff < Diff[1] then -1
# else if Diff < 0 and Diff > Diff[1] then 1
# else 0;


# color changes, lowest and highest
def Diff_color_num =
      if Diff > 0 and Diff < Diff[1] then -1
 else if Diff < 0 and Diff > Diff[1] then 1
 else 0;


# require x colors in a row to be considered a new color
input consecutive_bars = 2;
def qty = consecutive_bars;
def numsum = if Sum(Diff_color_num, qty) == (Diff_color_num * qty) then 1 else 0;

def new_color = if (numsum and getvalue(diff_color_num, qty) != diff_color_num) then 1 else 0;
#def new_color = if (numsum and getvalue(numsum, qty) and getvalue(diff_color_num, qty) != diff_color_num) then 1 else 0;


# create a trade signal , from the 1st of consequetive signals
def trade = if bn == 1 then 0
 else if Diff_color_num == -1 and new_color then -1
 else if Diff_color_num == 1 and new_color then 1
 else trade[1];


def buy = (trade[0] == 1 and trade[1] == -1);
def sell = (trade[0] == -1 and trade[1] == 1);
input order_size = 1;
AddOrder(OrderType.BUY_TO_OPEN, buy, open[-1], order_size, Color.GREEN, Color.GREEN, name = "BUY");
AddOrder(OrderType.SELL_TO_CLOSE, sell, open[-1], order_size, Color.RED, Color.RED, name = "SELL");

# AddOrder ( type, condition, price, tradeSize, tickColor, arrowColor, name);  


#------------------------------------------

input test1_bubbles = no;
AddChartBubble(test1_bubbles, low*0.996,
diff_color_num + "\n" +
new_color
, Color.YELLOW, no);


input test2_trade = no;
def y = 1;
plot z1 = if test2_trade then ref1 + (trade*y) else na;


input test3_signals = no;
plot up = if test3_signals and new_color and Diff_color_num > 0 then low*0.999 else na;
up.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#up.SetDefaultColor(Color.green);
up.AssignValueColor(if Diff_color_num == 1 then color.green else if Diff_color_num == 2 then color.dark_green else color.black);
up.setlineweight(3);
up.hidebubble();

plot dwn = if test3_signals and  new_color and Diff_color_num < 0 then high*1.001 else na;
dwn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#dwn.SetDefaultColor(Color.red);
dwn.AssignValueColor(if Diff_color_num == -1 then color.red else if Diff_color_num == -2 then color.dark_red else color.black);
dwn.setlineweight(3);
dwn.hidebubble();



#plot zValue = value;
#plot zAvg = avg;
#plot zDiff = diff;
#plot ZeroLine = 0;

#plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;
#plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;

#UpSignal.SetHiding(!showBreakoutSignals);
#DownSignal.SetHiding(!showBreakoutSignals);

#Value.SetDefaultColor(GetColor(1));
#Avg.SetDefaultColor(GetColor(8));
#Diff.SetDefaultColor(GetColor(5));
#Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
#Diff.SetLineWeight(3);
#Diff.DefineColor("Positive and Up", Color.GREEN);
#Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
#Diff.DefineColor("Negative and Down", Color.RED);
#Diff.DefineColor("Negative and Up", Color.DARK_RED);
#Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.Color("Positive and Up") else Diff.Color("Positive and Down") else if Diff < Diff[1] then Diff.Color("Negative and Down") else Diff.Color("Negative and Up"));
#ZeroLine.SetDefaultColor(GetColor(0));
#UpSignal.SetDefaultColor(Color.UPTICK);
#UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#DownSignal.SetDefaultColor(Color.DOWNTICK);
#DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#

WMT 1hour
with MACD and floatingPL study loaded
b6OVEXK.jpg
 
Solution
My bad for stating Study instead of Strategy...thank you for correcting me. And thank you so much for the code, that is exactly what I wanted. I like the change you made to my original idea. Thank you, thank you!!
 
this is a strategy, not a study. Must be saved in the STRATEGY tab to the right of the STUDY tab
for the upper chart

arrows from MACD histogram colors

changes in the histogram colors don't always match price action

this uses the rules that determine MACD histogram colors, and builds on them to determine buy and sell signals.
..a buy is when red changes to dark_red (Diff_color_num = 1)
..a sell is when green changes to dark_green (Diff_color_num = -1)

to reduce excess signals, i added a rule, to have x bars in a row with same color after a color change. (default is consecutive_bars = 2)
..find all buy and sell signals (arrows visible with test3_signals = yes)
..create a trade signal, based on the first consecutive signal (visible with test2_trade = yes)
.. addorders() use trade signal


Code:
# macd_histo_colors_arrows_strat

#https://usethinkscript.com/threads/accessing-items-in-macd-diff.17075/
#Accessing items in MACD.Diff
#Peppermshrimp  11/2

#Is there a way to reference 4 different ‘trends’ in MACD.Diff, other than just to change the colors? I am referring to ‘Negative and Down’, ‘Negative and Up’, ‘Positive and Down’, and ‘Positive and Up’. (see images below) I would like to set up a Strategy that puts an up arrow on the chart when these switch between ‘Negative and Down’ to ‘Negative and Up’, and puts a down arrow on the chart when they switch from ‘Positive and Down’ to ‘Positive and Up’. The only thinkscript I can find with respect to these 4 trends is when the color is being defined.

def na = double.nan;
def bn = barnumber();

def ref1 = if bn == 1 then close else ref1[1];

# macd
# TD Ameritrade IP Company, Inc. (c) 2007-2023
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;

def Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
def Avg = MovingAverage(averageType, Value, MACDLength);
def Diff = Value - Avg;

# create # 's for diff colors ,  -2 to 2
# all color transistitions
#def Diff_color_num =
#      if Diff > 0 and Diff > Diff[1] then 2
# else if Diff < 0 and Diff < Diff[1] then -2
# else if Diff > 0 and Diff < Diff[1] then -1
# else if Diff < 0 and Diff > Diff[1] then 1
# else 0;


# color changes, lowest and highest
def Diff_color_num =
      if Diff > 0 and Diff < Diff[1] then -1
 else if Diff < 0 and Diff > Diff[1] then 1
 else 0;


# require x colors in a row to be considered a new color
input consecutive_bars = 2;
def qty = consecutive_bars;
def numsum = if Sum(Diff_color_num, qty) == (Diff_color_num * qty) then 1 else 0;

def new_color = if (numsum and getvalue(diff_color_num, qty) != diff_color_num) then 1 else 0;
#def new_color = if (numsum and getvalue(numsum, qty) and getvalue(diff_color_num, qty) != diff_color_num) then 1 else 0;


# create a trade signal , from the 1st of consequetive signals
def trade = if bn == 1 then 0
 else if Diff_color_num == -1 and new_color then -1
 else if Diff_color_num == 1 and new_color then 1
 else trade[1];


def buy = (trade[0] == 1 and trade[1] == -1);
def sell = (trade[0] == -1 and trade[1] == 1);
input order_size = 1;
AddOrder(OrderType.BUY_TO_OPEN, buy, open[-1], order_size, Color.GREEN, Color.GREEN, name = "BUY");
AddOrder(OrderType.SELL_TO_CLOSE, sell, open[-1], order_size, Color.RED, Color.RED, name = "SELL");

# AddOrder ( type, condition, price, tradeSize, tickColor, arrowColor, name); 


#------------------------------------------

input test1_bubbles = no;
AddChartBubble(test1_bubbles, low*0.996,
diff_color_num + "\n" +
new_color
, Color.YELLOW, no);


input test2_trade = no;
def y = 1;
plot z1 = if test2_trade then ref1 + (trade*y) else na;


input test3_signals = no;
plot up = if test3_signals and new_color and Diff_color_num > 0 then low*0.999 else na;
up.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#up.SetDefaultColor(Color.green);
up.AssignValueColor(if Diff_color_num == 1 then color.green else if Diff_color_num == 2 then color.dark_green else color.black);
up.setlineweight(3);
up.hidebubble();

plot dwn = if test3_signals and  new_color and Diff_color_num < 0 then high*1.001 else na;
dwn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#dwn.SetDefaultColor(Color.red);
dwn.AssignValueColor(if Diff_color_num == -1 then color.red else if Diff_color_num == -2 then color.dark_red else color.black);
dwn.setlineweight(3);
dwn.hidebubble();



#plot zValue = value;
#plot zAvg = avg;
#plot zDiff = diff;
#plot ZeroLine = 0;

#plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;
#plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;

#UpSignal.SetHiding(!showBreakoutSignals);
#DownSignal.SetHiding(!showBreakoutSignals);

#Value.SetDefaultColor(GetColor(1));
#Avg.SetDefaultColor(GetColor(8));
#Diff.SetDefaultColor(GetColor(5));
#Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
#Diff.SetLineWeight(3);
#Diff.DefineColor("Positive and Up", Color.GREEN);
#Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
#Diff.DefineColor("Negative and Down", Color.RED);
#Diff.DefineColor("Negative and Up", Color.DARK_RED);
#Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.Color("Positive and Up") else Diff.Color("Positive and Down") else if Diff < Diff[1] then Diff.Color("Negative and Down") else Diff.Color("Negative and Up"));
#ZeroLine.SetDefaultColor(GetColor(0));
#UpSignal.SetDefaultColor(Color.UPTICK);
#UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#DownSignal.SetDefaultColor(Color.DOWNTICK);
#DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#

WMT 1hour
with MACD and floatingPL study loaded
b6OVEXK.jpg
great job. thank you! question.. it is possible to add to the strategy to trade until 15:55 kill any trade before the market close? thank you in advance
 
great job. thank you! question.. it is possible to add to the strategy to trade until 15:55 kill any trade before the market close? thank you in advance
here you go

set this to yes
input close_trades_at_day_end = yes;

i used this code
https://usethinkscript.com/threads/...bar-of-the-day-in-thinkorswim.526/#post-13462

Code:
# macd_histo_colors_arrows_strat_02

# add- sell at day end


#https://usethinkscript.com/threads/accessing-items-in-macd-diff.17075/
#Accessing items in MACD.Diff
#Peppermshrimp  11/2

#Is there a way to reference 4 different ‘trends’ in MACD.Diff, other than just to change the colors? I am referring to ‘Negative and Down’, ‘Negative and Up’, ‘Positive and Down’, and ‘Positive and Up’. (see images below) I would like to set up a Strategy that puts an up arrow on the chart when these switch between ‘Negative and Down’ to ‘Negative and Up’, and puts a down arrow on the chart when they switch from ‘Positive and Down’ to ‘Positive and Up’. The only thinkscript I can find with respect to these 4 trends is when the color is being defined.


def na = Double.NaN;
def bn = BarNumber();

def ref1 = if bn == 1 then close else ref1[1];

# macd
# TD Ameritrade IP Company, Inc. (c) 2007-2023
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;

def Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
def Avg = MovingAverage(averageType, Value, MACDLength);
def Diff = Value - Avg;

# create # 's for diff colors ,  -2 to 2
# all color transistitions
#def Diff_color_num = 
#      if Diff > 0 and Diff > Diff[1] then 2
# else if Diff < 0 and Diff < Diff[1] then -2
# else if Diff > 0 and Diff < Diff[1] then -1
# else if Diff < 0 and Diff > Diff[1] then 1
# else 0;


# color changes, lowest and highest
def Diff_color_num = 
      if Diff > 0 and Diff < Diff[1] then -1
 else if Diff < 0 and Diff > Diff[1] then 1
 else 0;

#-------------------------------

# find last bar of day
#https://usethinkscript.com/threads/finding-the-first-and-last-bar-of-the-day-in-thinkorswim.526/#post-13462
# korygill  Jan 9, 2020  #10
# GetDayValues
# Author: Kory Gill, @korygill

input Offset = 0;
# #  hint Offset: use this to squeeze the Nth bar inwards.

# logic
def nan = Double.NaN;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBarOfDay = if
    (beforeStart[1 + Offset] == 1 and beforeStart[Offset] == 0) or
    (isRollover[Offset] and beforeStart[Offset] == 0)
    then 1
    else 0;
def lastBarOfDay = if
    (afterEnd[-1 - Offset] == 1 and afterEnd[Offset] == 0) or
    (isRollover[-1 - Offset] and firstBarOfDay[-1 - Offset])
    then 1
    else 0;

input test_last = no;
plot t = if test_last and lastBarOfDay then low*0.990 else nan;
t.SetPaintingStrategy(PaintingStrategy.POINTS);
t.SetDefaultColor(Color.cyan);
t.setlineweight(4);


#-------------------------------

# require x colors in a row to be considered a new color
input consecutive_bars = 2;
def qty = consecutive_bars;
def numsum = if Sum(Diff_color_num, qty) == (Diff_color_num * qty) then 1 else 0;

def new_color = if (numsum and GetValue(Diff_color_num, qty) != Diff_color_num) then 1 else 0;
#def new_color = if (numsum and getvalue(numsum, qty) and getvalue(diff_color_num, qty) != diff_color_num) then 1 else 0;

input close_trades_at_day_end = yes;
def end = close_trades_at_day_end and lastBarOfDay;


# create a trade signal , from the 1st of consequetive signals
def trade = if bn == 1 then 0
 else if trade[1] == 1 and end[-1] then -1
 else if Diff_color_num == -1 and new_color then -1
 else if Diff_color_num == 1 and new_color then 1
 else trade[1];


def buy = (trade[0] == 1 and trade[1] == -1);
def sell = (trade[0] == -1 and trade[1] == 1);
input order_size = 1;
AddOrder(OrderType.BUY_TO_OPEN, buy, open[-1], order_size, Color.GREEN, Color.GREEN);
AddOrder(OrderType.SELL_TO_CLOSE, sell, open[-1], order_size, Color.RED, Color.RED);

# AddOrder ( type, condition, price, tradeSize, tickColor, arrowColor, name);    


#------------------------------------------

input test1_bubbles = no;
AddChartBubble(test1_bubbles, low * 0.996,
Diff_color_num + "\n" +
new_color
, Color.YELLOW, no);


input test2_trade = no;
def y = 1;
plot z1 = if test2_trade then ref1 + (trade * y) else na;


input test3_signals = no;
plot up = if test3_signals and new_color and Diff_color_num > 0 then low * 0.999 else na;
up.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#up.SetDefaultColor(Color.green);
up.AssignValueColor(if Diff_color_num == 1 then Color.GREEN else if Diff_color_num == 2 then Color.DARK_GREEN else Color.BLACK);
up.SetLineWeight(3);
up.HideBubble();

plot dwn = if test3_signals and  new_color and Diff_color_num < 0 then high * 1.001 else na;
dwn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#dwn.SetDefaultColor(Color.red);
dwn.AssignValueColor(if Diff_color_num == -1 then Color.RED else if Diff_color_num == -2 then Color.DARK_RED else Color.BLACK);
dwn.SetLineWeight(3);
dwn.HideBubble();



#plot zValue = value;
#plot zAvg = avg;
#plot zDiff = diff;
#plot ZeroLine = 0;

#plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;
#plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;

#UpSignal.SetHiding(!showBreakoutSignals);
#DownSignal.SetHiding(!showBreakoutSignals);

#Value.SetDefaultColor(GetColor(1));
#Avg.SetDefaultColor(GetColor(8));
#Diff.SetDefaultColor(GetColor(5));
#Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
#Diff.SetLineWeight(3);
#Diff.DefineColor("Positive and Up", Color.GREEN);
#Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
#Diff.DefineColor("Negative and Down", Color.RED);
#Diff.DefineColor("Negative and Up", Color.DARK_RED);
#Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.Color("Positive and Up") else Diff.Color("Positive and Down") else if Diff < Diff[1] then Diff.Color("Negative and Down") else Diff.Color("Negative and Up"));
#ZeroLine.SetDefaultColor(GetColor(0));
#UpSignal.SetDefaultColor(Color.UPTICK);
#UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
#DownSignal.SetDefaultColor(Color.DOWNTICK);
#DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#
 
I've tried to understand this code so I can make a few changes, but ThinkScript seems counter intuitive to me.

I have a few questions that I haven't been able to find in ThinkScript documentation.

1 - Obviously there are things already setup when a strategy is executed that you can access like, close, open, etc. Is there documentation somewhere that lists what those things are?​
2 - Is a strategy called every time there is a new candlestick? And are all variables global, so they are not reset when the strategy is called? The reason I ask is because of the line of code on Line 12:​
Code:
def ref1 = if bn == 1 then close else ref1[1];

I don't understand where ref1[1] comes from if ref1 is being defined on this line. Is ThinkScript using time travel or quantum physics? ;-)​
3 - when you reference older data, for example: Diff < Diff[1], which one is the current and which one is previous? Are the array elements going backwards, so close[-1] is the next candlestick to be drawn?​
4 - what's the difference between '=' and '=='? It looks like '=' assigns a value and '==' checks a against a value. Is that correct?​
5 - Is ThinkScript cap-sensitive?​
 
I've tried to understand this code so I can make a few changes, but ThinkScript seems counter intuitive to me.

I have a few questions that I haven't been able to find in ThinkScript documentation.

1 - Obviously there are things already setup when a strategy is executed that you can access like, close, open, etc. Is there documentation somewhere that lists what those things are?​
2 - Is a strategy called every time there is a new candlestick? And are all variables global, so they are not reset when the strategy is called? The reason I ask is because of the line of code on Line 12:​
0]
1. Complete list of fundamentals:
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Fundamentals

2. Explanation of time travel: :geek:
https://usethinkscript.com/threads/recursion-store-a-value-signal-in-thinkorswim.16490/
https://usethinkscript.com/threads/understanding-recursive-definitions-in-thinkorswim.11287/

3. to reference a previous bar: use [1] read more:
https://tlc.thinkorswim.com/center/reference/thinkScript/tutorials/Advanced/Chapter-10---Referencing-Historical-Data#:~:text=The example will plot the difference between the current Close price and the Close price 5 bars ago.

4. == is only used in calculations.
plot scan = close == oversold;
= is a reserved thinkscript word for variable creation:
plot x = 10 ;
def x = 10;
input x = 10 ;
 
Last edited:
@MerryDay Those links are exactly what I have been looking for, thank you!! So the 'array' is backwards, positive is the past, negative is the future. I wonder who came up with the brilliant idea?! :cautious: (I'll get used to it)

I have quite a ways to go before i can write my own code, so can I ask for help with a few things I'd like to add to the code @halcyonguy posted above?
I'd like to add the following:
1 - add another 'buy', even if a 'sell' has not taken place.
2 - add 'and Value<0' to the buy scenario...which will reduce the number of time a buy is triggered.
3 - add a bell/ding when a buy/sell is triggered.

Thank you. This is a great group, I really appreciate everyone's willingness to teach other's ThinkScript.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
337 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top