How to make a calculator and repeat function on Trading View? - calculator
Excuse me! I'm a beginner programmer from Japan.
I'm not a native English user, so I'll show you my broken English!
[Pine-script on Trading View]
Now, I want to create a calculator works on the Trading View's charts.
By clicking 3 prices(high or low price on bars) on the chart,
and calculate it by using 3 prices.
Plot it's result on the chart.
After that, reset and repeat function for accepting new inputs by clicking.
[I want to make functions, like below ↓]
my image and process
click bar on the chart
create new labels on the point I clicked
calculate and plot results on the chart
reset and repeat function(keep plotted past results)
It seems simple, but I confess several problems.
[problems]
1.How to get Price & Time at the same time.
→ I want only one click can get Price and Time. (In other words, get X-axis and Y-axis value at once.)
But, is there any way?
2. How to repeat this function?
→ I have no good idea how to repeat function.
There is a way but not smart idea (delete the indicator and add it again).
I know it is not ordinary usage for a indicator, but calculator needs reset button, push it reset and function again with leaving plotted past results on the chart.
//#version=5
indicator("ABC_caluculator", overlay=true)
Price_A = input.price(defval=0, title = "A", group="ABC_caluculator", confirm=true)
Price_B = input.price(defval=0, title = "B", group="ABC_caluculator", confirm=true)
Price_C = input.price(defval=0, title = "C", group="ABC_caluculator", confirm=true)
Price_N = Price_C + (Price_B - Price_A)
Price_V = Price_B + (Price_B - Price_C)
Price_E = Price_B + (Price_B - Price_A)
Price_NT= Price_C + (Price_C - Price_A)
//Table
//Format
tblPos = input.string(title="Position on Chart", defval="Bottom Right", options=["Top Left", "Top Right", "Bottom Left", "Bottom Right", "Middle Left", "Middle Right", "Middle Bottom" ], group = "Table Customization")
tblposition = tblPos == "Top Left" ? position.top_left : tblPos == "Top Right" ? position.top_right : tblPos == "Bottom Left" ? position.bottom_left : tblPos == "Bottom Right" ? position.bottom_right : tblPos == "Middle Left" ? position.middle_left : tblPos == "Middle Right" ? position.middle_right : position.bottom_center
text_halign = input.string(defval = "Center", title="Horizontal Alignment", options=["Left", "Center", "Right"], group = "Table Customization")
text_halign_pos = text_halign == "Left" ? text.align_left : text_halign == "Center" ? text.align_center : text.align_right
text_valign = input.string(defval = "Center", title="Vertical Alignment", options=["Top", "Center", "Bottom"], group = "Table Customization")
text_valign_pos = text_valign == "Top" ? text.align_top : text_valign == "Center" ? text.align_center : text.align_bottom
text_size = input.string(defval = "Auto", title="Text Size", options=["Auto", "Tiny", "Small", "Normal", "Large", "Huge"], group = "Table Customization")
text_size_op = text_size == "Auto" ? size.auto : text_size == "Tiny" ? size.tiny : text_size == "Small" ? size.small : text_size == "Normal" ? size.normal : text_size == "Large" ? size.large : size.huge
//Dimensions
border_width = input.int(defval=1, title="Border Width", group = "Dimensions")
frame_width = input.int(defval=5, title="Frame Width", group = "Dimensions")
table_width = input.int(defval=5, title="Cell Width", group = "Dimensions")
table_height = input.int(defval=5, title="Cell Height", group = "Dimensions")
//Colors
tblBorderColor = input.color(title="Border Color", defval=#636363, group = "Table Styles")
celllBgColor = input.color(title="Background Color", defval=#d1d1d1, group = "Table Styles")
cellTextColor = input.color(title="Text Color", defval=#000000, group = "Table Styles")
resultsTable = table.new(position = tblposition, columns = 6, rows = 6, bgcolor = #ffffff, border_width = border_width,frame_color = tblBorderColor, frame_width = frame_width)
//A,B,C
table.cell(resultsTable, column=0, row=0, text= "A", width = table_width, height = table_height, text_size = text_size_op, text_color=cellTextColor, text_halign=text_halign_pos, text_valign=text_valign_pos, bgcolor=celllBgColor)
table.cell(resultsTable, column=1, row=0, text= str.tostring(Price_A), width = table_width, height = table_height, text_size = text_size_op, text_color=cellTextColor, text_halign=text_halign_pos, text_valign=text_valign_pos, bgcolor=celllBgColor)
table.cell(resultsTable, column=0, row=1, text= "B", width = table_width, height = table_height, text_size = text_size_op, text_color=cellTextColor, text_halign=text_halign_pos, text_valign=text_valign_pos, bgcolor=celllBgColor)
table.cell(resultsTable, column=1, row=1, text= str.tostring(Price_B), width = table_width, height = table_height, text_size = text_size_op, text_color=cellTextColor, text_halign=text_halign_pos, text_valign=text_valign_pos, bgcolor=celllBgColor)
table.cell(resultsTable, column=0, row=2, text= "C", width = table_width, height = table_height, text_size = text_size_op, text_color=cellTextColor, text_halign=text_halign_pos, text_valign=text_valign_pos, bgcolor=celllBgColor)
table.cell(resultsTable, column=1, row=2, text= str.tostring(Price_C), width = table_width, height = table_height, text_size = text_size_op, text_color=cellTextColor, text_halign=text_halign_pos, text_valign=text_valign_pos, bgcolor=celllBgColor)
//N,V,E,NT
table.cell(resultsTable, column=2, row=0, text= "N値", width = table_width, height = table_height, text_size = text_size_op, text_color=cellTextColor, text_halign=text_halign_pos, text_valign=text_valign_pos, bgcolor=celllBgColor)
table.cell(resultsTable, column=3, row=0, text= str.tostring(Price_N), width = table_width, height = table_height, text_size = text_size_op, text_color=cellTextColor, text_halign=text_halign_pos, text_valign=text_valign_pos, bgcolor=celllBgColor)
table.cell(resultsTable, column=2, row=1, text= "V値", width = table_width, height = table_height, text_size = text_size_op, text_color=cellTextColor, text_halign=text_halign_pos, text_valign=text_valign_pos, bgcolor=celllBgColor)
table.cell(resultsTable, column=3, row=1, text= str.tostring(Price_V), width = table_width, height = table_height, text_size = text_size_op, text_color=cellTextColor, text_halign=text_halign_pos, text_valign=text_valign_pos, bgcolor=celllBgColor)
table.cell(resultsTable, column=2, row=2, text= "E値", width = table_width, height = table_height, text_size = text_size_op, text_color=cellTextColor, text_halign=text_halign_pos, text_valign=text_valign_pos, bgcolor=celllBgColor)
table.cell(resultsTable, column=3, row=2, text= str.tostring(Price_E), width = table_width, height = table_height, text_size = text_size_op, text_color=cellTextColor, text_halign=text_halign_pos, text_valign=text_valign_pos, bgcolor=celllBgColor)
table.cell(resultsTable, column=2, row=3, text= "NT値", width = table_width, height = table_height, text_size = text_size_op, text_color=cellTextColor, text_halign=text_halign_pos, text_valign=text_valign_pos, bgcolor=celllBgColor)
table.cell(resultsTable, column=3, row=3, text= str.tostring(Price_NT), width = table_width, height = table_height, text_size = text_size_op, text_color=cellTextColor, text_halign=text_halign_pos, text_valign=text_valign_pos, bgcolor=celllBgColor)
You can't work with click on the chart with pinescript.
Related
How can i make fair value gap boxes and hide when filled automatically? calculation would be low[0] > high{-[2]
Fair value gap coding in pinescript? I have tried to write code but unable to add more functions on it. //#version=5 indicator('Fair Value Gap Akash Mehto', overlay=true) boxLength = input.int(title='boxLength', defval=6, group='General', inline='General') boxTransparency = input.int(title='boxTransparency', defval=85, group='General', inline='General') bullishFvg = low[0] > high[2] bearishFvg = high[0] < low[2] if bullishFvg box.new(left=bar_index - 2, top=low[0], right=bar_index + boxLength, bottom=high[2], bgcolor=color.new(color.rgb(28, 202, 121), boxTransparency), text="FVG", text_size = "tiny", text_halign = text.align_right, text_color = color.green, border_color=color.new(color.green, boxTransparency)) if bearishFvg box.new(left=bar_index - 2, top=low[2], right=bar_index + boxLength, bottom=high[0], bgcolor=color.new(color.rgb(240, 46, 46), boxTransparency), text="FVG", text_size = "tiny", text_halign = text.align_right, text_color = color.red, border_color=color.new(color.green, boxTransparency))
Plotshape and label
In below code, I am getting syntax error for plotshape and several for label. Even for max / min i am getting too many arguments. I used label to get the value from intraburst formula as text. Will need help to resolve these. //#version=4 study("Intraburst", overlay=true) truemove = abs(open[2] - close) dayTrueHigh = max(high[1], high, close[2]) dayTrueLow = min(low[1], low, close[2]) dayTrueRange = dayTrueHigh - dayTrueLow intraburstLevel = truemove / dayTrueRange * 100 if intraburstLevel > 30 plotshape(series=intraburstLevel, text="ON", style=shape.circle, location=location.abovebar, color=color.green, size=size.normal) else plotshape(series=intraburstLevel, text="OFF", style=shape.circle, location=location.abovebar, color=color.red, size=size.normal) label = label.new(bar_index=0, yloc=y_top, xloc=xloc.bar_index, text=tostring(intraburstLevel, "0.00"), size=size.large, color=color.black) label.location = location.top label.y = y_top label.x = x_right label.text = tostring(intraburstLevel, "0.00")
In series, use a ternary Something like that: plotshape( intraburstLevel > 30 ? intraburstLevel : na, text = "ON", style = shape.circle, location = location.abovebar, color = color.green, size = size.normal) plotshape( intraburstLevel <= 30 ? intraburstLevel : na, text = "OFF", style = shape.circle, location = location.abovebar, color = color.red, size = size.normal) and the label label.new( bar_index, close, tostring (intraburstLevel, "##.00"), color = color.black, textcolor = color.white)
draw line and color the area above in indicator in Trading View with Pine Script
I want to draw a horizontal line at fixed value 30.000 and color the area above in light green in the DMI Indicator. How is this possible? Thanks //#version=5 indicator(title="Directional Movement Index", shorttitle="DMI", format=format.price, precision=4, timeframe="", timeframe_gaps=true) lensig = input.int(14, title="ADX Smoothing", minval=1, maxval=50) len = input.int(14, minval=1, title="DI Length") up = ta.change(high) down = -ta.change(low) plusDM = na(up) ? na : (up > down and up > 0 ? up : 0) minusDM = na(down) ? na : (down > up and down > 0 ? down : 0) trur = ta.rma(ta.tr, len) plus = fixnan(100 * ta.rma(plusDM, len) / trur) minus = fixnan(100 * ta.rma(minusDM, len) / trur) sum = plus + minus adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), lensig) plot(adx, color=#F50057, title="ADX") plot(plus, color=#2962FF, title="+DI") plot(minus, color=#FF6D00, title="-DI")
You can use the hline and fill functions https://www.tradingview.com/pine-script-reference/v5/#fun_fill https://www.tradingview.com/pine-script-reference/v5/#fun_hline hline(30, title = "30 Line", color=color.blue, linestyle=hline.style_solid, linewidth=2) fill(30, 100, color = color.new(color.green, 90))
tm_compass does not appear inside of graph
I'm trying to make a map of Europe using tmap and the eurostat package. I want to add a compass and a scale bar to the map. However they don't appear inside the graph, but outside of the map, at the bottom. Does anyone know what I'm doing wrong? I want the compass at the left top of the map, and the scale bar at the right bottom. countries = gisco_get_countries( year = "2016", epsg = "3035", resolution = "3" ) br = c(0,40,50,65,80,150) tm_shape(countries, bbox = c(23, 14, 74, 55) * 10e4) + tm_fill("#E0E0E0") + tm_shape(nuts2.sf) + tm_fill( "fatal_inj_30day", breaks = br, style = "fixed", palette = "Blues", alpha = .7, title = "Fatalities per million inhabitants \n(2018-2019)" ) + tm_compass(position = c("left","top")) + tm_scale_bar(position = c("right","bottom")) + tm_shape(countries) + tm_borders(lwd = .25) + tm_layout( bg.color = "#F2F2F2", outer.bg.color = "white", legend.bg.color = "white", legend.frame = "black", legend.title.size = 0.8, inner.margins = c(0, 0, 0, 0), outer.margins = c(0, 0, 0, 0), frame = TRUE, frame.lwd = 0, attr.outside = TRUE, legend.position = c("right", "top"), main.title = "Note: regions with 10 fatalities or less are not included in the Figure", main.title.position = "left", main.title.size = 0.7 )
D3 displaying same period of time in xAxis
I'm trying to display 36 hours array labels = ["01:00", "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00", "00:00", "01:00", "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00"] with data data = [16.1, 15.55, 14.12, 11.81, 9.637, 8.022, 6.625, 5.105, 5.216, 8.398, 11.4, 10.86, 10.52, 11.14, 15.37, 13.26, 11.33, 8.572, 12.21, 16.98, 12.43, 10.4, 10.09, 10.19, 9.361, 9.068, 9.763, 12.06, 15.52, 17.32, 15.53, 14.46, 14.05, 24.24, 12.26, 11.01] but displayed only 24 elements from 1:00 to 00:00. How can I configure D3 axis for displaying data with repeated time stamps?
Don't just use the hours, create the axis with full timestamps and then format the axis labels to only show the time: var width = 600; var height = 50; var yPad = 50; var xPad = 50; var xTicks = 18; var now = d3.utcHour(new Date()); var h36 = d3.utcHour.offset(now, 36); var svg = d3 .select("#d3") .append('svg') .attr('width', width) .attr('height', height); var xScale = d3 .scaleTime() .domain([now, h36]) .rangeRound([1 * xPad, width - xPad]); var xAxis = d3 .axisBottom(xScale) .ticks(xTicks) .tickFormat(d3.timeFormat('%H00')); svg .append('g') .attr('class', 'xaxis') .attr('transform', 'translate(0, ' + (height - yPad) + ')') .call(xAxis); <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.13.0/d3.min.js"></script> <div id="d3"></div>