Long when strategy say and sell when opposite signal appears.
Thinkscript indicator code
- Code: Select all
#
# Hull Moving Average Concavity and Turning Points
# or
# The Second Derivative of the Hull Moving Average
#
# Author: Seth Urion (Mashume)
# Version: 2020-05-01 V4
#
# https://usethinkscript.com/threads/hull-moving-average-turning-points-and-concavity-2nd-derivatives.1803/
#
declare upper;
input price = close;
input HMA_Length = 21;
input lookback = 2;
input show_labels = Yes;
input stddev_len = 21;
plot HMA = HullMovingAvg(price = price, length = HMA_Length);
def delta = HMA[1] - HMA[lookback + 1];
def delta_per_bar = delta / lookback;
def next_bar = HMA[1] + delta_per_bar;
def concavity = if HMA > next_bar then 1 else -1;
plot turning_point = if concavity[1] != concavity then HMA else double.nan;
HMA.AssignValueColor(color = if concavity[1] == -1 then
if HMA > HMA[1] then color.dark_orange else color.red else
if HMA < HMA[1] then color.dark_green else color.green);
HMA.SetLineWeight(3);
turning_point.SetLineWeight(4);
turning_point.SetPaintingStrategy(paintingStrategy = PaintingStrategy.POINTS);
turning_point.SetDefaultColor(color.white);
plot MA_Max = if HMA[-1] < HMA and HMA > HMA[1] then HMA else Double.NaN;
MA_Max.SetDefaultColor(Color.WHITE);
MA_Max.SetPaintingStrategy(PaintingStrategy.SQUARES);
MA_Max.SetLineWeight(3);
plot MA_Min = if HMA[-1] > HMA and HMA < HMA[1] then HMA else Double.Nan;
MA_Min.SetDefaultColor(Color.WHITE);
MA_Min.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
MA_Min.SetLineWeight(3);
plot sell = if turning_point and concavity == -1 then high else double.nan;
sell.SetDefaultColor(Color.DARK_ORANGE);
sell.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
sell.SetLineWeight(5);
plot buy = if turning_point and concavity == 1 then low else double.nan;
buy.SetDefaultColor(Color.CYAN);
buy.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
buy.SetLineWeight(5);
def divergence = HMA - next_bar;
addLabel(show_labels, concat("DIVERGENCE: " , divergence * 10000), color = if concavity < 0 then if divergence[1] > divergence then Color.dark_RED else color.PINK else if divergence[1] < divergence then color.dark_green else color.dark_orange);
def divergence_stddev = StandardDeviation(price = divergence, length = stddev_len);
addLabel(show_labels, concat("STDDEV: " , divergence_stddev * 10000), color = if absValue(divergence) > absValue(divergence_stddev) then color.blue else color.dark_gray);
# CCD_D -> ConCave Down and Decreasing
# CCD_I -> ConCave Down and Increasing
# CCU_D -> ConCave Up and Decreasing
# CCU_I -> ConCave Up and Increasing
plot CCD_D = if concavity == -1 and HMA < HMA[1] then HMA else double.nan;
CCD_D.SetDefaultColor(Color.RED);
CCD_D.SetLineWeight(3);
plot CCD_I = if concavity == -1 and HMA >= HMA[1] then HMA else double.nan;
CCD_I.SetDefaultColor(Color.DARK_ORANGE);
CCD_I.SetLineWeight(3);
plot CCU_D = if concavity == 1 and HMA <= HMA[1] then HMA else double.nan;
CCU_D.SetDefaultColor(COLOR.DARK_GREEN);
CCU_D.SetLineWeight(3);
plot CCU_I = if concavity == 1 and HMA > HMA[1] then HMA else double.nan;
CCU_I.SetDefaultColor(COLOR.GREEN);
CCU_I.SetLineWeight(3);
######################################################
# New Code To Add Alerts For When Bar Colors Go From
# Red -> Dark Green -> Green (LONG) And When They Go
# Green -> Orange -> Red (SHORT).
######################################################
# GO back through the bars, find when the last Dark Green section
# started then see if the color before that was Red
Def RedToDarkGreen = fold idx5 = 1 to 100 with p5 = 0 do if !isNaN(GetValue(CCU_D,idx5)) && !isNaN(GetValue(CCD_D,idx5+1)) && p5 == 0 then idx5 else if !isNaN(GetValue(CCU_D,idx5)) && isNaN(GetValue(CCU_D,idx5+1)) && p5 == 0 then Double.NaN else p5;
# GO back through the bars, find when the last Orage section
# started then see if the color before that was Green
Def GreenToOrange = fold idx6= 1 to 100 with p6 = 0 do if !isNaN(GetValue(CCD_I,idx6)) && !isNaN(GetValue(CCU_I,idx6+1)) && p6 == 0 then idx6 else if !isNaN(GetValue(CCD_I,idx6)) && isNaN(GetValue(CCD_I,idx6+1)) && p6 == 0 then Double.NaN else p6;
# Long signal when last bar line color is Green, line color of the bar
# before that was Dark Green and the color before that was Red
Def RedToDarkGreenToGreen = CCU_I[1]&& CCU_D[2] && !isNaN(RedToDarkGreen);
alert(RedToDarkGreenToGreen, "RED TO DARK GREEN TO GREEN - GO LONG", alert.bar, sound.chimes);
# Short signal when last bar line color is Red, line color of the bar
# before that was Red and the color before that was Orange
Def GreenToOrangeToRed = CCD_D[1] && CCD_I[2] && !isNaN(GreenToOrange);
alert(GreenToOrangeToRed, "GREEN TO ORANGE TO RED - GO SHORT", alert.bar, sound.chimes);
######################################################
# END Code To Add Alerts For When Bar Colors Go From
# Red -> Dark Green -> Green (LONG) And When They Go
# Green -> Orange -> Red (SHORT).
######################################################
Thinkscript strategy code
- Code: Select all
input price = close;
input HMA_Length = 21;
input lookback = 2;
def HMA = HullMovingAvg(price = price, length = HMA_Length);
def delta = HMA[1] - HMA[lookback + 1];
def delta_per_bar = delta / lookback;
def next_bar = HMA[1] + delta_per_bar;
def concavity = if HMA > next_bar then 1 else -1;
def turning_point = if concavity[-1] != concavity then HMA else Double.NaN;
plot buy = if turning_point and concavity == -1 then low else double.nan;
buy.SetDefaultColor(Color.YELLOW);
buy.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
buy.SetLineWeight(5);
plot sell = if turning_point and concavity == 1 then high else double.nan;
sell.SetDefaultColor(Color.DARK_ORANGE);
sell.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
sell.SetLineWeight(5);