# Percent Distance Between EMAs or Any 2 Plots For ThinkOrSwim

#### MerryDay

Staff member
Staff
VIP
Scan For Price Within A Percentage Of Moving Average
1. Where price is below 100 ma
2. Where price is rising (hl2 is greater than hl2 from 1 bars ago)
3. Where price is within -2% of the 100 ma
The scan below utilizes @markos script from post #8
Ruby:
``````# MA Distance Percentage
# Paris
# 4.13.2018
# Computes percentage the current close is above/below the 200 DSMA
# Displays on a lower chart

declare lower;

input length = 200;

def data = close;
def avg = Average(data, length);
def pct = (data/avg) - 1;

plot dist = pct;
plot zero = 0;

zero.SetDefaultColor(Color.WHITE);
zero.SetLineWeight(2);
AddLabel(1, "Percentage from " + length + " MA: " + AsPercent(pct), if pct > 0 then Color.GREEN else Color.PINK);``````
Copy the above code
In Studies, click Create
Paste the above study
Name the study: Pct_From_MA
Save
Click on the scanner.

Where price is below ma
1. Click on +Add filter
2. Click on the pencil icon next to the filter you just added
3. Click edit
4. In the left column, click on the 1st pull-down window, click study
5. Type in Pct_From_MA
6. Under Plot, click on the pull-down window, choose dist
7. In length change the 200 ma length to 100 ma length .... (or whatever moving average length that you want)
8. In the middle column, choose Less than or equal to .... (if scanning for above ma change this to Greater than)
9. In the right column, click on the pull-down window, click value
10. Change 100 in the next box to 0
11. Save

Where price is rising (hl2 is greater than hl2 from 1 bars ago)
1. Click on +Add filter
2. Click on the pencil icon next to the filter you just added
3. Click edit
4. In the left column, click on the 1st pull-down window, click price
5. Click hl2 .... (or what ever representation of price that floats your boat)
6. In the middle column, choose greater than or equal to
7. In the right column, click on the pull-down window, click price
8. Click hl2 .... (or what ever representation of price that floats your boat)
9. In the box under hl2, change the 0 to 1 offset
10. Save

Where price is within -2% of the ma
1. Click on +Add filter
2. Click on the pencil icon next to the filter you just added
3. Click edit
4. In the left column, click on the 1st pull-down window, click study
5. Type in Pct_From_MA
6. Under Plot, click on the pull-down window, choose dist
7. In length change the 200 ma length to 100 ma length .... (or whatever moving average length that you want)
8. In the middle column, choose greater than or equal to .... (or Less than if scanning for above ma)
9. In the right column, click on the pull-down window, click value
10. In the box under value, change the value from 100 to -0.02 .... (negative values represent under ma; positive values for above ma)
11. Save .... (change the percentage to anything you want)

Remember: Change the aggregation of each filter to what you want Here is a Shared Link: http://tos.mx/kXoe4Db
The shared link will only work if you copy and pasted the above study and named it: Pct_From_MA
A poster was having difficulty scanning for within a percentage of ma so I wrote this brief tutorial. I don't use moving average studies so I can not assist w/ how people are utilizing this in their strategies.

Last edited:

##### Well-known member
Does anybody have code for a scan that can filter by a stocks distance from a MA? For Example .5% away from 200 SMA (simple moving average).

Last edited by a moderator:

#### mc01439

##### Well-known member
2019 Donor
VIP
@YungTraderFromMontana P is greater than .50 would be the scan.

Code:
``````#start code
# define the percent difference of close - 200SMA

def a = Average(close, 200);
def d = close - a;
plot p = (d / close) * 100;

#end code``````

#### klepa

##### New member
Hi,

Trying to find if the current price(close) is more than X% than lowest green candle's close price, during the reg session (or at least within last X bars). Is there better way to code this?

Code:
``````plot digg =
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1)
# and so on...
;

diff.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);``````

Last edited:

#### klepa

##### New member
Figured out myself, if anyone is interested, here's the solution:

Code:
``plot l = if SecondsFromTime(0930) > 0 then (Lowest(if close > open then close else Highest(high, 10), 10)) else double.nan;``

#### diazlaz

##### Well-known member
2019 Donor
VIP
@klepa - tried something - how would you use/trade this? Ruby:
``````#Find percent distance between lowest green candle and current price?
#Request by @klepa
#
#2019.12.07 - diazlaz - initial port/Interpretation
input lookbackPeriod = 100; #1 to 256 bars
input showColorBars = yes;

def digg =
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 1 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 2 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 3 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 4 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 5 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 6 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 7 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 8 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 9 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 10 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 11 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 12 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 13 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 14 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 15 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 16 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 17 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 18 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 19 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 20 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 21 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 22 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 23 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 24 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 25 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 26 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 27 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 28 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 29 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 30 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 31 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 32 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 33 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 34 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 35 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 36 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 37 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 38 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 39 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 40 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 41 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 42 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 43 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 44 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 45 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 46 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 47 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 48 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 49 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 50 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 51 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 52 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 53 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 54 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 55 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 56 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 57 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 58 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 59 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 60 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 61 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 62 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 63 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 64 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 65 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 66 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 67 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 68 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 69 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 70 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 71 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 72 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 73 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 74 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 75 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 76 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 77 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 78 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 79 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 80 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 81 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 82 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 83 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 84 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 85 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 86 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 87 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 88 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 89 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 90 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 91 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 92 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 93 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 94 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 95 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 96 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 97 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 98 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 99 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 100 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 101 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 102 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 103 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 104 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 105 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 106 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 107 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 108 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 109 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 110 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 111 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 112 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 113 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 114 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 115 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 116 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 117 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 118 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 119 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 120 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 121 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 122 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 123 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 124 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 125 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 126 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 127 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 128 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 129 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 130 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 131 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 132 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 133 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 134 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 135 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 136 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 137 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 138 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 139 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 140 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 141 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 142 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 143 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 144 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 145 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 146 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 147 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 148 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 149 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 150 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 151 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 152 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 153 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 154 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 155 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 156 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 157 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 158 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 159 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 160 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 161 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 162 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 163 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 164 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 165 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 166 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 167 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 168 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 169 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 170 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 171 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 172 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 173 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 174 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 175 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 176 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 177 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 178 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 179 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 180 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 181 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 182 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 183 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 184 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 185 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 186 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 187 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 188 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 189 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 190 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 191 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 192 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 193 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 194 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 195 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 196 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 197 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 198 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 199 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 200 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 201 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 202 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 203 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 204 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 205 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 206 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 207 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 208 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 209 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 210 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 211 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 212 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 213 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 214 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 215 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 216 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 217 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 218 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 219 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 220 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 221 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 222 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 223 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 224 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 225 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 226 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 227 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 228 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 229 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 230 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 231 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 232 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 233 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 234 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 235 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 236 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 237 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 238 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 239 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 240 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 241 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 242 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 243 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 244 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 245 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 246 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 247 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 248 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 249 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 250 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 251 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 252 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 253 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 254 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 255 <= lookbackPeriod or
(close > open and SecondsFromTime(0930) > 0 and (close/close-1)*100>1) and 256 <= lookbackPeriod;

def sClose = if digg then close else sClose;
AddLabel(1,"Last: " + AsDollars(sClose));

#COLORBARS
AssignPriceColor(if showColorBars then
if digg then Color.WHITE
else Color.GRAY
else
COLOR.CURRENT
);``````

#### klepa

##### New member
@diazlaz it's just part of the study I'm working on.

#### veerasareddy

##### New member
@BenTen Could you please help to compile this code for me. Scan for stocks whose price is X% above or below MA.

#### tomsk

##### Well-known member
VIP
Here is a scan for stocks with a close that is 10% above 50 day EMA. I just ran that scan query on the S&P 500 and found 2 results on a daily agg.

Code:
``close crosses above ExpAverage(close, 50) * 1.1``

#### Jimmy

##### New member
Looking to put together a watch column/chart label that shows the following:
1) distance from the 9 sma both in percentage and actual price difference
2) distance from VWAP both in percentage and actual price difference.
Then label with percentage and actual price difference with the following conditions.
If price is above both the 9sma and VWAP label is green.
If price is below the 9 but above VWAP label is yellow.
If price is below VWAP but above the 9 label is orange
If price is below both the 9sma and VWAP label is red.
Would like to use on multiple time frames.
Example... daily, hourly, 10 min.

I can make individual labels showing percentage away from the 9 and VWAP but don't know the input for actual price or how to combine all the info into one label.
Thanks in advance for any insight,
Jimmy

#### dj45

##### Member
I'm guessing someone has built this wheel before. Basically looking to plot the distance in % that close price is from a moving average. Since this plot will essentially oscillate, plotting horizontal lines to mark x% above and x% below makes it easy to see if price is overextended in either direction. So user defined inputs would be:
1. length of MA
2. simple or exponential
3. values for horizontal lines
Edit: here the code I found:

Code:
``````# MovingAvgDistance
# DREWGRIFFITH5 (C) 2015

declare lower;

input price = close;
input averageType = {default Exponential, Simple, Weighted, Wilders, Hull};
input length = 300;

def avg;

switch (averageType) {

case Exponential:
avg = ExpAverage(price, length);
case Simple:
avg = Average(price, length);
case Weighted:
avg = wma(price, length);
case Wilders:
avg = WildersAverage(price, length);
case Hull:
avg = HullMovingAvg(price, length);

}

plot dist = ((price - avg) / ((price + avg) / 2)) * 100;
dist.SETLINEWEIGHT(3);

plot prev_high = Highest(dist, LENGTH = length);
plot prev_low = Lowest(dist, LENGTH = length);
prev_high.hide();
prev_low.hide();

dist.SETDEFAULTCOLOR(Color.cyan);
dist.AssignValueColor(if dist > prev_high then Color.RED else if dist < prev_low then Color.GREEN else Color.gray);

plot centerline = 0;
centerline.SETDEFAULTCOLOR(Color.yellow);

AddLabel(dist, dist, if dist > prev_high then Color.RED else if dist < prev_low then Color.GREEN else Color.gray);``````

Last edited by a moderator:

#### horserider

##### Well-known member
VIP
Code:
``````declare lower;

input price = close;
input length = 200;
input AverageType = {default Simple, Exponential};
input pdl = 3;
input ndl = -3;
def MA = MovingAverage(AverageType, ( price), length);
plot DEV = (price / MA - 1 ) * 1000;

plot pdll = pdl;
plot ndll = ndl;
plot zero = 0;

DEV.SetDefaultColor(GetColor(1));

AddLabel(1, "Percentage from " + length + " MA: " + AsPercent(DEV * .01), if DEV > 0 then Color.GREEN else Color.PINK);``````

Last edited:

#### markos

##### Well-known member
VIP
Here's another way to do it in a label:

Code:
``````# MA Distance Percentage
# Paris
# 4.13.2018
# Computes percentage the current close is above/below the 200 DSMA
# Displays on a lower chart

declare lower;

input length = 200;

def data = close;
def avg = Average(data, length);
def pct = (data/avg) - 1;

plot dist = pct;
plot zero = 0;

zero.SetDefaultColor(Color.WHITE);
zero.SetLineWeight(2);

AddLabel(1, "Percentage from " + length + " MA: " + AsPercent(pct), if pct > 0 then Color.GREEN else Color.PINK);``````

The script below can be used if you want to scan for price trading between two different moving averages.

Code:
``````# Stock price trading between two moving averages

input price = close;
input length1 = 50;
input length2 = 200;
input displace = 0;

def AvgExp1 = ExpAverage(price[-displace], length1);
def AvgExp2 = ExpAverage(price[-displace], length2);

def between_ma = price > AvgExp1 and price < AvgExp2;
plot scan = if between_ma then 1 else 0;``````

• MColb, Sport87 and dj45

#### horserider

##### Well-known member
VIP
Markos stole the label idea and added it to the previous study. • • adii800, dj45 and markos

#### G81moyano

##### New member
Hello team.

Do you have or could help me out to create some indicator to add to WL column showing wthat percentage remain to reach a specific level or in this case a specific moving average? (EMA200)?

Thank you

#### mc01439

##### Well-known member
2019 Donor
VIP
Try this;

Code:
``````input price = close;
input length = 200;
input Showlabels = yes;

def AvgExp = ExpAverage(price, length);
def diff = close - AvgExp;

def diffUP = diff > AvgExp;
def diffDN =  diff < AvgExp;
def diffnet = !diffUP and !diffDN;

AddLabel(Showlabels and  diffUP, "Price From 200EMA @ " + diff, Color.GREEN);
AddLabel(Showlabels and  diffDN, "Price From 200EMA @ " + diff, Color.RED);
AddLabel(Showlabels and  diffnet, "Price From 200EMA @ " + diff, Color.YELLOW);``````

#### G81moyano

##### New member
Thanks a lot... I have an image to show how looks like but ... Do I have a chance to see how many % I have with the price when its going to touch this EMA?
an example...if 9.35 the distance between EMA and close price is 30 cents and in 9:54 the distance is 10 cents do I have a chance to see it into a custom study into WL ( I have a picture but I dont have any idea to share it with you) #### mc01439

##### Well-known member
2019 Donor
VIP
Thanks a lot... I have an image to show how looks like but ... Do I have a chance to see how many % I have with the price when its going to touch this EMA?
an example...if 9.35 the distance between EMA and close price is 30 cents and in 9:54 the distance is 10 cents do I have a chance to see it into a custom study into WL ( I have a picture but I dont have any idea to share it with you) Here you go. Turn which one you want with the input (yes or no).

Code:
``````input price = close;
input length = 200;
input Showlabels = yes;
input ShowPCTlabels = yes;

def AvgExp = ExpAverage(price, length);
def diff = close - AvgExp;
def pctdiff = (diff / AvgExp) * 100;

def waveUP = diff > AvgExp;
def waveDN =  diff < AvgExp;
def wavenet = !waveUP and !waveDN;

AddLabel(Showlabels and  waveUP, "Price From 200EMA @ " + diff, Color.GREEN);
AddLabel(Showlabels and  waveDN, "Price From 200EMA @ " + diff, Color.RED);
AddLabel(Showlabels and  wavenet, "Price From 200EMA @ " + diff, Color.YELLOW);

def pctUP = pctdiff > AvgExp;
def pctDN =  pctdiff < AvgExp;
def pctnet = !pctUP and !pctDN;

AddLabel(ShowPCTlabels and  pctUP, "Percent From 200EMA @ " + pctdiff, Color.GREEN);
AddLabel(ShowPCTlabels and  pctDN, "Percent From 200EMA @ " + pctdiff, Color.RED);
AddLabel(ShowPCTlabels and  pctnet, "Percent From 200EMA @ " + pctdiff, Color.YELLOW);``````

##### Member
@tomsk is there anyway i can make a watchlist column out of this? if the distance is less than .25 then green..else red...please? thanks!

#### nervo88

##### New member
Hello All !

I'm new for scripts in the scanner.

1. I'm trying to create a scan with two conditions but with the OR option:

EMA 9 crosses 50 EMA , price closed

OR

EMA 9 cross 200 EMA , price closed

2. Is there an option to add conditions like: EMA9 very close to 50 EMA (like in % maybe? ) so it will almost indicate a cross. ofcorse not a 100% , but still, is this possible?

Thank you for your help ! ## 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
• Add-ons and resources
• 1 full year of unlimited support

### Frequently Asked Questions

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?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.