# 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.
#Set up time of day to trade
def Today = if GetDay() == GetLastDay() then 1 else 0;
input NumberDays = 10;
def ShowDay = if GetDay() >= GetLastDay()-NumberDays then 1 else 0;
input StartTradingTime = 1000;
input EndTradingTime = 1600;
def TradeTime = SecondsFromTime(StartTradingTime) >= 0 and SecondsTillTime(EndTradingTime) >=0;
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;
# 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;
# 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 = 100;
AddLabel(yes, if TradeTime and ShowDay then "True" else "False"); #Verifying Time of day
AddOrder(OrderType.BUY_TO_OPEN, TradeTime and ShowDay and buy, open[-1], order_size, Color.GREEN, Color.GREEN, name = "B");
AddOrder(OrderType.SELL_TO_CLOSE, TradeTime and ShowDay and sell, open[-1], order_size, Color.RED, Color.RED, name = "S");
# alerts , sounds
#alert(condition, text, alert type, sound);
alert(buy, "crossed up" ,alert.BAR, sound.DING); # Sound.Ding, higher , up sound
alert(sell, "crossed down" ,alert.BAR, sound.bell); # Sound.Bell, lower , down sound