Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- indicator("Screener", overlay = true, max_bars_back = 500)
- import TradingView/ta/5 as ta
- ////////////
- // INPUTS //
- TF = input.timeframe('1', 'timeframe')
- tpRatio = input.float(1, 'TP Ratio')
- u01 = input.bool(true, title = "", group = 'Symbols', inline = 's01')
- u02 = input.bool(true, title = "", group = 'Symbols', inline = 's02')
- u03 = input.bool(true, title = "", group = 'Symbols', inline = 's03')
- u04 = input.bool(true, title = "", group = 'Symbols', inline = 's04')
- u05 = input.bool(true, title = "", group = 'Symbols', inline = 's05')
- u06 = input.bool(true, title = "", group = 'Symbols', inline = 's06')
- u07 = input.bool(true, title = "", group = 'Symbols', inline = 's07')
- u08 = input.bool(true, title = "", group = 'Symbols', inline = 's08')
- u09 = input.bool(true, title = "", group = 'Symbols', inline = 's09')
- u10 = input.bool(true, title = "", group = 'Symbols', inline = 's10')
- u11 = input.bool(true, title = "", group = 'Symbols', inline = 's11')
- u12 = input.bool(true, title = "", group = 'Symbols', inline = 's12')
- u13 = input.bool(true, title = "", group = 'Symbols', inline = 's13')
- u14 = input.bool(true, title = "", group = 'Symbols', inline = 's14')
- u15 = input.bool(true, title = "", group = 'Symbols', inline = 's15')
- u16 = input.bool(true, title = "", group = 'Symbols', inline = 's16')
- u17 = input.bool(true, title = "", group = 'Symbols', inline = 's17')
- u18 = input.bool(true, title = "", group = 'Symbols', inline = 's18')
- // u19 = input.bool(true, title = "", group = 'Symbols', inline = 's19')
- // u20 = input.bool(true, title = "", group = 'Symbols', inline = 's20')
- s01 = input.symbol('AUDUSD', group='Symbols', inline='s01')
- s02 = input.symbol('EURUSD', group='Symbols', inline='s02')
- s03 = input.symbol('GBPJPY', group='Symbols', inline='s03')
- s04 = input.symbol('USDCAD', group='Symbols', inline='s04')
- s05 = input.symbol('XAUUSD', group='Symbols', inline='s05')
- s06 = input.symbol('USDJPY', group='Symbols', inline='s06')
- s07 = input.symbol('AUDNZD', group='Symbols', inline='s07')
- s08 = input.symbol('NZDUSD', group='Symbols', inline='s08')
- s09 = input.symbol('USDCHF', group='Symbols', inline='s09')
- s10 = input.symbol('EURJPY', group='Symbols', inline='s10')
- s11 = input.symbol('EURGBP', group='Symbols', inline='s11')
- s12 = input.symbol('GBPAUD', group='Symbols', inline='s12')
- s13 = input.symbol('AUDJPY', group='Symbols', inline='s13')
- s14 = input.symbol('EURCAD', group='Symbols', inline='s14')
- s15 = input.symbol('GBPCHF', group='Symbols', inline='s15')
- s16 = input.symbol('GBPUSD', group='Symbols', inline='s16')
- s17 = input.symbol('GBPNZD', group='Symbols', inline = 's17')
- s18 = input.symbol('NZDJPY', group='Symbols', inline = 's18')
- // s19 = input.symbol('NZDAUD', group='Symbols', inline = 's19')
- // s20 = input.symbol('CHFJPY', group='Symbols', inline = 's20')
- col_width = input.float(2, title = "Column Width (%)", group = 'Table Theme')
- textSize = switch input.string("Small", "Size", options = ['Auto', 'Tiny', 'Small', 'Normal', 'Large', 'Huge'], group = 'Table Theme')
- "Auto" => size.auto
- "Tiny" => size.tiny
- "Small" => size.small
- "Normal" => size.normal
- "Large" => size.large
- "Huge" => size.huge
- location = switch input.string("Top Right", "Table Location",
- options = ['Top Right', 'Top Center', 'Top Left', 'Middle Right', 'Middle Center', 'Middle Left', 'Bottom Right', 'Bottom Center', 'Bottom Left'], group = 'Table Theme')
- "Top Right" => position.top_right
- "Top Center" => position.top_center
- "Top Left" => position.top_left
- "Middle Right" => position.middle_right
- "Middle Center" => position.middle_center
- "Middle Left" => position.middle_left
- "Bottom Right" => position.bottom_right
- "Bottom Center" => position.bottom_center
- "Bottom Left" => position.bottom_left
- //////////////////
- // CALCULATIONS //
- // @function this is used to make a label on the last bar. This is used for debugging
- print(txt) =>
- // Create label on the first bar.
- var lbl = label.new(bar_index, na, txt, xloc.bar_index, yloc.price, color(na), label.style_none, #cfcfff, size.large, text.align_left)
- // On next bars, update the label's x and y position and the text it displays.
- label.set_xy(lbl, bar_index + 20, ta.highest(10)[1])
- label.set_text(lbl, txt)
- // Trade UDT
- type trade
- varip bool isRunning = false
- varip float entryPrice = 0
- varip float tpPrice = 0
- varip float slPrice = 0
- varip string direction = ''
- varip int barIndex = 0
- varip string symbol = ''
- method setToDefault(trade this) =>
- this.isRunning := false
- this.entryPrice := 0
- this.tpPrice := 0
- this.slPrice := 0
- this.direction := ''
- this.barIndex := 0
- // Data UDT
- type data
- int num = 0
- int signal = -1
- float spanB = 0
- float prevClosePrice = 0
- float currClosePrice = 0
- float currHigh = 0
- float currLow = 0
- int barIndex = 0
- dataMtx = matrix.new<data>(0, 1, data.new())
- // Get only symbol
- only_symbol(s) =>
- array.get(str.split(s, ":"), 1)
- id_symbol(s)=>
- switch s
- 1 => only_symbol(s01) //s01, s02, s03 etc. are all inputs like this: s01 = input.symbol('AUDUSD', group='Symbols', inline='s01')
- 2 => only_symbol(s02)
- 3 => only_symbol(s03)
- 4 => only_symbol(s04)
- 5 => only_symbol(s05)
- 6 => only_symbol(s06)
- 7 => only_symbol(s07)
- 8 => only_symbol(s08)
- 9 => only_symbol(s09)
- 10 => only_symbol(s10)
- 11 => only_symbol(s11)
- 12 => only_symbol(s12)
- 13 => only_symbol(s13)
- 14 => only_symbol(s14)
- 15 => only_symbol(s15)
- 16 => only_symbol(s16)
- 17 => only_symbol(s17)
- 18 => only_symbol(s18)
- // 19 => only_symbol(s19)
- // 20 => only_symbol(s20)
- => na
- // @function checks if there is a buy or sell signal based on an Ichimoku strategy
- signal() =>
- int signal = -1
- [tenkan, kijun, spanA, spanB, chikou] = ta.ichimoku(9, 26, 52)
- float spanA1 = spanA[26]
- float spanB1 = spanB[26]
- 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)
- 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)
- 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)
- 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
- 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)
- 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
- if lowIndex <= priceCrossoverIndex // if 2 lows came after the most recent occurence of price crossing over the cloud
- signal := 0
- if highIndex <= priceCrossunderIndex // if 2 highs came after the most recent occurence of price crossing under the cloud
- signal := 1
- [signal, spanB1]
- // @function returns a data object
- screener_func(numSym) =>
- [_signal, _spanB] = signal()
- data.new(num = numSym, signal = _signal, spanB = _spanB, prevClosePrice = close[1], currClosePrice = close, currHigh = high, currLow = low, barIndex = bar_index)
- // @function adds rows to a dataMtx which are filled with a specific symbol's data
- screenerFun(numSym, sym, flg, tf) =>
- data symbolData = request.security(sym, tf, screener_func(numSym))
- if flg
- arr = array.from(symbolData)
- matrix.add_row(dataMtx, matrix.rows(dataMtx), arr)
- screenerFun(01, s01, u01, TF),
- screenerFun(02, s02, u02, TF),
- screenerFun(03, s03, u03, TF),
- screenerFun(04, s04, u04, TF),
- screenerFun(05, s05, u05, TF),
- screenerFun(06, s06, u06, TF),
- screenerFun(07, s07, u07, TF),
- screenerFun(08, s08, u08, TF),
- screenerFun(09, s09, u09, TF),
- screenerFun(10, s10, u10, TF),
- screenerFun(11, s11, u11, TF),
- screenerFun(12, s12, u12, TF),
- screenerFun(13, s13, u13, TF),
- screenerFun(14, s14, u14, TF),
- screenerFun(15, s15, u15, TF),
- // screenerFun(16, s16, u16, TF),
- // screenerFun(17, s17, u17, TF),
- // screenerFun(18, s18, u18, TF),
- // screenerFun(19, s19, u19, TF),
- // screenerFun(20, s20, u20, TF)
- testMsgs = array.new_string(0, '')
- alertMsgs = array.new_string(0, '')
- string msg = ''
- string currTime = str.format_time(time, "HH:mm:ss'_'dd-MM-yyyy", timezone = 'Asia/Kolkata')
- var tradeMtx = matrix.new<trade>(15, 1, trade.new())
- 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
- // draws the 1st row of the table on bar 0
- if bar_index == 0
- table.cell(tbl, 0, 0, 'Sym', width = col_width, text_halign = text.align_center, bgcolor = #000000, text_color = color.white, text_size = textSize)
- table.cell(tbl, 1, 0, 'Sig', width = col_width, text_halign = text.align_center, bgcolor = #000000, text_color = color.white, text_size = textSize)
- table.cell(tbl, 2, 0, 'Entry', width = col_width, text_halign = text.align_center, bgcolor = #000000, text_color = color.white, text_size = textSize)
- table.cell(tbl, 3, 0, 'TP', width = col_width, text_halign = text.align_center, bgcolor = #000000, text_color = color.white, text_size = textSize)
- table.cell(tbl, 4, 0, 'SL', width = col_width, text_halign = text.align_center, bgcolor = #000000, text_color = color.white, text_size = textSize)
- 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
- if matrix.rows(dataMtx) > 0
- for i = 0 to matrix.rows(dataMtx) - 1
- // Getting this current symbol's data
- data symbolData = matrix.get(dataMtx, i, 0)
- float ichimokuSignal = symbolData.signal
- float spanB = symbolData.spanB
- string symbol = id_symbol(symbolData.num)
- float prevClosePrice = symbolData.prevClosePrice
- float currHigh = symbolData.currHigh
- float currLow = symbolData.currLow
- color ichimokuColor = ichimokuSignal == 1 ? color.red : ichimokuSignal == 0 ? color.green : #323232
- // Getting this current symbol's trade info
- trade symbolTrade = matrix.get(tradeMtx, i, 0)
- symbolTrade.symbol := symbol
- bool isRunning = symbolTrade.isRunning
- float entryPrice = symbolTrade.entryPrice
- float tpPrice = symbolTrade.tpPrice
- float slPrice = symbolTrade.slPrice
- string direction = symbolTrade.direction
- int barIndex = symbolTrade.barIndex
- // Checking if trades got closed
- if isRunning and direction == 'Sell'
- // Update the sl in the trade and the table
- // symbolTrade.slPrice := spanB
- // table.cell(tbl, 4, i + 1, str.tostring(spanB), bgcolor = color.red, text_color = #000000, text_size = textSize)
- if entryPrice >= slPrice or currHigh >= slPrice //if the sell hit its sl
- symbolTrade.setToDefault()
- isRunning := symbolTrade.isRunning
- table.cell_set_bgcolor(tbl, 2, i + 1, bgcolor = #323232)
- table.cell_set_bgcolor(tbl, 3, i + 1, bgcolor = #323232)
- table.cell(tbl, 4, i + 1, 'Closed at SL\n' + str.tostring(slPrice), bgcolor = #c8b50a, text_color = #000000, text_size = textSize)
- string newText = str.format('"SL_Sell_{0}": ', symbol) + str.format('"{0}|{1}|{2}|{3}|{4}|{5}"', entryPrice, tpPrice, slPrice, TF, barIndex, currTime)
- alertMsgs.push(newText)
- if entryPrice <= tpPrice or currLow <= tpPrice //if the sell hit its tp
- symbolTrade.setToDefault()
- isRunning := symbolTrade.isRunning
- table.cell_set_bgcolor(tbl, 2, i + 1, bgcolor = #323232)
- table.cell_set_bgcolor(tbl, 4, i + 1, bgcolor = #323232)
- table.cell(tbl, 3, i + 1, 'Closed at TP\n' + str.tostring(tpPrice), bgcolor = #c8b50a, text_color = #000000, text_size = textSize)
- string newText = str.format('"TP_Sell_{0}": ', symbol) + str.format('"{0}|{1}|{2}|{3}|{4}|{5}"', entryPrice, tpPrice, slPrice, TF, barIndex, currTime)
- alertMsgs.push(newText)
- if isRunning and direction == 'Buy'
- // Update the sl in the trade and the table
- // symbolTrade.slPrice := spanB
- // table.cell(tbl, 4, i + 1, str.tostring(spanB), bgcolor = color.green, text_color = #000000, text_size = textSize)
- if entryPrice <= slPrice or currLow <= slPrice //if the buy hit its sl
- symbolTrade.setToDefault()
- isRunning := symbolTrade.isRunning
- table.cell_set_bgcolor(tbl, 2, i + 1, bgcolor = #323232)
- table.cell_set_bgcolor(tbl, 3, i + 1, bgcolor = #323232)
- table.cell(tbl, 4, i + 1, 'Closed at SL\n' + str.tostring(slPrice), bgcolor = #c8b50a, text_color = #000000, text_size = textSize)
- string newText = str.format('"SL_Buy_{0}": ', symbol) + str.format('"{0}|{1}|{2}|{3}|{4}|{5}"', entryPrice, tpPrice, slPrice, TF, barIndex, currTime)
- alertMsgs.push(newText)
- if entryPrice >= tpPrice or currHigh >= tpPrice //if the buy hit its tp
- symbolTrade.setToDefault()
- isRunning := symbolTrade.isRunning
- table.cell_set_bgcolor(tbl, 2, i + 1, bgcolor = #323232)
- table.cell_set_bgcolor(tbl, 4, i + 1, bgcolor = #323232)
- table.cell(tbl, 3, i + 1, 'Closed at TP\n' + str.tostring(tpPrice), bgcolor = #c8b50a, text_color = #000000, text_size = textSize)
- string newText = str.format('"TP_Buy_{0}": ', symbol) + str.format('"{0}|{1}|{2}|{3}|{4}|{5}"', entryPrice, tpPrice, slPrice, TF, barIndex, currTime)
- alertMsgs.push(newText)
- // plot the row
- table.cell(tbl, 0, i + 1, symbol + ' ' + TF, text_halign = text.align_left, bgcolor = #dcc200, text_color = #000000, text_size = textSize)
- table.cell(tbl, 1, i + 1, '', bgcolor = ichimokuColor, text_color = #ffffff)
- if isRunning == false
- table.cell(tbl, 2, i + 1, '', bgcolor = #323232)
- table.cell(tbl, 3, i + 1, '', bgcolor = #323232)
- table.cell(tbl, 4, i + 1, '', bgcolor = #323232)
- // Checking for new signals to open trades
- if symbolTrade.isRunning == false
- if ichimokuSignal == 1
- float sl = spanB
- float pips = (prevClosePrice>sl ? prevClosePrice-sl : sl-prevClosePrice) * tpRatio
- float tp = prevClosePrice - pips
- string newText = str.format('""Sell"_{1}": ', symbol) + str.format('"{0}|{1}|{2}|{3}|{4}"', prevClosePrice, tp, sl, TF, currTime)
- alertMsgs.push(newText)
- table.cell(tbl, 2, i + 1, "Sell" + "\n" + str.tostring(prevClosePrice), bgcolor = color.red, text_color = #000000, text_size = textSize)
- table.cell(tbl, 3, i + 1, str.tostring(tp), bgcolor = color.red, text_color = #000000, text_size = textSize)
- table.cell(tbl, 4, i + 1, str.tostring(sl), bgcolor = color.red, text_color = #000000, text_size = textSize)
- symbolTrade.isRunning := true
- symbolTrade.entryPrice := prevClosePrice
- symbolTrade.tpPrice := tp
- symbolTrade.slPrice := sl
- symbolTrade.direction := "Sell"
- symbolTrade.barIndex := barIndex
- if ichimokuSignal == 0
- float sl = spanB
- float pips = (prevClosePrice>sl ? prevClosePrice-sl : sl-prevClosePrice) * tpRatio
- float tp = prevClosePrice + pips
- string newText = str.format('""Buy"_{1}": ', symbol) + str.format('"{0}|{1}|{2}|{3}|{4}"', prevClosePrice, tp, sl, TF, currTime)
- alertMsgs.push(newText)
- table.cell(tbl, 2, i + 1, "Buy" + "\n" + str.tostring(prevClosePrice), bgcolor = color.green, text_color = #000000, text_size = textSize)
- table.cell(tbl, 3, i + 1, str.tostring(tp), bgcolor = color.green, text_color = #000000, text_size = textSize)
- table.cell(tbl, 4, i + 1, str.tostring(sl), bgcolor = color.green, text_color = #000000, text_size = textSize)
- symbolTrade.isRunning := true
- symbolTrade.entryPrice := prevClosePrice
- symbolTrade.tpPrice := tp
- symbolTrade.slPrice := sl
- symbolTrade.direction := "Buy"
- symbolTrade.barIndex := barIndex
- 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)
- testMsgs.push(testMsg)
- if array.size(alertMsgs) > 0
- msg += '{' + alertMsgs.join(',') + '}'
- alert(msg, alert.freq_once_per_bar)
- print(testMsgs.join(',\n'))
Advertisement