LBRThreeTen Oscillator Zero Line Crosses


Category:
0
0

Hi Pete. I created a fairly simple script that is actually working and returning the result I had intended. But for future reference for someone working hard at learning how to thinkScript, I was wondering if you wouldn’t mind looking at my script and letting me know if there was an easier way to accomplish the same result. I’m feeling pretty confident that there is but I’m not yet advanced enough to figure it out on my own. I’m quite comfortable with the basics and even a few of the more advanced concepts, but beyond that I’m still a little confused. Anyway, the code is looking for the following 3 conditions using the built-in LBRThreeTen Oscillator. Thank you so much for all you do!

1). Slowline crosses above Zeroline

2). Fastline crosses below Zeroline within 12 bars of condition 1

3). On the 3rd bar of the Fastline being below the Zeroline, and the Slowline remaining above the Zeroline, the condition returns “true”

I hope that wasn’t too confusing.

File attached:

Attachments:
Marked as spam
Posted by (Questions: 5, Answers: 9)
Asked on July 27, 2020 11:27 pm
185 views
0
Private answer

"...an easier way to accomplish the same result...".

Not really. That is a pretty convoluted trail to follow. There is a "more elegant way". But the code is still extremely complex and difficult for the layman to understand.

My solution completes this in only three lines of code (more elegant but not less complex). I will only include the lines of code you would add to the original code provided by Thinkorswim:

def slowCrossAboveZero = SlowLine[1] < 0 and SlowLine > 0;
def fastLineCrossBelowZero = Highest(slowCrossAboveZero, 12) > 0 and FastLine[1] > 0 and FastLine < 0;
plot signal = fastLineCrossBelowZero[2] and Highest(FastLine > 0, 2) < 1 and Highest(SlowLine < 0, 2) < 1;

You can break that down into it's individual components to learn how each of those advanced techniques are working.

Edit: In the comments section below the request was made to include the opposite direction for this signal.

def slowCrossBelowZero = SlowLine[1] > 0 and SlowLine < 0;
def fastLineCrossAboveZero = Highest(slowCrossBelowZero, 12) > 0 and FastLine[1] < 0 and FastLine > 0;
plot signal = fastLineCrossAboveZero[2] and Highest(FastLine < 0, 2) < 1 and Highest(SlowLine > 0, 2) < 1;

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on July 28, 2020 8:52 am
0
Thank you Pete. This was the answer I was looking for. Your code is definitely more advanced than my limited knowledge in thinkScript, but I will spend the time necessary to analyze it as best I can and turn it into a good learning opportunity. Just one quick question if I may. Would there be any difference in the usage of computing resources with my script versus yours since mine contains more lines of code?
( at July 28, 2020 10:21 am)
0
That is tough to know for Thinkorswim. The platform does not give us any way to measure how long it takes to execute any particular section of code. However with my approach, you can replace the fixed values of 12 and 2 with user inputs so the code is much more flexible. Then you could change the behavior of the code through user inputs rather than having to change the code itself.
( at July 28, 2020 10:35 am)
0
Thank you for the quick reply. And yes I agree that your approach is much better, more streamlined, and allows for more flexibility. I've already made the changes to my script that you suggested in your original reply, and that's an excellent suggestion about creating user inputs for flexibility. I will definitely do that. I'm learning so much from you and all your resources, and I cannot thank you enough. As for my follow-up question, that was more for future reference and future scripts. Thanks again and good health to you sir.
( at July 28, 2020 11:37 am)
0
Hello again Pete. I feel like an idiot asking this because it should be simple based on the code you provided above, and its only 3 lines of code! Anyway Im creating a bearish version of the same code and I thought I made the appropriate changes, but its not generating any signals or error messages. I think it may be something to do with the ”highest” and ”lowest” functions which Im still not entirely clear on, but working on it. I played around with the code for a couple hours but Im still not getting any signals. I hope you wont mind looking at this and letting me know where Im going wrong. Im confident it will be quick and obvious for you. def slowCrossBelowZero = SlowLine[1] > 0 and SlowLine 0 and FastLine[1] 0; plot signal = fastLineCrossAboveZero[2] and Lowest(FastLine < 0, 2) 0, 2) < 1; Its basically just the opposite of the code you provided above......slow line crossing below zero and fast line crossing above zero.
( at August 3, 2020 11:19 pm)
0

There is nothing wrong with your attempt (accept that it is wrong). Most folks do not understand how I use the Highest() function to check if signals have been true or false over a given period of time. So I can hide one of my secret weapons in plain site and very few eyes will be able to understand how it works. Perhaps seeing the flip side of the signal will reveal it’s secret power? I have updated my answer to include the same signal in the opposite direction. I have not tested it. Let me know how it works.

( at August 4, 2020 8:21 am)
0
Thank you Pete. I think I had a light bulb moment. I was thinking “Highest” and “Lowest” had to do with the bars themselves, but unless I’m mistaken, you’re looking for the highest number of OCCURANCES and whether those occurrences returns a true or false value for the number of bars within the condition (> 0 or < 1). Am I correct? And yes your code does work as expected and returns the appropriate signals based on my stated conditions.
( at August 4, 2020 1:34 pm)
0
I think you understand it correctly. In Thinkorswim true conditions have two values (yes, 1). Likewise false conditions have two values (no, 0). So yes and 1 are used interchangeably and no and 0 are used interchangeably. This comes in very handy when you want to perform mathematical operations on true/false conditions. We can add them up or check for values above 0 or below 1. If the highest value of any true false condition within the last 12 bars is greater than zero then we can be certain the condition was true for at least one of those previous 12 bars. If the sum of the same 12 bars for the true condition is 3 then we know for certain the condition was true for 3 out of those 12 bars. Very powerful. So powerful in fact that I use this on other platforms even though they don't use a numerical equivalent for true/false. I simply write my statements so that true assigns a value of 1 and false assigns a value of 0. Then I can use the same techniques in other programming languages.
( at August 4, 2020 4:51 pm)
0
Thank you so much for the detailed explanation. That's basically what I was trying to say but you said it much better than I could. :) I do believe I have a pretty good grasp on the "Highest" and "Lowest" functions now, and how to use them, thanks to you. It's a great tool to add to my growing thinkScript toolbox. I actually took your 3 lines of code and converted it into 5 (broke part of it up and added a couple def statements) in order to accommodate some user inputs, and I still made good use of the "Highest" function. It also confirmed for me that I understand how to use that valuable tool now, and to my surprise, after making those changes, it tested out perfectly the very first time! You're a great teacher Pete. Thank you again.
( at August 4, 2020 6:52 pm)
0
Totally awesome. You just made my day. This is exactly why I do what I do. You are well on your way.
( at August 4, 2020 10:12 pm)