Advertisement
sygma1982

Auto GRID BOT - PINESCRIPT

Jun 4th, 2023
559
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 40.00 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © lovealgotrading
  3.  
  4. // First of all I want to say thank you to © thequantscience.
  5.  
  6. //@version=5
  7. strategy(
  8. title = 'AUTOMATIC GRID BOT STRATEGY [lovealgotrading]',
  9. overlay = true,
  10. commission_type = strategy.commission.percent,
  11. commission_value = 0.075,
  12. pyramiding = 20,
  13. default_qty_type = strategy.percent_of_equity,
  14. close_entries_rule = 'ANY',
  15. initial_capital = 1000
  16. )
  17.  
  18.  
  19. /////////////////////////////// ALGORITHM BACKTEST SOURCE CODE ////////////////////////////////////
  20. startDate = input.time(defval=timestamp('1 nov 2022 13:30 +0000'), title='Start_Date', group = " ############# ⏰ BACKTEST DATE ⏰ ############ " )
  21. endDate = input.time(defval=timestamp('20 oct 2030 13:30 +0000'), title='End_Date', group = " ############# ⏰ BACKTEST DATE ⏰ ############ " )
  22.  
  23. inDateRange = time > startDate and time < endDate
  24.  
  25.  
  26. high_price = input.price(
  27. defval = 0.00,
  28. title = 'Range High price: ',
  29. group = " ############## ➡️ HIGH GRID PRICE ⬅️ ############ ",
  30. confirm = true,
  31. tooltip = "Top grid price."
  32. )
  33.  
  34. low_price = input.price(
  35. defval = 0.00,
  36. title = 'Range Low price: ',
  37. group = " ############## ➡️ LOW GRID PRICE ⬅️ ############# ",
  38. confirm = true,
  39. tooltip = "Bottom grid price."
  40. )
  41.  
  42.  
  43. // ###########################################################################################
  44.  
  45. trade_direction = input.string(title='Trade Direction', group = " ##############🎯 TRADE DIRECTION 🎯############# ", options=['LONG', 'SHORT', 'BOTH'], defval='BOTH')
  46.  
  47. // ###########################################################################################
  48.  
  49. use_stop = input.bool(defval = false , title = "Use StopLoss", group = " ############# ⛔️ Strategy STOP Settings ⛔️ ############ ")
  50.  
  51. close_bot_after_stop = input.bool(defval = false , title = "Close Bot After Stop Loss",tooltip = "Dont open new positions after stop loss", group = " ############# ⛔️ Strategy STOP Settings ⛔️ ############ ")
  52.  
  53. stop_close_all = ""
  54.  
  55. if stop_close_all[1] != "" and close_bot_after_stop
  56. stop_close_all := "DONT"
  57.  
  58. use_stop_default = input.bool(defval = true , title = "Use StopLoss Where HIGH and LOW GRID PRICE", group = " ############# ⛔️ Strategy STOP Settings ⛔️ ############ ")
  59.  
  60.  
  61. high_price_stop = input.price(
  62. defval = 99999,
  63. title = 'High STOP Price: ',
  64. group = " ############# ⛔️ Strategy STOP Settings ⛔️ ############ ",
  65. confirm = false,
  66. tooltip = "Top STOP price."
  67. )
  68.  
  69. low_price_stop = input.price(
  70. defval = 0.00,
  71. title = 'Low STOP Price: ',
  72. group = " ############# ⛔️ Strategy STOP Settings ⛔️ ############ ",
  73. confirm = false,
  74. tooltip = "Bottom STOP price."
  75. )
  76.  
  77. if use_stop_default
  78. high_price_stop := high_price
  79. low_price_stop := low_price
  80.  
  81. // ###########################################################################################
  82.  
  83. dolar = input.int(defval = 100, title = "$ Per Position", group = " ############# 🤖 ALGO TRADE ALERTS 🤖 ############ ")
  84.  
  85. // ###########################################################################################
  86.  
  87.  
  88. Long_message = input("", group = " ############# 🤖 ALGO TRADE ALERTS 🤖 ############ ")
  89. Long_Exit_message = input("", group = " ############# 🤖 ALGO TRADE ALERTS 🤖 ############ ")
  90. Long_Stop_message = input("", group = " ############# 🤖 ALGO TRADE ALERTS 🤖 ############ ")
  91.  
  92.  
  93. Short_message = input("", group = " ############# 🤖 ALGO TRADE ALERTS 🤖 ############ ")
  94. Short_Exit_message = input("", group = " ############# 🤖 ALGO TRADE ALERTS 🤖 ############ ")
  95. Short_Stop_message = input("", group = " ############# 🤖 ALGO TRADE ALERTS 🤖 ############ ")
  96.  
  97.  
  98. // ###########################################################################################
  99.  
  100.  
  101. float qty_new = dolar / close
  102.  
  103.  
  104. if close > 5000
  105. qty_new := math.round(dolar / close, 3)
  106. else if close < 5000 and close > 200
  107. qty_new := math.round(dolar / close, 2)
  108. else if close < 200 and close > 50
  109. qty_new := math.round(dolar / close, 1)
  110. else if close < 50
  111. qty_new := math.round(dolar / close, 0)
  112.  
  113. // ###########################################################################################
  114.  
  115. ten_grid = input.bool(
  116. defval = false,
  117. title = "10 grid levels",
  118. group = " ############ GRID CONFIGURATION ############ ",
  119. tooltip = "10 grid levels",
  120. confirm = true
  121. )
  122.  
  123. tewnty_grid = input.bool(
  124. defval = true,
  125. title = "20 grid levels",
  126. group = " ############ GRID CONFIGURATION ############ ",
  127. tooltip = "20 grid levels",
  128. confirm = true
  129. )
  130.  
  131.  
  132. // ###########################################################################################
  133.  
  134. show_stop_res = input.bool(true, title = "Show Stop Price",group = " ############# 📈 Graphics Settings 📈 ########### ")
  135. show_grid_level_bg = input.bool(true, title = "Show Grid Levels BG",group = " ############# 📈 Graphics Settings 📈 ########### ")
  136.  
  137. // ###########################################################################################
  138.  
  139.  
  140. grid_range = high_price - low_price
  141.  
  142. percent_change = ((high_price - low_price) / low_price) * (1.00 / 9.00)
  143.  
  144.  
  145. var float grid_1 = 0
  146. var float grid_2 = 0
  147. var float grid_3 = 0
  148. var float grid_4 = 0
  149. var float grid_5 = 0
  150. var float grid_6 = 0
  151. var float grid_7 = 0
  152. var float grid_8 = 0
  153. var float grid_9 = 0
  154. var float grid_10 = 0
  155. var float grid_11 = 0
  156. var float grid_12 = 0
  157. var float grid_13 = 0
  158. var float grid_14 = 0
  159. var float grid_15 = 0
  160. var float grid_16 = 0
  161. var float grid_17 = 0
  162. var float grid_18 = 0
  163. var float grid_19 = 0
  164. var float grid_20 = 0
  165.  
  166. var float factor = 0
  167.  
  168. long_1 = false
  169. long_2 = false
  170. long_3 = false
  171. long_4 = false
  172. long_5 = false
  173. long_6 = false
  174. long_7 = false
  175. long_8 = false
  176. long_9 = false
  177. long_10 = false
  178.  
  179. short_1 = false
  180. short_2 = false
  181. short_3 = false
  182. short_4 = false
  183. short_5 = false
  184. short_6 = false
  185. short_7 = false
  186. short_8 = false
  187. short_9 = false
  188. short_10 = false
  189.  
  190.  
  191. if ten_grid == true
  192.  
  193. percent_change := ((high_price - low_price) / low_price) * (1.00 / 9.00)
  194.  
  195. factor := grid_range / 9
  196.  
  197. grid_1 := (high_price)
  198. grid_2 := (high_price - (factor * 1))
  199. grid_3 := (high_price - (factor * 2))
  200. grid_4 := (high_price - (factor * 3))
  201. grid_5 := (high_price - (factor * 4))
  202. grid_6 := (high_price - (factor * 5))
  203. grid_7 := (high_price - (factor * 6))
  204. grid_8 := (high_price - (factor * 7))
  205. grid_9 := (high_price - (factor * 8))
  206. grid_10 := (low_price)
  207.  
  208. long_1 := ta.crossunder(close, ((grid_5+grid_6)/2))
  209. long_2 := ta.crossunder(close, ((grid_6+grid_7)/2))
  210. long_3 := ta.crossunder(close, ((grid_7+grid_8)/2))
  211. long_4 := ta.crossunder(close, ((grid_8+grid_9)/2))
  212. long_5 := ta.crossunder(close, ((grid_9+grid_10)/2))
  213.  
  214. short_1 := ta.crossover(close, ((grid_6+grid_5)/2))
  215. short_2 := ta.crossover(close, ((grid_5+grid_4)/2))
  216. short_3 := ta.crossover(close, ((grid_4+grid_3)/2))
  217. short_4 := ta.crossover(close, ((grid_3+grid_2)/2))
  218. short_5 := ta.crossover(close, ((grid_2+grid_1)/2))
  219.  
  220.  
  221. if tewnty_grid == true
  222.  
  223. percent_change := ((high_price - low_price) / low_price) * (1.00 / 19.00)
  224.  
  225. factor := grid_range / 19
  226.  
  227. grid_1 := (high_price)
  228. grid_2 := (high_price - (factor * 1))
  229. grid_3 := (high_price - (factor * 2))
  230. grid_4 := (high_price - (factor * 3))
  231. grid_5 := (high_price - (factor * 4))
  232. grid_6 := (high_price - (factor * 5))
  233. grid_7 := (high_price - (factor * 6))
  234. grid_8 := (high_price - (factor * 7))
  235. grid_9 := (high_price - (factor * 8))
  236. grid_10 := (high_price - (factor * 9))
  237. grid_11 := (high_price - (factor * 10))
  238. grid_12 := (high_price - (factor * 11))
  239. grid_13 := (high_price - (factor * 12))
  240. grid_14 := (high_price - (factor * 13))
  241. grid_15 := (high_price - (factor * 14))
  242. grid_16 := (high_price - (factor * 15))
  243. grid_17 := (high_price - (factor * 16))
  244. grid_18 := (high_price - (factor * 17))
  245. grid_19 := (high_price - (factor * 18))
  246. grid_20 := (low_price)
  247.  
  248. long_1 := close < ((grid_10+grid_11)/2)
  249. long_2 := close < ((grid_11+grid_12)/2)
  250. long_3 := close < ((grid_12+grid_13)/2)
  251. long_4 := close < ((grid_13+grid_14)/2)
  252. long_5 := close < ((grid_14+grid_15)/2)
  253. long_6 := close < ((grid_15+grid_16)/2)
  254. long_7 := close < ((grid_16+grid_17)/2)
  255. long_8 := close < ((grid_17+grid_18)/2)
  256. long_9 := close < ((grid_18+grid_19)/2)
  257. long_10 := close < ((grid_19+grid_20)/2)
  258.  
  259. short_10 := close >((grid_1+grid_2)/2)
  260. short_9 := close >((grid_2+grid_3)/2)
  261. short_8 := close >((grid_3+grid_4)/2)
  262. short_7 := close >((grid_4+grid_5)/2)
  263. short_6 := close >((grid_5+grid_6)/2)
  264. short_5 := close >((grid_6+grid_7)/2)
  265. short_4 := close >((grid_7+grid_8)/2)
  266. short_3 := close >((grid_8+grid_9)/2)
  267. short_2 := close >((grid_9+grid_10)/2)
  268. short_1 := close > ((grid_10+grid_11)/2)
  269.  
  270. // ###########################################################################################
  271.  
  272. // ###########################################################################################
  273.  
  274. if ten_grid and ( trade_direction == 'LONG' or trade_direction == 'BOTH')
  275. if long_1 and strategy.opentrades == 0 and inDateRange and stop_close_all == ""
  276. strategy.entry(id = "L_1", direction = strategy.long,alert_message = Long_message, qty = qty_new )
  277.  
  278. if strategy.opentrades == 1
  279. strategy.exit(id = "E_1",from_entry = "L_1", qty_percent = 100,alert_message =Long_Exit_message,stop = 0, limit = strategy.opentrades.entry_price(strategy.opentrades - 1) * (1+percent_change))
  280.  
  281.  
  282. if long_2 and strategy.opentrades == 1 and inDateRange and stop_close_all == ""
  283. strategy.entry(id = "L_2", direction = strategy.long,alert_message = Long_message, qty = qty_new )
  284.  
  285.  
  286. if strategy.opentrades == 2
  287. strategy.exit(id = "E_2",from_entry = "L_2", qty_percent = 100,alert_message =Long_Exit_message, limit = strategy.opentrades.entry_price(strategy.opentrades - 1) * (1+percent_change))
  288.  
  289.  
  290. if long_3 and strategy.opentrades == 2 and inDateRange and stop_close_all == ""
  291. strategy.entry(id = "L_3", direction = strategy.long,alert_message = Long_message, qty = qty_new )
  292.  
  293.  
  294. if strategy.opentrades == 3
  295. strategy.exit(id = "E_3",from_entry = "L_3", qty_percent = 100,alert_message =Long_Exit_message,limit = strategy.opentrades.entry_price(strategy.opentrades - 1) * (1+percent_change))
  296.  
  297.  
  298.  
  299. if long_4 and strategy.opentrades == 3 and inDateRange and stop_close_all == ""
  300. strategy.entry(id = "L_4", direction = strategy.long,alert_message = Long_message, qty = qty_new )
  301.  
  302.  
  303.  
  304. if strategy.opentrades == 4
  305. strategy.exit(id = "E_4",from_entry = "L_4", qty_percent = 100,alert_message =Long_Exit_message,limit = strategy.opentrades.entry_price(strategy.opentrades - 1) * (1+percent_change))
  306.  
  307.  
  308.  
  309. if long_5 and strategy.opentrades == 4 and inDateRange and stop_close_all == ""
  310. strategy.entry(id = "L_5", direction = strategy.long,alert_message = Long_message, qty = qty_new )
  311.  
  312.  
  313. if strategy.opentrades == 5
  314. strategy.exit(id = "E_5",from_entry = "L_5", qty_percent = 100,alert_message =Long_Exit_message, limit = strategy.opentrades.entry_price(strategy.opentrades - 1) * (1+percent_change))
  315.  
  316.  
  317. // ###########################################################################################
  318.  
  319. if ten_grid and ( trade_direction == 'SHORT' or trade_direction == 'BOTH')
  320.  
  321. if short_1 and strategy.opentrades == 0 and inDateRange and stop_close_all == ""
  322. strategy.entry(id = "S_1", direction = strategy.short,alert_message = Short_message, qty = qty_new )
  323.  
  324. if strategy.opentrades == 1
  325. strategy.exit(id = "ES_1",from_entry = "S_1", qty_percent = 100,alert_message =Short_Exit_message, limit = strategy.opentrades.entry_price(strategy.opentrades - 1)*(1-percent_change))
  326.  
  327. if short_2 and strategy.opentrades == 1 and inDateRange and stop_close_all == ""
  328. strategy.entry(id = "S_2",direction = strategy.short,alert_message = Short_message, qty = qty_new )
  329.  
  330.  
  331. if strategy.opentrades == 2
  332. strategy.exit(id = "ES_2",from_entry = "S_2", qty_percent = 100,alert_message =Short_Exit_message, limit = strategy.opentrades.entry_price(strategy.opentrades - 1)*(1-percent_change))
  333.  
  334.  
  335. if short_3 and strategy.opentrades == 2 and inDateRange and stop_close_all == ""
  336. strategy.entry(id = "S_3", direction = strategy.short,alert_message = Short_message, qty = qty_new )
  337.  
  338.  
  339. if strategy.opentrades == 3
  340. strategy.exit(id = "ES_3",from_entry = "S_3", qty_percent = 100,alert_message =Short_Exit_message, limit = strategy.opentrades.entry_price(strategy.opentrades - 1)*(1-percent_change))
  341.  
  342.  
  343. if short_4 and strategy.opentrades == 3 and inDateRange and stop_close_all == ""
  344. strategy.entry(id = "S_4", direction = strategy.short,alert_message = Short_message, qty = qty_new )
  345.  
  346.  
  347. if strategy.opentrades == 4
  348. strategy.exit(id = "ES_4",from_entry = "S_4", qty_percent = 100,alert_message =Short_Exit_message,limit = strategy.opentrades.entry_price(strategy.opentrades - 1)*(1-percent_change))
  349.  
  350.  
  351. if short_5 and strategy.opentrades == 4 and inDateRange and stop_close_all == ""
  352. strategy.entry(id = "S_5", direction = strategy.short,alert_message = Short_message, qty = qty_new )
  353.  
  354.  
  355. if strategy.opentrades == 5
  356. strategy.exit(id = "ES_5",from_entry = "S_5", qty_percent = 100,alert_message =Short_Exit_message, limit = strategy.opentrades.entry_price(strategy.opentrades - 1)*(1-percent_change))
  357.  
  358.  
  359.  
  360. // ###########################################################################################
  361.  
  362. // ###########################################################################################
  363. long_exit_price = str.tostring(math.round_to_mintick(close*(1+percent_change)))
  364. short_exit_price = str.tostring(math.round_to_mintick(close*(1-percent_change)))
  365.  
  366.  
  367. if tewnty_grid and close > low_price and ( trade_direction == 'LONG' or trade_direction == 'BOTH')
  368.  
  369. if long_1 and strategy.opentrades == 0 and inDateRange and stop_close_all == ""
  370. strategy.entry(id = "L_1", direction = strategy.long,alert_message = Long_message, qty = qty_new )
  371.  
  372. if strategy.opentrades == 1
  373. strategy.exit(id = "E_1",from_entry = "L_1", qty_percent = 100,alert_message = Long_Exit_message,stop = 0, limit = strategy.opentrades.entry_price(strategy.opentrades - 1) * (1+percent_change))
  374.  
  375.  
  376. if long_2 and strategy.opentrades == 1 and inDateRange and close < strategy.opentrades.entry_price(strategy.opentrades - 1) * (1-percent_change) and stop_close_all == ""
  377. strategy.entry(id = "L_2", direction = strategy.long,alert_message = Long_message, qty = qty_new )
  378.  
  379.  
  380. if strategy.opentrades == 2
  381. strategy.exit(id = "E_2",from_entry = "L_2", qty_percent = 100,alert_message =Long_Exit_message, limit = strategy.opentrades.entry_price(strategy.opentrades - 1) * (1+percent_change))
  382.  
  383.  
  384. if long_3 and strategy.opentrades == 2 and inDateRange and close < strategy.opentrades.entry_price(strategy.opentrades - 1) * (1-percent_change) and stop_close_all == ""
  385. strategy.entry(id = "L_3", direction = strategy.long,alert_message = Long_message, qty = qty_new )
  386.  
  387.  
  388. if strategy.opentrades == 3
  389. strategy.exit(id = "E_3",from_entry = "L_3", qty_percent = 100,alert_message =Long_Exit_message,limit = strategy.opentrades.entry_price(strategy.opentrades - 1) * (1+percent_change))
  390.  
  391.  
  392. if long_4 and strategy.opentrades == 3 and inDateRange and close < strategy.opentrades.entry_price(strategy.opentrades - 1) * (1-percent_change) and stop_close_all == ""
  393. strategy.entry(id = "L_4", direction = strategy.long,alert_message = Long_message, qty = qty_new )
  394.  
  395.  
  396. if strategy.opentrades == 4
  397. strategy.exit(id = "E_4",from_entry = "L_4", qty_percent = 100,alert_message =Long_Exit_message,limit = strategy.opentrades.entry_price(strategy.opentrades - 1) * (1+percent_change))
  398.  
  399.  
  400. if long_5 and strategy.opentrades == 4 and inDateRange and close < strategy.opentrades.entry_price(strategy.opentrades - 1) * (1-percent_change) and stop_close_all == ""
  401. strategy.entry(id = "L_5", direction = strategy.long,alert_message = Long_message, qty = qty_new )
  402.  
  403. if strategy.opentrades == 5
  404. strategy.exit(id = "E_5",from_entry = "L_5", qty_percent = 100,alert_message =Long_Exit_message, limit = strategy.opentrades.entry_price(strategy.opentrades - 1) * (1+percent_change))
  405.  
  406.  
  407. if long_6 and strategy.opentrades == 5 and inDateRange and close < strategy.opentrades.entry_price(strategy.opentrades - 1) * (1-percent_change) and stop_close_all == ""
  408. strategy.entry(id = "L_6", direction = strategy.long,alert_message = Long_message, qty = qty_new )
  409.  
  410. if strategy.opentrades == 6
  411. strategy.exit(id = "E_6",from_entry = "L_6", qty_percent = 100,alert_message =Long_Exit_message, limit = strategy.opentrades.entry_price(strategy.opentrades - 1) * (1+percent_change))
  412.  
  413. if long_7 and strategy.opentrades == 6 and inDateRange and close < strategy.opentrades.entry_price(strategy.opentrades - 1) * (1-percent_change) and stop_close_all == ""
  414. strategy.entry(id = "L_7", direction = strategy.long,alert_message = Long_message, qty = qty_new )
  415.  
  416. if strategy.opentrades == 7
  417. strategy.exit(id = "E_7",from_entry = "L_7", qty_percent = 100,alert_message =Long_Exit_message, limit = strategy.opentrades.entry_price(strategy.opentrades - 1) * (1+percent_change))
  418.  
  419. if long_8 and strategy.opentrades == 7 and inDateRange and close < strategy.opentrades.entry_price(strategy.opentrades - 1) * (1-percent_change) and stop_close_all == ""
  420. strategy.entry(id = "L_8", direction = strategy.long,alert_message = Long_message, qty = qty_new )
  421.  
  422. if strategy.opentrades == 8
  423. strategy.exit(id = "E_8",from_entry = "L_8", qty_percent = 100,alert_message =Long_Exit_message, limit = strategy.opentrades.entry_price(strategy.opentrades - 1) * (1+percent_change))
  424.  
  425. if long_9 and strategy.opentrades == 8 and inDateRange and close < strategy.opentrades.entry_price(strategy.opentrades - 1) * (1-percent_change) and stop_close_all == ""
  426. strategy.entry(id = "L_9", direction = strategy.long,alert_message = Long_message, qty = qty_new )
  427.  
  428. if strategy.opentrades == 9
  429. strategy.exit(id = "E_9",from_entry = "L_9", qty_percent = 100,alert_message =Long_Exit_message, limit = strategy.opentrades.entry_price(strategy.opentrades - 1) * (1+percent_change))
  430.  
  431. if long_10 and strategy.opentrades == 9 and inDateRange and close < strategy.opentrades.entry_price(strategy.opentrades - 1) * (1-percent_change) and stop_close_all == ""
  432. strategy.entry(id = "L_10", direction = strategy.long,alert_message = Long_message, qty = qty_new )
  433.  
  434. if strategy.opentrades == 10
  435. strategy.exit(id = "E_10",from_entry = "L_10", qty_percent = 100,alert_message =Long_Exit_message, limit = strategy.opentrades.entry_price(strategy.opentrades - 1) * (1+percent_change))
  436.  
  437.  
  438. // ##### STOP LOSS CODES #####
  439.  
  440. if use_stop and low < low_price_stop and strategy.position_size > 0
  441. strategy.close_all(alert_message =Long_Stop_message, immediately = true)
  442. stop_close_all := "DONT"
  443.  
  444.  
  445. // ###########################################################################################
  446.  
  447. if tewnty_grid and close < high_price and ( trade_direction == 'SHORT' or trade_direction == 'BOTH')
  448.  
  449. if short_1 and strategy.opentrades == 0 and inDateRange and stop_close_all == ""
  450. strategy.entry(id = "S_1", direction = strategy.short,alert_message = Short_message, qty = qty_new )
  451.  
  452. if strategy.opentrades == 1
  453. strategy.exit(id = "ES_1",from_entry = "S_1", qty_percent = 100,alert_message =Short_Exit_message, limit = strategy.opentrades.entry_price(strategy.opentrades - 1)*(1-percent_change))
  454.  
  455.  
  456. if short_2 and strategy.opentrades == 1 and inDateRange and close > strategy.opentrades.entry_price(strategy.opentrades - 1) * (1+percent_change) and stop_close_all == ""
  457. strategy.entry(id = "S_2",direction = strategy.short,alert_message = Short_message, qty = qty_new )
  458.  
  459.  
  460. if strategy.opentrades == 2
  461. strategy.exit(id = "ES_2",from_entry = "S_2", qty_percent = 100,alert_message =Short_Exit_message, limit = strategy.opentrades.entry_price(strategy.opentrades - 1)*(1-percent_change))
  462.  
  463.  
  464. if short_3 and strategy.opentrades == 2 and inDateRange and close > strategy.opentrades.entry_price(strategy.opentrades - 1) * (1+percent_change) and stop_close_all == ""
  465. strategy.entry(id = "S_3", direction = strategy.short,alert_message = Short_message, qty = qty_new )
  466.  
  467.  
  468. if strategy.opentrades == 3
  469. strategy.exit(id = "ES_3",from_entry = "S_3", qty_percent = 100,alert_message =Short_Exit_message, limit = strategy.opentrades.entry_price(strategy.opentrades - 1)*(1-percent_change))
  470.  
  471.  
  472. if short_4 and strategy.opentrades == 3 and inDateRange and close > strategy.opentrades.entry_price(strategy.opentrades - 1) * (1+percent_change) and stop_close_all == ""
  473. strategy.entry(id = "S_4", direction = strategy.short,alert_message = Short_message, qty = qty_new )
  474.  
  475.  
  476. if strategy.opentrades == 4
  477. strategy.exit(id = "ES_4",from_entry = "S_4", qty_percent = 100,alert_message =Short_Exit_message,limit = strategy.opentrades.entry_price(strategy.opentrades - 1)*(1-percent_change))
  478.  
  479.  
  480. if short_5 and strategy.opentrades == 4 and inDateRange and close > strategy.opentrades.entry_price(strategy.opentrades - 1) * (1+percent_change) and stop_close_all == ""
  481. strategy.entry(id = "S_5", direction = strategy.short,alert_message = Short_message, qty = qty_new )
  482.  
  483.  
  484. if strategy.opentrades == 5
  485. strategy.exit(id = "ES_5",from_entry = "S_5", qty_percent = 100,alert_message =Short_Exit_message, limit = strategy.opentrades.entry_price(strategy.opentrades - 1)*(1-percent_change))
  486.  
  487.  
  488. if short_6 and strategy.opentrades == 5 and inDateRange and close > strategy.opentrades.entry_price(strategy.opentrades - 1) * (1+percent_change) and stop_close_all == ""
  489. strategy.entry(id = "S_6", direction = strategy.short,alert_message = Short_message, qty = qty_new )
  490.  
  491.  
  492. if strategy.opentrades == 6
  493. strategy.exit(id = "ES_6",from_entry = "S_6", qty_percent = 100,alert_message =Short_Exit_message, limit = strategy.opentrades.entry_price(strategy.opentrades - 1)*(1-percent_change))
  494.  
  495.  
  496. if short_7 and strategy.opentrades == 6 and inDateRange and close > strategy.opentrades.entry_price(strategy.opentrades - 1) * (1+percent_change) and stop_close_all == ""
  497. strategy.entry(id = "S_7", direction = strategy.short,alert_message = Short_message, qty = qty_new )
  498.  
  499. if strategy.opentrades == 7
  500. strategy.exit(id = "ES_7",from_entry = "S_7", qty_percent = 100,alert_message =Short_Exit_message, limit = strategy.opentrades.entry_price(strategy.opentrades - 1)*(1-percent_change))
  501.  
  502.  
  503. if short_8 and strategy.opentrades == 7 and inDateRange and close > strategy.opentrades.entry_price(strategy.opentrades - 1) * (1+percent_change) and stop_close_all == ""
  504. strategy.entry(id = "S_8", direction = strategy.short,alert_message = Short_message, qty = qty_new )
  505.  
  506. if strategy.opentrades == 8
  507. strategy.exit(id = "ES_8",from_entry = "S_8", qty_percent = 100,alert_message =Short_Exit_message, limit = strategy.opentrades.entry_price(strategy.opentrades - 1)*(1-percent_change))
  508.  
  509. if short_9 and strategy.opentrades == 8 and inDateRange and close > strategy.opentrades.entry_price(strategy.opentrades - 1) * (1+percent_change) and stop_close_all == ""
  510. strategy.entry(id = "S_9", direction = strategy.short,alert_message = Short_message, qty = qty_new )
  511.  
  512. if strategy.opentrades == 9
  513. strategy.exit(id = "ES_9",from_entry = "S_9", qty_percent = 100,alert_message =Short_Exit_message, limit = strategy.opentrades.entry_price(strategy.opentrades - 1)*(1-percent_change))
  514.  
  515. if short_10 and strategy.opentrades == 9 and inDateRange and close > strategy.opentrades.entry_price(strategy.opentrades - 1) * (1+percent_change) and stop_close_all == ""
  516. strategy.entry(id = "S_10", direction = strategy.short,alert_message = Short_message, qty = qty_new )
  517.  
  518. if strategy.opentrades == 10
  519. strategy.exit(id = "ES_10",from_entry = "S_10", qty_percent = 100,alert_message =Short_Exit_message, limit = strategy.opentrades.entry_price(strategy.opentrades - 1)*(1-percent_change))
  520.  
  521.  
  522. // ###########################################################################################
  523.  
  524. if use_stop and high > high_price_stop and strategy.position_size < 0
  525. strategy.close_all(alert_message =Short_Stop_message, immediately = true)
  526. stop_close_all := "DONT"
  527.  
  528.  
  529. bgcolor(stop_close_all == "DONT" and stop_close_all[1] != "DONT" ? color.rgb(255, 42, 42) : na )
  530.  
  531. //// ########################################################################
  532. // Plot Functions
  533.  
  534. plot(strategy.position_size < 0 ? strategy.opentrades.entry_price(strategy.opentrades - 1)*(1-percent_change) : na , color= color.rgb(247, 85, 85), linewidth= 1, style=plot.style_cross)
  535. plot(strategy.position_size > 0 ? strategy.opentrades.entry_price(strategy.opentrades - 1)*(1+percent_change) : na , color= color.rgb(136, 247, 85), linewidth= 1, style=plot.style_cross)
  536.  
  537. plot(inDateRange ? high_price_stop : na, color= color.rgb(255, 37, 37), linewidth =4 )
  538. plot(inDateRange ? low_price_stop : na, color= color.rgb(255, 34, 37), linewidth =4 )
  539.  
  540. plot(inDateRange ? high_price : na, color= color.rgb(0, 0, 0), linewidth =3 )
  541. plot(inDateRange ? low_price : na, color= color.rgb(255, 255, 255), linewidth =3 )
  542. plot(inDateRange ? (high_price + low_price) / 2 : na , color= color.rgb(0, 68, 255), linewidth = 2 )
  543.  
  544. // ###########################################################################################
  545.  
  546.  
  547. var float new_ten_grid_1 = na
  548. var float new_ten_grid_2 = na
  549. var float new_ten_grid_3 = na
  550. var float new_ten_grid_4 = na
  551. var float new_ten_grid_5 = na
  552. var float new_ten_grid_6 = na
  553. var float new_ten_grid_7 = na
  554. var float new_ten_grid_8 = na
  555. var float new_ten_grid_9 = na
  556. var float new_ten_grid_10 = na
  557.  
  558.  
  559. if ten_grid == true and show_grid_level_bg and inDateRange
  560.  
  561. new_ten_grid_1 := grid_1
  562. new_ten_grid_2 := grid_2
  563. new_ten_grid_3 := grid_3
  564. new_ten_grid_4 := grid_4
  565. new_ten_grid_5 := grid_5
  566. new_ten_grid_6 := grid_6
  567. new_ten_grid_7 := grid_7
  568. new_ten_grid_8 := grid_8
  569. new_ten_grid_9 := grid_9
  570. new_ten_grid_10 := grid_10
  571.  
  572.  
  573. else if ten_grid == false
  574.  
  575. new_ten_grid_1 := na
  576. new_ten_grid_2 := na
  577. new_ten_grid_3 := na
  578. new_ten_grid_4 := na
  579. new_ten_grid_5 := na
  580. new_ten_grid_6 := na
  581. new_ten_grid_7 := na
  582. new_ten_grid_8 := na
  583. new_ten_grid_9 := na
  584. new_ten_grid_10 := na
  585.  
  586.  
  587. fill(plot(inDateRange ? new_ten_grid_1 : na , color = color.new(color.green, 90)),
  588. plot(inDateRange ? new_ten_grid_2 : na , color = color.new(color.green, 90)),
  589. color = color.new(color.green, 90))
  590. fill(plot(inDateRange ? new_ten_grid_2 : na , color = color.new(color.green, 85)),
  591. plot(inDateRange ? new_ten_grid_3 : na , color = color.new(color.green, 85)),
  592. color = color.new(color.green, 85))
  593. fill(plot(inDateRange ? new_ten_grid_3 : na , color = color.new(color.green, 80)),
  594. plot(inDateRange ? new_ten_grid_4 : na , color = color.new(color.green, 80)),
  595. color = color.new(color.green, 80))
  596. fill(plot(inDateRange ? new_ten_grid_4 : na , color = color.new(color.green, 70)),
  597. plot(inDateRange ? new_ten_grid_5 : na , color = color.new(color.green, 70)),
  598. color = color.new(color.green, 70))
  599. fill(plot(inDateRange ? new_ten_grid_5 : na , color = color.new(color.green, 60)),
  600. plot(inDateRange ? new_ten_grid_6 : na, color = color.new(color.green, 60)),
  601. color = color.new(color.green, 60))
  602. fill(plot(inDateRange ? new_ten_grid_6 : na, color = color.new(color.red, 60)),
  603. plot(inDateRange ? new_ten_grid_7 : na, color = color.new(color.red, 60)),
  604. color = color.new(color.red, 60))
  605. fill(plot(inDateRange ? new_ten_grid_7 : na, color = color.new(color.red, 70)),
  606. plot(inDateRange ? new_ten_grid_8 : na, color = color.new(color.red, 70)),
  607. color = color.new(color.red, 70))
  608. fill(plot(inDateRange ? new_ten_grid_8 : na, color = color.new(color.red, 80)),
  609. plot(inDateRange ? new_ten_grid_9 : na, color = color.new(color.red, 80)),
  610. color = color.new(color.red, 80))
  611. fill(plot(inDateRange ? new_ten_grid_9 : na, color = color.new(color.red, 85)),
  612. plot(inDateRange ? new_ten_grid_10 : na, color = color.new(color.red, 85)),
  613. color = color.new(color.red, 85))
  614.  
  615.  
  616.  
  617. // // ###########################################################################################
  618.  
  619. var float new_twenty_grid_1 = na
  620. var float new_twenty_grid_2 = na
  621. var float new_twenty_grid_3 = na
  622. var float new_twenty_grid_4 = na
  623. var float new_twenty_grid_5 = na
  624. var float new_twenty_grid_6 = na
  625. var float new_twenty_grid_7 = na
  626. var float new_twenty_grid_8 = na
  627. var float new_twenty_grid_9 = na
  628. var float new_twenty_grid_10 = na
  629. var float new_twenty_grid_11 = na
  630. var float new_twenty_grid_12 = na
  631. var float new_twenty_grid_13 = na
  632. var float new_twenty_grid_14 = na
  633. var float new_twenty_grid_15 = na
  634. var float new_twenty_grid_16 = na
  635. var float new_twenty_grid_17 = na
  636. var float new_twenty_grid_18 = na
  637. var float new_twenty_grid_19 = na
  638. var float new_twenty_grid_20 = na
  639.  
  640.  
  641.  
  642. if tewnty_grid == true and show_grid_level_bg and inDateRange
  643.  
  644. new_twenty_grid_1 := grid_1
  645. new_twenty_grid_2 := grid_2
  646. new_twenty_grid_3 := grid_3
  647. new_twenty_grid_4 := grid_4
  648. new_twenty_grid_5 := grid_5
  649. new_twenty_grid_6 := grid_6
  650. new_twenty_grid_7 := grid_7
  651. new_twenty_grid_8 := grid_8
  652. new_twenty_grid_9 := grid_9
  653. new_twenty_grid_10 := grid_10
  654. new_twenty_grid_11 := grid_11
  655. new_twenty_grid_12 := grid_12
  656. new_twenty_grid_13 := grid_13
  657. new_twenty_grid_14 := grid_14
  658. new_twenty_grid_15 := grid_15
  659. new_twenty_grid_16 := grid_16
  660. new_twenty_grid_17 := grid_17
  661. new_twenty_grid_18 := grid_18
  662. new_twenty_grid_19 := grid_19
  663. new_twenty_grid_20 := grid_20
  664.  
  665.  
  666.  
  667. else if tewnty_grid == false
  668.  
  669. new_twenty_grid_1 := na
  670. new_twenty_grid_2 := na
  671. new_twenty_grid_3 := na
  672. new_twenty_grid_4 := na
  673. new_twenty_grid_5 := na
  674. new_twenty_grid_6 := na
  675. new_twenty_grid_7 := na
  676. new_twenty_grid_8 := na
  677. new_twenty_grid_9 := na
  678. new_twenty_grid_10 := na
  679. new_twenty_grid_11 := na
  680. new_twenty_grid_12 := na
  681. new_twenty_grid_13 := na
  682. new_twenty_grid_14 := na
  683. new_twenty_grid_15 := na
  684. new_twenty_grid_16 := na
  685. new_twenty_grid_17 := na
  686. new_twenty_grid_18 := na
  687. new_twenty_grid_19 := na
  688. new_twenty_grid_20 := na
  689.  
  690.  
  691.  
  692. fill(plot(inDateRange ? new_twenty_grid_1 : na , color = color.new(color.green, 90)),
  693. plot(inDateRange ? new_twenty_grid_2 : na , color = color.new(color.green, 90)),
  694. color = color.new(color.green, 90))
  695. fill(plot(inDateRange ? new_twenty_grid_2 : na , color = color.new(color.green, 85)),
  696. plot(inDateRange ? new_twenty_grid_3 : na , color = color.new(color.green, 85)),
  697. color = color.new(color.green, 85))
  698. fill(plot(inDateRange ? new_twenty_grid_3 : na , color = color.new(color.green, 80)),
  699. plot(inDateRange ? new_twenty_grid_4 : na , color = color.new(color.green, 80)),
  700. color = color.new(color.green, 80))
  701. fill(plot(inDateRange ? new_twenty_grid_4 : na , color = color.new(color.green, 70)),
  702. plot(inDateRange ? new_twenty_grid_5 : na , color = color.new(color.green, 70)),
  703. color = color.new(color.green, 70))
  704. fill(plot(inDateRange ? new_twenty_grid_5 : na , color = color.new(color.green, 60)),
  705. plot(inDateRange ? new_twenty_grid_6 : na , color = color.new(color.green, 60)),
  706. color = color.new(color.green, 60))
  707. fill(plot(inDateRange ? new_twenty_grid_6 : na , color = color.new(color.green, 60)),
  708. plot(inDateRange ? new_twenty_grid_7 : na , color = color.new(color.green, 60)),
  709. color = color.new(color.green, 60))
  710. fill(plot(inDateRange ? new_twenty_grid_7 : na , color = color.new(color.green, 70)),
  711. plot(inDateRange ? new_twenty_grid_8 : na , color = color.new(color.green, 70)),
  712. color = color.new(color.green, 70))
  713. fill(plot(inDateRange ? new_twenty_grid_8 : na , color = color.new(color.green, 80)),
  714. plot(inDateRange ? new_twenty_grid_9 : na , color = color.new(color.green, 80)),
  715. color = color.new(color.green, 80))
  716. fill(plot(inDateRange ? new_twenty_grid_9 : na , color = color.new(color.green, 85)),
  717. plot(inDateRange ? new_twenty_grid_10 : na , color = color.new(color.green, 85)),
  718. color = color.new(color.green, 85))
  719. fill(plot(inDateRange ? new_twenty_grid_10 : na , color = color.new(color.red, 90)),
  720. plot(inDateRange ? new_twenty_grid_11 : na , color = color.new(color.red, 90)),
  721. color = color.new(color.red, 90))
  722. fill(plot(inDateRange ? new_twenty_grid_11 : na , color = color.new(color.red, 85)),
  723. plot(inDateRange ? new_twenty_grid_12 : na , color = color.new(color.red, 85)),
  724. color = color.new(color.red, 85))
  725. fill(plot(inDateRange ? new_twenty_grid_12 : na , color = color.new(color.red, 80)),
  726. plot(inDateRange ? new_twenty_grid_13 : na , color = color.new(color.red, 80)),
  727. color = color.new(color.red, 80))
  728. fill(plot(inDateRange ? new_twenty_grid_13 : na , color = color.new(color.red, 70)),
  729. plot(inDateRange ? new_twenty_grid_14 : na , color = color.new(color.red, 70)),
  730. color = color.new(color.red, 70))
  731. fill(plot(inDateRange ? new_twenty_grid_14 : na , color = color.new(color.red, 60)),
  732. plot(inDateRange ? new_twenty_grid_15 : na , color = color.new(color.red, 60)),
  733. color = color.new(color.red, 60))
  734. fill(plot(inDateRange ? new_twenty_grid_15 : na , color = color.new(color.red, 60)),
  735. plot(inDateRange ? new_twenty_grid_16 : na , color = color.new(color.red, 60)),
  736. color = color.new(color.red, 60))
  737. fill(plot(inDateRange ? new_twenty_grid_16 : na , color = color.new(color.red, 70)),
  738. plot(inDateRange ? new_twenty_grid_17 : na , color = color.new(color.red, 70)),
  739. color = color.new(color.red, 70))
  740. fill(plot(inDateRange ? new_twenty_grid_17 : na , color = color.new(color.red, 80)),
  741. plot(inDateRange ? new_twenty_grid_18 : na , color = color.new(color.red, 80)),
  742. color = color.new(color.red, 80))
  743. fill(plot(inDateRange ? new_twenty_grid_18 : na , color = color.new(color.red, 85)),
  744. plot(inDateRange ? new_twenty_grid_19 : na , color = color.new(color.red, 85)),
  745. color = color.new(color.red, 85))
  746. fill(plot(inDateRange ? new_twenty_grid_19 : na , color = color.new(color.red, 85)),
  747. plot(inDateRange ? new_twenty_grid_20 : na , color = color.new(color.red, 85)),
  748. color = color.new(color.red, 85))
  749.  
  750. // // ###########################################################################################
  751.  
  752. //###############################################################################################
  753. //------------------------------Table
  754. //###############################################################################################
  755. showDashboard = input.bool(true, "Show Table", group=" ############# 🔳 TABLE Settings 🔳 ############ ")
  756. locationDashboard = input.string("Top Right", "Table Location", ["Top Right", "Middle Right", "Bottom Right", "Top Center", "Middle Center", "Bottom Center", "Top Left", "Middle Left", "Bottom Left"], group=" ############# 🔳 TABLE Settings 🔳 ############ ")
  757. sizeDashboard = input.string("Normal", "Table Size", ["Large", "Normal", "Small", "Tiny"], group=" ############# 🔳 TABLE Settings 🔳 ############ ")
  758.  
  759. var dashboard_loc = locationDashboard == "Top Right" ? position.top_right : locationDashboard == "Middle Right" ? position.middle_right : locationDashboard == "Bottom Right" ? position.bottom_right : locationDashboard == "Top Center" ? position.top_center : locationDashboard == "Middle Center" ? position.middle_center : locationDashboard == "Bottom Center" ? position.bottom_center : locationDashboard == "Top Left" ? position.top_left : locationDashboard == "Middle Left" ? position.middle_left : position.bottom_left
  760. var dashboard_size = sizeDashboard == "Large" ? size.large : sizeDashboard == "Normal" ? size.normal : sizeDashboard == "Small" ? size.small : size.tiny
  761.  
  762. if showDashboard
  763. var myTable = table.new(position = dashboard_loc, columns = 5, rows = 2,bgcolor = color.white, border_width = 1, frame_color = color.rgb(25, 150, 25), frame_width = 3, border_color = color.rgb(25, 150, 25))
  764.  
  765.  
  766. table.cell(table_id = myTable, column = 0, row = 0, text = "Open positions" , text_color = color.white, bgcolor = color.orange, text_size=dashboard_size)
  767. table.cell(table_id = myTable, column = 1, row = 0, text = "Open profit" , text_color = color.white, bgcolor = color.orange, text_size=dashboard_size)
  768. table.cell(table_id = myTable, column = 2, row = 0, text = "Total trades" , text_color = color.white, bgcolor = color.orange, text_size=dashboard_size)
  769. table.cell(table_id = myTable, column = 3, row = 0, text = "Total profit" , text_color = color.white, bgcolor = color.orange, text_size=dashboard_size)
  770. table.cell(table_id = myTable, column = 4, row = 0, text = "Current equity" , text_color = color.white, bgcolor = color.orange, text_size=dashboard_size)
  771.  
  772.  
  773.  
  774. table.cell(table_id = myTable, column = 0, row = 1, text = str.tostring(strategy.opentrades) , text_font_family = font.family_monospace, text_size=dashboard_size)
  775. table.cell(table_id = myTable, column = 1, row = 1, text = "$" + str.tostring(math.round(strategy.openprofit, 1)) , bgcolor = strategy.openprofit >= 0 ? color.green : color.red , text_font_family = font.family_monospace, text_size=dashboard_size)
  776. table.cell(table_id = myTable, column = 2, row = 1, text = str.tostring(strategy.closedtrades) , text_font_family = font.family_monospace, text_size=dashboard_size)
  777. table.cell(table_id = myTable, column = 3, row = 1, text = "$" + str.tostring(math.round(strategy.netprofit, 1)) , bgcolor = strategy.netprofit >= 0 ? color.green : color.red , text_font_family = font.family_monospace, text_size=dashboard_size)
  778. table.cell(table_id = myTable, column = 4, row = 1, text = "$" + str.tostring(math.round(strategy.equity, 1)) , bgcolor = strategy.equity >= 0 ? color.green : color.red , text_font_family = font.family_monospace, text_size=dashboard_size)
Advertisement
Comments
  • hemal9022
    1 year
    Comment was deleted
  • hemal9022
    273 days
    # text 0.12 KB | 0 0
    1. i found all types of premium tradingview indicators codes available on telegram - https://t.me/tradingview_premium_indicator
Add Comment
Please, Sign In to add comment
Advertisement