xmd79

Bjorgum Double Tap

Aug 21st, 2022
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.48 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. // © Bjorgum
  3.  
  4. // ________________________________________________________________
  5. // |\______________________________________________________________/|
  6. // || ________________ ||
  7. // || ___ __ )_____(_)___________________ ____ ________ ___ ||
  8. // || __ __ |____ /_ __ \_ ___/_ __ `/ / / /_ __ `__ \ ||
  9. // || _ /_/ /____ / / /_/ / / _ /_/ // /_/ /_ / / / / / ||
  10. // || /_____/ ___ / \____//_/ _\__, / \__,_/ /_/ /_/ /_/ ||
  11. // || /___/ /____/ ||
  12. // ||______________________________________________________________||
  13. // |/______________________________________________________________\|
  14.  
  15.  
  16. //@version=5
  17.  
  18. strategy(
  19. title = "Bjorgum Double Tap",
  20. shorttitle = "Bj Double Tap",
  21. overlay = true,
  22. max_lines_count = 500,
  23. max_labels_count = 500,
  24. precision = 3,
  25. default_qty_type = strategy.cash,
  26. commission_value = 0.04,
  27. commission_type = strategy.commission.percent,
  28. slippage = 1,
  29. currency = currency.USD,
  30. default_qty_value = 1000,
  31. initial_capital = 1000)
  32.  
  33. // ══════════════════════════════════ //
  34. // —————> Immutable Constants <—————— //
  35. // ══════════════════════════════════ //
  36.  
  37. string useStratTip = "Enables or disables the strategy tester allowing a change between either an indicator or a strategy."
  38. string dLongTip = "Detect long setups."
  39. string dShortTip = "Detect short setups."
  40. string FLIPTip = "Allow entry in the opposite bias while already in a position."
  41.  
  42. string startTip = "Start date & time to begin backtest period. Useful for beginning new bot. eg. Set time to now to make broker emulator in a flat position with the proper starting captial before setting alerts"
  43. string endTip = "End date & time to stop searching for setups for back testing."
  44.  
  45. string tolTip = "The difference in height allowable for the signifcant points of the pattern expressed as a percent of the pattern height. Example: The points can vary in height by 15% of the pattern height from one another."
  46. string lenTip = "The length used to calcuate significant points to form pivots for pattern detection. Example: The highest or lowest point in 50 bars."
  47. string fibTip = "The fib target extension projected from the neckline of the pattern in the direction of the pattern bias expressed as a percent. Example: 100% is a 1:1 measurment of the height from the pattern neckline."
  48. string stopPerTip = "The fib extension of the pattern height measured from the point of invalidation. Example: 0% would be the high point of a double top. 50% would be halfway between the top and the neckline."
  49. string offsetTip = "The number of bars lines are extended into the future during an ongoing pattern."
  50.  
  51. string atrStopTip = "Enables an ATR trailing stop once the target extension is breached. NOTE: This disables a take profit order in the strategy format."
  52. string atrLenTip = "The number of bars used in the ATR calculation."
  53. string atrMultTip = "The multiplier of the ATR value to subtract from the swing low or swing high point. Example: 1 is 100% of the ATR, 2 is 2x the ATR value."
  54. string lookbackTip = "The number of bars to look back to find a swing high or swing low to be used in the trailing stop calculation. Example: 1 ATR subtracted from the lowest point in 5 bars. 1 would use the lowest point within the last bar."
  55.  
  56. string tableTip = "Show the data table for trade limit levels during an active trade. (table will only show when a pattern is present on the chart, or in bar replay)."
  57. string labelTip = "Updates the double top/bottom label with 'Success' or 'Failure' and updates color based on the outcome of the pattern."
  58.  
  59. string thirdTip = "Where would you like to send your alerts?"
  60. string pPhraseTip = "A custom passphrase to authenticate your json message with a touch more security."
  61. string aTronKeyTip = "The name of your Alertatron API keys. (Add the ticker in brackets). Example: MyKeys(XBTUSD)."
  62. string tickerTip = "The ticker to be traded when using Trading Connector"
  63. string LongTip = "3Commas start long deal bot keys."
  64. string LongEndTip = "3Commas end long deal bot keys."
  65. string ShortTip = "3Commas start short deal bot keys."
  66. string ShortEndTip = "3Commas end short deal bot keys."
  67.  
  68. string dt = "Double Top"
  69. string db = "Double Bottom"
  70. string suc = " - Success"
  71. string fail = " - Failure"
  72. string winStr = "Target reached! 💰"
  73. string loseStr = "Stopped out... 🤷‍♂"
  74. string tabPerc = "{0, number, #.##}\n({1, number, #.##}%)"
  75. string tcStop = "slmod slprice={0} tradeid={1}"
  76. string dExit = "'{'\"content\": \"```Bjorgum {0}\\n\\n\\t'{{ticker}}' '{{interval}}'\\n\\n\\t{1}```\"'}'"
  77.  
  78. string S1 = "tiny" , string P1 = "top"
  79. string S2 = "small" , string P2 = "middle"
  80. string S3 = "normal" , string P3 = "bottom"
  81. string S4 = "large" , string P4 = "left"
  82. string S5 = "huge" , string P5 = "center"
  83. string S6 = "auto" , string P6 = "right"
  84. var string tnB = "" , string A1 = "Custom Json"
  85. string altStr = "" , string A2 = "Trading Connector"
  86. string tUp = "" , string A3 = "Alertatron"
  87. string dCordWin = "" , string A4 = "3Commas"
  88. string dCordLose = "" , string A5 = "Discord"
  89.  
  90. float pos = strategy.position_size
  91. int sync = bar_index
  92. bool confirm = barstate.isconfirmed
  93. var int dir = na
  94. var float lmt = na
  95. var float stp = na
  96. string altExit = na
  97.  
  98. bool FLAT = pos == 0
  99. bool LONG = pos > 0
  100. bool SHORT = pos < 0
  101. var int tradeId = 0
  102.  
  103. color col1 = color.new(#b2b5be, 15)
  104. color col2 = color.new(#b2b5be, 87)
  105. color col3 = color.new(#ffffff, 0)
  106. color col4 = color.new(#17ff00, 15)
  107. color col5 = color.new(#ff0000, 15)
  108. color col6 = color.new(#ff5252, 0)
  109.  
  110. var matrix<float> logs = matrix.new<float> (5, 3)
  111. var line [] zLines = array.new_line (5)
  112. var line [] tLines = array.new_line (5)
  113. var line [] bLines = array.new_line (5)
  114. var label[] bullLb = array.new_label ()
  115. var label[] bearLb = array.new_label ()
  116.  
  117. int timeStart = timestamp("01 Jan 2000")
  118. int timeEnd = timestamp("01 Jan 2099")
  119.  
  120. // ══════════════════════════════════ //
  121. // —————————> User Input <——————————— //
  122. // ══════════════════════════════════ //
  123.  
  124. string GRP1 = "════  Detection and Trade Parameters  ════"
  125. bool useStrat = input.bool (true , "Use Strategy" , group= GRP1, tooltip= useStratTip)
  126. bool dLong = input.bool (true , "Detect Bottoms" , group= GRP1, tooltip= dLongTip )
  127. bool dShort = input.bool (true , "Detect Tops" , group= GRP1, tooltip= dShortTip )
  128. bool FLIP = input.bool (true , "Flip Trades" , group= GRP1, tooltip= FLIPTip )
  129. float tol = input.float (15 , "Pivot Tolerance" , group= GRP1, tooltip= tolTip , minval= 1)
  130. int len = input.int (50 , "Pivot Length" , group= GRP1, tooltip= lenTip , minval= 1)
  131. float fib = input.float (100 , "Target Fib" , group= GRP1, tooltip= fibTip , minval= 0)
  132. int stopPer = input.int (0 , "Stop Loss Fib" , group= GRP1, tooltip= stopPerTip )
  133. int offset = input.int (30 , "Line Offset" , group= GRP1, tooltip= offsetTip , minval= 0)
  134.  
  135. string GRP2 = "═══════════ Time Filter ═══════════"
  136. int startTime = input.time(timeStart , "Start Filter" , group= GRP2, tooltip= startTip)
  137. int endTime = input.time(timeEnd , "End Filter" , group= GRP2, tooltip= endTip )
  138.  
  139. string GRP3 = "══════════ Trailing Stop ══════════"
  140. bool atrStop = input.bool (false , "Use Trail Stop" , group= GRP3, tooltip= atrStopTip )
  141. int atrLength = input.int (14 , "ATR Length" , group= GRP3, tooltip= atrLenTip , minval= 1)
  142. float atrMult = input.float (1 , "ATR Multiplier" , group= GRP3, tooltip= atrMultTip , minval= 0)
  143. int lookback = input.int (5 , "Swing Lookback" , group= GRP3, tooltip= lookbackTip, minval= 1)
  144.  
  145. string GRP5 = "════════════ Colors ════════════"
  146. color col = input.color (col1 , "Lines        " , group= GRP5, inline= "41")
  147. color zCol = input.color (col3 , "Patterns       " , group= GRP5, inline= "42")
  148. int hWidth = input.int (1 , "" , group= GRP5, inline= "41", minval= 1)
  149. int zWidth = input.int (1 , "" , group= GRP5, inline= "42", minval= 1)
  150. color colf = input.color (col2 , "Stop Fill" , group= GRP5)
  151. color tCol = input.color (col4 , "Target Color" , group= GRP5)
  152. color sCol = input.color (col5 , "Stop Color" , group= GRP5)
  153. color trailCol = input.color (col6 , "Trail Color" , group= GRP5)
  154.  
  155. string GRP6 = "═════════  Table and Label  ═════════"
  156. bool showTable = input.bool (true , "Show Table" , group= GRP6, tooltip= tableTip)
  157. bool setLab = input.bool (true , "Update Label" , group= GRP6, tooltip= labelTip)
  158. string labSize = input.string("small" , "Label Text Size" , group= GRP6, options= [S1, S2, S3, S4, S5, S6])
  159. string textSize = input.string("normal" , "Table Text Size" , group= GRP6, options= [S1, S2, S3, S4, S5, S6])
  160. string tableYpos = input.string("bottom" , "Table Position" , group= GRP6, options= [P1, P2, P3])
  161. string tableXpos = input.string("right" , "" , group= GRP6, options= [P4, P5, P6])
  162.  
  163. string GRP7 = "══════════ Alert Strings ══════════"
  164. string thirdParty = input.string(A1 , "3rd Party" , group= GRP7, tooltip= thirdTip, options= [A1, A2, A3, A4, A5])
  165. string pPhrase = input.string("1234" , "Json Passphrase" , group= GRP7, tooltip= pPhraseTip )
  166. string aTronKey = input.string("myKeys" , "Alertatron Key" , group= GRP7, tooltip= aTronKeyTip)
  167. string tcTicker = input.string("" , "TC Ticker" , group= GRP7, tooltip= tickerTip )
  168. string c3Long = input.string("" , "3Comma Long" , group= GRP7, tooltip= LongTip )
  169. string c3LongEnd = input.string("" , "3Comma Long End" , group= GRP7, tooltip= LongEndTip )
  170. string c3Short = input.string("" , "3Comma Short" , group= GRP7, tooltip= ShortTip )
  171. string c3ShortEnd = input.string("" , "3Comma Short End", group= GRP7, tooltip= ShortEndTip)
  172.  
  173. // ══════════════════════════════════ //
  174. // ————> Variable Calculations <————— //
  175. // ══════════════════════════════════ //
  176.  
  177. bool dif = stopPer != 0
  178. int set = sync + offset
  179.  
  180. float atr = ta.atr (14)
  181. float sLow = ta.lowest (lookback) - (atr * atrMult)
  182. float sHigh = ta.highest (lookback) + (atr * atrMult)
  183.  
  184. float pivHigh = ta.highest (len)
  185. float pivLows = ta.lowest (len)
  186.  
  187. float hbar = ta.highestbars (len)
  188. float lbar = ta.lowestbars (len)
  189.  
  190. // ══════════════════════════════════ //
  191. // ———> Functional Declarations <———— //
  192. // ══════════════════════════════════ //
  193.  
  194. High(m) =>
  195. float result = (m == 1 ? high : low)
  196.  
  197. Low(m) =>
  198. float result = (m == 1 ? low : high)
  199.  
  200. perD(_p) =>
  201. float result = (_p - close) / close * 100
  202.  
  203. _coords(_x, _i) =>
  204. x = matrix.get (_x, _i, 0)
  205. y = matrix.get (_x, _i, 1)
  206. [int(x), y]
  207.  
  208. _arrayLoad(_x, _max, _val) =>
  209. array.unshift (_x, _val)
  210. if array.size (_x) > _max
  211. array.pop (_x)
  212.  
  213. _matrixPush(_mx, _max, _row) =>
  214. matrix.add_row(_mx, matrix.rows (_mx), _row)
  215. if matrix.rows (_mx) > _max
  216. matrix.remove_row (_mx, 0)
  217.  
  218. _mxLog(_cond, _x, _y) =>
  219. float[] _row = array.from (sync, _y, 0)
  220. if _cond
  221. _matrixPush (_x, 5, _row)
  222.  
  223. _mxUpdate(_cond, _dir, _x, y) =>
  224. int m = _dir ? 1 : -1
  225. int _end = matrix.rows (_x) -1
  226. if _cond and y * m > matrix.get (_x, _end, 1) * m
  227. matrix.set (_x, _end, 0, sync)
  228. matrix.set (_x, _end, 1, y)
  229.  
  230. _extend(_x, _len) =>
  231. for l in _x
  232. line.set_x2(l, _len)
  233.  
  234. _hLine(_l, x2, y2, y3, y4, y5, _t) =>
  235. line l1 = line.new (x2 , y2, set, y2, color= col, width= hWidth)
  236. line l2 = line.new (x2 , y4, set, y4, color= col, width= hWidth)
  237. array.set (_l , 3, l1)
  238. array.set (_l , 2, l2)
  239. array.set (_l , 1, line.new (x2 , y3, set, y3, color= col, width= hWidth))
  240. array.set (_l , 0, line.new (sync-1, _t, set, _t, color= col, width= hWidth))
  241. linefill.new (l1 , l2, colf)
  242. if stopPer != 0
  243. array.set (_l, 4, line.new (sync-1, y5, set, y5, color= col, width= hWidth))
  244.  
  245. _zLine(x1, y1, x2, y2, x3, y3, x4, y4) =>
  246. array.set (zLines, 3, line.new (x1, y1, x2 , y2 , color= zCol, width= zWidth))
  247. array.set (zLines, 2, line.new (x2, y2, x3 , y3 , color= zCol, width= zWidth))
  248. array.set (zLines, 1, line.new (x3, y3, x4 , y4 , color= zCol, width= zWidth))
  249. array.set (zLines, 0, line.new (x4, y4, sync, close, color= zCol, width= zWidth))
  250.  
  251. _label(x, y, m) =>
  252. m > 0 ?
  253. _arrayLoad (bearLb, 1, label.new (x, y, dt, color= color(na), style= label.style_label_down, textcolor= col, size= labSize)) :
  254. _arrayLoad (bullLb, 1, label.new (x, y, db, color= color(na), style= label.style_label_up, textcolor= col, size= labSize))
  255.  
  256. _labelUpdate(_x, _y, m) =>
  257. if (_x or _y) and setLab
  258. label lab = array.get (m > 0 ? bearLb : bullLb, 0)
  259. string oStr = (m > 0 ? dt : db)
  260. string nStr = oStr + (_x ? suc : fail)
  261. label.set_text (lab, nStr)
  262. label.set_textcolor (lab, _x ? tCol : sCol)
  263.  
  264. _atrTrail(_cond, _lt, _s, m) =>
  265. var float _stop = na
  266. var bool _flag = na
  267. var bool _trail = na
  268. _flag := _cond
  269. _stop := _s
  270. _trail := _flag ? false : _trail
  271. if atrStop and useStrat
  272. if _lt
  273. _flag := false
  274. _trail := true
  275. _stop := m == -1 ? _lt ? sLow : math.max(_stop, sLow) : _stop
  276. _stop := m == 1 ? _lt ? sHigh : math.min(_stop, sHigh) : _stop
  277. if High(m) * m > _stop * m
  278. _flag := true
  279. _trail := false
  280. [_flag, _stop, _trail]
  281.  
  282. _inTrade(_cond, _x, _e, m) =>
  283. var bool _flag = na
  284. var float _stop = na
  285. var float _limit = na
  286. line l1 = array.get (_x, 0)
  287. float lp = line.get_price(l1, sync)
  288. line l2 = array.get (_x, dif ? 4 : 2)
  289. float ls = line.get_price(l2, sync)
  290. bool win = Low (m) * m <= lp * m and not _e
  291. bool lose = High(m) * m >= ls * m and not _e
  292. _flag := _cond
  293. _stop := _e ? ls : _stop
  294. _limit := _e ? lp : _limit
  295. if win or lose
  296. _flag := true
  297. _extend (_x , sync)
  298. _labelUpdate (win , lose, m)
  299. line.set_color (win ? l1 : l2, win ? tCol : sCol)
  300. array.fill (_x , na)
  301. array.fill (zLines, na)
  302. [_f, _s, _t] = _atrTrail (_flag , win , _stop, m)
  303. _flag := atrStop ? _f : _flag
  304. _stop := _t and _s * m < _stop * m and confirm ? _s : _stop
  305. [_flag, _stop, _t, _limit]
  306.  
  307. _double(_cond, _l, _x, m) =>
  308. var bool _flag = na
  309. int _rows = matrix.rows (_x)
  310. _flag := _cond
  311. if _flag
  312. [x1, y1] = _coords (_x , _rows - 5)
  313. [x2, y2] = _coords (_x , _rows - 4)
  314. [x3, y3] = _coords (_x , _rows - 3)
  315. [x4, y4] = _coords (_x , _rows - 2)
  316. bool traded = matrix.get (_x , _rows - 2, 2)
  317. float height = math.avg (y2 , y4) - y3
  318. float _high = y2 + height * (tol/ 100)
  319. float _low = y2 - height * (tol/ 100)
  320. float _t = y3 - height * (fib/ 100)
  321. float y5 = y2 * m < y4 * m ? y2 : y4
  322. float y6 = y2 * m > y4 * m ? y2 : y4
  323. float y7 = y6 - height *(stopPer/100)
  324. bool result = y1*m < y3*m and y4*m <= _high*m and y4*m >= _low*m and close*m < y3*m and not (close[1]*m < y3*m) and not traded
  325. if result and _flag and (m > 0 ? dShort : dLong)
  326. _hLine (_l, x2, y5, y3, y6, y7, _t)
  327. _zLine (x1, y1, x2, y2, x3, y3, x4, y4)
  328. _label (x4, y6, m)
  329. matrix.set (_x, _rows - 2, 2, 1)
  330. _flag := false
  331. _flag
  332.  
  333. _scan(_l, _x, m) =>
  334. var bool _cond = true
  335. _cond := _double (_cond, _l, _x, m)
  336. bool enter = _cond[1] and not _cond
  337. [f,s,t,l] = _inTrade (_cond, _l, enter, m)
  338. _cond := f
  339. _extend(_l, set)
  340. [f,s,t,l]
  341.  
  342. _populate(_n, _x, _i, _col) =>
  343. for [i, _a] in _x
  344. if not na(_a)
  345. table.cell(table_id = _n, column = _i ,
  346. row = i, bgcolor = na ,
  347. text = _a, text_color = _col,
  348. text_size = textSize)
  349.  
  350. // ══════════════════════════════════ //
  351. // ————————> Logical Order <————————— //
  352. // ══════════════════════════════════ //
  353.  
  354. dir := not hbar ? 1 : not lbar ? 0 : dir
  355.  
  356. bool dirUp = dir != dir[1] and dir
  357. bool dirDn = dir != dir[1] and not dir
  358.  
  359. bool setUp = not hbar and dir
  360. bool setDn = not lbar and not dir
  361.  
  362. _mxLog (dirUp or dirDn, logs, dirUp ? pivHigh : pivLows)
  363. _mxUpdate (setUp or setDn, dir, logs, setUp ? pivHigh : pivLows)
  364.  
  365. [bear,ss,ts,sl] = _scan(tLines, logs, 1)
  366. [bull,ls,tl,ll] = _scan(bLines, logs, -1)
  367.  
  368. bool st = SHORT ? ts : false
  369. bool lt = LONG ? tl : false
  370.  
  371. bool sell = bear[1] and not bear
  372. bool buy = bull[1] and not bull
  373.  
  374. color ssCol = st or st[1] ? trailCol : na
  375. color lsCol = lt or lt[1] ? trailCol : na
  376.  
  377. bool longEntry = buy and (FLAT or (SHORT and FLIP))
  378. bool shortEntry = sell and (FLAT or (LONG and FLIP))
  379.  
  380. bool dateFilter = time >= startTime and time <= endTime
  381.  
  382. tradeId += longEntry or shortEntry ? 1 : 0
  383.  
  384. lmt := atrStop ? na :
  385. shortEntry ? sl : longEntry ? ll : lmt
  386. stp := shortEntry ? ss : longEntry ? ls : stp
  387.  
  388. plot (atrStop ? ss : na, "stop", ssCol, style= plot.style_linebr)
  389. plot (atrStop ? ls : na, "limit", lsCol, style= plot.style_linebr)
  390.  
  391. bgcolor (not dateFilter ? color.new(color.red,80) : na, title= "Filter Color")
  392.  
  393. // ══════════════════════════════════ //
  394. // —————————> Data Display <————————— //
  395. // ══════════════════════════════════ //
  396.  
  397. if showTable
  398.  
  399. string tls = bull ? na : str.format(tabPerc, ls, perD(ls))
  400. string tss = bear ? na : str.format(tabPerc, ss, perD(ss))
  401. string tll = bull ? na : str.format(tabPerc, ll, perD(ll))
  402. string tsl = bear ? na : str.format(tabPerc, sl, perD(sl))
  403.  
  404. string[] titles = array.from(na, bull ? na : "Bullish", bear ? na : "Bearish")
  405.  
  406. string[] stops = array.from("Stop" , tls, tss)
  407. string[] limtis = array.from("Target", tll, tsl)
  408.  
  409. table bjTab = table.new(tableYpos + "_" + tableXpos, 3, 3, border_color= color.new(color.gray, 60), border_width= 1)
  410.  
  411. if not bear or not bull
  412. _populate(bjTab, titles, 0, color.white)
  413. _populate(bjTab, stops, 1, color.red)
  414. if not (na(ll) or lt) or not (na(sl) or st)
  415. _populate(bjTab, limtis, 2, color.green)
  416.  
  417. // ══════════════════════════════════ //
  418. // ——————> String Variables <———————— //
  419. // ══════════════════════════════════ //
  420.  
  421. bool cSon = thirdParty == A1
  422. bool tCon = thirdParty == A2
  423. bool aTron = thirdParty == A3
  424. bool c3 = thirdParty == A4
  425. bool dCord = thirdParty == A5
  426.  
  427. if cSon and useStrat
  428.  
  429. string json =
  430.  
  431. "'{'
  432. \n \"passphrase\": \"{0}\",
  433. \n \"time\": '\"{{timenow}}\"',
  434. \n \"ticker\": '\"{{ticker}}\"',
  435. \n \"plot\": '{'
  436. \n \"stop_price\": {1, number, #.########},
  437. \n \"limit_price\": {2, number, #.########}
  438. \n '}',
  439. \n \"strategy\": '{'
  440. \n \"position_size\": '{{strategy.position_size}}',
  441. \n \"order_action\": '\"{{strategy.order.action}}\"',
  442. \n \"market_position\": '\"{{strategy.market_position}}\"',
  443. \n \"market_position_size\": '{{strategy.market_position_size}}',
  444. \n \"prev_market_position\": '\"{{strategy.prev_market_position}}\"',
  445. \n \"prev_market_position_size\": '{{strategy.prev_market_position_size}}'
  446. \n '}'
  447. \n'}'"
  448.  
  449. altStr := str.format(json, pPhrase, stp, lmt)
  450.  
  451. if tCon and useStrat
  452.  
  453. string tcTrade =
  454.  
  455. "'{{strategy.order.action}}' tradesymbol={0} tradeid={1}"
  456. + (atrStop ? "" : ' tpprice={2, number, #.########}')
  457. + ' slprice={3, number, #.########}'
  458.  
  459. altStr := str.format(tcTrade, tcTicker, tradeId, lmt, stp)
  460. tUp := str.format(tcStop, stp, tradeId)
  461.  
  462. if aTron and useStrat
  463.  
  464. string altEnt = aTronKey + " '{'\ncancel(which=all);\nmarket(position='{{strategy.position_size}}');\n"
  465. string altEnd = "'}'\n#bot"
  466. string altBrkt =
  467.  
  468. (atrStop ? "stopOrder(" : "stopOrTakeProfit(")
  469. + (atrStop ? "" : "tp=@{0, number, #.########}, ")
  470. + (atrStop ? "offset=@" : "sl=@") + "{1, number, #.########}, position=0, reduceOnly=true"
  471. + (atrStop ? ", tag=trail" : "")
  472. + ");\n"
  473.  
  474. string enter = altEnt + altBrkt + altEnd
  475. string exit = altEnt + altEnd
  476.  
  477. altStr := str.format(enter, lmt, stp)
  478. altExit := na(altExit) ? exit : altExit
  479.  
  480. string stopUpdate = aTronKey + " '{'\ncancel(which=tagged, tag=trail);\n" + altBrkt + altEnd
  481.  
  482. tUp := atrStop ? str.format(stopUpdate, lmt, stp) : tUp
  483.  
  484. if dCord and useStrat
  485.  
  486. tnB := longEntry ? db : shortEntry ? dt : tnB
  487.  
  488. string postTrade =
  489.  
  490. "'{'\"content\": \"```🚨 Bjorgum {0} detected 🚨\\n\\n\\t'{{ticker}}' '{{interval}}'\\n\\n\\t"
  491. + (atrStop ? "" : "🎯 Target: {1, number, #.########}\\n\\t")
  492. + "🛑 Stop: {2, number, #.########}```\"'}'"
  493.  
  494. altStr := str.format(postTrade, tnB, lmt, stp)
  495. dCordWin := str.format(dExit, tnB, winStr)
  496. dCordLose := str.format(dExit, tnB, loseStr)
  497.  
  498. if c3 and useStrat
  499.  
  500. c3Long := SHORT and buy ? str.format("[{0}, {1}]", c3ShortEnd, c3Long) : c3Long
  501. c3Short := LONG and sell ? str.format("[{0}, {1}]", c3LongEnd, c3Short) : c3Short
  502.  
  503. // ══════════════════════════════════ //
  504. // ——————> Strategy Execution <—————— //
  505. // ══════════════════════════════════ //
  506.  
  507. strategy.entry("Long" , strategy.long , comment= "Long", when= useStrat and longEntry and dateFilter, alert_message= c3 ? c3Long : altStr)
  508. strategy.entry("Short", strategy.short, comment= "Short", when= useStrat and shortEntry and dateFilter, alert_message= c3 ? c3Short : altStr)
  509.  
  510. strategy.exit("Long Exit",
  511. "Long",
  512. stop = stp,
  513. limit = lmt,
  514. comment = "L Exit",
  515. alert_message = c3 ? c3LongEnd : aTron ? altExit : tCon ? "closelong" : dCord ? na : altStr,
  516. alert_profit = dCord ? dCordWin : na,
  517. alert_loss = dCord ? dCordLose : na)
  518.  
  519. strategy.exit("Short Exit",
  520. "Short",
  521. stop = stp,
  522. limit = lmt,
  523. comment = "S Exit",
  524. alert_message = c3 ? c3ShortEnd : aTron ? altExit : tCon ? "closeshort" : dCord ? na : altStr,
  525. alert_profit = dCord ? dCordWin : na,
  526. alert_loss = dCord ? dCordLose : na)
  527.  
  528. // ══════════════════════════════════ //
  529. // —————> Alert Functionality <—————— //
  530. // ══════════════════════════════════ //
  531.  
  532. if (lt and ls != ls[1] or st and ss != ss[1]) and (tCon or aTron)
  533. alert(tUp, alert.freq_once_per_bar_close)
  534.  
  535. if not useStrat and (buy or sell)
  536. alert((buy ? db : dt) + ' Detected', alert.freq_once_per_bar)
  537.  
  538. // ____ __ _ ____
  539. // ( __)( ( \( \
  540. // ) _) / / ) D (
  541. // (____)\_)__)(____/
  542.  
Add Comment
Please, Sign In to add comment