Using custom study to generate scan


Category:
0
0

Hi Pete,

I am posting this question again, hopefully this time with additional clarity. I have also attached my .ts file. I will admit that I am a novice at using ToS and programming in general, so regardless of the usefulness of this study/strategy, it has primarily served as a way for me to practice using thinkscript.

I have created a study that is a sort of combination of a pattern, study, and strategy all in one. I hope for it to become useful in the case of a double bottom, that visually would appear as two consecutive right triangles (you can get an idea of what this looks like if you run the study on DCIX looking at a 1Y:1D chart, image attached below). As of now the strategy does not perform exactly as I would like it too, but this is my first rough draft of the code.

I should note that I choose to create my own way of tracking profit/loss because I wanted to be able to execute multiple buy or sell orders in series and was unable to figure out how to do this using the ToS built in strategy methods.

Anyway the issue I am having with this study is at the end of it; I wanted to be able to execute scanning criteria within this study rather than within the stock hacker (mostly for organization, I only wanted to have to edit my code in one file). The output of the scanning criteria would declare a boolean variable that could then be referenced in a scan (searching only for stocks that output a true condition or the inverse). Below is a snippet of my code for quick reference, as well as the custom study filter I am using to call this file. If you have any other questions regarding my code, please ask.

 

------------------Study Code-----------------
def criteria1 = average(volume,50) > 5000;
def criteria2 = close >= .10 and close <= 30;

# criteria3 was used to find stocks that had many "events" as I called them;
# it does not serve a purpose, was just a test to see if scanning was possible
def criteria3 = sum(condition_final,253) >= 5;
def scan1 = criteria1 and criteria2 and criteria3;

# criteria4 is trying to scan for stocks that had a negative profit/loss to improve my code,
# this still does not fix the scan if I just look at the p_l for the current day (p_l < 0)
def criteria4 = lowest(p_l,253) < 0;
def scan2 = criteria1 and criteria2 and criteria4;
--------------------Scan Code----------------------
AA_BF_NC()."scan2" is equal to 1 within 1 bars 
# this works for scanning when I change it to "scan1" but yeilds no results if I put
# "scan2" even though there are obviously are stocks that meet this criteria
# negative p_l can be seen on the stock VTVT (1Y:1D), it also meets criteria1 and criteria2
Attachments:
RESOLVED
Marked as spam
Posted by (Questions: 2, Answers: 1)
Asked on June 1, 2018 10:22 am
1595 views
0

I seemed to have forgot to specify my question. When scanning using this method I am shown no results (i.e. No matching symbols) even though there should be. I don’t believe this is too computationally demanding, but I can’t find any other reason why it would not show any results. Is there a flaw in my code or method?

( at June 1, 2018 10:31 am)
0
Private answer

Something broke when you tried to post your code. Some of the symbols were converted to HTML character references. So your code cannot be copied and pasted for testing. Even if this were not the case, the code you provided is incomplete. You are missing declarations and value assignments for condition_final and p_l.

First item to note. Referencing custom studies in a Study Filter has been shown NOT to work. Perhaps the developers at Thinkorswim have updated their platform to support this. But I thought I would mention that as it’s not so obvious. Always best to use the full code for a Study Filter.

Second, even if you could reference a custom study. The only items you can reference are plot statements. Since scan1 and scan2 are def and not plot, neither of these should work.

In summary, I really can’t do a thing to help you get this working because I don’t have the full code. I understand you may have reasons to prevent this code from being published in an open forum. In that case you may consider our premium services: https://www.hahn-tech.com/about/

 

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on June 1, 2018 11:26 am
0

I attached my .ts file to the question above. On my screen the link is shown directly above the picture I posted. That file has my full code. I am perfectly comfortable with sharing the code as I stated, this is just to learn.

( at June 1, 2018 1:11 pm)
0

Thanks. I missed that link. So did you catch the part about referencing custom studies? Did you try running the scan using the full code?

If your study is designed to run on daily charts, why do you specify a secondary aggregation period here:

def agg = AggregationPeriod.DAY;
def best_change = ((Highest(high(period = agg), 2) / close(period = agg)[2]) – 1) * 100;

( at June 1, 2018 1:50 pm)
0

The second aggregation period was something I added while making the code before learning about ‘declare hide_on_intraday’. Corrrect me if I am wrong, but it should not cause any issues it’s just a redundancy.

I read what you said about referencing custom studies, but it does seem to work, except for when trying to reference ‘scan2’. I believe the developers allowed for this kind of referencing, since when I reference the code in the custom study filter I stated above using:
AA_BF_NC(),”scan1″ is equal to 1 within 1 bars
I receive a message in a pop up window that states, “This filter is linked to the custom thinkScript:[AA_BF_NC]. If you make changes to any linked script those changes WILL NOT be reflected in this filter.” Each time I change how the ‘scan1’ value is determined, and then re-scan I get different results, which makes me believe that it is referencing correctly.

I think the error with referencing the profit/loss is that the value changes depending how much historical data is displayed on the chart, and since you can change the chart type/time then the scan may not have a specified finite time scale to look at, do you think that could be the issue?

( at June 1, 2018 2:06 pm)
0

Please don’t argue the point. Try the scan with full code before we go any further. It is not possible to reference a def statement from any study. Let alone a custom study. When referencing a study you can only reach plot statements. So the fact that you think you are getting results with one but not the other tells me the results are not correct for the one that you think is working.

Please stop resisting this and presenting arguments of why it should work. The first step to troubleshoot this is to use the full code.

Last note. Secondary aggregation periods are not permitted in Study Filters. So take that out before proceeding with the test of your full code.

( at June 1, 2018 2:17 pm)
0

I removed the secondary aggregation period from the code. Declared both scan1 and scan2 as plot statements instead of def statements. When scanning I am using a custom study filter with a single line of code to reference either scan1 or scan2 (of the form, AA_BF_NC().”scan1″ is equal to 1 within 1 bars). Is this the proper method to call the study for scanning purposes?

( at June 1, 2018 2:34 pm)
0

I am just about done here. I told you to use the full code. Why is that so impossible for you to accept?

You also mentioned the amount of historical data on chart compared to scan. Thinkorswim claims the daily time frame used in a scan contains two years of historical data. A while back that broke. Not sure if they fixed it. But it got cut down to only one year.

( at June 1, 2018 3:00 pm)
0

Here you go. Previous post in which we determined it was NOT possible to reference a custom study plot. https://www.hahn-tech.com/ans/is-there-a-way-of-referencing-another-user-defined-study-in-a-user-defined-study/

I have no idea why on earth you will not believe me. If you don’t trust me then why bother asking me for assistance?

( at June 1, 2018 3:10 pm)
0

I am not intentionally ignoring your comments and I am very appreciative of you taking your time to help me. When you say to use the full code, do you mean to copy my study into the custom study filter within the stock hacker?

( at June 1, 2018 3:19 pm)
0

I copied my study into the custom study filter within the stock hacker and I seem to get correct results. Sorry it took me so long to figure out what you were saying, I understand how frustrating that must have been. I meant no disrespect. Thank you for taking the time to correct my misconceptions and help me understand how to use ToS better.

( at June 1, 2018 3:53 pm)
0

Whew! So glad we persevered until we reached a solution. I love to teach and tend be a bit stubborn. So where others may give up, I tend to double down until we reach a breakthrough. Congratulations on getting the code to work. I’m sure you learned a lot. And others will as well. Don’t go to far away. I’m looking forward to more posts from you as you continue learning.

( at June 1, 2018 6:52 pm)