incorrect fill when plot is in stepline slyle - plot

//#version=5
indicator(title="Weeks_OC", shorttitle="Weeks_OC", overlay=true)
w_close = request.security(syminfo.tickerid, "W", close, barmerge.gaps_off, barmerge.lookahead_on)
w_open = request.security(syminfo.tickerid, "W", open,barmerge.gaps_off, barmerge.lookahead_on)
p1=plot(w_open,title='w_open', color = w_close >= w_open ? color.blue : color.red, style=plot.style_stepline, linewidth=3)
p2=plot(w_close,title='w_close',color = w_close >= w_open ? color.blue : color.red, style=plot.style_stepline, linewidth=3)
fill(plot1=p1, plot2=p2, color = w_close >= w_open ? color.blue : color.red)
PineScript v5
Simple indicator that shows open-close area of a week.
Fill function seems works incorrectly when given plot is in stepline slyle

I think you have Extended hours enabled. Please switch it to Regular Trading Hours.
I think this is what you're looking for:
//#version=5
indicator(title="Weeks_OC", shorttitle="Weeks_OC", overlay=true)
var int transp = input.int(50, 'Transparency', 0, 100)
var color c_blue = color.new(color.blue, transp)
var color c_red = color.new(color.red, transp)
[w_open,w_close] = request.security(syminfo.tickerid, 'W', [open,close], barmerge.gaps_off, barmerge.lookahead_on)
newWeek = ta.change(weekofyear)
mycolor = newWeek ? na : w_close >= w_open ? c_blue : c_red
p1 = plot(w_open, 'w_open', mycolor)
p2 = plot(w_close, 'w_close', mycolor)
fill(p1, p2, mycolor)
Which yields

Related

How to create TradingView indicators that look like this?

I've been trying to create similar indicator to this
This is the reference
I have the code for RSI, Stochastic RSI, MACD and 50/100/200 MA (with crosses), but I couldn't find any information on how to create the visuals like in the reference altough I was studying for quite a long time
Here is the code for these indicators:
50/100/200 MA with crosses (coded by myself)
indicator(title="3 EMA with Cross", shorttitle="3EMA Cross", overlay=true)
fast = 50
mid = 100
slow = 200
fastEMA = ta.ema(close, fast)
midEMA = ta.ema(close, mid)
slowEMA = ta.ema(close, slow)
bullishCross = ta.crossover(fastEMA, slowEMA)
bearishCross = ta.crossunder(fastEMA, slowEMA)
if (bullishCross)
lbl = label.new(bar_index, low, "Golden Cross")
label.set_color(lbl, color.green)
label.set_yloc(lbl, yloc.belowbar)
label.set_style(lbl, label.style_label_up)
if (bearishCross)
lbl = label.new(bar_index, low, "Death Cross")
label.set_color(lbl, color.red)
label.set_yloc(lbl, yloc.abovebar)
label.set_style(lbl, label.style_label_down)
plot(fastEMA, color=color.green, linewidth=2)
plot(midEMA, color=color.yellow, linewidth=2)
plot(slowEMA, color=color.red, linewidth=2)
RSI
indicator(title="Relative Strength Index", shorttitle="RSI", format=format.price, precision=2,
timeframe="", timeframe_gaps=true)
ma(source, length, type) =>
switch type
"SMA" => ta.sma(source, length)
"Bollinger Bands" => ta.sma(source, length)
"EMA" => ta.ema(source, length)
"SMMA (RMA)" => ta.rma(source, length)
"WMA" => ta.wma(source, length)
"VWMA" => ta.vwma(source, length)
rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")
maLengthInput = input.int(14, title="MA Length", group="MA Settings")
bbMultInput = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group="MA Settings")
up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ma(rsi, maLengthInput, maTypeInput)
isBB = maTypeInput == "Bollinger Bands"
plot(rsi, "RSI", color=#7E57C2)
plot(rsiMA, "RSI-based MA", color=color.yellow)
rsiUpperBand = hline(70, "RSI Upper Band", color=#787B86)
hline(50, "RSI Middle Band", color=color.new(#787B86, 50))
rsiLowerBand = hline(30, "RSI Lower Band", color=#787B86)
fill(rsiUpperBand, rsiLowerBand, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill")
bbUpperBand = plot(isBB ? rsiMA + ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Upper Bollinger Band", color=color.green)
bbLowerBand = plot(isBB ? rsiMA - ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Lower Bollinger Band", color=color.green)
fill(bbUpperBand, bbLowerBand, color= isBB ? color.new(color.green, 90) : na, title="Bollinger Bands Background Fill")
MACD
indicator(title="Moving Average Convergence Divergence", shorttitle="MACD", timeframe="",
timeframe_gaps=true)
// Getting inputs
fast_length = input(title="Fast Length", defval=12)
slow_length = input(title="Slow Length", defval=26)
src = input(title="Source", defval=close)
signal_length = input.int(title="Signal Smoothing", minval = 1, maxval = 50, defval = 9)
sma_source = input.string(title="Oscillator MA Type", defval="EMA", options=["SMA", "EMA"])
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])
// Plot colors
col_macd = input(#2962FF, "MACD Line  ", group="Color Settings", inline="MACD")
col_signal = input(#FF6D00, "Signal Line  ", group="Color Settings", inline="Signal")
col_grow_above = input(#26A69A, "Above   Grow", group="Histogram", inline="Above")
col_fall_above = input(#B2DFDB, "Fall", group="Histogram", inline="Above")
col_grow_below = input(#FFCDD2, "Below Grow", group="Histogram", inline="Below")
col_fall_below = input(#FF5252, "Fall", group="Histogram", inline="Below")
// Calculating
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist = macd - signal
plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ?
col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))
plot(macd, title="MACD", color=col_macd)
plot(signal, title="Signal", color=col_signal)
Stochastic RSI
smoothK = input.int(3, "K", minval=1)
smoothD = input.int(3, "D", minval=1)
lengthRSI = input.int(14, "RSI Length", minval=1)
lengthStoch = input.int(14, "Stochastic Length", minval=1)
src = input(close, title="RSI Source")
rsi1 = ta.rsi(src, lengthRSI)
k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)
d = ta.sma(k, smoothD)
plot(k, "K", color=#2962FF)
plot(d, "D", color=#FF6D00)
h0 = hline(80, "Upper Band", color=#787B86)
h1 = hline(20, "Lower Band", color=#787B86)
fill(h0, h1, color=color.rgb(33, 150, 243, 90), title="Background")```
//MACD
indicator(title="Moving Average Convergence Divergence", shorttitle="MACD", timeframe="", timeframe_gaps=true)
// Getting inputs
fast_length = input(title="Fast Length", defval=12)
slow_length = input(title="Slow Length", defval=26)
src = input(title="Source", defval=close)
signal_length = input.int(title="Signal Smoothing", minval = 1, maxval = 50, defval = 9)
sma_source = input.string(title="Oscillator MA Type", defval="EMA", options=["SMA", "EMA"])
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])
// Plot colors
col_macd = input(#2962FF, "MACD Line  ", group="Color Settings", inline="MACD")
col_signal = input(#FF6D00, "Signal Line  ", group="Color Settings", inline="Signal")
col_grow_above = input(#26A69A, "Above   Grow", group="Histogram", inline="Above")
col_fall_above = input(#B2DFDB, "Fall", group="Histogram", inline="Above")
col_grow_below = input(#FFCDD2, "Below Grow", group="Histogram", inline="Below")
col_fall_below = input(#FF5252, "Fall", group="Histogram", inline="Below")
// Calculating
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist = macd - signal
plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))
plot(macd, title="MACD", color=col_macd)
plot(signal, title="Signal", color=col_signal)
You can use labels with offset for that. Offset will help you with the placement.
Update the labels with each bar and delete the old bars.
A simple example:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © vitruvius
//#version=5
indicator("My script")
rsi = ta.rsi(close, 14)
ema50 = ta.ema(close, 50)
rsi_off = 50
ema50_off = 25
rsi_up_trend = rsi < 30
ema_up_trend = close > ema50
var label l_rsi = na
var label l_ema50 = na
if barstate.islast
l_rsi := label.new(bar_index - rsi_off, 1, "RSI", color=rsi_up_trend ? color.green : color.red, style=label.style_triangleup, textcolor=color.white)
l_ema50 := label.new(bar_index - ema50_off, 1, "EMA50", color=ema_up_trend ? color.green : color.red, style=label.style_triangleup, textcolor=color.white)
label.delete(l_rsi[1])
label.delete(l_ema50[1])

Bokeh vbar Hover tool

I have a multiple data bar graph and want to show the max value of each bar upon hover. I have already changed the code a couple of times, but never succeeded...
Here is the code :
'''
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Panel, Tabs
from bokeh.models.tools import HoverTool
modalites = ['0-510','511-1002', '1003-2167', '>2168']
valeur1 = list(tables.loc[0])
valeur2 = list(tables.loc[1])
valeur3 = list(tables_2.loc[0])
valeur4 = list(tables_2.loc[1])
source1 = ColumnDataSource({'x' : modalites, 'valeur 1' : valeur1, 'valeur 2' : valeur2})
source2 = ColumnDataSource({'x' : modalites, 'valeur 1' : valeur3, 'valeur 2' : valeur4})
p1 = figure(title ='Répartition des sinistres en fonction des surfaces', x_range = modalites, plot_width=600, plot_height=400)
p2 = figure(title ='Répartition des sinistres en fonction des surfaces', x_range = modalites, plot_width=600, plot_height=400)
from bokeh.transform import dodge
abscisses_1 = dodge(field_name = 'x',
value = -0.25,
range = p1.x_range)
abscisses_2 = dodge(field_name = 'x',
value = 0,
range = p1.x_range)
p1.vbar(x = abscisses_1, top = 'valeur 1', width = 0.2, source = source1, color = 'green', legend = 'pas de sinistre')
p1.vbar(x = abscisses_2, top = 'valeur 2', width = 0.2, source = source1, color = "red", legend = "sinistre")
p1.legend.location = "top_left"
p1.legend.orientation = "horizontal"
p1.xgrid.grid_line_color = None
p2.vbar(x = abscisses_1, top = 'valeur 1', width = 0.2, source = source2, color = 'green', legend = 'pas de sinistre')
p2.vbar(x = abscisses_2, top = 'valeur 2', width = 0.2, source = source2, color = "red", legend = "sinistre")
p2.legend.location = "top_right"
p2.legend.orientation = "horizontal"
p2.xgrid.grid_line_color = None
tab1=Panel(child=p1, title='en %')
tab2=Panel(child=p2, title='en valeur absolue')
tabs=Tabs(tabs=[tab1, tab2])
h2 = HoverTool(tooltips = [( "nb bâtiments concernés:", "#top")])
p2.add_tools(h2)
show(tabs)
'''
If it helps, here is list(tables_2.loc[0]):[2303, 2184, 1909, 1511]
and list(tables_2.loc[1]):[271, 418, 587, 1046]
The problem of your code is in the line where you define your HoverTool. You are using #top, but in your ColumnDataSource top is not defiened.
h2 = HoverTool(tooltips = [( "nb bâtiments concernés:", "#top")])
If your want to use top as the maximum of valeur1 and valeur2 you may have to write something like this.
top = [max(v1,v2) for v1, v2 in zip(valeur3, valeur4)]
source2 = ColumnDataSource({'x' : modalites, 'valeur1' : valeur3, 'valeur2' : valeur4, 'top': top})
In this case top is well defiened and your code should work without changes.
By the way. You have defiened a HoverTool only for p2. If you need one on p1 as well, you have to do similar things on source1.
Output

Plotted arrows aren't displayed below or above bar graph in Pine

I have the next code.
//#version=4
study(title="MACD", shorttitle="MACD")
// Getting inputs
fast_length = input(title="Fast Length", type=input.integer, defval=11)
slow_length = input(title="Slow Length", type=input.integer, defval=22)
src = input(title="Source", type=input.source, defval=close)
signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)
sma_source = input(title="Simple MA(Oscillator)", type=input.bool, defval=false)
sma_signal = input(title="Simple MA(Signal Line)", type=input.bool, defval=false)
// Plot colors
col_grow_above = #26A69A
col_grow_below = #FFCDD2
col_fall_above = #B2DFDB
col_fall_below = #EF5350
col_macd = #0094ff
col_signal = #ff6a00
// Calculating
fast_ma = sma_source ? sma(src, fast_length) : ema(src, fast_length)
slow_ma = sma_source ? sma(src, slow_length) : ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)
hist = macd - signal
plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below) ), transp=0 )
plot(macd, title="MACD", color=col_macd, transp=0)
plot(signal, title="Signal", color=col_signal, transp=0)
currMacd = hist[0]
prevMacd = hist[1]
buy = currMacd > 0 and prevMacd <= 0
sell = currMacd < 0 and prevMacd >= 0
timetobuy = buy==1
timetosell = sell==1
tup = barssince(timetobuy[1])
tdn = barssince(timetosell[1])
long = tup>tdn?1:na
short = tdn>tup?1:na
plotshape(long==1?buy:na, color=#26A69A, location=location.abovebar, style=shape.arrowup, title="Buy Arrow", transp=0)
plotshape(short==1?sell:na, color=#EF5350, location=location.belowbar, style=shape.arrowdown, title="Sell Arrow", transp=0)
As you can see, it is the macd histogram code with some plot code to graph arrows near the histogram. I configured their location as above and below. However, they are displayed far from the graph.
How can I plot the arrows above and below the macdh bars?
By using location=location.abovebar the function is putting the level of the symbol's price in play, which ruins your indicator's scale. The solution is to use location=location.top instead:
plotshape(long==1?buy:na, color=#26A69A, location=location.top, style=shape.arrowup, title="Buy Arrow", transp=0)
plotshape(short==1?sell:na, color=#EF5350, location=location.bottom, style=shape.arrowdown, title="Sell Arrow", transp=0)

<SOLVED> How to plot a sphere as wireframe with back view hidden, in R?

Using R, I would like to plot a sphere with latitude and longitude lines, but without any visibility of hidden part of the sphere. And, ideally, I'd like to have the initial view start out with a specific tilt (but that's down the road).
This matlab question gets to the idea
Plotting a wireframe sphere in Python hidding backward meridians and parallels
... but it's matlab. The closest solution that stackoverflow suggested
Plot Sphere with custom gridlines in R
doesn't help with the hidden line aspect.
The closest I got was editting a sphereplot routine:
library(sphereplot)
matt.rgl.sphgrid <- function (radius = 1, col.long = "red", col.lat = "blue", deggap = 15,
longtype = "H", add = FALSE, radaxis = TRUE, radlab = "Radius")
{
if (add == F) {
open3d(userMatrix = rotationMatrix((90)*pi/180, 1, 0, 0)) #changed
}
for (lat in seq(-90, 90, by = deggap)) {
if (lat == 0) {
col.grid = "grey50"
}
else {
col.grid = "grey"
}
#create an array here using the sph2car call below, then rotate those and
#set the appropriate ones to NA before passing that array to this call
#ditto for the next plot3d call as well
plot3d(sph2car(long = seq(0, 360, len = 100), lat = lat,
radius = radius, deg = T),
col = col.grid, add = T,
type = "l")
}
for (long in seq(0, 360 - deggap, by = deggap)) {
if (long == 0) {
col.grid = "grey50"
}
else {
col.grid = "grey"
}
plot3d(sph2car(long = long, lat = seq(-90, 90, len = 100),
radius = radius, deg = T),
col = col.grid, add = T,
type = "l")
}
if (longtype == "H") {
scale = 15
}
if (longtype == "D") {
scale = 1
}
# rgl.sphtext(long = 0, lat = seq(-90, 90, by = deggap), radius = radius,
# text = seq(-90, 90, by = deggap), deg = TRUE, col = col.lat)
# rgl.sphtext(long = seq(0, 360 - deggap, by = deggap), lat = 0,
# radius = radius, text = seq(0, 360 - deggap, by = deggap)/scale,
# deg = TRUE, col = col.long)
}
matt.rgl.sphgrid(radaxis=FALSE)
But I can't figure out how to hide the lines.
Any pointers or examples I've overlooked?
SOLUTION: Just prior to the plot3d calls, set any negative values in "y" (in this case, given a first rotation of 90 degrees) to NA

How to put a text legend on a ggplot graph of data?

I have some data:
donnees <- structure(list(mean.time = c(66.2181493259296, 38.4214009587611,28.3780846407761, 23.6036479741174, 21.1194982724492, 18.2304201307749, 17.255935716945, 15.1999929855212, 14.4373235262462), interval.time = c(2.02676652919753, 1.11352244913202, 0.852970451889973, 0.659463003712615, 0.637078718678222, 0.555560539640353, 0.523901049738113, 0.459577876757762, 0.42846867586966), mean.speed = c(93.3820017657387, 161.279607915939, 218.506927701215,257.296969741989, 295.210612887155, 344.350019217133, 364.011355824999, 409.234700329235, 437.398681209406), interval.speed = c(2.88657084170069, 5.02519369247631, 6.79320499564379, 7.66765106354858, 9.45539113245263, 10.9383151684182, 12.2017419883015, 13.715012760365, 16.6173823082674), mean.dist = c(5059.20542230044, 5025.32849954932, 4979.53881596498, 4994.56758567708, 4980.47651740827, 4991.06590346422, 4984.66564161804, 4932.67139724921, 4991.32367321949), interval.distance = c(44.8556012621449, 41.8511306706474, 42.6986600944198, 43.1898193770464, 42.7477524465553, 43.4197913676737, 42.9860392430236, 41.6610917291015, 42.4239685871405), lambda = 1:9), .Names = c("mean.time", "interval.time", "mean.speed", "interval.speed", "mean.dist", "interval.distance", "lambda"), row.names = c(NA, 9L), class = "data.frame")
I use the following function to get the confident interval for a given lambda:
int.ech = function(vector,conf.level=0.95,na.rm=T){
if (length(vector)==0) { cat("Erreur ! Le vecteur ",substitute(vector),"est vide.\n")}
else { s = var(vector,na.rm=na.rm)
n = length(vector)-sum(is.na(vector))
ddl = n - 1 ; proba = (1-conf.level)*100 ; proba = (100-proba/2)/100
t_student = qt(proba, ddl) # quantile
intervalle = t_student * sqrt(s/n)
moyenne = mean(vector,na.rm=na.rm) ; return(intervalle) }
}
Then, I plot the data with ggplot :
ggplot(donnees, aes(x = lambda, y = donnees$mean.speed))+
geom_point() + geom_path(color = "blue") +
geom_errorbar(aes(ymin = donnees$mean.speed - donnees$interval.speed, ymax = donnees$mean.speed + donnees$interval.speed)) +
labs(title = 'Distibution of the infection speed (m/min) once the malware have reached the edge in function of lambda (users/km)', x = "lambda : users density (users/km)", y = "Infection speed (m/min)") +
labs(linetype='custom title')
I have a text which I want to put on legend of this graph :
texte <- c(paste("window =",win,"km",sep= " "),paste("r_infection =", radius*1000,"m",sep=" "),paste("gamma =",gamma,"km/km^2",sep=" "))
The problem is when I want to execute the legend function :
legend("topleft", legend = texte)
I have this error :
Error in strwidth(legend, units = "user", cex = cex, font = text.font) : plot.new has not been called yet
For information, I get the same answer when I just execute :
ggplot(donnees, aes(x = lambda, y = meanspeed)) + legend("topleft", legend = texte)
Thank you for your help.

Resources