Trouble understanding value vs Series in PineScript - series

greenCandleCount=0
if close > open
greenCandleCount := greenCandleCount+1
plot(greenCandleCount)
In the above example, greenCandleCount is a value in the first line. But, close and open are both Series of floats. So in line 3, greenCandleCount seems to get converted into series. Because the line 2 comparison operation seems to produce a Series of booleans. Somehow a single variable lying inside an if block where the expression is a Series, also gets expanded into a Series. So why does this happen?

greenCandleCount doesn't get converted to series. The way you've declared it, it was always a series variable. At the start of the next bar, greenCandleCount would be reset to 0
With the code you've provided, greenCandleCount would always be either 1 or 0 and you'd be able to reference the previous bar's greenCandleCount state the same as any other series variable ie greenCandleCount[1]
If you wish to have a variable that is only initialized once and maintains it's value across bars it must be declared with the var keyword ie
var greenCandleCount = 0
Then your code will count the number of green candles on the entire chart.
https://www.tradingview.com/pine-script-reference/v4/#op_var

Related

Check one series value, change corresponding point colour in 2nd series

I have an automated pareto chart working well in Excel.
I want to change it to check if points in the cumulative percentage series have a value of less than 80% and colour the same number point in the count series if this is the case.
Tried this; doesn't throw any errors but also doesn't work.
I think it should be iterating through a list of values, checking if they are <80 then updating the point at the corresponding i in the count series.
Set percVals = paretoChart.Chart.SeriesCollection(percentSeries).Values
For Each i in percVals
If percVals(i) = <80 Then
With paretoChart.Chart.SeriesCollection(countSeries).Points(i)
.Format.Fill.ForeColor.RGB = RGB(0,0,225)
.Format.Line.ForeColor.RGB = RGB(0,0,225)
End With
End If
Next i
The "paretoChart" variable is set earlier in the code and is working fine throughout.
I've tried without the 'with' statement. No difference.
There is a similar question comparing the two values across series, but I don't think I can adapt that to work here. Any ideas appreciated!

Writing excel functions in vba

I'm trying to write the match and index functions as vba code but its not working due to several errors.
In excel, I have a table R5:AD33
I used to match functions to get my row and column number to input into my index function.
=MATCH(R35,Q5:Q33, 0)
=MATCH(R36, R3:AD3,0)
Then I use the index function to give me the cell value corresponding to the row and column number.
=INDEX(R5:AD33,S35,S36)
I tried to input this as code however it gives me 2 errors I cant solve to proceed debugging.
This line of code below gives me the "assignment to constant not permitted". I'm not sure where the constant is as x is a variable.
WorksheetFunction.Match("NPS", Sheets("sheet2").ListObjects("Table1"), 0) = x
There was also a user type not defined error but it didn't highlight a particular line of code.
Thanks.

Changing graph range based on specific cell data

See image for what I have going on
So the red column will at some point, which is different every day, change from 20 to 50. The start cell up top uses that lookup formula to find when this is. So for this example it is 10/11/16 14:37. I want to graph the green and blue columns, starting at that cell time and ending at the end time (which is just a max() of the times to get the end time).
I've looked into offset and name manager but I am really confused on those. Either using VBA or something else to change the graph (on a different sheet) to start at whatever that start time is would be awesome.
You can try the following:
In the column AL set, in each cell, the value to be defined by the following formula
AL1 = IF(AI1<50,0,1)
AL2 and following = IF(AL1=1,1,IF(AI2<50,0,1))
Then define each AM to be AL*AK, and plot AM.

VB.NET CHART- Display a subset of the total data points

VB 2008
.NET FRAMEWORK 3.5
MSCHART - FASTLINE CHART TYPE
Is it possible to have an MS Chart control contain 20,000 data points, but only show the last 100?
I know that I can select the last 100 from my datatable and use it as a datasource.
Chart1.DataSource = cMs2.dsData.Tables("readings").Select(wFilter, wSort).Take(100)
That's not what I want.
I know that I can populate an array or collection with the last 100 data points and use it as a data source.
Chart1.Series("readings").Points.DataBindXY(colCtr, colReadings)
That's not what I want.
I need to do 1 of 2 things:
Manually add data points and be able to show only the last 100 or last 1000 of them that just came in. This must be done without re-populating the chart. Just show a portion of the complete set of data points.
wSample = wSample + 1
Chart1.Series("readings").Points.AddXY(wSample, wReading)
Chart1.Series("readings").SHOWONLYTHELAST100DATAPOINTSWITHOUTCLEARING
Initialize a chart with a certain number of the total readings with a databind, then manually add new data points one at a time while removing the oldest data point. For example, initialize the chart with 100 data points, then add a new data point, remove the first data point, getting us back to 100. (I'm successfully doing this one, except the chart doesn't behave like I expect. The chart grows, remaining blank/empty where the 'removed' data points were. I do chart.update but it doesn't refresh it.) Note that I am allowed to take more time to initialize the chart (clear/populate), I don't have that time to do it as each new data point comes in.
wSample = wSample + 1
Chart1.Series("readings").Points.AddXY(wSample, wReading)
If Chart1.Series("readings").Points.Count > 100 Then
Chart1.Series("readings").Points.RemoveAt(0)
Chart1.Update()
End If
NOTE: Doing a process that causes me to have to clear and rebind the data to handle the addition of a single data point causes me problems because it takes too long. I'm just looking for the quickest, most efficient way to handle this. Thank you for taking the time to read this...!
You can manually set the Minimum and Maximum values of each axis by modifying the .Minimum and .Maximum values of the axes like
Chart1.ChartAreas(0).AxisX.Minimum = 100 'Example value
Chart1.ChartAreas(0).AxisX.Minimum = 200 'Example value
As discussed in the comments to your question you can either use your method #2 (add datapoint and delete datapoint index 0 and then select the new minimum and maximum X-Value from the series with LinQ:
Chart1.ChartAreas(0).AxisX.Minimum = (From p As DataVisualization.Charting.DataPoint In Chart1.Series(0).Points Select p.XValue).Min
Chart1.ChartAreas(0).AxisX.Maximum = (From p As DataVisualization.Charting.DataPoint In Chart1.Series(0).Points Select p.XValue).Max
or you can, as you have now done, just set the minimum value to a X-value some points before the last point.

Find Min/Max from 1 textbox

I'm trying to find the Min/Max value of a textbox that has 1 variable and display it to the user, so the variable changes everytime the button is clicked. How would I find the maximum value of something that is constantly changing? The trick is that I can NOT use if statements or case statements. I'm totally at a loss here.
Ok, the things that limit you.
One variable
no if/case statements
The lesson seems to revolve around using Math.Max().
Math.Max(), as we can see on MSDN returns
the larger of two 32-bit signed integers.
The one variable we are going to use needs to exist outside of the button's click event. So, just make it a class variable.
This variable will essentially store the largest value. Math.Max() returns the largest of two values... see what I am getting at here? You can pass the current largest variable as a parameter to Math.Max() without any issues.
Example:
Dim max As Integer
max = Math.Max(1, 100)
'max would be 100
max = Math.Max(max, 10)
'max would be 100
max = Math.Max(max, 1000)
'max would be 1000