View difference between Paste ID: WqgXQy9i and xiSQxxV7
SHOW: | | - or go back to the newest paste.
1
//@version=5
2
indicator("Screener", overlay = true, max_bars_back = 500)
3
4
import TradingView/ta/5 as ta
5
6
7
8
9
10
11
12
13
14
15
16
////////////
17
// INPUTS //
18
19
TF = input.timeframe('1', 'timeframe')
20
tpRatio = input.float(1, 'TP Ratio')
21
22
u01 = input.bool(true,  title = "", group = 'Symbols', inline = 's01')
23
u02 = input.bool(true,  title = "", group = 'Symbols', inline = 's02')
24
u03 = input.bool(true,  title = "", group = 'Symbols', inline = 's03')
25
u04 = input.bool(true,  title = "", group = 'Symbols', inline = 's04')
26
u05 = input.bool(true,  title = "", group = 'Symbols', inline = 's05')
27
u06 = input.bool(true,  title = "", group = 'Symbols', inline = 's06')
28
u07 = input.bool(true,  title = "", group = 'Symbols', inline = 's07')
29
u08 = input.bool(true,  title = "", group = 'Symbols', inline = 's08')
30
u09 = input.bool(true,  title = "", group = 'Symbols', inline = 's09')
31
u10 = input.bool(true,  title = "", group = 'Symbols', inline = 's10')
32
u11 = input.bool(true,  title = "", group = 'Symbols', inline = 's11')
33
u12 = input.bool(true,  title = "", group = 'Symbols', inline = 's12')
34
u13 = input.bool(true,  title = "", group = 'Symbols', inline = 's13')
35
u14 = input.bool(true,  title = "", group = 'Symbols', inline = 's14')
36
u15 = input.bool(true,  title = "", group = 'Symbols', inline = 's15')
37
u16 = input.bool(true,  title = "", group = 'Symbols', inline = 's16')
38
u17 = input.bool(true,  title = "", group = 'Symbols', inline = 's17')
39
u18 = input.bool(true,  title = "", group = 'Symbols', inline = 's18')
40
// u19 = input.bool(true,  title = "", group = 'Symbols', inline = 's19')
41
// u20 = input.bool(true,  title = "", group = 'Symbols', inline = 's20')
42
43
s01 = input.symbol('AUDUSD', group='Symbols', inline='s01')
44
s02 = input.symbol('EURUSD', group='Symbols', inline='s02')
45
s03 = input.symbol('GBPJPY', group='Symbols', inline='s03')
46
s04 = input.symbol('USDCAD', group='Symbols', inline='s04')
47
s05 = input.symbol('XAUUSD', group='Symbols', inline='s05')
48
s06 = input.symbol('USDJPY', group='Symbols', inline='s06')
49
s07 = input.symbol('AUDNZD', group='Symbols', inline='s07')
50
s08 = input.symbol('NZDUSD', group='Symbols', inline='s08')
51
s09 = input.symbol('USDCHF', group='Symbols', inline='s09')
52
s10 = input.symbol('EURJPY', group='Symbols', inline='s10')
53
s11 = input.symbol('EURGBP', group='Symbols', inline='s11')
54
s12 = input.symbol('GBPAUD', group='Symbols', inline='s12')
55
s13 = input.symbol('AUDJPY', group='Symbols', inline='s13')
56
s14 = input.symbol('EURCAD', group='Symbols', inline='s14')
57
s15 = input.symbol('GBPCHF', group='Symbols', inline='s15')
58
s16 = input.symbol('GBPUSD', group='Symbols', inline='s16')
59
s17 = input.symbol('GBPNZD', group='Symbols', inline = 's17')
60
s18 = input.symbol('NZDJPY', group='Symbols', inline = 's18')
61
// s19 = input.symbol('NZDAUD', group='Symbols', inline = 's19')
62
// s20 = input.symbol('CHFJPY', group='Symbols', inline = 's20')
63
64
col_width = input.float(2, title = "Column Width (%)", group = 'Table Theme')
65
textSize = switch input.string("Small", "Size", options = ['Auto', 'Tiny', 'Small', 'Normal', 'Large', 'Huge'], group = 'Table Theme')
66
    "Auto" => size.auto
67
    "Tiny" => size.tiny
68
    "Small" => size.small
69
    "Normal" => size.normal
70
    "Large" => size.large
71
    "Huge" => size.huge
72
location = switch input.string("Top Right", "Table Location",
73
 options = ['Top Right', 'Top Center', 'Top Left', 'Middle Right', 'Middle Center', 'Middle Left', 'Bottom Right', 'Bottom Center', 'Bottom Left'], group = 'Table Theme')
74
    "Top Right" => position.top_right
75
    "Top Center" => position.top_center
76
    "Top Left" => position.top_left
77
    "Middle Right" => position.middle_right
78
    "Middle Center" => position.middle_center
79
    "Middle Left" => position.middle_left
80
    "Bottom Right" => position.bottom_right
81
    "Bottom Center" => position.bottom_center
82
    "Bottom Left" => position.bottom_left
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//////////////////
100
// CALCULATIONS //
101
102
103
// @function this is used to make a label on the last bar. This is used for debugging
104
print(txt) =>
105
    // Create label on the first bar.
106
    var lbl = label.new(bar_index, na, txt, xloc.bar_index, yloc.price, color(na), label.style_none, #cfcfff, size.large, text.align_left)
107
    // On next bars, update the label's x and y position and the text it displays.
108
    label.set_xy(lbl, bar_index + 20, ta.highest(10)[1])
109
    label.set_text(lbl, txt)
110
111
// Trade UDT
112
type trade
113
    varip bool isRunning = false
114
    varip float entryPrice = 0
115
    varip float tpPrice = 0
116
    varip float slPrice = 0
117
    varip string direction = ''
118
    varip int barIndex = 0
119
    varip string symbol = ''
120
121
method setToDefault(trade this) =>
122
    this.isRunning := false
123
    this.entryPrice := 0
124
    this.tpPrice := 0
125
    this.slPrice := 0
126
    this.direction := ''
127
    this.barIndex := 0
128
129
130
// Data UDT
131
type data
132
    int num = 0
133
    int signal = -1
134
    float spanB = 0
135
    float prevClosePrice = 0
136
    float currClosePrice = 0
137
    float currHigh = 0
138
    float currLow = 0
139
    int barIndex = 0
140
141
dataMtx = matrix.new<data>(0, 1, data.new())
142
143
144
// Get only symbol
145
only_symbol(s) => 
146
    array.get(str.split(s, ":"), 1)
147
148
149
id_symbol(s)=>
150
    switch s
151
        1  => only_symbol(s01) //s01, s02, s03 etc. are all inputs like this: s01 = input.symbol('AUDUSD', group='Symbols', inline='s01')
152
        2  => only_symbol(s02)
153
        3  => only_symbol(s03)
154
        4  => only_symbol(s04)
155
        5  => only_symbol(s05)
156
        6  => only_symbol(s06)
157
        7  => only_symbol(s07)
158
        8  => only_symbol(s08)
159
        9  => only_symbol(s09)
160
        10 => only_symbol(s10)
161
        11  => only_symbol(s11)
162
        12  => only_symbol(s12)
163
        13  => only_symbol(s13)
164
        14  => only_symbol(s14)
165
        15  => only_symbol(s15)
166
        16  => only_symbol(s16)
167
        17  => only_symbol(s17)
168
        18  => only_symbol(s18)
169
        // 19  => only_symbol(s19)
170
        // 20  => only_symbol(s20)
171
        => na
172
173
174
// @function checks if there is a buy or sell signal based on an Ichimoku strategy
175
signal() => 
176
    int signal = -1
177
    [tenkan, kijun, spanA, spanB, chikou] = ta.ichimoku(9, 26, 52)
178
    float spanA1 = spanA[26]
179
    float spanB1 = spanB[26]
180
181
    bool bullCond = tenkan[1] > kijun[1] and chikou[1] > spanA1 and spanA1 > spanB1 // bullish condition (tenkan & kijun in a bullish cross, chikou above cloud, Span A above Span B)
182
    bool bearCond = kijun[1] > tenkan[1] and chikou[1] < spanA1 and spanB1 > spanA1 // bearish condition (tenkan & kijun in a bearish cross, chikou below cloud, Span B above Span A)
183
    
184
    int lowIndex = ta.barssince(bullCond ? (low[1] > spanA[26] and low[1] > spanB[26]) and (low[2] > spanA[27] and low[2] > spanB[27]) : close > 0) + 2 // checking for latest 2 lows above the cloud. Then, getting the 2nd bar (shift 2)
185
    int priceCrossoverIndex = ta.barssince(bullCond ? low[1] > (spanA[26] > spanB[26] ? spanA[26] : spanB[26]) and low[2] <= (spanA[27] > spanB[27] ? spanA[27] : spanB[27]):close > 0) + 1 // The bar at which the latest low was above the cloud
186
187
    int highIndex = ta.barssince(bearCond ? (high[1] < spanA[26] and high[1] < spanB[26]) and (high[2] < spanA[27] and high[2] < spanB[27]) : close > 0) + 2 // checking for latest 2 highs below the cloud. Then, getting the 2nd bar (shift 2)
188
    int priceCrossunderIndex = ta.barssince(bearCond ? high[1] < (spanA[26] < spanB[26] ? spanA[26] : spanB[26]) and high[2] >= (spanA[27] < spanB[27] ? spanA[27] : spanB[27]):close > 0) + 1 // The bar at which the latest high was below the cloud
189
190
    if lowIndex <= priceCrossoverIndex // if 2 lows came after the most recent occurence of price crossing over the cloud
191
        signal := 0
192
193
    if highIndex <= priceCrossunderIndex // if 2 highs came after the most recent occurence of price crossing under the cloud
194
        signal := 1
195
196
    [signal, spanB1]
197
198
199
200
// @function returns a data object 
201
screener_func(numSym) =>
202
    [_signal, _spanB] = signal()
203
    data.new(num = numSym, signal = _signal, spanB = _spanB, prevClosePrice = close[1], currClosePrice = close, currHigh = high, currLow = low, barIndex = bar_index)
204
205
206
// @function adds rows to a dataMtx which are filled with a specific symbol's data
207
screenerFun(numSym, sym, flg, tf) =>
208
    data symbolData = request.security(sym, tf, screener_func(numSym)) 
209
    if flg 
210
        arr = array.from(symbolData)
211
        matrix.add_row(dataMtx, matrix.rows(dataMtx), arr)
212
213
214
215
216
screenerFun(01, s01, u01, TF),  
217
screenerFun(02, s02, u02, TF),  
218
screenerFun(03, s03, u03, TF),    
219
screenerFun(04, s04, u04, TF),  
220
screenerFun(05, s05, u05, TF),  
221
screenerFun(06, s06, u06, TF),  
222
screenerFun(07, s07, u07, TF),  
223
screenerFun(08, s08, u08, TF),
224
screenerFun(09, s09, u09, TF), 
225
screenerFun(10, s10, u10, TF),
226
screenerFun(11, s11, u11, TF),
227
screenerFun(12, s12, u12, TF),
228
screenerFun(13, s13, u13, TF),
229
screenerFun(14, s14, u14, TF), 
230
screenerFun(15, s15, u15, TF),
231
// screenerFun(16, s16, u16, TF),
232
// screenerFun(17, s17, u17, TF),
233
// screenerFun(18, s18, u18, TF),
234
// screenerFun(19, s19, u19, TF),
235
// screenerFun(20, s20, u20, TF)
236
237
238
239
240
241
242
243
244
245
246
testMsgs = array.new_string(0, '')
247
alertMsgs = array.new_string(0, '')
248
string msg = ''
249
string currTime = str.format_time(time, "HH:mm:ss'_'dd-MM-yyyy", timezone = 'Asia/Kolkata')
250
var tradeMtx = matrix.new<trade>(15, 1, trade.new())
251
var tbl = table.new(location, 5, 21, frame_color=#151715, frame_width=1, border_width=2, border_color=color.new(color.white, 100)) // index 0 - symbol, index 1 - signal, index 2 - entry, index 3 - tp, index 3 - sl
252
253
// draws the 1st row of the table on bar 0
254
if bar_index == 0
255
    table.cell(tbl, 0, 0, 'Sym', width = col_width, text_halign = text.align_center, bgcolor = #000000, text_color = color.white, text_size = textSize)
256
    table.cell(tbl, 1, 0, 'Sig', width = col_width, text_halign = text.align_center, bgcolor = #000000, text_color = color.white, text_size = textSize)
257
    table.cell(tbl, 2, 0, 'Entry', width = col_width, text_halign = text.align_center, bgcolor = #000000, text_color = color.white, text_size = textSize)
258
    table.cell(tbl, 3, 0, 'TP', width = col_width, text_halign = text.align_center, bgcolor = #000000, text_color = color.white, text_size = textSize)
259
    table.cell(tbl, 4, 0, 'SL', width = col_width, text_halign = text.align_center, bgcolor = #000000, text_color = color.white, text_size = textSize)
260
261
262
if barstate.islast // barstate.isnew cannot be done because this needs to be done on every tick so that we can know when an exit happens
263
    if matrix.rows(dataMtx) > 0
264
        for i = 0 to matrix.rows(dataMtx) - 1
265
266
            // Getting this current symbol's data
267
            data symbolData = matrix.get(dataMtx, i, 0)
268
            float ichimokuSignal = symbolData.signal
269
            float spanB = symbolData.spanB
270
            string symbol = id_symbol(symbolData.num)
271
            float prevClosePrice = symbolData.prevClosePrice
272
            float currHigh = symbolData.currHigh
273
            float currLow = symbolData.currLow
274
            color ichimokuColor  = ichimokuSignal == 1 ? color.red : ichimokuSignal == 0 ? color.green : #323232 
275
276
            // Getting this current symbol's trade info
277
            trade symbolTrade = matrix.get(tradeMtx, i, 0)
278
            symbolTrade.symbol := symbol 
279
            bool isRunning = symbolTrade.isRunning
280
            float entryPrice = symbolTrade.entryPrice
281
            float tpPrice = symbolTrade.tpPrice
282
            float slPrice = symbolTrade.slPrice
283
            string direction = symbolTrade.direction
284
            int barIndex = symbolTrade.barIndex
285
            
286
            
287
288
            // Checking if trades got closed 
289
            if isRunning and direction == 'Sell'
290
291
                // Update the sl in the trade and the table
292
                // symbolTrade.slPrice := spanB
293
                // table.cell(tbl, 4, i + 1, str.tostring(spanB), bgcolor = color.red, text_color = #000000, text_size = textSize) 
294
295
                if entryPrice >= slPrice or currHigh >= slPrice //if the sell hit its sl
296
                    symbolTrade.setToDefault()
297
                    isRunning := symbolTrade.isRunning
298
                    table.cell_set_bgcolor(tbl, 2, i + 1, bgcolor = #323232)
299
                    table.cell_set_bgcolor(tbl, 3, i + 1, bgcolor = #323232)
300
                    table.cell(tbl, 4, i + 1, 'Closed at SL\n' + str.tostring(slPrice), bgcolor = #c8b50a, text_color = #000000, text_size = textSize)
301
                    string newText = str.format('"SL_Sell_{0}": ', symbol) + str.format('"{0}|{1}|{2}|{3}|{4}|{5}"', entryPrice, tpPrice, slPrice, TF, barIndex, currTime) 
302
                    alertMsgs.push(newText)
303
304
                if entryPrice <= tpPrice or currLow <= tpPrice //if the sell hit its tp
305
                    symbolTrade.setToDefault()
306
                    isRunning := symbolTrade.isRunning
307
                    table.cell_set_bgcolor(tbl, 2, i + 1, bgcolor = #323232)
308
                    table.cell_set_bgcolor(tbl, 4, i + 1, bgcolor = #323232)
309
                    table.cell(tbl, 3, i + 1, 'Closed at TP\n' + str.tostring(tpPrice), bgcolor = #c8b50a, text_color = #000000, text_size = textSize)
310
                    string newText = str.format('"TP_Sell_{0}": ', symbol) + str.format('"{0}|{1}|{2}|{3}|{4}|{5}"', entryPrice, tpPrice, slPrice, TF, barIndex, currTime)  
311
                    alertMsgs.push(newText)
312
313
            if isRunning and direction == 'Buy'
314
315
                // Update the sl in the trade and the table
316
                // symbolTrade.slPrice := spanB
317
                // table.cell(tbl, 4, i + 1, str.tostring(spanB), bgcolor = color.green, text_color = #000000, text_size = textSize)
318
                
319
                if entryPrice <= slPrice or currLow <= slPrice //if the buy hit its sl
320
                    symbolTrade.setToDefault()
321
                    isRunning := symbolTrade.isRunning
322
                    table.cell_set_bgcolor(tbl, 2, i + 1, bgcolor = #323232)
323
                    table.cell_set_bgcolor(tbl, 3, i + 1, bgcolor = #323232)
324
                    table.cell(tbl, 4, i + 1, 'Closed at SL\n' + str.tostring(slPrice), bgcolor = #c8b50a, text_color = #000000, text_size = textSize)
325
                    string newText = str.format('"SL_Buy_{0}": ', symbol) + str.format('"{0}|{1}|{2}|{3}|{4}|{5}"', entryPrice, tpPrice, slPrice, TF, barIndex, currTime)
326
                    alertMsgs.push(newText)    
327
328
                if entryPrice >= tpPrice or currHigh >= tpPrice //if the buy hit its tp
329
                    symbolTrade.setToDefault()
330
                    isRunning := symbolTrade.isRunning
331
                    table.cell_set_bgcolor(tbl, 2, i + 1, bgcolor = #323232)
332
                    table.cell_set_bgcolor(tbl, 4, i + 1, bgcolor = #323232)
333
                    table.cell(tbl, 3, i + 1, 'Closed at TP\n' + str.tostring(tpPrice), bgcolor = #c8b50a, text_color = #000000, text_size = textSize)
334
                    string newText = str.format('"TP_Buy_{0}": ', symbol) + str.format('"{0}|{1}|{2}|{3}|{4}|{5}"', entryPrice, tpPrice, slPrice, TF, barIndex, currTime)  
335
                    alertMsgs.push(newText)
336
           
337
                
338
339
            // plot the row
340
            table.cell(tbl, 0, i + 1, symbol + ' ' + TF, text_halign = text.align_left, bgcolor = #dcc200, text_color = #000000, text_size = textSize)
341
            table.cell(tbl, 1, i + 1, '', bgcolor = ichimokuColor, text_color = #ffffff)
342
            if isRunning == false
343
                table.cell(tbl, 2, i + 1, '', bgcolor = #323232)
344
                table.cell(tbl, 3, i + 1, '', bgcolor = #323232)
345
                table.cell(tbl, 4, i + 1, '', bgcolor = #323232)
346
347
348
            // Checking for new signals to open trades  
349
            if symbolTrade.isRunning == false
350
                if ichimokuSignal == 1
351
                    float sl = spanB
352
                    float pips = (prevClosePrice>sl ? prevClosePrice-sl : sl-prevClosePrice) * tpRatio
353
                    float tp = prevClosePrice - pips
354
355
                    string newText = str.format('""Sell"_{1}": ', symbol) + str.format('"{0}|{1}|{2}|{3}|{4}"', prevClosePrice, tp, sl, TF, currTime)
356
                    alertMsgs.push(newText)
357
358
                    table.cell(tbl, 2, i + 1, "Sell" + "\n" + str.tostring(prevClosePrice), bgcolor = color.red, text_color = #000000, text_size = textSize)
359
                    table.cell(tbl, 3, i + 1, str.tostring(tp), bgcolor = color.red, text_color = #000000, text_size = textSize)
360
                    table.cell(tbl, 4, i + 1, str.tostring(sl), bgcolor = color.red, text_color = #000000, text_size = textSize)
361
362
                    symbolTrade.isRunning := true
363
                    symbolTrade.entryPrice := prevClosePrice
364
                    symbolTrade.tpPrice := tp
365
                    symbolTrade.slPrice := sl
366
                    symbolTrade.direction := "Sell"
367
                    symbolTrade.barIndex := barIndex
368
369
                if ichimokuSignal == 0
370
                    float sl = spanB
371
                    float pips = (prevClosePrice>sl ? prevClosePrice-sl : sl-prevClosePrice) * tpRatio
372
                    float tp = prevClosePrice + pips
373
374
                    string newText = str.format('""Buy"_{1}": ', symbol) + str.format('"{0}|{1}|{2}|{3}|{4}"', prevClosePrice, tp, sl, TF, currTime)
375
                    alertMsgs.push(newText)
376
377
                    table.cell(tbl, 2, i + 1, "Buy" + "\n" + str.tostring(prevClosePrice), bgcolor = color.green, text_color = #000000, text_size = textSize)
378
                    table.cell(tbl, 3, i + 1, str.tostring(tp), bgcolor = color.green, text_color = #000000, text_size = textSize)
379
                    table.cell(tbl, 4, i + 1, str.tostring(sl), bgcolor = color.green, text_color = #000000, text_size = textSize)
380
381
                    symbolTrade.isRunning := true
382
                    symbolTrade.entryPrice := prevClosePrice
383
                    symbolTrade.tpPrice := tp
384
                    symbolTrade.slPrice := sl
385
                    symbolTrade.direction := "Buy"
386
                    symbolTrade.barIndex := barIndex
387
388
            testMsg = str.format('{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}', i, symbolTrade.isRunning, symbolTrade.entryPrice, symbolTrade.tpPrice, symbolTrade.slPrice, symbolTrade.direction, symbolTrade.barIndex, symbolTrade.symbol) 
389
            testMsgs.push(testMsg)
390
            
391
            
392
           
393
394
    if array.size(alertMsgs) > 0
395
        msg += '{' + alertMsgs.join(',') + '}'
396
        alert(msg, alert.freq_once_per_bar)
397
398
399
400
print(testMsgs.join(',\n'))