In pine script, is the line 'WTCrossUp = wt2 - wt1 <= 0' a condition statement? - conditional-statements

Can you tell me if this line in Pine script is a condition statement please ? What does it do ?
WTCrossUp = wt2 - wt1 <= 0
Thank you !

It is a boolean operation. WTCrossUp will be assignend either true or false.
ie
if wt2 - wt1 is less than or equal to zero then WTCrossUp will be true

Related

Trigger Point of Moving average crossover

I am trying to define the trigger point when wt1(Moving average 1) crosses over wt2(moving average 2) and add it to the column ['side'].
So basically add 1 to side at the moment wt1 crosses above wt2.
This is the current code I am using but doesn't seem to be working.
for i in range(len(df)):
if df.wt1.iloc[i] > df.wt2.iloc[i] and df.wt1.iloc[i-1] < df.wt2.iloc[i-1]:
df.side.iloc[1]
If I do the following:
long_signals = (df.wt1 > df.wt2)
df.loc[long_signals, 'side'] = 1
it return the value of 1 the entire time wt1 is above wt2, which is not what i am trying to do.
Expected outcome is when wt1 crosses above wt2 side should be labeled as 1.
Help would be appreciated!
Use shift in your condition:
long_signals = (df.wt1 > df.wt2) & (df.wt1.shift() <= df.wt2.shift())
df.loc[long_signals, 'side'] = 1
df
if you do not like NaNs in 'side', use df.fillna(0) at the end
Your first piece of code also works with the following small modification
for i in range(len(df)):
if df.wt1.iloc[i] > df.wt2.iloc[i] and df.wt1.iloc[i-1] <= df.wt2.iloc[i-1]:
df.loc[i,'side'] = 1

Adding column value for a list of indexes

I have a list of indexes and trying to populate a column 'Type' for these rows only.
What I tried to do:
index_list={1,5,9,10,13}
df.loc[index_list,'Type']=="gain+loss"
Output:
1 False
5 False
9 False
10 False
13 False
But the output just gives the list with all False instead of populating these rows.
Thanks for any advice.
You need to put a single equal instead of double equal. In python, and in most progamming languages, == is the comparison operator. In your case you need the assignment operator =.
So the following code will do what you want :
index_list={1,5,9,10,13}
df.loc[index_list,'Type']="gain+loss"

How to set multiple conditions for a Dataframe while modifying the values?

So, I'm looking for an efficient way to set up values within an existing column and setting values for a new column based on some conditions. If I have 10 conditions in a big data set, do I have to write 10 lines? Or can I combine them somehow...haven't figured it out yet.
Can you guys suggest something?
For example:
data_frame.loc[data_frame.col1 > 50 ,["col1","new_col"]] = "Cool"
data_frame.loc[data_frame.col2 < 100 ,["col1","new_col"]] = "Cool"
Can it be written in a single expression? "&" or "and" don't work...
Thanks!
yes you can do it,
here is an example:
data_frame.loc[(data_frame["col1"]>100) & (data_frame["col2"]<10000) | (data_frame["col3"]<500),"test"] = 0
explanation:
the filter I used is (with "and" and "or" conditions): (data_frame["col1"]>100) & (data_frame["col2"]<10000) | (data_frame["col3"]<500)
the column that will be changed is "test" and the value will be 0
You can try:
all_conditions = [condition_1, condition_2]
fill_with = [fill_condition_1_with, fill_condition_2_with]
df[["col1","new_col"]] = np.select(all_conditions, fill_with, default=default_value_here)

IF OR Statements which always evaluate to TRUE

Recently I found an error which was due to the way I constructed an IF OR statement.
The original statement:
If a = 1 OR 2 OR 3 Then
`Execute code 1`
`Else if a = 4 OR 5 OR 6 Then`
`Execute code 2`
`Else`
`Do nothing`
The corrected code:
If a = 1 OR a = 2 OR a = 3 Then
`Execute code 1`
`Else if a = 4 OR a = 5 OR a = 6 Then`
`Execute code 2`
`Else`
`Do nothing`
So, the issue was the first part of the IF statement would always evaluate true regardless of the value of a, because it also evaluates the boolean value of the number 2, which is true. So simply spelling out that each number should be compared to the value of a fixes that issue.
My questions are:
1. When using an if statement and you have a comparative operation ( if a =1) followed by "OR 2", in what situation would you actually want to look at the boolean value of the number versus comparing the number against the value of the previously referenced variable?
Is there a way to identify if statements present in the code which will logically ALWAYS be true (and probably have a error)?
Edit: the_lotus pointed out that the boolean value of each number was not being evaluated but bitwise operations were performed between all the values and then a boolean evaluation was performed on the result. In this particular case it may be possible for it to evaluate either true or false depending on the value of a, though I am still interested in identifying if statements which always evaluate true.
As per your Edit, since you want to check if the value of A falls within a range, what you could do is store the ranges as an array and perform IndexOf to see if the value of A is in the collection.
Here is a quick example:
Dim a As Integer = 4
Dim collection1() As Integer = {1, 2, 3}
Dim collection2() As Integer = {4, 5, 6}
If Array.IndexOf(collection1, a) > -1 Then
Console.WriteLine("a = 1, 2, or 3")
ElseIf Array.IndexOf(collection2, a) > -1 Then
Console.WriteLine("a = 4, 5, or 6")
Else
Console.WriteLine("a is not a value between 1-6")
End If
Fiddle: Live Demo

OR Condition - true if AND would apply?

Imagine you have the following sentences:
I am happy.
I am very cool.
I am very happy.
You're looking for a sentence that satisfies the following condition:
NOT(very OR happy)
It's clear that nor sentence 1 nor sentence 2 satisfies this condition. However, does sentence 3 satisfy this condition? Since it doesn't contain "very OR happy", but "very AND happy".
It is satisfying the condition 3 as well ie, it contains both. "very OR happy" is also true and "very AND happy" is also true.
In the logical statement if out of the two statements in OR condition the first is true and the next statement is not evaluated. However in case of AND if the first statement evaluates to false then the next statement is not evaluated.
The OR condition for a and b means at least one of them is true.
I am very happy.
Very = true, Happy = true
Put that into NOT(very OR happy) gives you:
NOT(true OR true)
Which is equal to:
NOT(true)
Which is equal to:
false