-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsusan.pine
266 lines (214 loc) · 12.8 KB
/
susan.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
// @version=5
indicator("Susan", "", overlay = true)
// User configurations
show_hide_peak_valley_dots = input(title = "Show Peak & Valley Dots", defval = true)
atr_length = input(title = "ATR Length", defval = 14)
// Global Current Candle Indicators
atr = ta.atr(atr_length)
// ***** Susan Peak & Valley Functions ***** //
var offset = -1
calcPeaks(high) =>
if ((high[2] < high[1] and high[0] < high[1]) or (high[3] < high[2] and high[2] == high[1] and high[0] < high[1]) or (high[4] < high[3] and high[3] == high[1] and high[2] == high[1] and high[0] < high[1]))
high[1]
else
na
calcValley(low) =>
if (low[2] > low[1] and low[0] > low[1])
low[1]
else
na
// ****** ************************ ********** //
var previousPreviousPeak = high
var previousPreviousPeakIndex = bar_index
var previousPeak = high
var previousPeakIndex = bar_index
var currentPeak = high
var currentPeakIndex = bar_index
var peakLine = line.new(x1 = 0, y1 = 0, x2 = 0, y2 = 0)
var previousPeakLine = line.new(x1 = 0, y1 = 0, x2 = 0, y2 = 0)
peaksV = calcPeaks(high)
if (peaksV)
peaksV := high[1]
previousPreviousPeak := previousPeak
previousPreviousPeakIndex := previousPeakIndex
previousPeak := currentPeak
previousPeakIndex := currentPeakIndex
currentPeak := high[1]
currentPeakIndex := bar_index[1]
plot(show_hide_peak_valley_dots ? peaksV : na, title='Peaks', color=color.new(color.yellow, 0), linewidth=3, style=plot.style_circles, offset = -1)
// delete old peak lines
line.delete(peakLine)
line.delete(previousPeakLine)
// draw new lines
peakLine := line.new(x1 = previousPeakIndex, y1 = previousPeak, x2 = currentPeakIndex, y2 = currentPeak, xloc = xloc.bar_index, extend = extend.right)
previousPeakLine := line.new(x1 = previousPreviousPeakIndex, y1 = previousPreviousPeak, x2 = previousPeakIndex, y2 = previousPeak, xloc = xloc.bar_index, extend = extend.right)
// style peak lines
line.set_color(id = peakLine, color = color.yellow)
line.set_color(id = previousPeakLine, color = color.yellow)
line.set_style(id = previousPeakLine, style = line.style_dashed)
// VALLEYS
var previousPreviousValley = low
var previousPreviousValleyIndex = bar_index
var previousValley = low
var previousValleyIndex = bar_index
var currentValley = low
var currentValleyIndex = bar_index
var valleyLine = line.new(x1 = 0, y1 = 0, x2 = 0, y2 = 0)
var previousValleyLine = line.new(x1 = 0, y1 = 0, x2 = 0, y2 = 0)
valleyV = calcValley(low)
if (valleyV)
valleyV := low[1]
previousPreviousValley := previousValley
previousPreviousValleyIndex := previousValleyIndex
previousValley := currentValley
previousValleyIndex := currentValleyIndex
currentValley := low[1]
currentValleyIndex := bar_index[1]
plot(show_hide_peak_valley_dots ? valleyV : na, title='Valley', color = color.new(color.orange, 0), linewidth = 3, style = plot.style_circles, offset = -1)
// delete old valley lines
line.delete(valleyLine)
line.delete(previousValleyLine)
// draw new lines
valleyLine := line.new(x1 = previousValleyIndex, y1 = previousValley, x2 = currentValleyIndex, y2 = currentValley, xloc = xloc.bar_index, extend = extend.right)
previousValleyLine := line.new(x1 = previousPreviousValleyIndex, y1 = previousPreviousValley, x2 = previousValleyIndex, y2 = previousValley, xloc = xloc.bar_index, extend = extend.right)
// style valley lines
line.set_color(id = valleyLine, color = color.orange)
line.set_color(id = previousValleyLine, color = color.orange)
line.set_style(id = previousValleyLine, style = line.style_dashed)
// DAILY PEAKS (780 minutes in a trading day including extended hours)
// To avoid differences on historical and realtime bars, you can use this technique, which only returns a value from the higher timeframe on the bar after it completes:
indexHighTF = barstate.isrealtime ? 1 : 0
indexCurrTF = barstate.isrealtime ? 0 : 1
// peaks1D = request.security(syminfo.tickerid, "D", calcPeaks(high), gaps = barmerge.gaps_on, lookahead = barmerge.lookahead_off)
// [valleys1D, target_index, valleyLows] = request.security(syminfo.tickerid, "D", [calcValley(low), bar_index, low[indexHighTF]], gaps = barmerge.gaps_on, lookahead = barmerge.lookahead_on)[indexCurrTF]
// [peaks1D, valley1D, target_index, dailyLows] = request.security(syminfo.tickerid, "D", [calcPeaks(high[indexHighTF]), calcValley(low)], gaps = barmerge.gaps_on, lookahead = barmerge.lookahead_on)[indexCurrTF]
// TODO: to kind of plot the correct values, the offset are 780 minutes / the minutes of the current timeframe that is
// specified here.. https://www.tradingcode.net/tradingview/time-frame-period/
// plot((valley1D and is_valid_target) ? dailyLows[index_diff] : na, title='Valleys Daily', color=color.new(color.orange, 0), linewidth=3, style=plot.style_cross, offset = 0)
// var float previousDailyPeak = peaks1D
// var int previousDailyPeakTime = time1D
// var float currentDailyPeak = peaks1D
// var int currentDailyPeakTime = time1D
// var dailyPeakLine = line.new(x1 = 0, y1 = 0, x2 = 0, y2 = 0)
// if barstate.islastconfirmedhistory
// label.new(bar_index, high, "OHLC values: " + str.tostring(valley1D))
// if (currentDailyPeak != peaks1D)
// previousDailyPeak := currentDailyPeak
// previousDailyPeakTime := previousDailyPeakTime
// currentDailyPeak := peaks1D
// currentDailyPeakTime := time1D
// TODO: I think the offset here needs to be -x amount of time that makes up 1 day.. like a multiplier
// Ex: 4hr would be -2... 1hr would be -8..
// plot(valley1D, title='Valley Daily', color=color.new(color.orange, 0), linewidth=3, style=plot.style_cross, offset = 0)
// plot(peaks1D, title='Peaks Daily', color=color.new(color.yellow, 0), linewidth=3, style=plot.style_cross, offset = 0)
// if (currentDailyPeak > previousDailyPeak)
// line.delete(dailyPeakLine)
// dailyPeakLine := line.new(x1 = previousDailyPeakTime, y1 = previousDailyPeak, x2 = currentDailyPeakTime, y2 = currentDailyPeak, xloc = xloc.bar_time, extend = extend.right)
// line.set_color(id = dailyPeakLine, color = color.blue)
// else
// line.delete(dailyPeakLine)
// ********** HIGH AND LOW ********** //
// Daily levels
[dailyhigh, dailylow, dayopen, previousdayhigh, previousdaylow, previousdayclose] = request.security(syminfo.tickerid, 'D', [high, low, open, high[1], low[1], close])
// Premarket high and low
t = time("1440","0400-0930")
is_first = na(t[1]) and not na(t) or t[1] < t
ending_hour = 9
ending_minute = 30
pm_high = float(na)
pm_low = float(na)
k = int(na)
if is_first and barstate.isnew and (hour < ending_hour or hour >= 16 or hour == ending_hour and minute < ending_minute)
pm_high := high
pm_low := low
pm_low
else
pm_high := pm_high[1]
pm_low := pm_low[1]
pm_low
if high > pm_high and (hour < ending_hour or hour >= 1600 or hour == ending_hour and minute < ending_minute)
pm_high := high
pm_high
if low < pm_low and (hour < ending_hour or hour >= 1600 or hour == ending_hour and minute < ending_minute)
pm_low := low
pm_low
LastOnly = true
if LastOnly == true
k := -9999
k
else
k := 0
k
td = time - time[5]
// TODO: toggle to shower ATR ranges since at higher timeframes it looks insane
displayAtrRanges = str.tonumber(timeframe.period) < 15
// Daily High
dayHighAtrUpper = plot(displayAtrRanges ? dailyhigh + atr : na, color = color.new(color.red, 100), title = 'Day High Upper')
dayHighAtrLower = plot(displayAtrRanges ? dailyhigh - atr : na, color = color.new(color.red, 100), title = 'Day High Lower')
fill(dayHighAtrUpper, dayHighAtrLower, color = color.new(#ffffff, 90))
plot(dailyhigh, style=plot.style_line, title='Day High', color=color.new(#ffffff, 0), linewidth=1, trackprice=true, offset=k)
dh = label.new(x=time + td, y=dailyhigh, text='Day High', xloc=xloc.bar_time, style=label.style_none, textcolor=#ffffff, size=size.normal, textalign=text.align_center)
label.delete(dh[1])
// Daily Low
dayLowAtrUpper = plot(displayAtrRanges ? dailylow + atr : na, color = color.new(color.red, 100), title = 'Day Low Upper')
dayLowAtrLower = plot(displayAtrRanges ? dailylow - atr : na, color = color.new(color.red, 100), title = 'Day Low Lower')
fill(dayHighAtrUpper, dayHighAtrLower, color = color.new(#9c27b0, 90))
plot(dailylow, style=plot.style_line, title='Day Low', color=color.new(#9c27b0, 0), linewidth=1, trackprice=true, offset=k)
dl = label.new(x=time + td, y=dailylow, text='Day Low', xloc=xloc.bar_time, style=label.style_none, textcolor=#9c27b0, size=size.normal, textalign=text.align_center)
label.delete(dl[1])
// Previous Day High
pDayHighAtrUpper = plot(displayAtrRanges ? previousdayhigh + atr : na, color = color.new(color.red, 100), title = 'Previous Day High Upper')
pDayHighAtrBottom = plot(displayAtrRanges ? previousdayhigh - atr : na, color = color.new(color.red, 100), title = 'Previous Day High Lower')
fill(pDayHighAtrUpper, pDayHighAtrBottom, color = color.new(#fffb00, 90))
plot(previousdayhigh, style=plot.style_line, title='Previous High', color=color.new(#fffb00, 0), linewidth=1, trackprice=true, offset=k)
pdh = label.new(x=time + td, y=previousdayhigh, text='Previous High', xloc=xloc.bar_time, style=label.style_none, textcolor=#fbff00, size=size.normal, textalign=text.align_center)
label.delete(pdh[1])
// Previous Day Low
pDayLowAtrUpper = plot(displayAtrRanges ? previousdaylow + atr : na, color = color.new(color.red, 100) , title = 'Previous Day Low Upper')
pDayLowAtrBottom = plot(displayAtrRanges ? previousdaylow - atr : na, color = color.new(color.red, 100), title = 'Previous Day Low Lower')
fill(pDayLowAtrUpper, pDayLowAtrBottom, color = color.new(#ff90e7, 90))
plot(previousdaylow, style=plot.style_line, title='Previous Low', color=color.new(#ff90e7, 0), linewidth=1, trackprice=true, offset=k)
pdl = label.new(x=time + td, y=previousdaylow, text='Previous Low', xloc=xloc.bar_time, style=label.style_none, textcolor=#ff90e7, size=size.normal, textalign=text.align_center)
label.delete(pdl[1])
// Pre-market High
pmHighAtrUpper = plot(displayAtrRanges ? pm_high + atr : na, color = color.new(color.red, 100), title = 'PM High Upper')
pmHighAtrBottom = plot(displayAtrRanges ? pm_high - atr : na, color = color.new(color.red, 100), title = 'PM High Lower')
fill(pmHighAtrUpper, pmHighAtrBottom, color = color.new(#5eff00, 90))
plot(pm_high, style=plot.style_line, title='Premarket high', trackprice=true, color=color.new(#5eff00, 0), linewidth=1, offset=k)
pmh = label.new(x=time + td, y=pm_high, text='PM High', xloc=xloc.bar_time, style=label.style_none, textcolor=#5eff00, size=size.normal, textalign=text.align_center)
label.delete(pmh[1])
// Pre-market Low
pmLowAtrUpper = plot(displayAtrRanges ? pm_low + atr : na, color = color.new(color.red, 100), title = 'PM Low Upper')
pmLowAtrBottom = plot(displayAtrRanges ? pm_low - atr : na, color = color.new(color.red, 100), title = 'PM Low Lower')
fill(pmLowAtrUpper, pmLowAtrBottom, color = color.new(#ff0000, 90))
plot(pm_low, style=plot.style_line, title='Premarket low', trackprice=true, color=color.new(#ff0000, 0), linewidth=1, offset=k)
pml = label.new(x=time + td, y=pm_low, text='PM Low', xloc=xloc.bar_time, style=label.style_none, textcolor=#ff0000, size=size.normal, textalign=text.align_center)
label.delete(pml[1])
// ****** ************************ ********** //
// ********** MOVING AVERAGES & VWAP ********** //
ema_5 = ta.ema(close, 5)
sma_15 = ta.sma(close, 15)
vwap = ta.vwap(close)
sma_200_daily = request.security(syminfo.tickerid, 'D', ta.sma(close, 200), barmerge.gaps_on, barmerge.lookahead_off)
plot(ema_5, title = 'SMA 5', color = color.new(color.green, 0))
plot(sma_15, title = 'SMA 15', color = color.new(color.purple, 0))
plot(vwap, title = 'VWAP', color = color.new(color.gray, 0))
// plot(sma_200_daily, title = 'Daily 200 SMA', color = close > sma_200_daily ? color.green : color.red)
crossOverMsg = '{ "username": "Susan", "content": "' + syminfo.ticker + ' 5ema has crossed over the 15sma on the 5m @ ' + str.tostring(close) + '" }'
crossUnderMsg = '{ "username": "Susan", "content": "' + syminfo.ticker + ' 5ema has crossed under the 15sma on the 5m @ ' + str.tostring(close) + '" }'
if (ta.crossover(ema_5, sma_15))
alert(crossOverMsg, alert.freq_once_per_bar)
if (ta.crossunder(ema_5, sma_15))
alert(crossUnderMsg, alert.freq_once_per_bar)
// ****** ************************ ********** //
// ********** Daily MACD Cross Alert ********** //
macdFast = 12
macdSlow = 26
dailyFastMA = request.security(syminfo.tickerid, 'D', ta.ema(close, macdFast), barmerge.gaps_on, barmerge.lookahead_off)
dailySlowMA = request.security(syminfo.tickerid, 'D', ta.ema(close, macdSlow), barmerge.gaps_on, barmerge.lookahead_off)
dailyMacd = dailyFastMA - dailySlowMA
dailyMacdSignal = ta.ema(dailyMacd, 9)
dailyMacdCrossOver = ta.crossover(dailyMacd, dailyMacdSignal)
dailyMacdCrossUnder = ta.crossunder(dailyMacd, dailyMacdSignal)
alertcondition(dailyMacdCrossOver or dailyMacdCrossUnder, title = 'Daily MACD Cross Alert')
// ****** ************************ ********** //