Pine Script - Tradingview -Need Delayed a Sequence of Triggers to happen not on the same bar in order to execute - triggers

Still new to Pine Script. Writing a bollinger band script and I need two triggers, where one trigger needs to happen 1st (close price crosses upper band) and set a variable to true and does not change until the 2 second trigger (exits me from trade) which is when price crosses middle band then will execute and close out trade.
The triggers won't be happening on the same bar.
I think I'm looking for the right variable that I can set true and retains its value through multiple bars until I change it. Like I said I'm new and looking for quick answer.

You can declare a variable for this purpose with the var keyword. It will hold it's value across bars until you modify it.
var bool upperBandCrossed = false
bool exitCond = false
if open < upperBB and close > upperBB
upperBandCrossed := true
if open > basis and close < basis and upperBandCrossed
exitCond := true
upperBandCrossed := false

Related

Auto select an option from a dropdown after a certain date

I'm creating a lost&found log for work, I have a dropdown list with options like "Collected", "Sent to guest", "Donated" etc.
I'd like to have the sheet automatically select 'Donated' after a certain date (maybe after 1 month) IF an option hasn't already been selected.
E.G. If somebody inputs a found pair of gloves on Nov 2nd, The empty dropdown will automatically change to 'Donated' on Dec 2nd unless somebody already selected another option (E.G. 'Collected').
You can accomplish this by either making use of Apps Script to check the conditions and make the corresponding changes or by directly implementing Sheets functions.
Sheets Functions
You can achieve the desired outcome by putting this formula on each status cell:
=IF(TODAY()-D4>30,"Donated","")
It will work as intended, the cell will not display anything until there are 30 days between the TODAY() date and the date provided in the D4 cell. If there are, the cell will display Donated. If another option from the drop-down is selected, that will erase the formula and only the new value will be displayed.
However, this method is more of a quick hack than an actual robust solution, as several things can go wrong. For instance, if an option from the drop-down is chosen by accident and then it is left blank again, the method will not work anymore for that line as the formula will have been permanently erased.
You can read about how TODAY() works here.
Apps Script (recommended)
The following script will check that, for every row in the sheet, both the status cell is blank and that subtracting the DATE FOUND is more than 30 days (unfortunately that has to be done in milliseconds).
function myFunction() {
var spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadSheet.getSheetByName("Sheet1");
sheet.getLastRow()
var range = sheet.getRange(4,1,sheet.getLastRow(), 7);
//SELECT A RANGE WITH THE 7TH FIRST COLUMNS AND AS MANY ROWS AS NECESSARY
//(WE ARE INTERESTED IN DATA STARTING FROM ROW 4TH)
var values = range.getValues();
var millisecondsIn30Days = 30 * 24 * 60 * 60 * 1000;
for(var row = 0; row<values.length; row++){
var status = values[row][6]; //STATUS IS THE 6TH ELEMENT IN THE ROW ARRAY (COLUMN G)
if(status == "" && (new Date() - values[row][3]) > millisecondsIn30Days){
//MORE THAN 30 DAYS AFTER FOUND
sheet.getRange(row+4,7).setValue("DONATED");
}
}
}
Of course, that script can be run manually or the job can be automated with an installable trigger that runs, for example, every month. You can read more about how to set up such triggers here.

Determine a fixed, defined period / value, Pine Script

How can I set a defined value.
In backtesting, the bars are processed from back to front. Is it possible in Pine Script that, for example, a certain function is always carried out on the last 3rd bar.
for example:
IF 3rd bar from the front THEN x: = x + 1
You can do whatever you want at any bar you want: you just need to specify your function within an if block where the condition must be true for the given bar you target.
if ( this_bar_is_the_bar_i_want_to_target )
// ... do stuff like x+1 etc...
Now you might ask "How to get ante-last bar ?". Basically, you can't because "ante-last bar" is relative and infinite. Meaning, on a 1 hour timeframe, at 11:00, the ante-last bar will be 10:00, but then at 12:00, the ante-last bar is not 10:00 anymore, it now is 11:00... and this is infinite.
Now, that being said, "ante-last bar" is dynamic, so it will always be changing after each new candle. So the correct approach would be to actually isolate the "current last bar" and from THIS last bar, try to work your calculations on the previous one. In that case, it would be something like that:
if (barstate.islast)
// ... do stuff with close[1] or high[1] or low[1] or open[1] ...
That means that you can isolate the last bar DYNAMICALLY (it will be another bar for each new candle in the chart), and from there you can call previous bar, or bar from 3 bars back, ...

LibreOffice BASIC function - Copy if over a value to a new sheet

I'm trying to create a simple function on a LibreOffice Calc sheet.
I have a table of information where cells A1:K2 has headings (the cells in Row 2 are merged)
all the information runs from A3:K176
The sheet has various functions running on it already, all I need is one more function.
At first I wanted it to automate when i open, but then i thought running the BASIC macro off of a button might be better, so i can edit and then just hit the button once I'm done.
I have the button linked up to run the macro already, but now im stuck... I have tried many ways of writing the code, but it seems that i am just too green to figure it out.
the last time i ran Excel advanced functions i was in high school.
ok, enough talking...
All I need the macro to do is once I hit the button, I need it to check column K and any value > 1 it needs to take that whole row and copy it then go to a target sheet, clear any data on the target sheet. then copy that set of rows to the next open row in the target sheet. possibly even exporting the result to a .txt file as a bonus.
this is a start...
in future I would like to add another button that will do the same function to a array lower down and put that into a different sheet.
Option Compatible
Sub SWD_AwakeningsTrade
Dim i, LastRow
LastRow = Sheets("Card_List").Range("A" & Rows.Count).End(xlUp).Row
Sheets("Awakenings For trade").range("A2:I500").clearContents
For i=2 to LastRow
If Sheets("Card_List").Cells(i,"K").Value = "(>1)" Then
Sheets("Card_List").Cells(i, "K").EntireRow.Copy
Destination:=Sheets("Awakenings For trade").Range("A" & Rows.Count.end(xlUp)
End if
next i
End Sub

Access Form with checkbox toggled textboxes to make visible

Im trying to cut down on the clutter of my form since the data im getting can fill in more or less fields from my table.
as of right now im trying to build an event but I do not know the right syntax to use to create my event.
right now i have:
= if toggle.onclick ="yes" then
data.visible=true
else
data.visible=false
end if
in the After Update tab of the Event tab.
I hope that gives you an idea of what im trying to do.
I have this on a test form so the only objects are:
checkbox name "toggle"
textbox name "data"
the text box is default to not visible at the moment.
my goal is to have a list of check boxes and once they are checked their corresponding text box would appear on a refresh. this way the workers wont be intimidated by the amount of textboxes are on my current form. also will reduce the vast clutter on the current form.
By default, the 'Toggle' value will be True or False - not 'yes' or 'no'. Thus the following is what you need to toggle fields:
Private Sub Toggle_AfterUpdate()
If Me.Toggle = True Then
Me.Data.Visible = True
Else
Me.Data.Visible = False
End If
End Sub

How to update another text box while typing in access 2007 form?

I have a few text boxes which have to be filled with numeric values from 0 to 100. Below them there is another text box which stands for a total (the sum of the values from the text boxes above). How can I update the sum text box while typing in any of the other text boxes above?
If you are happy that the sum box updates after a box is updated (enter, tab or such like is pressed), then this can be done without any code. First, you will need to set the format of the textboxes to be summed to numeric, then the control source of the sum box becomes:
=Nz([text0],0)+Nz([text2],0)+Nz([text4],0)+Nz([text6],0)+Nz([text8],0) ...
Note the use of Nz, it may be possible to eliminate this by setting the default value property of the various textboxes to be summed.
A large set of controls that need to be summed in this way is often an indication of an error in the design of the database. You would normally expect this kind of thing to be a separate recordset, which could more easily be summed.
I know this is old, but Google didn't come up with much for this topic and this thread didn't really help either. I was able to figure out a very easy way to do this, so hopefully anyone else looking for this will find this helpful.
My need was for actual text as opposed to numbers, but the same applies.
To do what the OP is asking for you'll need at least 3 textboxes. 1 is the textbox you want to have updated each time you type, 2 is the textbox you will be typing in, and 3 is a hidden textbox.
Set textbox 1 to reference the value of the hidden textbox 3 in its control source:
="something in my textbox " & [textbox3]
In the OnChange event of textbox 2 right a line that will set the value of the hidden textbox 3 to the Text property of textbox 2 that you are typing in:
Private Sub textbox2_Change()
Me.textbox3.Value = Me.textbox2.Text
End Sub
Each time the value of the hidden textbox 3 gets updated, the calculation/reference in the displayed textbox 1 will be updated. No need to save caret locations or anything else mentioned in this post.
I was able to do this in Access 2007 by using the On Lost Focus event of the text box.
Just put something like this on the On Lost focus event of each text box that you want to be added , just make sure to set the default value of each text box to 0.
Me.Totals.Value = Me.Text1.Value + Me.Text2.Value + etc..
The moment you click on the next text box or anywhere as long as it loses focus, your sum will already be on the Totals box. You may add as many text boxes as you like, just include them in the code.
This is problematic due to the asinine requirement in Access that you have to set focus to text areas before you can get their value. I would recommend you change your design so that the text field is updated in response to a button click instead of on change.
If you want to go the update-on-change route, you would attach change events to each of the addend text fields. The event handlers would need to save the caret position/selection length, update the sum in the output text field, and restore the caret position. You need to save/restore the caret position because this is lost when the focus changes.
Here's an example for two text fields (txt1 and txt2). The output field is named txtOutput.
Private Sub txt1_Change()
Dim caret_position As Variant
caret_position = Array(txt1.SelStart, txt1.SelLength)
UpdateSum
txt1.SetFocus
txt1.SelStart = caret_position(0)
txt1.SelLength = caret_position(1)
End Sub
Private Sub txt2_Change()
Dim caret_position As Variant
caret_position = Array(txt2.SelStart, txt2.SelLength)
UpdateSum
txt2.SetFocus
txt2.SelStart = caret_position(0)
txt2.SelLength = caret_position(1)
End Sub
Private Sub UpdateSum()
Dim sum As Variant
sum = CDec(0)
txt1.SetFocus
If IsNumeric(txt1.Text) Then
sum = sum + CDec(txt1.Text)
End If
txt2.SetFocus
If IsNumeric(txt2.Text) Then
sum = sum + CDec(txt2.Text)
End If
txtOutput.SetFocus
txtOutput.Text = sum
End Sub