Panzer12
New member
Hey guys. I'm trying to learn ThinkScript, so I'm creating different systems to learn various tricks. I've been experimenting with making a strategy using indicators. In this case, it's with MACD.
The following code is a collection of different codes that I've taken from various forums.
My main goal is to code a strategy where a buy or sell signal is generated when the MACD changes color and confirms after two consecutive bars of the same color. Additionally, I want to include an input for a stop-loss percentage in the same system.
This is what I have so far. Now, I don't quite understand if it's correct or how to modify it so that the confirmation signal is generated when there are consecutive bars of the same color.
Hope I explained myself clearly
Thanks fot your help!!!
It gives me the buy signal on the third (input) consecutive lower bar. But it is still the same color.
Edit:
Don´t understand the objective of this code.
The following code is a collection of different codes that I've taken from various forums.
My main goal is to code a strategy where a buy or sell signal is generated when the MACD changes color and confirms after two consecutive bars of the same color. Additionally, I want to include an input for a stop-loss percentage in the same system.
This is what I have so far. Now, I don't quite understand if it's correct or how to modify it so that the confirmation signal is generated when there are consecutive bars of the same color.
Hope I explained myself clearly
Thanks fot your help!!!
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];
#//================================================================//#
input offsetType = {default percent, value, tick};
input stop = 0.75;
def entryPrice = entryPrice();
def mult;
switch (offsetType) {
case percent:
mult = entryPrice / 100;
case value:
mult = 1;
case tick:
mult = tickSize();
}
def stopPrice = entryPrice - stop * mult;
#//================================================================//#
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(OrderType.SELL_TO_CLOSE, low <= stopPrice, tickColor = GetColor(5), arrowColor = GetColor(5), name = "Stop");
It gives me the buy signal on the third (input) consecutive lower bar. But it is still the same color.
Edit:
Don´t understand the objective of this code.
Code:
# 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;