# Crossover Scan Results

#### JeffryBatista

##### Member
I hope everyone is having an amazing day. I have been trying to make a scan identify when the displacedEMA is greater than the VariableMA, on the 15min, EXT turned on. I already tried saving it as a study and then plugging it into the scan but it didn't work. It sounds so simple but when I tried to code it into the scan I keep getting no results back and cant figure out why. I will leave the 3 different codes I have been putting in the scan down below. I would greatly appreciate any help given.

-------------------Test 1--------------------
DisplacedEMA("length" = 20, "displace length" = -15) is greater than VariableMA("length" = 15)

------------------Test 2--------------------
#DISPLACEDEMA
input price = close;
input length = 20;
input DisplaceLength = -15;

def DisplacedEMA = ExpAverage(data = price[DisplaceLength], length = length);

#VMA
input vprice = close;
input vlength = 15;

def tmp1 = if vprice > vprice[1] then vprice - vprice[1] else 0;
def tmp2 = if vprice[1] > vprice then vprice[1] - vprice else 0;
def d2 = sum(tmp1, vlength);
def d4 = sum(tmp2, vlength);
def cond = d2 + d4 == 0;
def ad3 = if cond then 0 else (d2 - d4) / (d2 + d4) * 100;
def coeff = 2 / (vlength + 1) * AbsValue(ad3) / 100;
def asd = compoundValue("visible data" = coeff * vprice + (if IsNaN(asd[1]) then 0 else asd[1]) * (1 - coeff), "historical data" = vprice
);
def VMA = asd;

plot scan = DisplacedEMA is greater than VMA;

-------------------Test 3--------------------
CrossoverSTRAT() is true within 2 bars

I basically made the 2 codes above custom studies, and offset them 2 bars and still got no results back
Scan Photos

This is what I want the scanner to tell me. In this case when the displacedEMA (blue line) is greater than VariableEMA(purple line) add "AAPL" to the scan results

Last edited:
Solution
@JeffryBatista So, this is what I have. Hopefully I have all this right.

The whole problem revolves around using the -15 value in the DisplacedEMA(). That input uses future data (in this case, 15 bars of it) that has not yet passed in time. Why is this important? Since the 15 bars of price data do not yet exist, there is no way that the scanner can find them. Trying to scan for these crossovers is the same as trying to scan for other studies that use future data such as the built-in TTM_ScalperAlert study, which uses 3 bars of future data (and always shows those perfect and untradeable arrows 3 or so bars after price has already passed that point). Even if the scanner would be able to find these crossovers, they would first show...
@JeffryBatista Try changing the displacement value of the DisplacedEMA() to "15" instead of "-15".

Edit: Nevermind, I'm thinking in the wrong direction. My apologies.

Last edited:
@JeffryBatista Try changing the displacement value of the DisplacedEMA() to "15" instead of "-15".
@Pensar tried that. It would completely change the placement of the crossover which is no good.

Edit: All good. I been trying to make this scan for so long now. Its annoying me @Pensar

@Pensar tried that. It would completely change the placement of the crossover which is no good.
You're right. I'll see if I can help figure it out.

You're right. I'll see if I can help figure it out.
If you figure anything out I'll greatly appreciate it. I have been attempting to make this scan for over 3 months and no luck @Pensar

Last edited:
@JeffryBatista So, this is what I have. Hopefully I have all this right.

The whole problem revolves around using the -15 value in the DisplacedEMA(). That input uses future data (in this case, 15 bars of it) that has not yet passed in time. Why is this important? Since the 15 bars of price data do not yet exist, there is no way that the scanner can find them. Trying to scan for these crossovers is the same as trying to scan for other studies that use future data such as the built-in TTM_ScalperAlert study, which uses 3 bars of future data (and always shows those perfect and untradeable arrows 3 or so bars after price has already passed that point). Even if the scanner would be able to find these crossovers, they would first show on the scanner 15 price bars after the point of the crossover. If you watch the two moving averages on a chart during market hours, they will first cross 15 bars after their intersection point. By placing the DisplacedEMA() and a MovAvgExponential() on a chart with the same length settings, you can easily verify for yourself that the DisplacedEMA() study is delayed in plotting by 15 bars.

All that being said, the easiest way to scan for these crosses would be to offset both studies 15 bars forward (this eliminates the use of future data yet keeps the relationship of the two averages the same) and then scan for a crossover. I know that this is certainly not what you wanted; however, its the best that I have.
Since a DisplacedEMA() is simply a EMA offset back a certain number of bars, it can be changed to a regular EMA. Your scan would look like this: `ExpAverage(20) > VariableMA(15)[15]`.

In this screenshot, the cyan arrow points to the crossover and the magenta arrow points to where price is trading at the time of the crossover. The orange arrow points to the spot where the two adjusted averages cross. You can see that scanning for either method should provide the same results.

If you can disprove my conclusion, I'll be glad to know it. Try this and see how it goes for you.

@JeffryBatista So, this is what I have. Hopefully I have all this right.

The whole problem revolves around using the -15 value in the DisplacedEMA(). That input uses future data (in this case, 15 bars of it) that has not yet passed in time. Why is this important? Since the 15 bars of price data do not yet exist, there is no way that the scanner can find them. Trying to scan for these crossovers is the same as trying to scan for other studies that use future data such as the built-in TTM_ScalperAlert study, which uses 3 bars of future data (and always shows those perfect and untradeable arrows 3 or so bars after price has already passed that point). Even if the scanner would be able to find these crossovers, they would first show on the scanner 15 price bars after the point of the crossover. If you watch the two moving averages on a chart during market hours, they will first cross 15 bars after their intersection point. By placing the DisplacedEMA() and a MovAvgExponential() on a chart with the same length settings, you can easily verify for yourself that the DisplacedEMA() study is delayed in plotting by 15 bars.

All that being said, the easiest way to scan for these crosses would be to offset both studies 15 bars forward (this eliminates the use of future data yet keeps the relationship of the two averages the same) and then scan for a crossover. I know that this is certainly not what you wanted; however, its the best that I have.
Since a DisplacedEMA() is simply a EMA offset back a certain number of bars, it can be changed to a regular EMA. Your scan would look like this: `ExpAverage(20) > VariableMA(15)[15]`.

In this screenshot, the cyan arrow points to the crossover and the magenta arrow points to where price is trading at the time of the crossover. The orange arrow points to the spot where the two adjusted averages cross. You can see that scanning for either method should provide the same results.

If you can disprove my conclusion, I'll be glad to know it. Try this and see how it goes for you.
Very very interesting. Im defiantly going to try this out right now. whether it works or not I just want to thank you for all the time you have put into this. I know finding this solution must have taken a lot of thought and so I appreciate you so much for that @Pensar

87k+ Posts
282 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?