dleather25 39 posts msg #155964 - Ignore dleather25 |
2/26/2021 12:49:51 AM
Hello, this may be a shot in the dark but I'm trying to develop a ThinkScript filter because Stockfetcher is only limited to penny stocks that are in the United States, I was wondering if anyone has any idea how to code ThinkScript and if they could give me a couple syntax examples that would be enough to get me started figuring out how to develop my own filters. I hate asking for help, but I am so used to this syntax and have developed some pretty decent stock filters here just want access to all of the stocks instead of just the ones based in the US.
If by chance anyone knows the coding for TOS this would help me get started
->Accumulation distribution is above accumulation distribution 3 days ago
close is below 10
close is above close 5 days ago
->OBV is above OBV 3 days ago
volume is above 100,000
average volume is above 100,000
->MACD Fast Line is below MACD Fast Line 4 days ago
->MACD Fast Line is below 0 for the last 5 days
plot (draw for SF) ma(50)
I put an arrow by the ones that are most difficult to code, I've spent hours trying to code but it always shows up red or doesn't allow me to apply it. Any help is appreciated!!!
|
lis 37 posts msg #156068 - Ignore lis |
3/8/2021 5:46:56 PM
First, a few more sites with good information on thinkScript -
ThinkOrSwim Learning Center's Tech Indicators reference:
https://tlc.thinkorswim.com/center/reference/Tech-Indicators
ThinkOrSwim Learning Center's thinkScript reference (including tutorials):
https://tlc.thinkorswim.com/center/reference/thinkScript
Hahn-Tech, LLC - basically a bunch of stuff around ThinkOrSwim and creating scans, studies, and strategies, including a forum-like membership:
https://www.hahn-tech.com
Now, I'm assuming by "filter" you mean you're looking for what TOS calls a "scan". Based on that assumption, here's something to start with...
* Declare variables with `def` or `input`. `input` causes you to be able to edit those values in the scan's settings.
* You probably know that you remark out a line with the `#` character.
* You probably know that you need to end each line with a semicolon (`;`)
* Where I've given URLs, you can find the parameters, if any, for a given study.
Using the parameters, you can set the values of your choice, such as with the MACD code I have below.
If you use the parameters in order, you don't need to specify parameter labels; however, if you use only some of the parameters, or they're not in order, you need to specify. For example:
MACD(fastLength, slowLength, MACDLength)
-- OR --
MACD("fast length"=12, "average type"=AverageType.EXPONENTIAL)
* The following sample code times out on execution, probably because of the way I threw in the MACD part. I don't get an orange triangle, meaning that the script is "too complex" in TOS' opinion. It attempts to bring results, but then times out. It works if you remark out the MACD condition, which I've done, as it's not really going to do what you want anyway.
* The following sample code is using some default values. Again, check the Learning Center for the parameters for which you might want to set values.
# https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/A-B/AccumulationDistribution
def accumulationDistributionAbove3DaysAgo = AccumulationDistribution() > AccumulationDistribution()[2];
def closeIsBelow10 = close < 10;
def closeIsAboveClose5DaysAgo = close > close[4];
# https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/O-Q/OnBalanceVolume
def OBVisAboveOBV3DaysAgo = OnBalanceVolume() > OnBalanceVolume()[2];
def volumeIsAbove100k = volume > 100000;
def averageVolumeIsAbove100k = Average(volume) > 100000;
#####
THE FOLLOWING CODE IS VALID, BUT THE RESULTS AREN'T.
Unlike SF, TOS' MACD doesn't have the fast and slow lines broken out.
The HAHN-TECH site I've referenced has an example calculation using the various parts that make up the MACD.
#####
# https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/M-N/MACD
# https://www.hahn-tech.com/ans/finding-a-macd-crossover-below-the-zero-line/
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
def MACDFastLineIsBelowFastLine4DaysAgo =
MACD(fastLength, slowLength, MACDLength) < MACD(fastLength, slowLength, MACDLength)[3];
def MACDFastLineIsBelowZeroForTheLast5Days = MACD(fastLength, slowLength, MACDLength) > 0 and MACD(fastLength, slowLength, MACDLength)[1] > 0 and MACD(fastLength, slowLength, MACDLength)[2] > 0 and MACD(fastLength, slowLength, MACDLength)[3] > 0 and MACD(fastLength, slowLength, MACDLength)[4] > 0;
#####
# final result for scan ("filter") - but with MACD stuff remarked out
plot scan =
accumulationDistributionAbove3DaysAgo and
closeIsBelow10 and
closeIsAboveClose5DaysAgo and
OBVisAboveOBV3DaysAgo and
volumeIsAbove100k and
averageVolumeIsAbove100k; # and
#MACDFastLineIsBelowFastLine4DaysAgo and
#MACDFastLineIsBelowZeroForTheLast5Days;
|