Repository navigation
Expand file tree
/
Copy pathichimoku_mtf.pinescript
More file actions
278 lines (224 loc) · 16.4 KB
/
Copy pathichimoku_mtf.pinescript
File metadata and controls
278 lines (224 loc) · 16.4 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
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
//@version=4
strategy("MTFIchi2", overlay=true, calc_on_every_tick=true, commission_type=strategy.commission.percent,commission_value=0.075, initial_capital = 100000, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// ——————————————————————————————————————————————————————————————————————————————————————
// ——————————————————————————————————— TIME RANGES —————————————————————————————————————
// ——————————————————————————————————————————————————————————————————————————————————————
startDate = input(title="Start Date", type=input.integer,
defval=1, minval=1, maxval=31)
startMonth = input(title="Start Month", type=input.integer,
defval=1, minval=1, maxval=12)
startYear = input(title="Start Year", type=input.integer,
defval=2017, minval=1800, maxval=2100)
endDate = input(title="End Date", type=input.integer,
defval=21, minval=1, maxval=31)
endMonth = input(title="End Month", type=input.integer,
defval=4, minval=1, maxval=12)
endYear = input(title="End Year", type=input.integer,
defval=2021, minval=1800, maxval=2100)
// Look if the close time of the current bar
// falls inside the date range
inDateRange = (time >= timestamp(syminfo.timezone, startYear,
startMonth, startDate, 0, 0)) and
(time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))
// ——————————————————————————————————————————————————————————————————————————————————————
// ——————————————————————————————————— CONFIGURATION ———————————————————————————————————
// ——————————————————————————————————————————————————————————————————————————————————————
C00 = "Original", C01 = "Aqua", C02 = "Black", C03 = "Blue", C04 = "Coral", C05 = "Gold", C06 = "Gray", C07 = "Green", C08 = "Lime", C09 = "Maroon", C10 = "Orange", C11 = "Pink", C12 = "Red", C13 = "Violet", C14 = "Yellow", C15 = "White"
_base = input(true, "─────── Base parameters ───────")
conversionPeriods = input(9, minval=1, title="Conversion Line Periods"),
basePeriods = input(26, minval=1, title="Base Line Periods")
laggingSpan2Periods = input(52, minval=1, title="Lagging Span 2 Periods"),
displacement = input(26, minval=1, title="Displacement")
isActiveFirst = input(true, "──────── First ichimoku ───────")
firstIchi = input("240", "► Time frame", type = input.resolution)
isActiveFirstTenkan = input(true, "Tenkan")
isActiveFirstKijun = input(true, "Kijun")
isActiveFirstChikou = input(true, "Chikou")
isActiveFirstKumo = input(true, "Kumo ──────────────")
isActiveFirstMonoKumo = input(false, " ▪ Monochrome")
isActiveFirstLabel = input(true, "Label ──────────────")
labelFirstOffset = input(4, " ▪ Offset", minval = -20, maxval = 20)
labelFirstColor = input(C00, " ▪ Color", options = [C00, C01, C02, C03, C04, C05, C06, C07, C08, C09, C10, C11, C12, C13, C14, C15])
isActiveFirstAbbreviation = input(true, " ▪ Abbreviation")
gaps_on = input(false, "gaps_on: check for data to be smooth lines, but monotone and potentially less accurate")
// ——————————————————————————————————————————————————————————————————————————————————————
// ————————————————————————————————————— CONSTANTS —————————————————————————————————————
// ——————————————————————————————————————————————————————————————————————————————————————
// ————— Ichimoku
tenkanPeriods=conversionPeriods
kijunPeriods=basePeriods
SSBPeriods=laggingSpan2Periods
displacementPeriods=displacement-1
// ————— Styles
colorVeryLightBlue=#B0B7E6
colorLightBlue=#8893D9
colorDarkBlue=#3E4EBD
colorVeryLightGreen=#9AD6B7
colorLightGreen=#59BD89
colorDarkGreen=#327E56
colorVeryLightOrange=#F5D8A0
colorLightOrange=#EEB448
colorDarkOrange=#EB8412
colorVeryLightPink=#E1AEF0
colorLightPink=#CF83E9
colorDarkPink=#B949DE
transp=65
colorTest1=#A7AEE2
colorTest2=#313E95
// ————— Time
timeOffset=time-time[1]
// ——————————————————————————————————————————————————————————————————————————————————————
// ————————————————————————————————————— FUNCTION ——————————————————————————————————————
// ——————————————————————————————————————————————————————————————————————————————————————
// ————— Converts color choise
f_stringInColor(_color) =>
// _color: color label (string format).
_color == "Aqua" ? #0080FFef :
_color == "Black" ? #000000ef :
_color == "Blue" ? #013BCAef :
_color == "Coral" ? #FF8080ef :
_color == "Gold" ? #CCCC00ef :
_color == "Gray" ? #808080ef :
_color == "Green" ? #008000ef :
_color == "Lime" ? #00FF00ef :
_color == "Maroon" ? #800000ef :
_color == "Orange" ? #FF8000ef :
_color == "Pink" ? #FF0080ef :
_color == "Red" ? #FF0000ef :
_color == "Violet" ? #AA00FFef :
_color == "Yellow" ? #FFFF00ef : #FFFFFFff
// ————— Converts current resolution
f_resolutionInString(_res) =>
// _res: resolution of any TF (in "timeframe.period" string format).
_res == "1" ? "1m" :
_res == "3" ? "3m" :
_res == "5" ? "5m" :
_res == "15" ? "15m" :
_res == "30" ? "30m" :
_res == "45" ? "45m" :
_res == "60" ? "1h" :
_res == "120" ? "2h" :
_res == "180" ? "3h" :
_res == "240" ? "4h" :
_res == "1D" ? "D" :
_res == "1W" ? "W" :
_res == "1M" ? "M" : _res
// ————— Returns the tenkan text
f_getTenkanText(_res, _isAbbreviation) =>
// _res: resolution of any TF (in "timeframe.period" string format).
tf = f_resolutionInString(_res)
_isAbbreviation ? "Tk " + tf : "Tenkan " + tf
// ————— Returns the tenkan text
f_getKijunText(_res, _isAbbreviation) =>
// _res: resolution of any TF (in "timeframe.period" string format).
tf = f_resolutionInString(_res)
_isAbbreviation ? "Kj " + tf : "Kijun " + tf
// ————— Returns the tenkan text
f_getChikouText(_res, _isAbbreviation) =>
// _res: resolution of any TF (in "timeframe.period" string format).
tf = f_resolutionInString(_res)
_isAbbreviation ? "Ck " + tf : "Chikou " + tf
// ————— Converts current "timeframe.multiplier" plus the TF into minutes of type float.
f_resolutionInMinutes() =>
timeframe.multiplier * (
timeframe.isseconds ? 1. / 60. :
timeframe.isminutes ? 1. :
timeframe.isdaily ? 1440. :
timeframe.isweekly ? 10080. :
timeframe.ismonthly ? 43800. : na)
// ————— Returns resolution of _resolution period in minutes.
f_timeFrameResolutionInMinutes(_res) =>
// _res: resolution of any TF (in "timeframe.period" string format).
security(syminfo.tickerid, _res, f_resolutionInMinutes())
// ————— Returns the average number of current chart bars in the given target HTF resolution (this reflects the dataset's history).
f_avgDilationOf(_res) =>
// _res: resolution of any TF (in "timeframe.period" string format).
b = barssince(change(time(_res)))
cumTotal = cum(b == 0 ? b[1] + 1 : 0)
cumCount = cum(b == 0 ? 1 : 0)
round(cumTotal / cumCount)
// ————— Returns the displacement of the resolution
f_getDisplacement(_res) =>
f_avgDilationOf(_res)*displacementPeriods
// ————— Returns average value between lowest and highest
f_avgLH(_len) => avg(lowest(_len), highest(_len))
// ————— Returns f_donchian data
f_donchian(_tf, _src) =>
security(syminfo.tickerid, _tf, _src[1], barmerge.gaps_off, lookahead = barmerge.lookahead_on)
// ————— Returns ichimoku data
f_ichimokuData(_isActive, _tf) =>
tk_avgLH = f_avgLH(tenkanPeriods)
kj_avgLH = f_avgLH(kijunPeriods)
ssb_avgLH = f_avgLH(SSBPeriods)
_isShow = _isActive and f_timeFrameResolutionInMinutes(_tf) >= f_resolutionInMinutes()
_displacement = _isShow ? f_getDisplacement(_tf) : na
_tenkan = _isShow ? f_donchian(_tf, tk_avgLH) : na
_kijun = _isShow ? f_donchian(_tf, kj_avgLH) : na
_chikou = _isShow ? f_donchian(_tf, close) : na
_SSA = _isShow ? avg(_tenkan, _kijun) : na
_SSB = _isShow ? f_donchian(_tf, ssb_avgLH) : na
_middleKumo = _isShow ? _SSA[0] > _SSB[0] ? _SSA[0] - (abs(_SSA[0]-_SSB[0])/2) : _SSA[0] + (abs(_SSA[0]-_SSB[0])/2) : na
[_displacement, _tenkan, _kijun, _chikou, _SSA, _SSB, _middleKumo]
// ——————————————————————————————————————————————————————————————————————————————————————
// ——————————————————————————————————— FIRST ICHIMOKU ——————————————————————————————————
// ——————————————————————————————————————————————————————————————————————————————————————
[firstDisplacement, firstTenkan, firstKijun, firstChikou, firstSSA, firstSSB, fisrtMiddleKumo] = f_ichimokuData(isActiveFirst, firstIchi)
// ————— Tenkan
plot(isActiveFirstTenkan ? firstTenkan : na, color=color.aqua, title="First Tenkan", linewidth=3)
// ————— Kijun
plot(isActiveFirstKijun ? firstKijun : na, color=color.red, title="First Kijun", linewidth=3)
// ————— Chikou
plot(isActiveFirstChikou ? firstChikou : na, offset = -firstDisplacement, color=color.yellow, title="First Chikou")
// ----- Calculate colors
Kumo_line_color = firstSSA > firstSSB ? color.new(color.lime, 0) : color.new(color.red, 0)
// ————— SSA + SSB
ssaFirst=plot(isActiveFirstKumo ? firstSSA : na, offset = firstDisplacement, color=Kumo_line_color, title="First SSA", linewidth=1)
ssbFirst=plot(isActiveFirstKumo ? firstSSB : na, offset = firstDisplacement, color=Kumo_line_color, title="First SSB", linewidth=1)
fill(ssaFirst, ssbFirst, color = gaps_on ? colorVeryLightBlue : (firstSSA > firstSSB ? color.green : color.maroon), transp=transp, title="First Kumo", fillgaps=true)
// ——————————————————————————————————————————————————————————————————————————————————————
// ——————————————————————————————————————— LABEL ————————————————————————————————————————
// ——————————————————————————————————————————————————————————————————————————————————————
var label firstTenkanLabel = na
var label firstKijunLabel = na
var label firstChikouLabel = na
var label firstKumoLabel = na
label.delete(firstTenkanLabel)
label.delete(firstKijunLabel)
label.delete(firstChikouLabel)
label.delete(firstKumoLabel)
if isActiveFirst and isActiveFirstLabel and f_timeFrameResolutionInMinutes(firstIchi) >= f_resolutionInMinutes()
offsetLabelFirst=time+(timeOffset*labelFirstOffset)
offsetFirst=labelFirstOffset < 0 ? labelFirstOffset : 0
offsetDisplacementFirst=timeOffset*(firstDisplacement-labelFirstOffset)
if isActiveFirstTenkan
firstTenkanLabel:=label.new(x=offsetLabelFirst, y=firstTenkan[offsetFirst], xloc=xloc.bar_time, style=label.style_none)
label.set_text(firstTenkanLabel, f_getTenkanText(firstIchi, isActiveFirstAbbreviation))
label.set_textcolor(firstTenkanLabel, labelFirstColor == 'Original' ? colorDarkBlue : f_stringInColor(labelFirstColor))
if isActiveFirstKijun
firstKijunLabel:=label.new(x=offsetLabelFirst, y=firstKijun[offsetFirst], xloc=xloc.bar_time, style=label.style_none)
label.set_text(firstKijunLabel, f_getKijunText(firstIchi, isActiveFirstAbbreviation))
label.set_textcolor(firstKijunLabel, labelFirstColor == 'Original' ? colorDarkBlue : f_stringInColor(labelFirstColor))
if isActiveFirstChikou
firstChikouLabel:=label.new(x=time-offsetDisplacementFirst,y=firstChikou[1], xloc=xloc.bar_time, style=label.style_none)
label.set_text(firstChikouLabel, f_getChikouText(firstIchi, isActiveFirstAbbreviation))
label.set_textcolor(firstChikouLabel, labelFirstColor == 'Original' ? colorDarkBlue : f_stringInColor(labelFirstColor))
if isActiveFirstKumo
firstKumoLabel:=label.new(x=time+offsetDisplacementFirst,y=fisrtMiddleKumo, xloc=xloc.bar_time, style=label.style_none)
label.set_text(firstKumoLabel, "Kumo "+f_resolutionInString(firstIchi))
label.set_textcolor(firstKumoLabel, labelFirstColor == 'Original' ? colorDarkBlue : f_stringInColor(labelFirstColor))
// ——————————————————————————————————————————————————————————————————————————————————————
// ——————————————————————————————————————— TRADES ———————————————————————————————————————
// ——————————————————————————————————————————————————————————————————————————————————————
// Analyze Buy/sell times
buy = close > firstSSA[26] and close > firstSSB[26] and crossover(firstTenkan, firstKijun)
sell = close < firstSSA[26] and close < firstSSB[26] and crossunder(firstTenkan, firstKijun)
strategy.entry("Buy", strategy.long, when = buy and inDateRange)
strategy.entry("Sell", strategy.short, when = sell and inDateRange)
if (strategy.position_size > 0)
longStopPrice = strategy.position_avg_price * (1 - 0.10)
strategy.exit("Stop Loss Long", "Buy", stop = longStopPrice )
if (strategy.position_size < 0)
shortStopPrice = strategy.position_avg_price * (1 + 0.10)
strategy.exit("Stop Loss Short", "Sell", stop = shortStopPrice )
if (not inDateRange)
strategy.close_all()