-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathMacTastic Strategy.txt
More file actions
executable file
·127 lines (98 loc) · 5.5 KB
/
MacTastic Strategy.txt
File metadata and controls
executable file
·127 lines (98 loc) · 5.5 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//@version=4
strategy("MacTastic Strategy", overlay=true)
///////////////////////////////////////////////////
var isLong = false
var isShort = false
// CONFIG
rsiLengthInput = input(15, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input(close, "Source", group="RSI Settings")
periodK = input(14, title="%K Length", minval=1, group="Stochastic")
smoothK = input(1, title="%K Smoothing", minval=1, group="Stochastic")
fastMA = input(title="Fast moving average", defval=12, minval=7, group="MACD")
slowMA = input(title="Slow moving average", defval=26, minval=7, group="MACD")
signalLength = input(9,minval=1, group="MACD")
adxThreshold = input(title="ADX Threshold", defval=0, minval=0, group="ADX")
adxlen = input(14, title="ADX Smoothing", group="ADX")
dilen = input(14, title="DI Length", group="ADX")
// 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)
sig = adx(dilen, adxlen)
// MACD
[currMacd,_,_] = macd(close[0], fastMA, slowMA, signalLength)
[prevMacd,_,_] = macd(close[1], fastMA, slowMA, signalLength)
signal = ema(currMacd, signalLength)
// RSI
up = rma(max(change(rsiSourceInput), 0), rsiLengthInput)
down = rma(-min(change(rsiSourceInput), 0), rsiLengthInput)
rsiM = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
// STOCHASTIC - 80/20 upper/lower bands
k = sma(stoch(close, high, low, periodK), smoothK)
stochOS = k[1] <= 20 or k[2] <= 20 or k[3] <= 20 or k[4] <= 20 or k[5] <= 20 or k[6] <= 20 or k[7] <= 20 or k[8] <= 20 or k[9] <= 20
stochOB = k[1] >= 80 or k[2] >= 80 or k[3] >= 80 or k[4] >= 80 or k[5] >= 80 or k[6] >= 80 or k[7] >= 80 or k[8] >= 80 or k[9] >= 80
weGoUp = sig > adxThreshold and not isLong and rsiM >= 50 and currMacd > signal and stochOS
weGoDown = sig > adxThreshold and not isShort and rsiM <= 50 and currMacd < signal and stochOB
upThrust = weGoUp and not weGoUp[1] and not weGoUp[2] and not weGoUp[3] and not weGoUp[4]
downThrust = weGoDown and not weGoDown[1] and not weGoDown[2] and not weGoDown[3] and not weGoDown[4]
// PLOT THE THINGS
plotshape(upThrust ? hl2 : na, title="MacTastic", text="MT", location=location.belowbar, style=shape.labelup, size=size.tiny, color=color.rgb(56, 182, 93), textcolor=color.white)
plotshape(downThrust ? hl2 : na, title="MacTastic", text="MT", location=location.abovebar, style=shape.labeldown, size=size.tiny, color=color.rgb(145, 42, 61), textcolor=color.white)
if weGoUp
isLong := true
isShort := false
if weGoDown
isLong := false
isShort := true
/////////////////////////////////////////////////////////////////
TimeWindow=time(timeframe.period,"1400-1900", "GMT")
if (weGoUp and TimeWindow)
strategy.entry("Trade", strategy.long, comment="Long")
if (weGoDown and TimeWindow)
strategy.entry("Trade", strategy.short, comment="Short")
trailSource = input(title="Trail Source", defval="Lows/Highs", options=["Lows/Highs", "Close", "Open"], confirm=true, group="Strategy")
trailMethod = input(title="Trail Method", defval="ATR", options=["ATR", "Percent"], confirm=true, group="Strategy")
trailPercent = input(title="Trail Percent", defval=10, minval=0.1, confirm=true, group="Strategy")
swingLookback = input(title="Lookback", defval=7, confirm=true, group="Strategy")
atrPeriod = input(title="ATR Period", defval=14, confirm=true, group="Strategy")
atrMultiplier = input(title="ATR Multiplier", defval=1.0, confirm=true, group="Strategy")
barTime = input(title="Bar Time", defval=timestamp("01 Jan 2021 13:30 +0000"), confirm=true, group="Strategy")
var float trailPrice = na
float next_trailPrice = na
atrValue = atr(atrPeriod) * atrMultiplier
float swingLow = lowest(low, swingLookback)
float swingHigh = highest(high, swingLookback)
if trailMethod == "ATR"
if trailSource == "Close"
next_trailPrice := strategy.position_size > 0 ? close - atrValue : close + atrValue
else if trailSource == "Open"
next_trailPrice := strategy.position_size > 0 ? open - atrValue : open + atrValue
else
next_trailPrice := strategy.position_size > 0 ? swingLow - atrValue : swingHigh + atrValue
if trailMethod == "Percent"
float percentMulti = strategy.position_size > 0 ? (100 - trailPercent) / 100 : (100 + trailPercent) / 100
if trailSource == "Close"
next_trailPrice := close * percentMulti
else if trailSource == "Open"
next_trailPrice := open * percentMulti
else
next_trailPrice := strategy.position_size > 0 ? swingLow * percentMulti : swingHigh * percentMulti
if strategy.position_size != 0 and barstate.isconfirmed
if (next_trailPrice > trailPrice or na(trailPrice)) and strategy.position_size > 0
trailPrice := next_trailPrice
if (next_trailPrice < trailPrice or na(trailPrice)) and strategy.position_size < 0
trailPrice := next_trailPrice
plot(strategy.position_size != 0 ? trailPrice : na, color=color.red, title="Trailing Stop")
strategy.exit(id="Trail Exit", from_entry="Trade", limit=na, stop=trailPrice)
if (strategy.position_size > 0 and close < trailPrice) or (strategy.position_size < 0 and close > trailPrice)
strategy.close("Trade")