Optuma Forums › Optuma Scripting › Scan For 50 sma higher than 150 sma but result are wrong › Reply To: Scan For 50 sma higher than 150 sma but result are wrong
- Topics: 79
- Replies: 252
- Posts: 331
I’ve been trying to post this response for some time but the Forum keeps rejecting it for some reason the Optuma Guru’s have not identified, so here I go trying again at their request.
I must admit that I really cannot work out what you are trying to do. You say you want to find stocks when the 50 sma is greater than the 150 sma, but then I find your script has ema20, ema40, sma50, sma150, ma50 and ma100. You do not describe what all the other moving averages are supposed to be doing.
Your script has numerous “AND”, “OR”, “>” and “<” operators, in many cases it being unclear in which sequence they should be evaluated. As written they will be evaluated sequentially.
What I mean is Var1 > Var2 Or Var3 < Var4 And Var5 > Var6 can result in a confused and unexpected result as the script will attempt to evaluate itself in a simple left to right sequence. To mark sure the sequence is evaluated correctly it needs to be written as (Var1 > Var2) Or (Var3 < Var4) And (Var5 > Var6). This clear definition of the groups of comparison and evaluations becomes even more critical as the scripts become more complex.
I’ve taken your script and split it into individual variables and shown some different bracketing arrangement that will produce very different results. As I can’t work out what you are trying to achieve with your complete script this will most probably not give you the results you are seeking.
ema20 = MA(BARS=20, STYLE=Exponential,CALC=Close);
ema40 = MA(BARS=40, STYLE=Exponential,CALC=Close);
ema50 = MA(BARS=50, STYLE=Simple, CALC=Close);
ema150 = MA(BARS=150, STYLE=Simple, CALC=Close) );
var1 = ( close() > MA(BARS=50,CALC=Close);
var2 = ( low() < MA(BARS=50, CALC=Close);
var3 = low()[-1] < MA(BARS=50, CALC=Close)[-1];
var4 = low()[-2] < MA(BARS=50, CALC=Close)[-2];
var5 = low()[-3] < MA(BARS=50, CALC=Close)[-3]));
var6 = ( close() > MA(BARS=100,CALC=Close);
var7 = ( low() < MA(BARS=100, CALC=Close);
var8 = low()[-1] < MA(BARS=100, CALC=Close)[-1];
var9 = low()[-2] < MA(BARS=100, CALC=Close)[-2];
var10 = low()[-3] < MA(BARS=100, CALC=Close)[-3]));
// Original evaluation sequence
Result1 = (ema20 > ema40) and (ema50 > ema150) and var11 and var2 or var3 or var4 or var5 or var6 and var7 or var8 or var9 or var10;
// Alternative evaluation sequence 1 - same operators but bracketing changes evaluation sequence, hence result
Result2 = (ema20 > ema40) and (ema50 > ema150) and var11 and (var2 or var3 or var4 or var5 or var6) and (var7 or var8 or var9 or var10);
//Alternative evaluation sequence 2 - again same operators but bracketing changes evaluation sequence, hence result
Result3 = (ema20 > ema40) and (ema50 > ema150) and (var11 and (var2 or var3)) or var4 or (var5 or (var6 and var7)) or (var8 or var9 or var10);
A few other comments:
· It is well worth splitting your script into multiple simple evaluations as I’ve done above, and then combine the variables. Doing this allows you to easily comment out (// at the start of the line) to test the script step by step. It also makes bracketing the various sub-scripts easier and clearer.
· I find it advisable to define all the properties of functions so you can be sure you have them the way you desire. (Many of your MA functions are relying in the default setting, whatever that may be.)
· Function like CLOSE() and LOW() require the round brackets.
· It’s simpler to put the bar offset for OPEN(), HIGH(), LOW() and CLOSE() between the round brackets, rather than appending the square bracketed offset, eg CLOSE(-1) is the same as CLOSE()[-1].
· I must admit I don’t like the use of variable names like var1, var2 etc. I prefer meaningful, even though longer, variable names that have some meaning, eg the ema50 and ema150 which immediately can be understood as exponential moving average 50 or 150. It makes working out what you were trying to do when you go back to your script after a break of a few days or more. Have a shot at guessing at ClGtMA50 and LoLtMA50 – they’d be typical variable names I’d use in the above script.
The Auld Tyma at