Scanning for Historical and Present Day Stocks


Category:
0
0

The following code creates a Green or Red arrow when the price moves above the SMA AND when the MACD crosses above zero.

I want to create a scan to scan for present day stocks and historical stocks. Is it possible to run this in the scanner and scan back say 6 months or a year ago and find companies that
meet this criteria on a given day in history and then I can click ahead in time to see how they performed based on the arrows?

Attachments:
Marked as spam
Posted by (Questions: 1, Answers: 0)
Asked on December 4, 2020 10:32 am
124 views
0
Private answer

Before you can use this as a scan you need to make some changes. The code you provided does not function as a scan at all so we cannot apply any modifications to solve what you requested until we first convert the code to work as a scan.

input size = 100;
input price = close;
input length = 10;
def sma = Average(price, length);
def smaUp = price >= sma;
def smaDown = price <= sma; #MACD input fastLength = 8; input slowLength = 17; input macdLength = 9; input averageType = AverageType.EXPONENTIAL; def value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength); def average = MovingAverage(averageType, value, macdLength); def diff = value - average; def zeroLine = 0; def upSignal = diff > zeroLine;
def downSignal = diff < zeroLine;
def status = if upSignal and smaUp then 1 else if downSignal and smaDown then 2 else status[1];
def bullish = upSignal and smaUp and status[1] == 2;
def bearish = downSignal and smaDown and status[1] == 1;
# use this to scan for bullish signals
plot scan = bullish;
# use this to scan for bearish signals
#plot scan = bearish;

Now that your code has been converted to work as a scan we can now show you how to adjust the scan signals to look back and find signals that were valid at some point in the past.

For each of the "plot scan" statements you simple add an index "[#]" to the end of the variable name. The "#" inside the brackets forces the code to look backwards that number of bars. So if you use [1] the code will be looking back one bar in the past. Change that to [10] and the scan will be looking back 10 bars in the past.

Here is what that looks like:

plot scan = bullish[10];

plot scan = bearish[10];

That's all there is to it.

Marked as spam
Posted by (Questions: 37, Answers: 4084)
Answered on December 4, 2020 5:02 pm