Two Security Comparison with Displacement

jpmcewen

New member
VIP
Hey Everyone,

I'm trying to put together (steal from others) my first study. I wanted to display (2) different securities and be able to displace the second security. This led me to the Comparison() and Displacer() functions but I couldn't get anything going.

Finally after a bunch of digging, I have a working indicator for candles only. The other chart types will display without the colors designated in the code. For example, a line chart will only show the 'downtick' color.

At least it is working for candlesticks so I hope someone can find it useful if not improve/expand up this.

Code:
############################################################
# TwoSecurityComparisonWithDisplacement
############################################################
#-----------------------------------------------------------
# Inspiration:
#-----------------------------------------------------------
#   -ADD AN INDEX OR FUTURE LOWER CHART (from StanL Archives, White Candles Used)
#   [email protected] - https://usethinkscript.com/threads/how-to-plot-asset-price-on-second-left-vertical-axis.3780/post-35013
#   [email protected] - https://usethinkscript.com/threads/add-chart-as-a-lower-study.286/
#
#   Relevant Notes:
#     -The AddChart function is unsupported in TOS, Hence there is no documentation to support its use and color formatting.
#     -Also, not all chart types are supported.
#
#-----------------------------------------------------------
# NOTES:
#-----------------------------------------------------------
#   Set Expansion Area (probably same as displacement) or swap symbols based on +/- displacement
#     On your Charts tab click Style --> Settings --> Time Axis --> Expansion area and enter
#     the number of bars to the right you'd like. Then click Apply.
#   Check left-axis checkbox
#===========================================================

declare upper;
#declare lower;

def NA = Double.NaN;

input chartType  = ChartType.CANDLE; #Hint: Change chart type at our own risk!
input symbol = "";
input displace = 0;
DefineGlobalColor("uptick", Color.BLUE);
DefineGlobalColor("downtick", Color.YELLOW);
DefineGlobalColor("neutral", Color.PLUM);
def h = high(symbol)[displace];
def l = low(symbol)[displace];
def o = open(symbol)[displace];
def c = close(symbol)[displace];
def bb = Max(o,c)==c;

AddChart(h,l,if bb and o == c then o else NA,c,type = chartType,GlobalColor("neutral"));
AddChart(h,l,if bb and o != c then o else NA,c,type = chartType,GlobalColor("uptick"));
AddChart(h,l,if !bb and o != c then o else NA,c,type = chartType,GlobalColor("downtick"));

uuSJnUk.png
 

Similar threads

Top