Pinescript - Simple if else - if-statement

I am currently trying to get back into coding (its been some time) and for some reason I cant get my pinescipt to execute properly.
The intent is that
Condition for strategy execution is met, on the first candle a particular strategy.entry with alert_message is executed (3commas deal start).
On the next candle, or any subsequent candle WHILE the deal is open, if the same condition is met a second strategy.entry and alert_message is executed (3commas deal add funds)
If following these the event is NOT met, strategy close is triggered.
At the moment, for some reason its only triggering the deal start repeatedly, and not jumping to the add - I know its a dumb mistake - I just cant see it!!!
base_Long_Order_Placed = 0
message_long_entry = "3commas deal start"
message_long_addition = "3commas deal add funds"
message_long_exit = "3commas close all deals"
longCondition = SECRET_SAUCE
if (longCondition == true and base_Long_Order_Placed == 0)
strategy.entry('Long', strategy.long, when = longCondition ==true and barstate.isconfirmed, alert_message = message_long_entry) /
base_Long_Order_Placed := 1
else if (longCondition == true and base_Long_Order_Placed == 1)
strategy.entry('Long', strategy.long, when = base_Long_Order_Placed == 1 and longCondition ==true and barstate.isconfirmed, alert_message = message_long_addition)
else if (longCondition == false)
strategy.close('Long', when=longCondition == false and barstate.isconfirmed, alert_message = message_long_exit)
base_Long_Order_Placed := 0
else
na

Your script will be executed on every bar. When you define a variable like below
base_Long_Order_Placed = 0
It will be re-defined every time and base_Long_Order_Placed == 0 will always be true. If you want to keep its value between executions, you need to use the var keyword.
var base_Long_Order_Placed = 0
Edit
var is just a keyword that tells the compiler to keep its value for the next execution. It is just a regular variable otherwise. It will keep its value unless you overwrite. It will not iterate unless you do
base_Long_Order_Placed := base_Long_Order_Placed + 1
So, you can definitely reset it when your condition is met.
if (deal_closed)
base_Long_Order_Placed := 0

Related

Why is my elif statement always executing in Godot?

I'm new to Godot programming, just to preface this. I'm trying to make it so that when you fall off of a platform without jumping you start accelerating downwards rather than just going down at a fixed speed. I almost have that, but my elif statement is happening every frame rather than the frame you leave the ground.
I've tried changing things, but I'm stuck at a logical dead end where any option I think of or find has resulted in the same or worse behavior.
extends KinematicBody2D
const UP_DIRECTION = Vector2.UP #up is up and down is down
# Declare member variables here.
var speed = 200 #speed of walk
var velocity = Vector2() #movement Vectors
var gravity = 1000 #gravity, it's real
var jump = 0 #jumping speed
var canjump = 0 #jumps left
var jumpcount = 1 #times you can jump
var nojump = false #did you fall or did you jump?
# Called every physics update.
func _physics_process(delta):
#jumping
if (canjump > 0 && Input.is_action_just_pressed("Jump") || is_on_floor() && canjump > 0 && Input.is_action_pressed("Jump")):
jump = -2000
canjump -= 1
nojump = false
#falling without jumping first
elif not (is_on_floor() && Input.is_action_just_pressed("Jump") && nojump == false):
jump = -gravity
canjump -= 1
nojump = false
#decelerating jumps
if (jump < 0):
jump += 45
#grounding jump variables
if(is_on_floor()):
canjump = jumpcount
nojump = true
#setting x and y
velocity.x = (Input.get_action_strength("Right") - Input.get_action_strength("Left")) * speed
velocity.y = gravity + jump
#using found inputs
velocity = move_and_slide(velocity, UP_DIRECTION)
pass
Here's my code, it tried commenting in the tdlr of what things are supposed to do. Again all I'm looking for is the reason why my elif statement is being applied every frame and how to fix that.
If you have a conditional that looks like this:
if condition:
prints("Something")
The execution flow would enter it when the condition is true.
Let us look at this as example:
if is_on_floor() && Input.is_action_just_pressed("Jump") && nojump == false:
prints("Something")
The above conditional requires three things:
is_on_floor() must be true.
Input.is_action_just_pressed("Jump") must be true.
nojump == false must be true. I mean, nojump must be false.
I think we can agree that does not happen all the time.
Now, if I negate the conditional:
if not condition:
prints("Something")
The execution flow would enter when the condition is false.
So, in a case like the one on your code:
if not (is_on_floor() && Input.is_action_just_pressed("Jump") && nojump == false):
prints("Something")
The conditional has one requirement:
not (is_on_floor() && Input.is_action_just_pressed("Jump") && nojump == false)`
Must be true. I mean:
is_on_floor() && Input.is_action_just_pressed("Jump") && nojump == false
Must be false. Double check D'Morgan's Law if you need to.
It is false when either:
is_on_floor() is false.
Or Input.is_action_just_pressed("Jump") is false.
Or nojump == false is false.
In other words, only one of these being false is sufficient.
To restate that. This condition:
not (is_on_floor() && Input.is_action_just_pressed("Jump") && nojump == false)`
Is equivalent to the following condition (by D'Morgan's law):
not is_on_floor() or not Input.is_action_just_pressed("Jump") or not nojump == false
By the way, using and and or is idiomatic in GDScript.
Wait, I can simplify that a bit:
not is_on_floor() or not Input.is_action_just_pressed("Jump") or nojump`
Thus, the execution flow will enter if either:
not is_on_floor() is true. I mean, if is_on_floor() is false.
not Input.is_action_just_pressed("Jump") is true. I mean, if Input.is_action_just_pressed("Jump") is false.
nojump is true.
And I reiterate that is either of them. Only one is sufficient. And I believe most of the time you haven't just pressed "Jump", also being on the air is enough. And I suspect nojump is false most of the time.
So, we can conclude that the execution flow will enter most of the time.
Write what you mean. You say in comments:
#falling without jumping first
Let us do that. What does falling mean? Does it mean on the air?
var falling := not is_on_floor()
Or do you also mean going down?
var falling := not is_on_floor() and velocity.dot(Vector2.DOWN) > 0.0
Alright, your pick. What does jumping first mean? Does it mean the player got in the air as a result of a jump? Ok... So you would do this when the player jumped:
jumped = true
Which I believe is this:
if canjump > 0 && Input.is_action_just_pressed("Jump"):
jumped = true
And reset it on the ground:
if is_on_floor():
jumped = false
Now we can say "falling without jumping first":
if falling and not jumped:
prints("something")
Almost as if it were the comment, but unlike the comment it can be executed.

Pine script condition 1 and condition 2 fulfilled at up to n steps back

Say I have condition 1 and condition 2. If condition 1 and condition 2 was met within up to, say, 5 bars, then I want to perform some action. As an example let's say condition 1 was met at current close, and condition 2 was fulfilled 5 bars ago, then I want to perform some action. How do I formulate that in Pine?
condition1 = ...
condition2 = ...
if (condition1(close)==true or condition1(close-2)==true or
condition1(close-3)==true or condition1(close-4)==true or
condition1(close-5)==true)
and (condition2(close)==true or condition2(close-2)==true or
condition2(close-3)==true or condition2(close-4)==true or
condition2(close-5)==true)
then...
Could it perhaps be formulated something like:
if condition1(close:close-5)== true and condition2(close:close-5)== true then ...
I have read e.g. this thread:
Change background for only the last 5 bars: A Very Simple Problem I can't crack
It sounds like a similar problem, but I am unsure about how to implement it.
a) You would need to use ta.barssince() function.
https://www.tradingview.com/pine-script-reference/v5/#fun_ta%7Bdot%7Dbarssince
//#version=5
indicator("My Script")
/// let's say condition 1 was met at current close, and condition 2 was fulfilled 5 bars ago, then I want to perform some action
ema10 = ta.ema(close, 10)
condition1 = close > open
condition2 = ta.crossover(ema10, close)
x = false
if condition1 and ta.barssince(condition2) == 5
x := true
plot(ema10)
bgcolor(condition2? color.orange:na)
bgcolor(x?color.green:na)
b) Another approach is to use history-referencing operator [].
https://www.tradingview.com/pine-script-docs/en/v5/language/Operators.html#history-referencing-operator
//#version=5
indicator("My Script")
// lets say you want to check if condition2 if true for 5 bars, and condition1 is true in the current bar
ema10 = ta.ema(close, 10)
condition1 = ta.crossover(ema10, close)
condition2 = close > open
condition3 = condition2[1] and condition2[2] and condition2[3] and condition2[4] and condition2[5]
x = false
if condition1 and condition3
x := true
plot(ema10)
bgcolor(condition2? color.orange:na)
bgcolor(x?color.green:na)

nextflow (groovy) check if item in Channel list

I am struggling to use an if/else on a containsAll() statement. It returns the correct true false value when tested with println(), but when put in an if statement it seems to always evaluate to true -- see below.
def examine_phenotype(pheno){
condition_values = \
Channel
.fromPath(pheno)
.splitCsv(header: true, sep: ',')
.map{ row ->
def condition = row.condition
return condition
}
.toList().view()
println(condition_values.containsAll('control'))
if(condition_values.containsAll('control')){
exit 1, "eval true"
}else{
exit 1, "eval false"
}
}
Console output for two different files, one with 'control' and one without 'control' in the column 'condition', which is the point of the function.
[normal, normal, normal, tumor, tumor, tumor]
DataflowInvocationExpression(value=false)
eval true
[control, control, control, tumor, tumor, tumor]
DataflowInvocationExpression(value=true)
eval true
Using collect() instead of toList() where each item within condition_values is enclosed with single quotes did not resolve the issue either. The clue might be in DataflowInvocationExpression but I am not up to speed on Groovy yet and am not sure how to proceed.
Testing the conditional within the function was not working, but applying filter{} and ifEmpty{} was able to produce the desired check:
ch_phenotype = Channel.empty()
if(pheno_path){
pheno_file = file(pheno_path)
ch_phenotype = examine_phenotype(pheno_file)
ch_phenotype.filter{ it =~/control/ }
.ifEmpty{ exit 1, "no control values in condition column"}
}
def examine_phenotype(pheno){
Channel
.fromPath(pheno)
.splitCsv(header: true, sep: ',')
.map{ row ->
def condition = row.condition
return condition
}
.toList()
}

Very simple if/else statements seem to be working backwards

I have been looking at this simple if/else statement in another larger project and I can't seem to find what I am doing wrong.
I have inserted Logger.log() in both if statements to try to root out the problem.
When I run the code, I get the following Log:
[19-02-24 08:50:05:427 PST] var Campus = Baylor
[19-02-24 08:50:05:428 PST] var TSTCCampus = TSTC
[19-02-24 08:50:05:428 PST] if IS= statement executed
[19-02-24 08:50:05:428 PST] else NOT= executed
The two variables are clearly NOT equal but the if = executes and the else != executes.
What am I doing wrong?
function myFunction() {
// call the Current Reults sheet and identify the Last Row of Responses
var RawFormResponsesSheet = SpreadsheetApp.getActive().getSheetByName("Current Results");
var CurrentSubmission = RawFormResponsesSheet.getLastRow(); // Retruns the Value of the Last Submission Row Number
var Campus = RawFormResponsesSheet.getRange(CurrentSubmission,3).getValue();
// call the Email Data sheet and identify certain cell values
var EmailDataSheet = SpreadsheetApp.getActive().getSheetByName("Email Data")
var TSTCCampus = EmailDataSheet.getRange(3, 4).getValue();
var BaylorCampus = EmailDataSheet.getRange(4, 4).getValue();
Logger.log("var Campus = " + Campus)
Logger.log("var TSTCCampus = " + TSTCCampus)
if (Campus = TSTCCampus){Logger.log ("if IS= statement executed")}
else {Logger.log ("else IS= executed")}
if (Campus != TSTCCampus){Logger.log ("if NOT= statement executed")}
else {Logger.log ("else NOT= executed")}
}
You need to use == as comparison operator.
There are 3 = operators/commands:
= is used to set the value of a variable
== equality operator, returns true if the elements have same value, performs type conversion
=== identity operator, similar to ==, but no type conversion
The following explains what type conversion is.
"5" == "5" returns true
"5" == 5 returns true
"5" === "5" returns true
"5" === 5 returns false
somevar = 5 assigns and returns 5, which is a truthy value, and thus if (x = 5) { will always execute the conditional body. Similarly if (x = 0) { will never execute the conditional body, because it assigns and returns 0, which is falsy.

WHILE loop to begin only when ELSEIF condition is true

I'm new to lua, and am trying to write some code to send information to a text file that is overwritten each loop cycle.The info sent to the text file is different based on a value that increases to a specific known number (3 for this example) and then decreases below that number in any order.
Here's where I am stuck:
I want to send different info to the text file using the same values
but based on whether the value has reached 3 or not, if that makes
sense.
Example in English:
if value is 2 and has not reached 3 then write: UP 2
if value is 2 and has reached 3 then write: DOWN 2
if value is 3 then write: TOP
My Question:
Can I get an infinite while loop to start inside another infinite while loop when a value meets a specific elseif condition. I want to try and use the loop structure below if possible.
My Code looks somewhat like this:
while true do
file = assert(io.open("file.txt","w+"))
value = ipc.readSD(0x0BE8)
if value = 0
file:write("UP 0")
file:close()
elseif value = 1
file:write("UP 1")
file:close()
elseif value = 2
file:write("UP 2")
file:close()
elseif value = 3
file:write("TOP")
file:close()
--How do I trigger/start this inner loop?
while true do
--now stay in here until break
file = assert(io.open("file.txt","w+"))
value = ipc.readSD(0x0BE8)
if
value = 0
file:write("DOWN 0")
file:close()
break
elseif
value = 1
file:write("DOWN 1")
file:close()
elseif
value = 2
file:write("DOWN 2")
file:close()
else
break
else
file:write("OUTSIDE")
end
end
There are several syntax errors in your code, it shouldn't compile at all.
Use == to compare equal instead of =
For every if / elseif, there should always be a corresponding
then.
For every if / while / do block, there should always be a
corresponding end.
As for your question, if I understand it correctly, you can use goto to simulate continue when you don't want to enter the inner loop:
while true do
value = getValue()
if value == 1 then
--not enter inner loop
goto continue
elseif value == 2 then
--enter inner loop
end
while true do
--inner loop
end
::continue::
end