araamas

Screener code

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