February 9, 2017 at 8:07 am #28711
Here’s an example of using the IF() function to rank a stock according to a technical indicator value. For those of you familiar with Excel, the IF statement is based on the same principle as it introduces decision-making in to the formula. If the condition is true then one value is given, or if it is false then another is given. The syntax used is IF(formula, true value, false value).
For example, IF (CLOSE()> MA(BARS=50),CLOSE(), MA(BARS=50)) will return the closing price if it is higher than the 50 day moving average (i.e. the true condition), or the moving average if it is lower (i.e. the condition is false).
Note that if the true and false values are not specified (i.e just the IF(CLOSE() > MA(BARS=50)) then it turns in to a true/false Boolean condition, with a value of 1 given to a true result, and zero for false. To have a green True or red False result in a column rather than a value simply use CLOSE() > MA(BARS=50) without using the IF function.
In this example the following values will be assigned to each symbol based on whether two moving averages are sloping up or not:
1 = 13 Exponential MA and 50EMA both sloping up (condition 1 is true, and condition 2 is true)
2 = 13EMA sloping up and 50EMA sloping down (true, false)
3 = 13EMA sloping down and 50EMA sloping up (false, true)
4 = 13EMA and 50EMA both sloping down (false, false)
This can be expressed in the Optuma Scripting Language as follows:123456MA1 = MA(BARS=13, STYLE=Exponential, CALC=Close);MA2 = MA(BARS=50, STYLE=Exponential, CALC=Close);IF(MA1 IsUp, IF(MA2 IsUp, 1, 2), IF(MA2 IsUp, 3, 4))
This states that if the variable MA1 (the 13EMA) is sloping up (i.e. true) it will then look at variable MA2 (50EMA) and if that is also true a value of 1 will be assigned, but if MA2 is down then it will be assigned a 2 (false).
If the initial MA1 IsUp condition is false (ie it’s sloping down) then it will look to the second nested IF statement to determine whether MA2 IsUp is true (3) or false (4).
When added to a custom column in a Watchlist the values will be calculated for each symbol – as verified by the 13EMA Up? and 50EMA Up? columns in the attached image.
For clients with a Professional or Enterprise Services subscription these values can then be grouped, and summary counts / averages applied to them. In this example of the S&P400 Midcap Index 162 members have a MA Rank value of 1, 10 with a 2, 94 with a 3, and 134 where both EMAs are sloping down:
March 12, 2019 at 8:23 pm #51587
- This topic was modified 5 years ago by Darren.
Using the IF statement above to calculated a ranking value of the EMA slopes in a watchlist, it’s possible to colour the watchlist label based on the value colour the watchlist label based on the value:January 12, 2022 at 11:13 am #67213Louis
- Topics: 40
- Replies: 69
- Posts: 109
Hi. I’d like to build on the idea of ranking using IF statements in a watchlist, if possible.
I have 2 IF statements. They both work. Each IF statement assigns a number if true and a different number if false. I want to use them together to save space on my watchlist. Below I am pasting 2 scripts, but in reality the IF statements are in different scripts/columns:12345678910111213//Define the 4 possibilitiesV1 = Close() >= HIGHESTHIGH(CLOSE(), BARS=21) ;V2 = Close() >= HIGHESTHIGH(CLOSE(), BARS=63) ;V3 = Close() >= HIGHESTHIGH(CLOSE(), BARS=252) ;V4 = Close() >= HIGHESTHIGH(CLOSE(), BARS=21, RANGE=All Time) ;//Script 1 works perfectly to pick up v1 and v2***If(v1, if(v2, 2, 1))//Script 2 works perfectly to pick up v3 and v4**If(v3, if(v4, 4, 3))
The idea is to have 1 column in the watchlist instead of 2 columns. I want to see 4 if it’s an all time closing high, 3 if its a 252 bar closing high, 2 if it’s a 63 bar closing high, and 1 if it’s a 21 bar closing high. I have tried many, may things and cannot seem to crack the code. Please let me know if I can accomplish this.
LouisJanuary 12, 2022 at 11:59 pm #67241
Hi Louis you need to nest all the IF statements together, as in my example in the first post.
However, because your ranking system isn’t mutually exclusive (i.e. if v4 is true then all the others must be true as well) then you have to start with V4 first and work backwards.123IF(V4,4, IF(v3, 3, IF(V2,2, IF(V1,1,0))))
January 13, 2022 at 3:25 am #67249Louis
- This reply was modified 1 year ago by Darren.
- Topics: 40
- Replies: 69
- Posts: 109
Thank you, Darren. This worked perfectly!
- You must be logged in to reply to this topic.