Hi all, I'm trying code a simple scanner for stocks in wich the 50 SMA is above the 200 SMA over the last 30 bars.
This is my code. It works for most of the stocks but for some reason I find a stock that doesn't match the criteria and has been selected by the scanner:
Any suggestion or can anybody find the bug?
Thank you!
Ed
This is my code. It works for most of the stocks but for some reason I find a stock that doesn't match the criteria and has been selected by the scanner:
Any suggestion or can anybody find the bug?
Thank you!
Ed
Code:
# Scan for 50 SMA above 200 SMA for 30 bars
# Define the length of the SMA
input length_50_SMA = 50;
input length_200_SMA = 200;
# Define the length of the lookback period
input lookback = 30;
# Calculate the SMA
def sma_50_Value = SimpleMovingAvg(close, length_50_SMA);
def sma_200_Value = SimpleMovingAvg(close, length_200_SMA);
# Check if the sma_50_Value is above the sma_200_Value for the specified lookback period
def isAbove = sma_50_Value > sma_200_Value;
# Count the number of bars where isAbove was true
def countIsAbove = if isAbove then 1 else 0;
def barsAbove = sum(countIsAbove, lookback);
# Create a custom scan
plot scan = barsAbove == lookback;