Take Pivot Low/High and draw trendline - pivot

I am trying to take the most recent last two Pivot High points (and Low) and draw a trendline from the last Pivot High to the current Pivot high. Since the pivothigh() function has the left and right bars both set to 5 I can always assume my currect Pivot point is bar_index[5]. The problem I am having is how do I keep up the with last pivot point that came before the current pivot? Not sure if there is some way when a pivot point is reached and labeled on the chart you can somehow save that candle time so you can reference it in the line.new() function. Here is my current code but the following code is what I manually did to figure out the last pivot point. Instead of typing in 13 or 18 it should be a variable that holds the previous candle that was a pivot point.
bar_index[13] - botc, low[18]
showpivot = input(true, title="Show Pivot Points")
lb = input(5, title="Left Bars", minval=1)
rb = input(5, title="Right Bars", minval=1)
float top = na
float bot = na
top := pivothigh(lb, rb)
bot := pivotlow(lb, rb)
plotshape(top and showpivot, text="[PH]", style=shape.labeldown, color=color.white, textcolor=color.black, location=location.abovebar, transp=0, offset = -rb)
plotshape(bot and showpivot, text="[PL]", style=shape.labeldown, color=color.white, textcolor=color.black, location=location.belowbar, transp=0, offset = -rb)
topc = 0, botc = 0
topc := top ? lb : nz(topc[1]) + 1
botc := bot ? lb : nz(botc[1]) + 1
var line divl = na
var label lab = na
if bot and showpivot
line.delete(divl)
divl := line.new(bar_index[13] - botc, low[18], bar_index[5], low[lb] , color = color.lime, extend=extend.right)```

I found some code that answered my question
https://www.tradingview.com/script/eXUYLaGv-Trend-Lines-v2/

Related

XSLFTable get size to see if it will fit in slide after insertion

I am working on a slide show where I insert N number of rows. There are two issues...
I don't know the number of rows, there is a max of 50 we will allow but even 50 will go out of the slide.
The text I will add to each column can also be somewhat long.
Right now, my current approach is allowing 15 rows, creating a new slide to add the next 15, and so on until I hit 50.
What I would prefer to do is get the size of the table and after I finish one row, I would like to check if it is overflowing out of the slide, and if it is, I'll remove it, make a new slide, and add it to the new table.
An alternative approach if possible, is keep the row height locked, and allow any extra text to kinda be hidden until the cell is selected (similar to an excel spreadsheet).
Using a similar approach here
Apache POI get Font Metrics
Solution:
stringList is repopulated for each row
int max = 0;
int j = 0;
for (String text : stringList) {
AttributedString attributedString = new AttributedString(text);
attributedString.addAttribute(
TextAttribute.FAMILY, "Avenir Book", 0, text.length());
attributedString.addAttribute(TextAttribute.SIZE, (float)14);
TextLayout layout = new TextLayout(attributedString.getIterator(), fontRenderContext);
Rectangle2D bounds = layout.getBounds();
max = Math.max(max, (int) Math.ceil((bounds.getWidth() * covertToEmu)
/ (table.getTblGrid().getGridColList().get(j).getW())));
j++
}
covertToEmu is just a number...bounds.getWidth() is in 72 dpi and table.getTblGrid().getGridColList().get(j).getW() (the width) is in EMU. 72 dpi is just the pixels in inches...which is 72 pixels per inch. An EMU per inch is 914400.
So convertToEmu is 914400 / 72 = 12700.
The max is the number of "rows" it takes...the rest is kinda hard coded, but I split the list of data I have into sublists and add it to each slide. I know 20 rows is a good fit so if it gets higher than that I create a new list, to add to a new slide.
Also worth noting I am using CTTable, which you can get from a method in XSLFTable.

Change color of the 7th bar only - (specific candle color Tradingview)

I would like to change the color of a single candle. For example only the 7th.
I tried with barcolor and offset but it also points out all the previous ones of 7, Can you help me to get only the 7th bar in yellow ?
//#version=5
indicator('Previous Candle High and Low', shorttitle='Prev. H/L', overlay=true)
dt = time - time[1]
patternLabelPosHigh = close[7]
barcolor(color=bar_index ? color.yellow : na, offset=-6)
Thank you !
If you would like to retroactively paint only the 7th bar to the left and redraw the results, you have to check the current bar's state.
The construction below looks redundant, however it covers all possible states of the market as far as I tested, the script below will constantly paint 7th bar to the left and redraw on every new bar:
//#version=5
indicator("Past barcolor()")
pastBar = input.int(7, title = "Past Bar", minval = 1)
barcolor((barstate.islast or barstate.isrealtime) and (not barstate.isconfirmed or barstate.islastconfirmedhistory) ? color.new(color.blue, 0) : na, offset = - pastBar)

Extending lines on pine-script

I've got this code that as it is now, is connecting the pivots high and the low ones but I'd like the lines to extend to create trend lines and to extend horizontally to create Support/Resistance lines as well.
In the image below you can see the blue lines that the script is now creating and the red ones that i created manually to explain what I'd like it to do.
Continuos red lines for trends and dashed for S/R
I tried using line.set_extend but this code extends the lines forever creating a very confusing chart. I'd like to be able to set a maximum length the lines can extend or even better, a maximum of trend lines that can be displayed at a single time. Unluckily this goes beyond my coding capabilities.
Hopefully some of you has the skills to help me.
Thank you :)
//#version=4
study("PivotsExt", overlay=true)
Barsleft = input(10)
Barsright = input(10)
ph = pivothigh(Barsleft, Barsright)
pl = pivotlow( Barsleft, Barsright)
ph_ext = ph
pl_ext = pl
if (ph)
ph_ext := ph
if (pl)
pl_ext := pl
plot(ph_ext, color= color.blue, offset=-Barsright)
plot(pl_ext, color= color.red, offset=-Barsright)
Here we declare your 2 variables holding the pivot levels on the first bar only using var, which makes their value persistent across bars. We also adapt the plot() calls so they don't plot a diagonal line when levels change:
//#version=4
study("PivotsExt", overlay=true)
Barsleft = input(10)
Barsright = input(10)
ph = pivothigh(Barsleft, Barsright)
pl = pivotlow( Barsleft, Barsright)
var ph_ext = ph
var pl_ext = pl
if (ph)
ph_ext := ph
if (pl)
pl_ext := pl
// plot(ph_ext, color= color.blue, offset=-Barsright, style = plot.style_circles)
plot(ph_ext, color= change(ph_ext) ? na : color.blue, offset=-Barsright)
plot(pl_ext, color= change(pl_ext) ? na : color.red, offset=-Barsright)

Bokeh: Control colors on Donut chart

I am using Bokeh to create a series of pie charts with bokeh.charts.Donut. The charts are based off of subsets of the same DataFrame, and all have the same category labels. I want to ensure that the same categories are displayed in the same colors across the various charts, but I haven't been able to figure out a consistent way of controlling the colors.
Currently I am sorting my input DataFrames by the label, and passing the same array of colors to the palette property of Donut. This still does not work as intended. Code is as follows:
main_colors = ['#10A400','#DB5E11','#C8C500','#CF102E','#00AFA8','#82BC00','#A40D7A','#FF7100','#1349BB']
#split out youth health problems
for_youth_health = detailed_assessment_safety.loc[detailed_assessment_safety.youth_health_prob.notnull()]
youth_health_issues = pd.DataFrame(for_youth_health.youth_health_prob.str.split(' ').tolist())
for col in youth_health_issues.columns:
newcol = 'youth_health_prob_' + str(col)
youth_health_issues = youth_health_issues.rename(columns={col:newcol})
youth_health_trans = pd.melt(youth_health_issues)
youth_health_trans = youth_health_trans.loc[youth_health_trans.value.notnull()]
youth_health_trans['issue_text'] = youth_health_trans.value.map(map_health_issues)
youth_health_trans.drop('value',axis=1,inplace=True)
youth_health_trans.sort_values(by='issue_text',ascending=True,inplace=True)
#pie of youth health issues
youth_health_issues = Donut(youth_health_trans,label='issue_text',
values='variable',agg='count',plot_width=plot_width,
plot_height=plot_height,title='Reported Youth Health Issues',
color=main_colors)
hover = HoverTool(point_policy='follow_mouse')
hover.tooltips = [('Number Reported','#values'),('Health Issue','#issue_text')]
youth_health_issues.add_tools(hover)
#split out adult health problems
for_adult_health = detailed_assessment_safety.loc[detailed_assessment_safety.adult_health_prob.notnull()]
adult_health_issues = pd.DataFrame(for_adult_health.adult_health_prob.str.split(' ').tolist())
for col in adult_health_issues.columns:
newcol = 'adult_health_prob_' + str(col)
adult_health_issues = adult_health_issues.rename(columns={col:newcol})
adult_health_trans = pd.melt(adult_health_issues)
adult_health_trans = adult_health_trans.loc[adult_health_trans.value.notnull()]
adult_health_trans['issue_text'] = adult_health_trans.value.map(map_health_issues)
adult_health_trans.drop('value',axis=1,inplace=True)
adult_health_trans.sort_values(by='issue_text',ascending=True,inplace=True)
#pie of adult health issues
adult_health_issues = Donut(adult_health_trans,label='issue_text',
values='variable',agg='count',plot_width=plot_width,
plot_height=plot_height,title='Reported Adult Health Issues',
palette=main_colors)
hover = HoverTool(point_policy='follow_mouse')
hover.tooltips = [('Number Reported','#values'),('Health Issue','#issue_text')]
adult_health_issues.add_tools(hover)
What's the proper way to map the same categories of Donut charts to colors across multiple charts? The other idea that I had was inserting a column into the DataFrame that mapped label values to colors, and then passing that column as an argument to Donut, but I couldn't make that work either. Any help is much appreciated.
After some experimentation, it turns out that when you pass an array of colors to the palette argument of Donut, the colors are associated with the donut slices based on an alphabetical sort of the slice name. So, the first color in your array of palette colors will be associated with the slice with the alphabetically first name, etc.

Unable to display 2D chart correctly with Teechart on Monotouch

Having fun evaluating Teechart for .NET Monotouch for iOS. Run into several problems that cannot solve. Basically I am trying to present a 2D bar chart with two or three series with up to 24 data points.
Here is my test code
chart3.Aspect.View3D = false;
chart3.Legend.Visible = false;
chart3.Chart.Aspect.ZoomScrollStyle = Steema.TeeChart.Drawing.Aspect.ZoomScrollStyles.Auto;
Axis left=chart3.Axes.Left;
left.Grid.Visible = false;
left.Automatic=false;
left.Minimum=0;
left.Maximum=20;
left.Increment=1;
Axis bottom=chart3.Axes.Bottom;
bottom.Visible=true;
bottom.Grid.Visible = false;
Steema.TeeChart.Styles.Bar bar1=new Steema.TeeChart.Styles.Bar();
chart3.Series.Add(bar1);
bar1.Add(12.0,"Jun 2012");
bar1.Add(8.0,"Jul 2012");
bar1.Add(0.5,"Aug 2012");
bar1.Add(6.7,"Sep 2012");
bar1.Pen.Width = 0;
bar1.Gradient.Visible = true;
bar1.GetSeriesMark += (series, e) => {object v=series.YValues[e.ValueIndex]; e.MarkText=""+v;};
Steema.TeeChart.Styles.Bar bar2=new Steema.TeeChart.Styles.Bar();
chart3.Series.Add(bar2);
bar2.Add(8.0,"Jun 2012");
bar2.Add(5.0,"Jul 2012");
bar2.Add(5.0,"Aug 2012");
bar2.Add(14.0,"Sep 2012");
bar2.Pen.Width = 0;
bar2.Gradient.Visible = true;
bar2.GetSeriesMark += (series, e) => {object v=series.YValues[e.ValueIndex]; e.MarkText=""+v;};
Above code creates two 2D bar style series with four points.
Here is the result I am getting
The major problem is that all bars are floating 0.5 point above zero (notice 8.5 on the left axis where value is 8). Scrolling up shows this
Second issue I am facing is that the library doesn't take into consideration Maximum value set for the last axis.
If I set Aspect.View3D to true that chart looks much better
3D comes with its own set of issues but we need 2D anyway.
My question is: what I am doing wrong?
yes, you're correct. I can reproduce the problem here.
A fix will be included into the next maintenance release which will be available soon.
In meantime, a workaround would be to set to Manual the ZoomScroll style, which makes to use the default zoom and scroll of the TeeChart library :
Code :
_controller.chart.Chart.Aspect.ZoomScrollStyle = Steema.TeeChart.Drawing.Aspect.ZoomScrollStyles.Manual

Resources