-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTurtles Indicators & Alerts
113 lines (89 loc) · 5.54 KB
/
Turtles Indicators & Alerts
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
//@version=5
indicator('Turtles Indicators & Alerts', shorttitle='Turtles Indicators & Alerts', overlay=true)
enter_fast = input.int(20, minval=1)
exit_fast = input.int(10, minval=1)
enter_slow = input.int(55, minval=1)
exit_slow = input.int(20, minval=1)
// === BACKTEST RANGE ===
FromMonth = input.int(defval=1, title='From Month', minval=1)
FromDay = input.int(defval=1, title='From Day', minval=1)
FromYear = input.int(defval=2019, title='From Year', minval=2019)
ToMonth = input.int(defval=1, title='To Month', minval=1)
ToDay = input.int(defval=1, title='To Day', minval=1)
ToYear = input.int(defval=9999, title='To Year', minval=9999)
fastL = ta.highest(enter_fast)
fastLC = ta.lowest(exit_fast)
fastS = ta.lowest(enter_fast)
fastSC = ta.highest(exit_fast)
slowL = ta.highest(enter_slow)
slowLC = ta.lowest(exit_slow)
slowS = ta.lowest(enter_slow)
slowSC = ta.highest(exit_slow)
enterL1 = high > fastL[1] and time > timestamp(FromYear, FromMonth, FromDay, 00, 00) and time < timestamp(ToYear, ToMonth, ToDay, 23, 59)
exitL1 = low <= fastLC[1] and time < timestamp(ToYear, ToMonth, ToDay, 23, 59)
enterL1_filt = enterL1 and not exitL1
exitL1_filt = exitL1 and not enterL1
prevL1 = 0
prevL1 := exitL1_filt ? 1 : enterL1_filt ? -1 : prevL1[1]
enterL1_final = enterL1_filt and prevL1[1] == 1
exitL1_final = exitL1_filt and prevL1[1] == -1
enterS1 = low < fastS[1] and time > timestamp(FromYear, FromMonth, FromDay, 00, 00) and time < timestamp(ToYear, ToMonth, ToDay, 23, 59)
exitS1 = high >= fastSC[1] and time < timestamp(ToYear, ToMonth, ToDay, 23, 59)
enterS1_filt = enterS1 and not exitS1
exitS1_filt = exitS1 and not enterS1
prevS1 = 0
prevS1 := exitS1_filt ? 1 : enterS1_filt ? -1 : prevS1[1]
enterS1_final = enterS1_filt and prevS1[1] == 1
exitS1_final = exitS1_filt and prevS1[1] == -1
enterL2 = high > slowL[1] and time > timestamp(FromYear, FromMonth, FromDay, 00, 00) and time < timestamp(ToYear, ToMonth, ToDay, 23, 59)
exitL2 = low <= slowLC[1] and time < timestamp(ToYear, ToMonth, ToDay, 23, 59)
enterL2_filt = enterL2 and not exitL2
exitL2_filt = exitL2 and not enterL2
prevL2 = 0
prevL2 := exitL2_filt ? 1 : enterL2_filt ? -1 : prevL2[1]
enterL2_final = enterL2_filt and prevL2[1] == 1
exitL2_final = exitL2_filt and prevL2[1] == -1
enterS2 = low < slowS[1] and time > timestamp(FromYear, FromMonth, FromDay, 00, 00) and time < timestamp(ToYear, ToMonth, ToDay, 23, 59)
exitS2 = high >= slowSC[1] and time < timestamp(ToYear, ToMonth, ToDay, 23, 59)
enterS2_filt = enterS2 and not exitS2
exitS2_filt = exitS2 and not enterS2
prevS2 = 0
prevS2 := exitS2_filt ? 1 : enterS2_filt ? -1 : prevS2[1]
enterS2_final = enterS2_filt and prevS2[1] == 1
exitS2_final = exitS2_filt and prevS2[1] == -1
//bgcolor(exitL1_final or enterS1_final or exitL2_final or enterS2_final? red: enterL1_final or exitS1_final or enterL2_final or exitS2_final? navy:white)
// Plotting
// fast L
plotshape(enterL1_final, color=color.new(#1e90ff, 0), text='OFL', style=shape.triangleup, location=location.belowbar, size=size.small)
plotshape(exitL1_final, color=color.new(#ff69b4, 0), text='CFL', style=shape.triangledown, location=location.abovebar, size=size.small)
// fast S
plotshape(enterS1_final, color=color.new(#ff69b4, 0), text='OFS', style=shape.triangledown, location=location.abovebar, size=size.small)
plotshape(exitS1_final, color=color.new(#1e90ff, 0), text='CFS', style=shape.triangleup, location=location.belowbar, size=size.small)
// slow L
plotshape(enterL2_final, color=color.new(#0000ff, 0), text='OSL', style=shape.triangleup, location=location.belowbar, size=size.small)
plotshape(exitL2_final, color=color.new(#ff0000, 0), text='CSL', style=shape.triangledown, location=location.abovebar, size=size.small)
// slow S
plotshape(enterS2_final, color=color.new(#ff0000, 0), text='OSS', style=shape.triangledown, location=location.abovebar, size=size.small)
plotshape(exitS2_final, color=color.new(#0000ff, 0), text='CSS', style=shape.triangleup, location=location.belowbar, size=size.small)
// Alerts
// fast L
alertcondition(enterL1_final, 'Enter fast L', 'Enter fast L')
alertcondition(exitL1_final, 'Exit fast L', 'Exit fast L')
// fast S
alertcondition(enterS1_final, 'Enter fast S', 'Enter fast S')
alertcondition(exitS1_final, 'Exit fast S', 'Exit fast S')
// slow L
alertcondition(enterL2_final, 'Enter slow L', 'Enter slow L')
alertcondition(exitL2_final, 'Exit slow L', 'Exit slow L')
// slow S
alertcondition(enterS2_final, 'Enter slow S', 'Enter slow S')
alertcondition(exitS2_final, 'Exit slow S', 'Exit slow S')
// Original code from strategy
//strategy.entry("fast L", strategy.long, when = enterL1 and (time > timestamp(FromYear, FromMonth, FromDay, 00, 00)) and (time < timestamp(ToYear, ToMonth, ToDay, 23, 59)))
//strategy.entry("fast S", strategy.short, when = enterS1 and (time > timestamp(FromYear, FromMonth, FromDay, 00, 00)) and (time < timestamp(ToYear, ToMonth, ToDay, 23, 59)))
//strategy.close("fast L", when = exitL1 and (time < timestamp(ToYear, ToMonth, ToDay, 23, 59)))
//strategy.close("fast S", when = exitS1 and (time < timestamp(ToYear, ToMonth, ToDay, 23, 59)))
//strategy.entry("slow L", strategy.long, when = enterL2 and (time > timestamp(FromYear, FromMonth, FromDay, 00, 00)) and (time < timestamp(ToYear, ToMonth, ToDay, 23, 59)))
//strategy.entry("slow S", strategy.short, when = enterS2 and (time > timestamp(FromYear, FromMonth, FromDay, 00, 00)) and (time < timestamp(ToYear, ToMonth, ToDay, 23, 59)))
//strategy.close("slow L", when = exitL2 and (time < timestamp(ToYear, ToMonth, ToDay, 23, 59)))
//strategy.close("slow S", when = exitS2 and (time < timestamp(ToYear, ToMonth, ToDay, 23, 59)))