xmd79

TSI-Bnf

Oct 7th, 2021
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.96 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. // © imjoy4u
  3.  
  4. //@version=4
  5. strategy("TSI-Bnf", overlay=true, initial_capital = 500000, commission_value=0.0116, commission_type = strategy.commission.percent )
  6.  
  7. amplitude = input(title="Amplitude", defval=3)
  8. channelDeviation = input(title="Channel Deviation", defval=2)
  9. showArrows = input(title="Show Arrows", defval=true)
  10. showChannels = input(title="Show Channels", defval=true)
  11. startDate = input(title="Start Date", type=input.integer,
  12. defval=1, minval=1, maxval=31)
  13. startMonth = input(title="Start Month", type=input.integer,
  14. defval=1, minval=1, maxval=12)
  15. startYear = input(title="Start Year", type=input.integer,
  16. defval=2018, minval=2018, maxval=2100)
  17. endDate = input(title="End Date", type=input.integer,
  18. defval=31, minval=1, maxval=31)
  19. endMonth = input(title="End Month", type=input.integer,
  20. defval=12, minval=1, maxval=12)
  21. endYear = input(title="End Year", type=input.integer,
  22. defval=2021, minval=1800, maxval=2100)
  23. rLong = input(title="RSI Long", defval=30)
  24. rShort = input(title="RSI Short", defval=70)
  25.  
  26. // study("True Strength Indicator", shorttitle="TSI", format=format.price, precision=4, resolution="")
  27. long = input(title="Long Length", type=input.integer, defval=25)
  28. short = input(title="Short Length", type=input.integer, defval=13)
  29. signal = input(title="Signal Length", type=input.integer, defval=13)
  30.  
  31. // TradeTime = input(title="Trade Timings",defval="0930-1515", type=input.session)
  32. // SqoffTime = input(title="Squareoff Timings",defval="1520-1530", type=input.session)
  33. // Barsinsession(TradeTime) => time(timeframe.period,TradeTime) != 0
  34.  
  35. // Insession = Barsinsession(TradeTime) ? 1 : 0
  36. // endofsession = Insession == 0 and Insession[1] == 1
  37.  
  38. // Sqsession = Barsinsession(SqoffTime) ? 1 : 0
  39. // SqTime = Sqsession == 1 and Sqsession[1] ==0
  40. afterStartDate = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0)) and (time <= timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))
  41.  
  42. // TLong = input(title="Trail Long %", defval=0.10, minval=0.00)
  43. // TShort = input(title="Trail Short%", defval=0.10, minval=0.00)
  44.  
  45. var int trend = 0
  46. var int nextTrend = 0
  47. var float maxLowPrice = nz(low[1], low)
  48. var float minHighPrice = nz(high[1], high)
  49. var int trig = 0
  50.  
  51. var float up = 0.0
  52. var float down = 0.0
  53. float atrHigh = 0.0
  54. float atrLow = 0.0
  55. float arrowUp = na
  56. float arrowDown = na
  57.  
  58. atr2 = atr(100) / 2
  59. dev = channelDeviation * atr2
  60.  
  61. highPrice = high[abs(highestbars(amplitude))]
  62. lowPrice = low[abs(lowestbars(amplitude))]
  63. highma = sma(high, amplitude)
  64. lowma = sma(low, amplitude)
  65.  
  66. if nextTrend == 1
  67. maxLowPrice := max(lowPrice, maxLowPrice)
  68.  
  69. if highma < maxLowPrice and close < nz(low[1], low)
  70. trend := 1
  71. nextTrend := 0
  72. minHighPrice := highPrice
  73. else
  74. minHighPrice := min(highPrice, minHighPrice)
  75.  
  76. if lowma > minHighPrice and close > nz(high[1], high)
  77. trend := 0
  78. nextTrend := 1
  79. maxLowPrice := lowPrice
  80.  
  81. if trend == 0
  82. if not na(trend[1]) and trend[1] != 0
  83. up := na(down[1]) ? down : down[1]
  84. arrowUp := up - atr2
  85. else
  86. up := na(up[1]) ? maxLowPrice : max(maxLowPrice, up[1])
  87. atrHigh := up + dev
  88. atrLow := up - dev
  89. else
  90. if not na(trend[1]) and trend[1] != 1
  91. down := na(up[1]) ? up : up[1]
  92. arrowDown := down + atr2
  93. else
  94. down := na(down[1]) ? minHighPrice : min(minHighPrice, down[1])
  95. atrHigh := down + dev
  96. atrLow := down - dev
  97.  
  98. ht = trend == 0 ? up : down
  99.  
  100. var color buyColor = color.blue
  101. var color sellColor = color.red
  102.  
  103. htColor = trend == 0 ? buyColor : sellColor
  104. htPlot = plot(ht, title="TrendSwitch", linewidth=2, color=htColor)
  105.  
  106. atrHighPlot = plot(showChannels ? atrHigh : na, title="ATR High", style=plot.style_circles, color=sellColor)
  107. atrLowPlot = plot(showChannels ? atrLow : na, title="ATR Low", style=plot.style_circles, color=buyColor)
  108.  
  109. fill(htPlot, atrHighPlot, title="ATR High Ribbon", color=sellColor)
  110. fill(htPlot, atrLowPlot, title="ATR Low Ribbon", color=buyColor)
  111.  
  112. buySignal = not na(arrowUp) and (trend == 0 and trend[1] == 1)
  113. sellSignal = not na(arrowDown) and (trend == 1 and trend[1] == 0)
  114.  
  115. plotshape(showArrows and buySignal ? atrLow : na, title="Arrow Up", style=shape.triangleup, location=location.belowbar, size=size.small, color=buyColor)
  116. plotshape(showArrows and sellSignal ? atrHigh : na, title="Arrow Down", style=shape.triangledown, location=location.abovebar, size=size.small, color=sellColor)
  117.  
  118. stopL= strategy.position_size >0 ? strategy.position_avg_price - 60 : atrLow
  119. stopS= strategy.position_size <0 ? strategy.position_avg_price + 60 : atrHigh
  120.  
  121. price = close
  122. double_smooth(src, long, short) =>
  123. fist_smooth = ema(src, long)
  124. ema(fist_smooth, short)
  125. pc = change(price)
  126. double_smoothed_pc = double_smooth(pc, long, short)
  127. double_smoothed_abs_pc = double_smooth(abs(pc), long, short)
  128. tsi_value = 100 * (double_smoothed_pc / double_smoothed_abs_pc)
  129. sign = ema(tsi_value, signal)
  130. sign1 = tsi_value[4]
  131.  
  132. sS= (tsi_value < sign)
  133. bS= (tsi_value > sign)
  134. buyT = (bS[1] == 0 and bS == 1)
  135. sellT = (sS[1] == 0 and sS == 1)
  136. trig := buyT == 1 ? 1 : sellT == 1 ? -1 : 0
  137.  
  138. plot(trig)
  139. plot(vwap, title="VWAP", color=color.purple, linewidth=2)
  140. // plot(bS, title="buyT")
  141. plot(pc/close*100,title="pc")
  142. // plot(tsi_value, color=#2962FF)
  143. // plot(ema(tsi_value, signal), color=#E91E63)
  144. // hline(0, title="Zero", color=#787B86)
  145.  
  146.  
  147.  
  148. longCondition = (trig== 1) and afterStartDate
  149. shortCondition= (trig==-1) and afterStartDate
  150. //if (longCondition)
  151. // if (afterStartDate and longCondition)
  152. strategy.entry("Long", strategy.long, qty = 2, when= longCondition , comment="B")
  153. strategy.exit("LE1", from_entry="Long", qty_percent = 50, trail_points = strategy.position_avg_price * 0.01/syminfo.mintick)
  154.  
  155. strategy.entry("Short", strategy.short, qty =2, when=shortCondition, comment="S")
  156. strategy.exit("SE1", from_entry="Short", qty_percent = 50, trail_points = strategy.position_avg_price * 0.01/syminfo.mintick)
  157. // strategy.exit("SE", from_entry="Short", trail_price = strategy.position_avg_price * 0.99)
  158. // strategy.exit("Exit", profit=(close*0.01/syminfo.mintick), loss=(150/syminfo.mintick), comment="Trail" )
  159. // strategy.exit("LX", "LE", profit = (close * 0.005/syminfo.mintick), stop= (close * 0.005/syminfo.mintick) , comment="S" )
  160.  
  161. // shortCondition = (trig[1] == -1)
  162. // if (afterStartDate and shortCondition)
  163. // strategy.entry("SE", strategy.short, comment="S")
  164. // strategy.exit("SX", "SE", profit = (close * 0.01/syminfo.mintick), stop= strategy.position_avg_price* 0.01/syminfo.mintick, comment="B")
  165.  
  166. // plot(close*0.005/syminfo.mintick)
  167.  
  168. //barOpenTime = time
  169.  
  170. //tt=timestamp(year(time), month(time), dayofmonth(time), hour(time), 2)
  171.  
  172. //if ( hour(time) == 15 and minute(time) > 26 )
  173.  
  174. // strategy.close("LE", comment="SCM")
  175. //if ( hour(time) == 15 and minute(time) > 26 )
  176. // strategy.close("SE", comment="SCM")
  177. EMA20 = ema(close, 20)
  178. plot(EMA20, title="EMA", color=color.white, linewidth=1)
Advertisement
Add Comment
Please, Sign In to add comment