Can anyone help getting a box to display using time as coordinates in Pinescript V5? - session

Hi beginner coder apologise for being basic, im trying to learn to code with pinescript, i just want to draw a box around the NY open, with 9:30-10:30 as left and right sides, and the high and low price within that hour as the top and bottom.
Its for making backtesting easier so eventually want it drawn between the user inputted time period on every NY open, but for now I cant get one to display at all.
I'm not getting any errors, is there a problem with how i'm creating the condition to only draw the box within the two allowed time periods?
Or am I not even close? :)
indicator("justabox", overlay=true)
startdate = input.time(timestamp("15 december 2022 04:00"), title = "Start Date")
enddate = input.time(timestamp("16 december 2022 04:00"), title = "end date")
nysess = not na (time("","0930-1030"))
daytime = time("1D")
onehour = 1000 * 60 * 60
oneminute = 1000 * 60
leftside = daytime + (onehour*9)+(oneminute*30)
rightside = daytime +(onehour*10) +(oneminute*30)
float top = na
float bottom = na
bool indate = na
if (timenow >= startdate and timenow<= enddate)
`indate := true`
else
`indate := false`
if (indate and nysess)
`top := ta.highest(high,12)`
`bottom := ta.lowest(low,12)`
`boxy = box.new(leftside, top, rightside, bottom)`
I was expecting a box to be drawn between 9:30am and 10:30 am with its top and bottom set at the high and low price within that hour.
i tried simplifying the code down to just the user inputted time period as a condition and setting the box at last_bar_index[120] and last_bar_index as its left and right without a session condition and i tried just drawing two lines at high and low price, im following tutorials and dont seem to be doing much different, but i cant find one specific to what im doing.
Thanks for your time.

Check whether the current bar is within the NY open session
nysess = time("0930-1030")
If the current bar is within the NY open session, draw a box
if nysess
top = high
bottom = low
leftside = time("0930")
rightside = time("1030")
boxy = box.new(leftside, top, rightside, bottom)

Related

Timevariation in Open air R-package

Usually, when using timevariation function for variation in pollutant concentration with time in openair R package, the hour of the day (on the x axis) spans from 0:00Hr to 23:00Hr.
For example;
timeVariation(filter(MetConc2),
pollutant = "PM2.5", ylab = "PM2.5 (ug/m3)")
But, I need to plot a graph indicating the time frame from 6:00Hr to 5:00Hr, please what code can I use for it. Can anyone help me out.

Counting data for a certain period, enter the counting period from date to date

I need to calculate the percentile of financial data for a certain period from the current date to the historical date, for example, for the last 12 months. It would be ideal if there was such an opportunity to enter a counting period from date to date, please help me
//#version=5
indicator("Percent", "Percent", true)
// Getting inputs
percentage = input(title="Персентиль", defval=75)
source = input(title="Source", defval=close)
endMonth = input.int(title="Месяц", defval=12)
// Time
var tn = timenow
length = timestamp(year(tn), month(tn)-endMonth,1,0,0,0)
// Plot colors
col_perc = input(color.red, "Персентиль", group="Color Settings", inline="Персентиль")
// Calculating
percen = ta.percentile_linear_interpolation(source, length, percentage)
if barstate.islast
// Create the label once, the first time the block executes on the last bar.
var lbl = label.new(na, na)
// label's information.
label.set_xy(lbl, bar_index, percen)
label.set_text(lbl, str.tostring(percen, format.mintick))
label.set_color(lbl, color.red)
plot(percen, title="Персентиль", style=plot.style_line, color=col_perc)
Here is a working version of what I want, but it counts down from the historical date to the current one, across the entire data history. I need to calculate for a certain period, starting from the current date and ending with the date entered in the history
//#version=5
indicator("Percent", "Percent", true)
// Getting inputs
length = input.int(title="Длина", defval=10)
percentage = input(title="Персентиль", defval=75)
source = request.financial(syminfo.tickerid, "ENTERPRISE_VALUE_EBITDA", "FY")
// Plot colors
col_perc = input(color.red, "Персентиль", group="Color Settings", inline="Персентиль")
// Calculating
percen = ta.percentile_linear_interpolation(source, length, percentage)
if barstate.islast
// Create the label
var lbl = label.new(na, na)
// label's information.
label.set_xy(lbl, bar_index, percen)
label.set_text(lbl, str.tostring(percen, format.mintick))
label.set_color(lbl, color.red)
plot(percen, title="Персентиль", style=plot.style_line, color=col_perc)

Excel - VBA - Access chart Axis - Speed issue

I am running the below code 400 times. I have 60 charts on the sheet. Execution time is 300 sec. If I remove this line
minVal = 0.02 * (cht.Chart.Axes(xlValue).MaximumScale - cht.Chart.Axes(xlValue).MinimumScale)
the speed improves to 190 seconds. This line impacts nothing given minVal is overwritten by 0 right after (for the purpose of the test). I am looking to understand why accessing the axis of the chart is so time consuming and for a workaround.
Sub quickAdjustLabels()
Dim cht As Excel.ChartObject
For Each cht In ActiveSheet.ChartObjects
isProdChart = 0
If cht.Chart.SeriesCollection(1).ChartType <> 5 Then 'different from pie
minVal = 0.02 * (cht.Chart.Axes(xlValue).MaximumScale - cht.Chart.Axes(xlValue).MinimumScale)
minVal = 0
For Each myCollection In cht.Chart.SeriesCollection
'if Stack and if not white visible (white visible are the bottom of waterfall charts / white unvisible are the NC stacks) => remove label is too small
If (myCollection.ChartType = xlColumnStacked Or myCollection.ChartType = xlColumnStacked100) And (myCollection.Format.Fill.Visible = msoFalse Or myCollection.Format.Fill.ForeColor.RGB <> 16777215) Then
myCollection.ApplyDataLabels
vals = myCollection.Values
For i = LBound(vals) To UBound(vals)
If Abs(vals(i)) < minVal Then myCollection.Points(i).HasDataLabel = False
Next
End If
If myCollection.Name = Range("Client") Then isProdChart = 1 'Identify productivity charts
Next myCollection
'Remove labels on productivity charts
If isProdChart = 1 Then
For Each myCollection In cht.Chart.SeriesCollection
If myCollection.ChartType = xlColumnStacked Then myCollection.DataLabels.Delete
Next
End If
End If
Next cht
End Sub
Your problem is not the statement that you pointed out, but actually the statements that apply the DataLabels:
myCollection.ApplyDataLabels
myCollection.Points(i).HasDataLabel = False
Setting the DataLabels take longer time the more points you have in your graph. So trying to avoid running these commands unnecessarily could potentially save you some time. Before setting the values, verify that it is necessary to change them
If Not myCollection.HasDataLabels Then
myCollection.ApplyDataLabels
End If
For i = LBound(Vals) To UBound(Vals)
shouldHaveLabel = True
If Abs(Vals(i)) < MinVal Then
shouldHaveLabel = False
End If
If myCollection.Points(i).HasDataLabel <> shouldHaveLabel Then
myCollection.Points(i).HasDataLabel = shouldHaveLabel
End If
Next
I hope this helps you.
I came to this conclusion by running your code on one of my excel-files with 56 graphs.
I added a time-measure that would tell me at the end of the execution how long time it took to execute, and ran it over and over again, commenting out different blocks of code until I could pinpoint which block was the one taking long time.
Dim tm As Date
tm = Now() 'get timestamp when execution started
...here goes the code to measure...
Debug.Print(Now()-tm)*24*60*60 'Show how many seconds execution took

1-2-3 star level awards algorithm

What I am trying to do is to have individual star counts per level based on player performance. (1-2-3 star awards.) This will be based on what region the player reaches. I know how to award the stars but keeping track of it all is throwing me problems. First lets say a player plays level 2 and receives 1 star for their performance. Then at a later time, s/he returns to the level and gets a 2 star. I would like the star count for that specific scene to update to two stars, while only adding 1 star ( The one extra s/he got this time) to the totalStarCount.
My initial plan was to have variables:
OldStarCount
NewStarCount
TotalStarCount
Then when a player reaches say region1, and is awarded one star, then NewStarCount would be set to one, then
TotalStarCount = TotalStarCount + (NewStarCount - OldStarCount);
Then update OldStarCount = NewStarCount;
Set NewStarCount = 0;
Move On to next Scene;
Am I approaching this the correct way? Any help would be greatly appreciated.
You could have something like this
int result = 0;
int totalStars = 0;
int[] starCounts = new int[NumberOfRegions};
...
currentRegion = 42;
result = play(currentRegion);
if(result > starCounts[currentRegion]){
totalStars += result - starCounts[currentRegion];
starCounts[currentRegion] = result;
}
This is just an example of what you could do. There are obvious scalability issues with this (what happens when you want to add new regions, etc), but you get the gist.

Visual Basic: Increasing integer every 3 clicks

So my question is pretty much what the title says: how do I get a certain integer to increase by 1 for every three clicks of a button? For example, let's say the integer is 900. I click button1 once, then again, then again, and on the third click the integer changes to 901. How can this be accomplished?
Thanks in advance.
Use a counter that is actually three times larger behind the scene. Start at 2700 instead of 900, and increase the value by one for each click.
When you want to display the value that increases every third click, you divide the counter by three:
displayValue = counter \ 3
Try this maybe this could help or give you idea
Dim click = 0
Dim num = 900
if (click % 3 = 0)
num+=1
else
click+=1
end if

Resources