I am trying to condition a time read as a string lies between a specified time interval using VBScript. Following is the script:
dim splitString, currentTime
splitString = Split("12 59 00")
currentTime = TimeSerial(splitString(0),splitString(1), splitString(2))
If ((DateAdd("n",-1,time())) <= currentTime < DateAdd("n", 1,time())) Then
Wscript.Echo currentTime
End If
This does not seem to work. Getting an echo even if the current time is outside the time interval.
In Python you can check 'is in an interval' using the short
if x <= y < z:
...
but in VBScript you need the longer
If x <= y And y < z Then
...
You need to evaluate both conditions independently.
dim splitString, currentTime
splitString = Split("3 39 00")
currentTime = TimeSerial(splitString(0),splitString(1), splitString(2))
If (DateAdd("n",-1,time()) <= currentTime) And (currentTime < DateAdd("n", 1,time())) Then
Wscript.Echo currentTime
End If
I used DateDiff. Modified the IF statement to:
If (DateDiff("n",DateAdd("n",-1,time()),currentTime) = 1 AND DateDiff("n",currentTime,DateAdd("n",1,time())) = 1) Then
Wscript.Echo currentTime
End If
Worked as expected.
Related
Hello I'm new to pine and i have a question.
I would like to write a script that helps me drawing lines connecting the close of the first candle in a selected range and the close of every other candle in the range.
I think I have some problem understanding pine runtime because using for loops or conditional structures seems bad but I can't find a solution around this.
I tried with if but had no succes, the idea was that
after i select the start/end point, the code should be something like this:
if bar_index > bar_index[barStart] and bar_index < bar_index[barEnd]
line.new(bar_index[barStart], close[barStart], bar_index, close)
else na
After this I tried with a for loop, again with no success:
for i = bar_index[barStart]+1 to bar_index[barEnd]
line.new(bar_index[barStart], close[barStart], bar_index[i], close[i])
The code I use to select the range and count the candles inside it is this one:
//#version=5
indicator("Close", overlay=true)
// Range Start
t0 = input.time(timestamp("20 Jul 2021 00:00 +0300"), confirm = true)
p0 = input.price(defval = 0, confirm = true)
// Range End
t1 = input.time(timestamp("20 Jul 2021 00:00 +0300"), confirm = true)
p1 = input.price(defval = 0, confirm = true)
///////////////////////////////////////////////////////////////////////////////////
// Bar counting
t_bar(_t) =>
var int _bar = na
if time_close[1] <= _t and time >= _t
_bar := bar_index
_bar
start = int(t_bar(t0))
end = int(t_bar(t1))
//Counting bars in the selected range
barStart = bar_index - start
barEnd = bar_index - end
barDelta = end - start
//Print results
plot(barStart, "Range start")
plot(barEnd, "Range end")
plot(barDelta, "Candles in range")
But from here on I don't know how to proceed. This should be pretty easy but I'm stuck.
What I'm trying to draw
Thank you to anyone willing to help!!
You don't need the loop or the input price variables. The lines can be drawn bar by bar as the script's execution enters your time range and the price variables can also be obtained at the same time.
//#version=5
indicator("Close", overlay=true)
// Range Start
t0 = input.time(timestamp("20 Jul 2021 00:00 +0300"), confirm = true)
// Range End
t1 = input.time(timestamp("20 Jul 2021 00:00 +0300"), confirm = true)
///////////////////////////////////////////////////////////////////////////////////
first_bar = time >= t0 and time[1] < t0
in_range = time > t0 and time <= t1
post_bar = time > t1 and time[1] <= t1
var float start_close = na
var int start_index = na
if first_bar
start_close := close
start_index := bar_index
if in_range and not first_bar
line.new(x1 = start_index, y1 = start_close, x2 = bar_index, y2 = close)
if post_bar
num_bars = bar_index[1] - start_index
delta = close[1] - start_close
info_text = "Start Bar : " + str.tostring(start_index) + "\nEnd Bar : " + str.tostring(bar_index[1]) + "\nNumber of bars : " + str.tostring(num_bars) + "\nPrice delta : " + str.tostring(delta)
label.new(x = bar_index[1], y = high[1], style = label.style_label_lower_left, size = size.small, text = info_text)
Follow up question :
To draw the lines on a higher timeframe and have them "persist" once you move to a lower timeframe is a bit trickier. You will have to use an input to manually set the higher timeframe as the script has no way to determine the previous timeframe that it was applied to.
When you set t0 and t1 on the higher timeframe, the timestamp values will correspond to the opening time for those higher time frame bars. This isn't ideal as the lower timeframe candle that starts at this same time isn't the close value we are after.
By using request.security() we can then get the actual closing time of the higher timeframe bar which has the closing value we do want.
So we can use time to determine when we've started the correct higher time frame bars and then use time_close to determine when we are on the lower time frame bar that coincides with the higher timeframe close.
//#version=5
indicator("MTF Close", overlay=true)
// Range Start
t0 = input.time(timestamp("20 Jul 2021 00:00 +0300"), confirm = true)
// Range End
t1 = input.time(timestamp("20 Jul 2021 00:00 +0300"), confirm = true)
///////////////////////////////////////////////////////////////////////////////////
tf = input.timeframe("240", title = "higher timeframe")
htf_close = request.security(syminfo.tickerid, tf, time_close)
is_htf_closing_bar = time_close == htf_close
new_htf = ta.change(time(tf)) != 0
var bool started_first_htf_bar = false
var float start_close = na
var int start_index = na
var bool started_last_htf_bar = false
if time >= t0 and time[1] < t0 and new_htf
started_first_htf_bar := true
else if new_htf
started_first_htf_bar := false
if started_first_htf_bar and is_htf_closing_bar and na(start_close)
start_close := close
start_index := bar_index
else if not started_first_htf_bar and is_htf_closing_bar and time > t0 and time < t1
line.new(x1 = start_index, y1 = start_close, x2 = bar_index, y2 = close)
if time >= t1 and time[1] < t1 and new_htf
started_last_htf_bar := true
else if new_htf
started_last_htf_bar := false
if started_last_htf_bar and is_htf_closing_bar
line.new(x1 = start_index, y1 = start_close, x2 = bar_index, y2 = close)
post_bar = new_htf and started_last_htf_bar[1]
if post_bar
num_bars = bar_index[1] - start_index
delta = close[1] - start_close
info_text = "Start Bar : " + str.tostring(start_index) + "\nEnd Bar : " + str.tostring(bar_index[1]) + "\nNumber of bars : " + str.tostring(num_bars) + "\nPrice delta : " + str.tostring(delta)
label.new(x = bar_index[1], y = high[1], style = label.style_label_lower_left, size = size.small, text = info_text)
Not sure if this is possible:
myString = 313233
counter = Len(myString)
Do while counter > 0
position = 1
take = 2
response.write (" counter:" & counter)
first_two = Mid(myString, position, take)
response.write (" Each loop:" & first_two)
position = position + 2
counter = counter - 2
Loop
Can someone tell me where I'm going wrong please? or is this possible via different method?
The code above is running once and returning: 31 not 31 32 33
Thanks.
UPDATE: Realized my error shortly after but now position won't increment to give to move up the variable
You set the position variable to 1 at the start of the Do loop. As it stands whenever Mid() is called it is passed position = 1 which will return 31.
Fix this by moving it outside the loop so position can increment.
myString = 313233
counter = Len(myString)
'Set before start of loop
position = 1
take = 2
Do while counter > 0
response.write (" counter:" & counter)
first_two = Mid(myString, position, take)
response.write (" Each loop:" & first_two)
'Use take variable so increment is always the same.
position = position + take
counter = counter - take
Loop
Code untested
I have a simple code which takes a long time to run. I was wondering if there is any way to make this run faster? Maybe this part (Cells(i, "U").Value = Cells(n, "X").Value) should not be used 2 times! Thanks!
For n = 3 To time_frame + 3
For i = 3 To 1002
If (Cells(i, "U").Value = Cells(n, "X").Value) And (Bed_in_use < 24) And Wait_L > 0 Then
Wait_L = Wait_L - (24 - Bed_in_use)
ElseIf (Cells(i, "U").Value = Cells(n, "X").Value) And (Bed_in_use < 24) And Wait_L <= 0 Then
Bed_in_use = Bed_in_use + 1
End If
Next i
Next n
MsgBox "The number of bed in use is " & Bed_in_use & ". There are " & Wait_L & " patients in the waiting list."
End Sub
Couple things will speed this up - The first was mentioned in the comments by #jcarroll, pulling the cells you need into an array and using that instead of making repeated calls to Cells.
The second is what you mentioned, structuring your If statements in a way that
you aren't making the same comparisons twice. For example, this has to be true for either condition...
Cells(i, "U").Value = Cells(n, "X").Value
...and this always has to be true:
Bed_in_use < 24
After Bed_in_use is 24 (or higher), you can exit out of the loop because you'll never satisfy either the If or the ElseIf statement. I'd re-roll it into something like this:
Dim values() As Variant
values = ActiveSheet.UsedRange '...or whatever Range you need.
For n = 3 To time_frame + 3
If Bed_in_use >= 24 Then Exit For
For i = 3 To 1002
If Bed_in_use >= 24 Then Exit For
If values(i, 21).Value = values(n, 24).Value Then
If Wait_L > 0 Then
Wait_L = Wait_L - (24 - Bed_in_use)
Else
Bed_in_use = Bed_in_use + 1
End If
End If
Next i
Next n
I'm not totally sure what your code is trying to do. But here is a sample of how you would compare two lists, and keep track of the total matches.
Sub test()
Dim arrayU() As Variant
Dim arrayX() As Variant
Dim LrowU As Integer
Dim LrowX As Integer
Dim i As Integer
Dim j As Integer
Dim bed_in_use As Integer
LrowU = Columns(21).Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LrowX = Columns(24).Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
ReDim arrayU(1 To LrowU)
ReDim arrayX(1 To LrowX)
For i = 1 To LrowU
arrayU(i) = Cells(i, 21)
Next i
i = 1
For i = 1 To LrowX
arrayX(i) = Cells(i, 24)
Next i
i = 1
j = 1
For i = 1 To LrowX
For j = 1 To LrowU
If arrayX(i) = arrayU(j) Then bed_in_use = bed_in_use + 1
Next j
Next i
MsgBox (bed_in_use)
End Sub
How can I use an if in another if in Visual Basic 6?
For example, in C++ I can start my statements with { and finish them with }, and I can use another if in the first statement.
For example:
if m>3
{ .....
if a>5
{
......
}
else {....}
} else ........
… but in Visual Basic there is no { } or something similar it
i want to writ a program that recive a time from user and Reduce 4:30 from it ! for example when user runs it , it asks : what time is it in Tehran ? and user enter time (for example 16:30) and then program Reduces 4 hour and 30 minutes from it ! and print : GMT time is : 12:00 ! i'll updat my post and put my code ! please read it !
Option Explicit
Private Sub Command1_Click()
Cls
Dim h As Integer, m As Integer, hm As Integer, mm As Integer, t As Integer
Dim fh As String, fm As String, fhm As String, fmm As String
t = InputBox("Enter time")
h = t \ 100
m = t Mod 100
Select Case h
Case 0 To 24
If m < 60 Then
If m >= 30 Then
hm = h - 4
mm = m - 30
If h < 10 Then fh = Format(h, String(2, "0")) Else fh = h
If m < 10 Then fm = Format(m, String(2, "0")) Else fm = m
If hm < 10 Then fhm = Format(hm, String(2, "0")) Else fhm = hm
If mm < 10 Then fmm = Format(mm, String(2, "0")) Else fmm = mm
Print
Print " Tehran Time is : "; fh; ":"; fm
Print
Print " GMT Time is : "; fhm; ":";
Else
hm = h - 5
mm = m + 30
If h < 10 Then fh = Format(h, String(2, "0")) Else fh = h
If m < 10 Then fm = Format(m, String(2, "0")) Else fm = m
If hm < 10 Then fhm = Format(hm, String(2, "0")) Else fhm = hm
If mm < 10 Then fmm = Format(mm, String(2, "0")) Else fmm = mm
Print
Print " Tehran Time is : "; fh; ":"; fm
Print
Print " GMT Time is : "; fhm; ":"; fmm
End If
End If
Case Else
Beep
Print
Print " Time is wrong !!! Try again "
End Select
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Form_Load()
End Sub
If clauses in VB are ended with "End If" (case insensitive). No brackets whatsoever.
You may nest as many if clauses as you want, as long as you close them:
If ... Then
*Do stuff*
If ... Then
*Do more stuff*
End If
End If
Single-line If statements can also be nested:
If (a) Then [a_stuff] : If (b) Then [a_and_b_stuff] Else [a_and_notb_stuff] Else [nota_stuff]
Note that no more [a_stuff] can appear after [a_and_notb_stuff], since there's no way to tell where b's Else-statements end, so nothing can appear in a clause after a nested If statement.
Any of the *_stuff parts can be empty. But you may have some difficulty with an empty [a_and_notb_stuff], since the VB6 IDE seems to be a bit temperamental here, and will try inserting an invalid third 'Else' statement there. To work around this you can put a ':' in place of [a_and_notb_stuff]. The colon will be removed by the IDE, but it won't then insert another Else statement.
Addendum: I should point out that this whole post should be treated as information, and not as a recommendation. Nesting single-line If statements like this makes for very unreadable and bug-prone code.
I have a search box.
My admin user might search for "#MG #EB dorchester".
In ASP, I need to count how many times the symbol "#" appears in the string. How is the possible?
Try this:
len(yourString) - len(replace(yourString, "#", ""))
Response.write ubound(split(str,"#"))
is enough for counting the occurance of a specific character
For JW01
Dim pos : pos = 0
Dim count : count = -1
Do
count = count + 1
pos = InStr(pos + 1, str, "#")
Loop While (pos > 0)
Try a while loop:
Do While (str.indexOf("#") != -1)
count = count + 1
str = right(str, len(str) - str.indexOf("#"))
Loop
EDIT:
This for loop might make more sense:
dim strLen, curChar, count
count = 0
int strLen = len(str)
for i = 1 to strLen
curChar = mid(str, i, 1)
if curChar = "#"
count = count + 1
end if
next
Replace the search with blank and find the difference between and original and new string will the number of time a string is present
Dim a = "I # am # Thirs#ty"
Dim count
count = Len(a) - Len(Replace(a,"#",""))
Response.write count
Function FnMatchedStringCountFromText(strText,strStringToSearch)
strLength = Len(strText)
strNumber = 1
IntCount = 0
For i = 1 to strLength
If Instr(1,strText,strStringToSearch,0) > 0 Then
stMatch = Instr(1,strText,strStringToSearch,0)
strText = Mid(strText,stMatch+2,strLength)
IntCount = IntCount+1
Else
Exit For
End If
Next
FnMatchedStringCountFromText = IntCount
End Function