does anyone know how to quit / try the next condition in a conde, when a condition fails?
As example:
(run* q (conde
[(conditionA) (conditionB) (conditionC)]
[(conditionA) (conditionD) (conditionE)]
[(conditionF) (conditionB) (conditionG)]
))
If the first goal
[(conditionA) (conditionB) (conditionC)]
fails at condition B it should be stop immediately, because the goal
[(conditionF) (conditionB) (conditionG)]
is definitely true.
Thanks!
Related
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)
In my smart contract, I want to check whether a boolean value is true, if not the smart contract should abort or throw an error like
(begin
(require-true value)
...continue
)
How can I do that?
You can use unwrap-panic in a function, e.g.
(define-private (require-true (value bool))
(unwrap-panic (if value (some true) none))
)
asserts! will return true and continue the execution only if the boolean expression it evaluates is true. If not it will return the thrown-value and exit the current control flow.
Signature is: (asserts! boolean-expression thrown-value)
Example that will return true and continue execution:
(asserts! (is-eq 1 1) (err 1))
All I’m looking for is for a way to create a Lua if statement that needs to meet 4 conditions before activating, for example
If (x == condition1 and x == condition2 and x ~= condition3 and x ~= condition4) then
Return true
End
I’m beginning at Lua right now and just want to know if this will work or if there is another way! (If someone has already had his problem/question and has an answer please link me) Thanks
You can write this as you have, except that if, return, and end must not be capitalized in Lua, and the parenthesis are not needed in the if statement (though this is not a syntax error):
if x == condition1 and x == condition2 and x ~= condition3 and x ~= condition4 then
return true
end
But, if you want to return a boolean value, it would be cleaner to return the result of the logical operators directly, avoiding the if statement altogether:
return x == condition1 and x == condition2 and x ~= condition3 and x ~= condition4
I'm trying to refactor an if-else chain that doesn't look particularly good. My common sense is telling me that I should be able to call my method only once but I can't figure out an elegant way to do it. Currently what I have is:
if(condition1)
do method1;
else if(condition2)
do method1;
Which looks hideous. There's repeat code! The best I can come up with is:
if(condition1 || (!condition1 && condition2))
do method1;
But this also looks bad, since I'm negating condition1 after the or, which seems unnecessary...
I made a truth table for this:
c1| c2| r
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 1
And in case anyone is interested, the real-life problem I'm having is that I got 2 intances of Fancytree in javascript and I want to set some rules to transferring nodes between them. Tree A can only transfer lone nodes to Tree B, while Tree B can reorder itself freely, so I put this on Tree B's dragDrop event:
if(data.otherNode.tree === node.tree){
data.otherNode.moveTo(node, data.hitMode);
}
else if(!data.otherNode.hasChildren()){
data.otherNode.moveTo(node, data.hitMode);
}
You can simplify even more - if the first condition is true, the method should be invoked regardless of the second condition. So the !condition1 in your refactored code is redundant. Instead, you could just have:
if(condition1 || condition2)
do method1;
In modern programming languages the if condition will even short circuit. This means that when the first condition is evaluated as true, the second condition won't even get evaluated.
What you suggested,
if(condition1 || (!condition1 && condition2))
do method1;
Is logically the same as
if(condition1 || condition2)
do method1;
So I think that is your best answer.
It's not logically the same as your truth table though, so either your truth table or your current code is wrong, if truth table r is meant to do
do method1;
In writing
if (condition1 || condition2) {
//code1
}
if condition1 is correct code1 is executed and if condition1 is incorrect then only condition2 is checked and code proceeds accordingly. Hence it would be same as
if ( condition1 ) {
//method1
} else if ( condition2 ) {
//method1
}
I'm trying to found a better way of handle exceptions in clojure, I'm using https://github.com/alaisi/postgres.async which thrown an exeption when fail, but I would prefer return false (because I'm using a validator) or even better something like a Either monad (or something more simple like this http://adambard.com/blog/acceptable-error-handling-in-clojure/ )
1) I'm trying to catch the exception and if exist return false, but the code doesnt works (doesnt return false and thrown the exception).
(try
(dosql [tx (<begin! db)
ucount (<execute! tx ["UPDATE groups SET garticlescounter=garticlescounter + 1 WHERE gid=$1"
groupid])
uartc (<execute! tx ["UPDATE subtopics SET starticlescount=starticlescount + 1 WHERE stid=$1"
sid])
uartins (<insert! tx
{:table "articles"}
{:aurl url :atitle title :asuttopicid sid :acommentcount 0 :alikescount 0 :auid uid})
ok? (<commit! tx)]
ok?)
(catch Exception _ false))
2)would be possible wrap in a way where if works return ok? and if doesnt works return false, or maybe [ok? nil] and [nil error] maybe a macro?
----thanks to swinn I did this
;must receive an optional parameter for error response or
; build a [nil ok] response but just know this works for me...
(defmacro async-try-block! [block]
`(let [chn# (!/chan 1)]
(!/go
(let [response# (try
~block
(catch Throwable e#))]
(if (instance? Throwable response#) (!/put! chn# false) (!/put! chn# response#))))
chn#))
(async-try-block!
(dosql [tx (<begin! db)
ucount (<execute! tx ["UPDATE groups SET garticlescounter=garticlescounter + 1 WHERE gid=$1"
groupid])
uartc (<execute! tx ["UPDATE subtopics SET starticlescount=starticlescount + 1 WHERE stid=$1"
sid])
uartins (<insert! tx
{:table "articles"}
{:aurl url :atitle title :asuttopicid sid :acommentcount 0 :alikescount 0 :auid uid})
ok? (<commit! tx)]
ok?))
I'm not familiar with the postgres.async library, butException is not the root of all Exceptions in the JVM, Throwable is.
Changing your catch to Throwable would be the first change I would suggest, but it seems that (<execute! ...) actually catches the exception for you and returns it, so you need to check the return value using (instance? Throwable)