-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathintraday-snapper.pine
415 lines (355 loc) · 19.2 KB
/
intraday-snapper.pine
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//@version=3
//
study(title="Big Snapper Alerts R3.0 by JustUncleL", shorttitle="SNAPPER", overlay=true )
//
// Author: JustUncleL
// Date: 19-Oct-2017
// Version: R3.0
//
// Description:
// This is a diversified Binary Option or Scalping Alert indicator originally designed for
// lower Time Frame Trend or Swing trading. Although you will find it a useful tool for
// higher time frames as well.
//
// The Alerts are generated by the changing direction of the ColouredMA (HullMA by default,
// also SSMA works well) or optionally by fastMA crossing ColouredMA. Then you have the choice of
// selecting the Directional filtering on these signals or a Bollinger Outside IN swing
// reversal filter.
//
// The filters include:
//
// Type 1 - The three MAs (EMAs 21,55,89 by default) in various combinations or by themselves.
// When only one directional MA selected then direction filter is given by ColouredMA
// above(up)/below(down) selected MA.
// If more than one MA selected the direction is given by MAs being in correct order
// for trend direction.
//
// Type 2 - The SuperTrend direction is used to filter ColouredMA signals.
//
// Type 3 - Bollinger Band Outside In is used to filter ColouredMA for swing reversals.
//
// Type 4 - No directional filtering, all signals from the ColouredMA are shown.
//
// Type 5 - Signals given by FastMA (eg length 7) crossing the ColouredMA (eg length 14),
// suggested FastMA should same type as ColouredMA (eq HullMA or SSMA) and
// no less than half the length.
//
// Notes: - Each Type can be combined with most other types to form more complex filtration.
// - Alerts can also be disabled completely if you just want one indicator with
// one colouredMA and/or 3xMAs and/or Bollinger Bands and/or SuperTrend
// painted on the chart.
//
// Warning- Be aware that combining Bollinger OutsideIn swing filter and a directional filter
// can be counter productive as they are opposites. So careful consideration is needed
// when combining Bollinger OutsideIn with any of the directional filters.
//
// Hints: - For Binary Options try ColouredMA = HullMA(13) or HullMA(8) with Type 2 or 3 Filter.
// - When using Trend filters SuperTrend and/or 3xMA Trend, you will find if price reverses
// and breaks back through the Big Fat Signal line, then this can be a good reversal trade.
// - Try using SSMA instead of HullMA for signal line, it is similar to Hull but remains
// smoother at low lengths and works well with the MA Cross signals.
//
// Mofidifications:
//
// R0.# : Original alpha unpublished versions.
//
// R1.# : Original beta unpublished versions.
//
// R2.0 : Original Published version.
//
// R3.0 : - Added new Signal Type 5: Signals come from Fast MA crossing slower ColouredMA.
// - Replaced OutsideIn Coloured MA signals with those generated from the MA cross.
// - Added selectable colours for Big fat marker, the big arrows colours must still
// be selected in the "Style" parameters of the script.
//
// References:
// Some explanation about the what Hull Moving averageis and ideas of how the generated in
// Snapper can be used.
// - https://tradingsim.com/blog/hull-ma/
// - http://forextradingstrategies4u.com/hull-moving-average-forex-trading-strategy/
//
// Some Code borrowed from:
// - "Scalp Jockey - MTF MA Cross Visual Strategizer by JayRogers"
//
// Inspiration from @vdubus
//
//
// -----------------------------------------------------------------------------
// Copyright 2017 JustUncleL
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// The GNU General Public License can be found here
// <http://www.gnu.org/licenses/>.
//
// -----------------------------------------------------------------------------
//
//
// === INPUTS ===
// Coloured MA - type, length, source
typeColoured = input(defval="HullMA", title="Coloured MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"], type=string)
lenColoured = input(defval=18, title="Coloured MA - Length", minval=1)
srcColoured = input(close,title="Coloured MA - Source")
// Fast MA - type, length
typeFast = input(defval="EMA", title="Fast MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"], type=string)
lenFast = input(defval=21, title="Fast MA - Length", minval=1)
// Medium MA - type, length
typeMedium = input(defval="EMA", title="Medium MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"], type=string)
lenMedium = input(defval=55, title="Medium MA - Length", minval=1)
// Slow MA - type, length
typeSlow = input(defval="EMA", title="Slow MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"], type=string)
lenSlow = input(defval=89, title="Slow MA Length", minval=1)
// 3xMA source
ma_src = input(close,title="3xMA and Bollinger Source")
//
filterOption = input("SuperTrend",title="Signal Filter Option : ",
options=["3xMATrend","SuperTrend","SuperTrend+3xMA","ColouredMA","No Alerts","MACross","MACross+ST","MACross+3xMA",
"OutsideIn:MACross","OutsideIn:MACross+ST","OutsideIn:MACross+3xMA"])
//
hideMALines = input(false)
hideSuperTrend = input(true)
hideBollingerBands = input(true)
hideTrendDirection = input(true)
//
disableFastMAFilter = input(false)
disableMediumMAFilter = input(false)
disableSlowMAFilter = input(false)
//
uKC = false // input(false,title="Use Keltner Channel (KC) instead of Bollinger")
bbLength = input(20,minval=2,step=1,title="Bollinge Bands Length")
bbStddev = input(2.0,minval=0.5,step=0.1,title="Bollinger Bands StdDevs")
oiLength = input(8, title="Bollinger Outside In LookBack")
//
SFactor = input(3.618, minval=1.0, title="SuperTrend Factor")
SPd = input(5, minval=1, title="SuperTrend Length")
//
buyColour_ = input("Green", title="BUY Marker Colour: ",options=["Green","Lime","Aqua","DodgerBlue","Gray","Yellow"])
sellColour_ = input("Maroon",title="SELL Marker Colour: ",options=[ "Maroon","Red","Fuchsia","Blue","Black","Orange"])
// --- Allocate Correct Filtering Choice
// Can only be one choice
uSuperTrendFilter = filterOption == "SuperTrend" ? true : false
u3xMATrendFilter = filterOption == "3xMATrend"? true : false
uBothTrendFilters = filterOption == "SuperTrend+3xMA"? true : false
//uOIFilter = filterOption == "OutsideIn:ClrMA" ? true : false
uOIMACrossFilter = filterOption == "OutsideIn:MACross" ? true : false
uOI3xMAFilter = filterOption == "OutsideIn:MACross+3xMA" ? true : false
uOISTFilter = filterOption == "OutsideIn:MACross+ST" ? true : false
uMACrossFilter = filterOption == "MACross" ? true : false
uMACrossSTFilter = filterOption == "MACross+ST" ? true : false
uMACross3xMAFilter = filterOption == "MACross+3xMA" ? true : false
// unless all 3 MAs disabled.
disable3xMAFilter = disableFastMAFilter and disableMediumMAFilter and disableSlowMAFilter
u3xMATrendFilter := disable3xMAFilter? false : u3xMATrendFilter
// if no filters selected then must be "No Filters" option
disableAllFilters = u3xMATrendFilter or uSuperTrendFilter or uBothTrendFilters or uOI3xMAFilter or uOISTFilter or
uOIMACrossFilter or uMACrossFilter or uMACrossSTFilter or uMACross3xMAFilter? false : true
// if "No Alerts" option selected, then disable all selections
disableAllFilters := filterOption == "No Alerts"? false : disableAllFilters
uSuperTrendFilter := filterOption == "No Alerts"? false : uSuperTrendFilter
u3xMATrendFilter := filterOption == "No Alerts"? false : u3xMATrendFilter
uBothTrendFilters := filterOption == "No Alerts"? false : uBothTrendFilters
//uOIFilter := filterOption == "No Alerts"? false : uOIFilter
uOIMACrossFilter := filterOption == "No Alerts"? false : uOIMACrossFilter
uOI3xMAFilter := filterOption == "No Alerts"? false : uOI3xMAFilter
uOISTFilter := filterOption == "No Alerts"? false : uOISTFilter
uMACrossFilter := filterOption == "No Alerts"? false : uMACrossFilter
uMACrossSTFilter := filterOption == "No Alerts"? false : uMACrossSTFilter
uMACross3xMAFilter := filterOption == "No Alerts"? false : uMACross3xMAFilter
// --- CONSTANTS ---
dodgerblue = #1E90FF
lightcoral = #F08080
buyColour = green // for big Arrows, must be a constant.
sellColour = maroon // for big Arrows
// Colour Selectable for Big Fat Bars.
buyclr = buyColour_=="Lime"?lime: buyColour_=="Aqua"?aqua: buyColour_=="DodgerBlue"?dodgerblue: buyColour_=="Gray"?gray: buyColour_=="Yellow"?yellow:green
sellclr = sellColour_=="Red"?red: sellColour_=="Fuchsia"?fuchsia: sellColour_=="Blue"?blue: sellColour_=="Black"?black: sellColour_=="Orange"?orange: maroon
// === /INPUTS ===
// === FUNCTIONS ===
// Returns MA input selection variant, default to SMA if blank or typo.
variant(type, src, len) =>
v1 = sma(src, len) // Simple
v2 = ema(src, len) // Exponential
v3 = wma(src, len) // Weighted
v4 = vwma(src, len) // Volume Weighted
v5 = 0.0
v5 := na(v5[1]) ? sma(src, len) : (v5[1] * (len - 1) + src) / len // Smoothed
v6 = 2 * v2 - ema(v2, len) // Double Exponential
v7 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len) // Triple Exponential
v8 = wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len))) // Hull WMA = (2*WMA (n/2) − WMA (n)), sqrt (n))
v11 = sma(sma(src,len),len) // Triangular
// SuperSmoother filter
// © 2013 John F. Ehlers
a1 = exp(-1.414*3.14159 / len)
b1 = 2*a1*cos(1.414*3.14159 / len)
c2 = b1
c3 = (-a1)*a1
c1 = 1 - c2 - c3
v9 = 0.0
v9 := c1*(src + nz(src[1])) / 2 + c2*nz(v9[1]) + c3*nz(v9[2])
// Zero Lag Exponential
e = ema(v1, len)
v10 = v1+(v1-e)
// return variant, defaults to SMA if input invalid.
type=="EMA"?v2 : type=="WMA"?v3 : type=="VWMA"?v4 : type=="SMMA"?v5 : type=="DEMA"?v6 : type=="TEMA"?v7 : type=="HullMA"?v8 : type=="SSMA"?v9 : type=="ZEMA"?v10 : type=="TMA"? v11: v1
// === /FUNCTIONS ===
// === SERIES VARIABLES ===
// MA's
ma_fast = variant(typeFast, ma_src, lenFast)
ma_medium = variant(typeMedium, ma_src, lenMedium)
ma_slow = variant(typeSlow, ma_src, lenSlow)
ma_coloured = variant(typeColoured, srcColoured, lenColoured)
// Get Direction of Coloured Moving Average
clrdirection = 1
clrdirection := rising(ma_coloured,2) ? 1 : falling(ma_coloured,2)? -1 : nz(clrdirection[1],1)
// get 3xMA trend direction based on selections.
madirection = ma_fast>ma_medium and ma_medium>ma_slow? 1 : ma_fast<ma_medium and ma_medium<ma_slow? -1 : 0
madirection := disableSlowMAFilter? ma_fast>ma_medium? 1 : ma_fast<ma_medium? -1 : 0 : madirection
madirection := disableMediumMAFilter? ma_fast>ma_slow? 1 : ma_fast<ma_slow? -1 : 0 : madirection
madirection := disableFastMAFilter? ma_medium>ma_slow? 1 : ma_medium<ma_slow? -1 : 0 : madirection
madirection := disableFastMAFilter and disableMediumMAFilter? ma_coloured>ma_slow? 1 : -1 : madirection
madirection := disableFastMAFilter and disableSlowMAFilter? ma_coloured>ma_medium? 1 : -1 : madirection
madirection := disableSlowMAFilter and disableMediumMAFilter? ma_coloured>ma_fast? 1 : -1 : madirection
//
// Supertrend Calculations
SUp=hl2-(SFactor*atr(SPd))
SDn=hl2+(SFactor*atr(SPd))
STrendUp = 0.0
STrendDown = 0.0
STrendUp := close[1]>STrendUp[1]? max(SUp,STrendUp[1]) : SUp
STrendDown := close[1]<STrendDown[1]? min(SDn,STrendDown[1]) : SDn
STrend = 0
STrend := close > STrendDown[1] ? 1: close< STrendUp[1]? -1: nz(STrend[1],1)
Tsl = STrend==1? STrendUp: STrendDown
// Standard Bollinger or KC Bands
basis = sma(ma_src, bbLength)
rangema = sma(tr, bbLength)
dev = uKC? bbStddev*rangema : bbStddev * stdev(ma_src, bbLength)
// Calculate Bollinger or KC Channel
upper = basis + dev
lower = basis - dev
// Lookback for previous highest bar index
noiupper = abs(highestbars(oiLength))
noilower = abs(lowestbars(oiLength))
// ColouredMA OutsideIn
//oiupper = clrdirection<0 and noiupper>0 and highest(oiLength)>upper[noiupper]? 1 : 0
//oilower = clrdirection>0 and noilower>0 and lowest(oiLength)<lower[noilower]? 1 : 0
// MACross OutsideIN
oiMACrossupper = crossunder(ma_fast,ma_coloured) and noiupper>0 and highest(oiLength)>upper[noiupper]? 1 : 0
oiMACrosslower = crossover(ma_fast,ma_coloured) and noilower>0 and lowest(oiLength)<lower[noilower]? 1 : 0
// === /SERIES VARIABLES ===
// === PLOTTING ===
// All the MA's
plot(ma_coloured, title="Coloured MA", color=clrdirection<0?lightcoral:blue, linewidth=3, transp=20)
plot(hideMALines?na:ma_fast, title="Fast MA", color=lime, linewidth=2, transp=20)
plot(hideMALines?na:ma_medium, title="Medium MA", color=red, linewidth=2, transp=10)
plot(hideMALines?na:ma_slow, title="Slow MA", color=gray, linewidth=2, transp=10)
// show 3xMA Trend Direction State.
dcolour = madirection==1?green:madirection==-1?red:yellow
plotshape(hideTrendDirection? na : madirection, title="3xMA Trend Direction", location=location.bottom,style=shape.square, color=dcolour,transp=10)
// SuperTrend
plot(hideSuperTrend?na:Tsl, color = STrend == 1 ? green : maroon, style = line , linewidth = 2,title = "SuperTrend")
// Bollinger Bands
p1=plot(hideBollingerBands?na:upper, title="BB upper", color=dodgerblue, linewidth=1, transp=20)
p2=plot(hideBollingerBands?na:lower, title="BB lower", color=dodgerblue, linewidth=1, transp=20)
fill(p1,p2,color=dodgerblue,transp=96,title="BB fill")
// === /PLOTTING ===
// === ALERTING ===
// 3xMA Filtering
_3xmabuy = 0
_3xmasell = 0
_3xmabuy := clrdirection == 1 and close>ma_fast and madirection==1 ? nz(_3xmabuy[1])+1 : clrdirection == 1 and madirection==1? nz(_3xmabuy[1])>0?nz(_3xmabuy[1])+1 : 0 : 0
_3xmasell := clrdirection ==-1 and close<ma_fast and madirection==-1 ? nz(_3xmasell[1])+1 : clrdirection ==-1 and madirection==-1? nz(_3xmasell[1])>0?nz(_3xmasell[1])+1 : 0 : 0
//
// SuperTrend Filtering
stbuy = 0
stsell = 0
stbuy := clrdirection == 1 and STrend==1 ? nz(stbuy[1])+1 : 0
stsell := clrdirection ==-1 and STrend==-1 ? nz(stsell[1])+1 : 0
//
// 3xMA & SuperTrend Filtering
//
st3xmabuy = 0
st3xmasell = 0
st3xmabuy := (disable3xMAFilter or _3xmabuy>0) and stbuy>0 ? nz(st3xmabuy[1])+1 : 0
st3xmasell := (disable3xMAFilter or _3xmasell>0) and stsell>0 ? nz(st3xmasell[1])+1 : 0
// Bollinger Outside In using ColuredMA direction Filter.
//oibuy = 0
//oisell = 0
//oibuy := clrdirection == 1 and oilower==1? nz(oibuy[1])+1 : 0
//oisell := clrdirection ==-1 and oiupper==1? nz(oisell[1])+1 : 0
// Bollinger Outside In using MACross signal Filter
oiMACrossbuy = 0
oiMACrosssell = 0
oiMACrossbuy := oiMACrosslower==1? nz(oiMACrossbuy[1])+1 : 0
oiMACrosssell := oiMACrossupper==1? nz(oiMACrosssell[1])+1 : 0
// Bollinger Outside In + 3xMA Filter
oi3xmabuy = 0
oi3xmasell = 0
oi3xmabuy := oiMACrossbuy>0 and (disable3xMAFilter or madirection==1)? nz(oi3xmabuy[1])+1 : 0
oi3xmasell := oiMACrosssell>0 and (disable3xMAFilter or madirection==-1)? nz(oi3xmasell[1])+1 : 0
// Bollinger Outside In + SuperTrend Filter
oistbuy = 0
oistsell = 0
oistbuy := oiMACrossbuy>0 and STrend==1? nz(oistbuy[1])+1 : 0
oistsell := oiMACrosssell>0 and STrend==-1? nz(oistsell[1])+1 : 0
// FastMA crossover HullMA and SuperTrend
macrossSTbuy = 0
macrossSTsell = 0
macrossSTbuy := crossover(ma_fast,ma_coloured) and STrend==1? nz(macrossSTbuy[1])+1 : 0
macrossSTsell := crossunder(ma_fast,ma_coloured) and STrend==-1? nz(macrossSTsell[1])+1 : 0
// FastMA crossover HullMA and 3xMA
macross3xMAbuy = 0
macross3xMAsell = 0
macross3xMAbuy := crossover(ma_fast,ma_coloured) and (disable3xMAFilter or madirection==1) ? nz(macross3xMAbuy[1])+1 : 0
macross3xMAsell := crossunder(ma_fast,ma_coloured) and (disable3xMAFilter or madirection==-1) ? nz(macross3xMAsell[1])+1 : 0
//
// Check any Alerts set
long = (u3xMATrendFilter and _3xmabuy==1) or (uSuperTrendFilter and stbuy==1) or (uBothTrendFilters and st3xmabuy==1) or
(uOI3xMAFilter and oi3xmabuy==1) or (uOISTFilter and oistbuy==1) or (uOIMACrossFilter and oiMACrossbuy==1) or
(uMACrossSTFilter and macrossSTbuy==1) or (uMACross3xMAFilter and macross3xMAbuy==1)
short = (u3xMATrendFilter and _3xmasell==1) or (uSuperTrendFilter and stsell==1) or (uBothTrendFilters and st3xmasell==1) or
(uOI3xMAFilter and oi3xmasell==1) or (uOISTFilter and oistsell==1) or (uOIMACrossFilter and oiMACrosssell==1) or
(uMACrossSTFilter and macrossSTsell==1) or (uMACross3xMAFilter and macross3xMAsell==1)
//
// If Alert Detected, then Draw Big fat liner
plotshape( long ? long : na, title="Long Line Marker",
location=location.belowbar,style=shape.arrowup, color=buyclr,transp=20,size=size.auto, text='████████████████',textcolor=buyclr)
plotshape( short ? short : na, title="Short Line Marker",
location=location.abovebar,style=shape.arrowdown, color=sellclr,transp=20,size=size.auto, text='████████████████',textcolor=sellclr)
// --- Arrow style signals
// No Filters only Hull Signals
hbuy = 0
hsell = 0
hbuy := clrdirection == 1 ? nz(hbuy[1])+1 : 0
hsell := clrdirection ==-1 ? nz(hsell[1])+1 : 0
// FastMA crossover HullMA
macrossbuy = 0
macrosssell = 0
macrossbuy := crossover(ma_fast,ma_coloured)? nz(macrossbuy[1])+1 : 0
macrosssell := crossunder(ma_fast,ma_coloured)? nz(macrosssell[1])+1 : 0
//
along = (disableAllFilters and hbuy==1) or (uMACrossFilter and macrossbuy==1)
ashort = (disableAllFilters and hsell==1) or (uMACrossFilter and macrosssell==1)
//
// If ColouredMA or MACross then draw big arrows
plotarrow( along ? 1 : ashort? -1 : na, title="ColouredMA or MACross Arrow", colorup=buyColour, colordown=sellColour, maxheight=100, minheight=50, transp=20)
// Update alarm conditions
long := long or along
short := short or ashort
// === /ALERTING ===
// === ALARMS ===
//
alertcondition(long or short, title="Signal Alert",message="SIGNAL")
alertcondition(long, title="Long Alert",message="LONG")
alertcondition(short, title="Short Alert",message="SHORT")
// === /ALARMS ===
//
//EOF