-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAbbas1.pine
429 lines (387 loc) · 20.7 KB
/
Abbas1.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//@version=5
indicator(title='Range | Blocks | NYOpens | Weekendicator | CME | BPR', shorttitle="Abbas1", overlay=true, precision=4, linktoseries=true, max_bars_back=1000, max_lines_count=500)
//Bollinger Bands
length = input.int(21, minval=1, group="BB")
src = input(close, title="Source", group="BB")
mult = input.float(2.8, minval=0.5, maxval=3, step=0.01, group="BB")
basis = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev
plot (basis, "BB Basis", color=color.new(#ff9800, 60))
p1a = plot (upper, "BB Upper", color=color.new(#089981, 60))
p1b = plot (lower, "BB Lower", color=color.new(#f23645, 60))
//weekendicator
multiplier = input.float(3, minval=0, maxval=10, step=0.1, group = "Weekend")
day_clr = dayofweek < 2 or dayofweek > 6 ? color.new(#9598a1, 80) : color.new(#9598a1, 100)
plot(((high / low) * ohlc4) * (1 + multiplier / 100), "Weekendicator Above", style=plot.style_line, color=day_clr)
plot(((low / high) * ohlc4) * (1 - multiplier / 100), "Weekendicator Below", style=plot.style_area, color=day_clr)
//range blocks
bool msb_sv = input.bool (true, title='Plot MSB lines', group="Range Blocks")
bool box_sv = input.bool (true, title='Plot Orderblocks', group="Range Blocks")
bool m_sv = input.bool (true, title='Plot Breakerblocks', group="Range Blocks")
bool range_sv = input.bool (true, title='Plot Range', group="Range Blocks")
bool range_eq_sv = input.bool (true, title='Plot Range 0.5 Line', group="Range Blocks")
bool range_q_sv = input.bool (true, title='Plot Range 0.25 and 0.75 Lines', group="Range Blocks")
color u_s = input.color (color.new(#089981, 80), title='Untested Supply Color', group="Range Blocks")
color t_s = input.color (color.new(#b2b5be, 80), title='Tested Supply Color', group="Range Blocks")
color u_d = input.color (color.new(#f23645, 80), title='Untested Demand Color', group="Range Blocks")
color t_d = input.color (color.new(#b2b5be, 80), title='Tested Demand Color', group="Range Blocks")
color u_b = input.color (color.new(#ff9800, 80), title='Untested Breaker Color', group="Range Blocks")
color t_b = input.color (color.new(#b2b5be, 80), title='Tested Breaker Color', group="Range Blocks")
var float[] pvh1_price = array.new_float (30, na) // high
var int[] pvh1_time = array.new_int (30, na)
var float[] pvl1_price = array.new_float (30, na) // low
var int[] pvl1_time = array.new_int (30, na)
var float[] pvh2_price = array.new_float (10, na) // higher high
var int[] pvh2_time = array.new_int (10, na)
var float[] pvl2_price = array.new_float (10, na) // lower low
var int[] pvl2_time = array.new_int (10, na)
var float htcmrll_price = na // high that created most recent ll
var int htcmrll_time = na
var float ltcmrhh_price = na // low that created most recent hh
var int ltcmrhh_time = na
var box[] long_boxes = array.new_box() // orderblocks
var box[] short_boxes = array.new_box()
var box[] m_long_boxes = array.new_box() // breakerblocks
var box[] m_short_boxes = array.new_box()
var line[] bull_bos_lines = array.new_line() // MSB lines
var line[] bear_bos_lines = array.new_line()
var line[] range_h_lines = array.new_line() // Range lines
var line[] range_25_lines = array.new_line()
var line[] range_m_lines = array.new_line()
var line[] range_75_lines = array.new_line()
var line[] range_l_lines = array.new_line()
var label[] la_ph2 = array.new_label() // 2nd order pivots
var label[] la_pl2 = array.new_label()
var float temp_pv_0 = na
var float temp_pv_1 = na
var float temp_pv_2 = na
var int temp_time = na
var float last_range_h = na
var float last_range_l = na
var line range_m = na
var line range_25 = na
var line range_75 = na
var float box_top = na
var float box_bottom = na
bool pvh = high < high[1] and high[1] > high[2]
bool pvl = low > low[1] and low[1] < low[2]
int pv1_time = bar_index[1]
float pv1_high = high[1]
float pv1_low = low[1]
bool new_ph_2nd = false
bool new_pl_2nd = false
if barstate.isconfirmed
if pvh
array.pop(pvh1_price)
array.pop(pvh1_time)
array.unshift(pvh1_price, pv1_high)
array.unshift(pvh1_time, pv1_time)
if array.size(pvh1_price) > 2
temp_pv_0 := array.get(pvh1_price, 0)
temp_pv_1 := array.get(pvh1_price, 1)
temp_pv_2 := array.get(pvh1_price, 2)
if temp_pv_0 < temp_pv_1 and temp_pv_1 > temp_pv_2
array.pop(pvh2_price)
array.pop(pvh2_time)
array.unshift(pvh2_price, temp_pv_1)
array.unshift(pvh2_time, array.get(pvh1_time, 1))
new_ph_2nd := true
if temp_pv_1 > array.get(pvh2_price, 1)
for i = 0 to array.size(pvl2_time) - 1 by 1
temp_ltcmrhh_time = array.get(pvl2_time, i)
if temp_ltcmrhh_time < array.get(pvh2_time, 0)
ltcmrhh_price := array.get(pvl2_price, i)
ltcmrhh_time := temp_ltcmrhh_time
break
if temp_pv_0 < ltcmrhh_price
if msb_sv
array.push(bear_bos_lines, line.new(x1=ltcmrhh_time, y1=ltcmrhh_price, x2=bar_index, y2=ltcmrhh_price, color=color.fuchsia, width=2))
box_top := array.get(pvh2_price, 0)
box_bottom := math.max(low[bar_index - array.get(pvh2_time, 0)], low[bar_index - array.get(pvh2_time, 0) + 1])
array.push(short_boxes, box.new(left=array.get(pvh2_time, 0), top=box_top, right=bar_index, bottom=box_bottom, bgcolor= box_sv ? u_s : na , border_color=na, extend=extend.right))
ltcmrhh_price := na
if pvl
array.pop(pvl1_price)
array.pop(pvl1_time)
array.unshift(pvl1_price, pv1_low)
array.unshift(pvl1_time, pv1_time)
if array.size(pvl1_price) > 2
temp_pv_0 := array.get(pvl1_price, 0)
temp_pv_1 := array.get(pvl1_price, 1)
temp_pv_2 := array.get(pvl1_price, 2)
if temp_pv_0 > temp_pv_1 and temp_pv_1 < temp_pv_2
array.pop(pvl2_price)
array.pop(pvl2_time)
array.unshift(pvl2_price, temp_pv_1)
array.unshift(pvl2_time, array.get(pvl1_time, 1))
new_pl_2nd := true
if temp_pv_1 < array.get(pvl2_price, 1)
for i = 0 to array.size(pvh2_time) - 1 by 1
temp_htcmrll_time = array.get(pvh2_time, i)
if temp_htcmrll_time < array.get(pvl2_time, 0)
htcmrll_price := array.get(pvh2_price, i)
htcmrll_time := temp_htcmrll_time
break
if temp_pv_0 > htcmrll_price
if msb_sv
array.push(bull_bos_lines, line.new(x1=htcmrll_time, y1=htcmrll_price, x2=bar_index, y2=htcmrll_price, color=color.olive, width=2))
box_top := math.min(high[bar_index - array.get(pvl2_time, 0)], high[bar_index - array.get(pvl2_time, 0) + 1])
box_bottom := array.get(pvl2_price, 0)
array.push(long_boxes, box.new(left=array.get(pvl2_time, 0), top=box_top, right=bar_index, bottom=box_bottom, bgcolor= box_sv ? u_d : na, border_color=na, extend=extend.right))
htcmrll_price := na
if array.size(short_boxes) > 0
for i = array.size(short_boxes) - 1 to 0 by 1
tbox = array.get(short_boxes, i)
top = box.get_top(tbox)
bottom = box.get_bottom(tbox)
ago = box.get_left(tbox)
if array.get(pvh1_price, 0) > bottom
if box_sv
box.set_bgcolor(tbox, t_s)
if array.get(pvl1_price, 0) > top
if m_sv
box.set_bgcolor(tbox, u_b)
array.push(m_long_boxes, tbox)
else
box.delete(tbox)
array.remove(short_boxes, i)
if msb_sv
line.delete(array.get(bear_bos_lines, i))
array.remove(bear_bos_lines, i)
if array.size(long_boxes) > 0
for i = array.size(long_boxes) - 1 to 0 by 1
lbox = array.get(long_boxes, i)
top = box.get_top(lbox)
bottom = box.get_bottom(lbox)
ago = box.get_left(lbox)
if array.get(pvl1_price, 0) < top
if box_sv
box.set_bgcolor(lbox, t_d)
if array.get(pvh1_price, 0) < bottom
if m_sv
box.set_bgcolor(lbox, u_b)
array.push(m_short_boxes, lbox)
else
box.delete(lbox)
array.remove(long_boxes, i)
if msb_sv
line.delete(array.get(bull_bos_lines, i))
array.remove(bull_bos_lines, i)
if array.size(m_short_boxes) > 0
for i = array.size(m_short_boxes) - 1 to 0 by 1
tbox = array.get(m_short_boxes, i)
top = box.get_top(tbox)
bottom = box.get_bottom(tbox)
ago = box.get_left(tbox)
if array.get(pvh1_price, 0) > bottom
box.set_bgcolor(tbox, t_b)
if array.get(pvl1_price, 0) > top
box.delete(tbox)
array.remove(m_short_boxes, i)
if array.size(m_long_boxes) > 0
for i = array.size(m_long_boxes) - 1 to 0 by 1
lbox = array.get(m_long_boxes, i)
top = box.get_top(lbox)
bottom = box.get_bottom(lbox)
ago = box.get_left(lbox)
if array.get(pvl1_price, 0) < top
box.set_bgcolor(lbox, t_b)
if array.get(pvh1_price, 0) < bottom
box.delete(lbox)
array.remove(m_long_boxes, i)
if range_sv and (new_ph_2nd or new_pl_2nd) and (array.get(pvh2_price, 0) < array.get(pvh2_price, 1) and array.get(pvl2_price, 0) > array.get(pvl2_price, 1) and array.get(pvh2_price, 0) > array.get(pvl2_price, 1) and array.get(pvl2_price, 0) < array.get(pvh2_price, 1)) and (array.get(pvl2_price, 1) > nz(last_range_h) or na(last_range_l)? true : (array.get(pvh2_price, 1) < last_range_l))
temp_time := math.min(array.get(pvh2_time, 1), array.get(pvl2_time, 1))
last_range_h := array.get(pvh2_price, 1)
last_range_l := array.get(pvl2_price, 1)
temp_pv_0 := (last_range_h + last_range_l)/2
temp_pv_1 := (last_range_h + temp_pv_0)/2
temp_pv_2 := (last_range_l + temp_pv_0)/2
array.push(range_h_lines, line.new(x1=temp_time, y1=last_range_h, x2=bar_index, y2=last_range_h, color=color.black, width=2, extend=extend.right))
array.push(range_l_lines, line.new(x1=temp_time, y1=last_range_l, x2=bar_index, y2=last_range_l, color=color.black, width=2, extend=extend.right))
if range_eq_sv
array.push(range_m_lines, line.new(x1=temp_time, y1=temp_pv_0, x2=bar_index, y2=temp_pv_0, color=color.gray, width=2, extend=extend.right))
if range_q_sv
array.push(range_25_lines, line.new(x1=temp_time, y1=temp_pv_1, x2=bar_index, y2=temp_pv_1, style=line.style_dashed, color=color.gray, width=1, extend=extend.right))
array.push(range_75_lines, line.new(x1=temp_time, y1=temp_pv_2, x2=bar_index, y2=temp_pv_2, style=line.style_dashed, color=color.gray, width=1, extend=extend.right))
if array.size(range_h_lines) > 0
for i = array.size(range_h_lines) - 1 to 0 by 1
range_h = array.get(range_h_lines, i)
top = line.get_y1(range_h)
range_l = array.get(range_l_lines, i)
bottom = line.get_y1(range_l)
temp_time := line.get_x1(range_h)
if range_eq_sv
range_m := array.get(range_m_lines, i)
if range_q_sv
range_25 := array.get(range_25_lines, i)
range_75 := array.get(range_75_lines, i)
if array.get(pvh1_price, 0) < bottom or array.get(pvl1_price, 0) > top
line.delete(range_h)
array.remove(range_h_lines, i)
line.delete(range_l)
array.remove(range_l_lines, i)
if range_eq_sv
line.delete(range_m)
array.remove(range_m_lines, i)
if range_q_sv
line.delete(range_25)
array.remove(range_25_lines, i)
line.delete(range_75)
array.remove(range_75_lines, i)
last_range_h := na
last_range_l := na
////////////////////////////////////////////////////////////////////////////////
//new york open etc
iMDisplay = input.bool (true, "Display", group="New York Midnight Open")
iMTime = input.session ('0400-0405:1234567', "Session", group="New York Midnight Open")
iMStyle = input.string ("Dashed", "Line Style", options=["Solid", "Dotted", "Dashed"], group="New York Midnight Open")
iMColor = input.color (#58A2B0, "Color", group="New York Midnight Open")
iMHistory = input.bool (false, "History", group="New York Midnight Open")
iMLabel = input.bool (true, "Show Label", group="New York Midnight Open")
i8Display = input.bool (true, "Display", group="New York 8:30 Open")
i8Time = input.session ('1230-1235:1234567', "Session", group="New York 8:30 Open")
i8Style = input.string ("Dotted", "Line Style", options=["Solid", "Dotted", "Dashed"], group="New York 8:30 Open")
i8Color = input.color (#58A2B0, "Color", group="New York 8:30 Open")
i8History = input.bool (false, "History", group="New York 8:30 Open")
i8Label = input.bool (true, "Show Label", group="New York 8:30 Open")
tMidnight = time ("1", iMTime)
t830 = time ("1", i8Time)
_MStyle = iMStyle == "Solid" ? line.style_solid : iMStyle == "Dotted" ? line.style_dotted : line.style_dashed
_8Style = i8Style == "Solid" ? line.style_solid : i8Style == "Dotted" ? line.style_dotted : line.style_dashed
//==== Midnight Open ====
if iMDisplay
var openMidnight = 0.0
if tMidnight
if not tMidnight[1]
openMidnight := open
else
openMidnight := math.max(open, openMidnight)
var label lb = na
var line lne = na
if openMidnight != openMidnight[1]
if barstate.isconfirmed
line.set_x2(lne, tMidnight)
lne := line.new(tMidnight, openMidnight, last_bar_time + 14400000/2, openMidnight, xloc.bar_time, extend.none, iMColor, _MStyle, 1)
if iMLabel
lb := label.new(last_bar_time + 14400000/2, openMidnight, "Midnight", xloc.bar_time, yloc.price, na, label.style_none, iMColor, size.normal, text.align_right)
label.delete(lb[1])
if not iMHistory
line.delete(lne[1])
//===========================
//==== 8:30 Open ====
if i8Display
var open830 = 0.0
if t830
if not t830[1]
open830 := open
else
open830 := math.max(open, open830)
var label lb2 = na
var line lne2 = na
if open830 != open830[1]
if barstate.isconfirmed
line.set_x2(lne2, t830 - 30600000)
lne2 := line.new(t830, open830, last_bar_time + 14400000/2, open830, xloc.bar_time, extend.none, i8Color, _8Style, 1)
if i8Label
lb2 := label.new(last_bar_time + 14400000/2, open830, "8:30", xloc.bar_time, yloc.price, na, label.style_none, i8Color, size.normal, text.align_right)
label.delete(lb2[1])
if not i8History
line.delete(lne2[1])
//===========================
// Balanced Price Range
bpr_threshold = input.float(0, step = 0.25, title = "BPR Threshold", tooltip = "Valid BPR's must have a range greater than this number", group = "Balanced Price Range")
bars_since = input(50, "Bars to Look Back for BPR", tooltip = "Only look for BPR's when a sequence of bearish and bullish FVG's are within this many bars of each other", group = "Balanced Price Range")
only_clean_bpr = input(false, "Only Clean BPR", tooltip = "Only show BPR's when price does not interfere with the range prior to its completion", group = "Balanced Price Range")
delete_old_bpr = input(true, "Delete Old BPR", tooltip = "Delete all BPR's that have been invalidated or overwritten. Only show current/active BPR's", group = "Balanced Price Range")
bearish_bpr_color = input.color(color.new(color.green, 70), group = "Balanced Price Range")
bullish_bpr_color = input.color(color.new(color.green, 70), group = "Balanced Price Range")
float box_high = na
float box_low = na
int box_left = 0
int box_right = 0
var box box_bearish = na
var box box_bullish = na
new_fvg_bearish = low[2] - high > 0
new_fvg_bullish = low - high[2] > 0
valid_high = high[1] > high[2] and high[1] > high[0]
valid_low = low[1] < low[2] and low[1] < low[0]
midline = (high - low) / 2 + low
valid_hammer = open > midline and close > midline
valid_shooter = open < midline and close < midline
// Bullish BPR
bull_num_since = ta.barssince(new_fvg_bearish)
bull_bpr_cond_1 = new_fvg_bullish and bull_num_since <= bars_since
bull_bpr_cond_2 = bull_bpr_cond_1 ? high[bull_num_since] + low[bull_num_since + 2] + high[2] + low > math.max(low[bull_num_since + 2], low) - math.min(high[bull_num_since], high[2]) : na
bull_combined_low = bull_bpr_cond_2 ? math.max(high[bull_num_since], high[2]) : na
bull_combined_high = bull_bpr_cond_2 ? math.min(low[bull_num_since + 2], low) : na
bull_bpr_cond_3 = true
if only_clean_bpr
for h = 2 to (bull_num_since)
if high[h] > bull_combined_low
bull_bpr_cond_3 := false
bull_result = bull_bpr_cond_1 and bull_bpr_cond_2 and bull_bpr_cond_3 and (bull_combined_high - bull_combined_low >= bpr_threshold)
if bull_result[1]
if delete_old_bpr and not na(box_bullish)
box.delete(box_bullish)
box_bullish := box.new(bar_index - bull_num_since - 1, bull_combined_high[1], bar_index + 1, bull_combined_low[1], border_color = bullish_bpr_color, border_width = 1, bgcolor = bullish_bpr_color)
if not na(box_bullish) and low > box.get_bottom(box_bullish)
box.set_right(box_bullish, bar_index + 1)
else if not na(box_bullish) and low < box.get_bottom(box_bullish)
if delete_old_bpr
box.delete(box_bullish)
else
box_bullish := na
// Bearish BPR
bear_num_since = ta.barssince(new_fvg_bullish)
bear_bpr_cond_1 = new_fvg_bearish and bear_num_since <= bars_since
bear_bpr_cond_2 = bear_bpr_cond_1 ? high[bear_num_since] + low[bear_num_since + 2] + high[2] + low > math.max(low[bear_num_since + 2], low) - math.min(high[bear_num_since], high[2]) : na
bear_combined_low = bear_bpr_cond_2 ? math.max(high[bear_num_since + 2], high) : na
bear_combined_high = bear_bpr_cond_2 ? math.min(low[bear_num_since], low[2]) : na
bear_bpr_cond_3 = true
if only_clean_bpr
for h = 2 to (bear_num_since)
if low[h] < bear_combined_high
bear_bpr_cond_3 := false
bear_result = bear_bpr_cond_1 and bear_bpr_cond_2 and bear_bpr_cond_3 and (bear_combined_high - bear_combined_low >= bpr_threshold)
if bear_result[1]
if delete_old_bpr and not na(box_bearish)
box.delete(box_bearish)
box_bearish := box.new(bar_index - bear_num_since - 1, bear_combined_high[1], bar_index + 1, bear_combined_low[1], border_color = bearish_bpr_color, border_width = 1, bgcolor = bearish_bpr_color)
if not na(box_bearish) and high < box.get_top(box_bearish)
box.set_right(box_bearish, bar_index + 1)
else if not na(box_bearish) and high > box.get_top(box_bearish)
if delete_old_bpr
box.delete(box_bearish)
else
box_bearish := na
////////////////////////////////////////////////////////////////////////////////
//CME gap show and fill
mode = input.string(
defval="1 - Close from current symbol",
title="Mode",
options=["1 - Close from current symbol", "2 - CME original close crice", "3 - CME settlement price"],
tooltip="In Mode 1 the closing price is determined in the current symbol but with the tradinghours from the CME futures contract. Mode 2 and 3 obtain the price directly from the CME futures contract and paint it in the chart of the current symbol. But note, that modes 2 and 3 may not give you the expected result, due to price differences in futures and spot prices.",
group="CME gap")
cme = request.security("CME:BTC1!", "60", close)
cmeSettlement = request.security("CME:BTC1!", "D", close, lookahead=barmerge.lookahead_on)
//Function to get friday closing price according to CME trading hours
getCloseCME() =>
cmeClose = 0.0
cmeClosePrev = nz(cmeClose[1], cmeClose)
showLine = 0
showLine := nz(showLine[1], showLine)
if mode == "1 - Close from current symbol"
cmeClose := dayofweek == 6 and time == timestamp('GMT-5', year, month, dayofmonth, 16, 0, 0) ? close[1] : cmeClosePrev
else if mode == "2 - CME original close crice"
cmeClose := dayofweek == 6 and time == timestamp('GMT-5', year, month, dayofmonth, 16, 0, 0) ? cme : cmeClosePrev
else if mode == "3 - CME settlement price"
cmeClose := dayofweek == 6 and time == timestamp('GMT-5', year, month, dayofmonth, 16, 0, 0) ? cmeSettlement : cmeClosePrev
showLine := showLine == 0 and time >= timestamp('GMT-5', year, month, dayofmonth, 16, 0, 0) and dayofweek >= 6 ? 1 : showLine == 1 and dayofweek <= 1 and time >= timestamp('GMT-5', year, month, dayofmonth, 17, 0, 0) ? 0 : showLine
[cmeClose, showLine]
[cmeClose, showLine] = getCloseCME()
//Plotting
plot1 = plot(showLine == 1 ? cmeClose : na, 'CME Friday Close', style=plot.style_linebr, linewidth=2, color=color.new(color.blue, 0))
plot2 = plot(close, 'Dummy plot for background', color=na)
fill(plot1, plot2, title='CME Background', color=close > cmeClose ? color.new(color.green, 80) : close < cmeClose ? color.new(color.red, 80) : na)