How to convert a scan to a chart study


Category:
0
0

Hey Pete,

I found an old scan you had made that would alert when a price crossed a previous day’s close (shown below). Is there a simple way to add a paintingStrategy to also mark an arrow on the chart when this occurs? Thanks for all your hard work!

input alertPeriodStart = 0930;
input alertPeriodEnd = 1600;
input numberOfDays = 0;
input numberOfYears = 0;
def startCounter = SecondsFromTime(alertPeriodStart);
def endCounter = SecondsTillTime(alertPeriodEnd);
def alertPeriod = if startCounter >= 0 and endCounter >= 0 then 1 else 0;
rec previousDayClose = if GetDay() <> GetDay()[1] then close[1] else previousDayClose[1];
def crossAbovePrevClose = close[1] < previousDayClose[1] and close > previousDayClose;
rec hasCrossed = if GetDay() <> GetDay()[1] and high < previousDayClose then no else if crossAbovePrevClose then yes else hasCrossed[1];
def scan = alertPeriod and !hasCrossed[1] and hasCrossed;
plot test = Highest(scan, 20) > 0;

 

 

Marked as spam
Posted by (Questions: 1, Answers: 0)
Asked on August 19, 2021 7:52 pm
125 views
0
Private answer

First order of business. Whenever you reference another resource online you really must provide a link to that resource. Providing the code is completely useless unless the viewers can see that code within the original context of which it was created. So here is the link to the post where that code was provided:

https://www.hahn-tech.com/ans/stocks-crossing-above-previous-day-close-after-1030/

Notice I have updated the title of your question and also moved this out of the Stock Scanners topic and into the Chart Studies topic. The basic premise of your request is how to convert code that functions as a scan into a form that displays as a chart study. More to the point, you wanted to see how to convert the signal from the scan to display as an arrow on the upper price graph.

We do not need to use any of the code you included in your question to demonstrate how to do this. I will use my own generic example so the solution can be understood and applied in whatever fashion the rest of our viewers desire.

Here is a generic scan. I have selected to build a moving average crossover study. The example is kept as simple as possible to allow viewers to focus on the most important details and not get confused by all the other elements. Three lines of code, the first two define simple moving average of 8 and 21 periods. The third line of code is the scan statement that evaluates to true when the fast sma crosses above the slow sma:

def maOne = Average(close, 8);
def maTwo = Average(close, 21);
plot scan = maOne crosses above maTwo;

Now I will show how that gets converted to a chart study:

plot maOne = Average(close, 8);
plot maTwo = Average(close, 21);
plot scan = maOne crosses above maTwo;
scan.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
scan.SetDefaultColor(Color.CYAN);

What changes did I make?

A. I changed the first two line from def to plot so that we can see the moving averages on the chart.

B. I then added two new lines of code. One that sets the plot style to display as an upward facing arrow.

Details here:

https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/SetPaintingStrategy

And I added another that sets the default color of the scan plot.

Details here:

https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/SetDefaultColor

Screenshot below shows the result.

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on August 20, 2021 8:27 am