-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathMACD PSAR Indicator.txt
More file actions
executable file
·69 lines (54 loc) · 3.83 KB
/
MACD PSAR Indicator.txt
File metadata and controls
executable file
·69 lines (54 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//@version=4
study(title = "MACD Psar v1.1", overlay = true, format=format.price, precision=2)
var crossoverBear = false
var crossoverBull = false
// gapMACD = input(title="MACD Gap Threshold", type=input.integer, defval=1, group="Basic Settings", tooltip="How much distance between MACD line and the zero line is acceptable to show an indicator? Larger numbers will filter out more noise")
adxThreshold = input(0, title="ADX Threshold", group="Basic Settings", tooltip="ADX value that must be reached in order for an indicator to display. Larger numbers will filter out more noise")
filter200 = input(false, title="Filter by 200 EMA", group="Basic Settings", tooltip="Use 200 EMA to determine trend direction. Only show SELL below it, only show BUY above it")
filter850 = input(false, title="Filter by 8/50 EMA", group="Basic Settings", tooltip="Use 8/50 inverse combo to show BUY/SELL. If 8 is above 50, then we're too high up and need to sell, thus only show SELL during that, and vice versa for BUY. NOTE: This is INVERSE")
psarStart = input(title="PSAR Start", type=input.float, step=0.001, defval=0.02, group="PSAR")
psarIncrement = input(title="PSAR Increment", type=input.float, step=0.001, defval=0.02, group="PSAR")
psarMaximum = input(title="PSAR Maximum", type=input.float, step=0.01, defval=0.2, group="PSAR")
// Average Directional Index
adxlen = input(14, title="ADX Smoothing", group="ADX")
dilen = input(14, title="DI Length", group="ADX")
dirmov(len) =>
up = change(high)
down = -change(low)
plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
truerange = rma(tr, len)
plus = fixnan(100 * rma(plusDM, len) / truerange)
minus = fixnan(100 * rma(minusDM, len) / truerange)
[plus, minus]
adx(dilen, adxlen) =>
[plus, minus] = dirmov(dilen)
sum = plus + minus
adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
adxValue = adx(dilen, adxlen)
// Parabolic SAR
psar = sar(psarStart, psarIncrement, psarMaximum)
psarDir = psar < close ? 1 : -1
// MACD
fastMA = input(title="Fast moving average", type=input.integer, defval=12, minval=7, group="MACD")
slowMA = input(title="Slow moving average", type=input.integer, defval=26, minval=7, group="MACD")
signalLength = input(9,minval=1)
[currMacd,_,_] = macd(close[0], fastMA, slowMA, signalLength)
[prevMacd,_,_] = macd(close[1], fastMA, slowMA, signalLength)
signal = ema(currMacd, signalLength)
if (cross(currMacd, signal) and currMacd < signal and currMacd >= -2)
crossoverBear := true
crossoverBull := false
if (cross(currMacd, signal) and currMacd > signal and currMacd <= 2)
crossoverBull := true
crossoverBear := false
iBuy = (crossoverBull and psar < close and adxValue > adxThreshold)
iSell = (crossoverBear and psar > close and adxValue > adxThreshold)
showBuy = iBuy and not iBuy[1]
showSell = iSell and not iSell[1]
filterBuy200 = (filter200 and ema(close,200) > close) or (filter850 and ema(close,8) < ema(close,50))
filterSell200 = (filter200 and ema(close,200) < close) or (filter850 and ema(close,8) > ema(close,50))
// plotchar(showBuy and adxValue > adxThreshold and not filterBuy200 ? 1 : na, title="Buy", char='❄', location=location.belowbar, color=color.rgb(37, 119, 79), textcolor=color.white)
// plotchar(showSell and adxValue > adxThreshold and not filterSell200 ? 1 : na, title="Sell", char='❄', location=location.abovebar, color=color.rgb(127, 37, 37), textcolor=color.white)
plotshape(showBuy and adxValue > adxThreshold and not filterBuy200 ? 1 : na, title="Buy", text="Buy", location=location.belowbar, style=shape.labelup, size=size.tiny, color=color.rgb(37, 119, 79), textcolor=color.white)
plotshape(showSell and adxValue > adxThreshold and not filterSell200 ? 1 : na, title="Sell", text="Sell", location=location.abovebar, style=shape.labeldown, size=size.tiny, color=color.rgb(127, 37, 37), textcolor=color.white)